其他分享
首页 > 其他分享> > Kubebuilder模块

Kubebuilder模块

作者:互联网

CRD创建

   Group表示CRD所属的组,它可以支持多种不同版本、不同类型的资源构建,Version表示CRD的版本号,Kind表示CRD的类型

kubebuilder create api --group ship --version v1beta1 --kind Demo
kubebuilder create api --group ship --version v1 --kind Test
kubebuilder create api --group ship --version v1beta1 --kind Test2

 生成的资源目录结构如下

[root@k8s-01 project2]# tree api/
api/
├── v1
│   ├── groupversion_info.go
│   ├── test_types.go
│   └── zz_generated.deepcopy.go
└── v1beta1
    ├── demo_types.go
    ├── groupversion_info.go
    ├── test2_types.go
    └── zz_generated.deepcopy.go

2 directories, 7 files
[root@k8s-01 project2]# 
[root@k8s-01 project2]# tree .
.
├── api
│   ├── v1
│   │   ├── groupversion_info.go
│   │   ├── test_types.go
│   │   └── zz_generated.deepcopy.go
│   └── v1beta1
│       ├── demo_types.go
│       ├── groupversion_info.go
│       ├── test2_types.go
│       └── zz_generated.deepcopy.go
├── bin
│   └── controller-gen
├── config
│   ├── crd
│   │   ├── kustomization.yaml
│   │   ├── kustomizeconfig.yaml
│   │   └── patches
│   │       ├── cainjection_in_demoes.yaml
│   │       ├── cainjection_in_test2s.yaml
│   │       ├── cainjection_in_tests.yaml
│   │       ├── webhook_in_demoes.yaml
│   │       ├── webhook_in_test2s.yaml
│   │       └── webhook_in_tests.yaml
│   ├── default
│   │   ├── kustomization.yaml
│   │   ├── manager_auth_proxy_patch.yaml
│   │   └── manager_config_patch.yaml
│   ├── manager
│   │   ├── controller_manager_config.yaml
│   │   ├── kustomization.yaml
│   │   └── manager.yaml
│   ├── prometheus
│   │   ├── kustomization.yaml
│   │   └── monitor.yaml
│   ├── rbac
│   │   ├── auth_proxy_client_clusterrole.yaml
│   │   ├── auth_proxy_role_binding.yaml
│   │   ├── auth_proxy_role.yaml
│   │   ├── auth_proxy_service.yaml
│   │   ├── demo_editor_role.yaml
│   │   ├── demo_viewer_role.yaml
│   │   ├── kustomization.yaml
│   │   ├── leader_election_role_binding.yaml
│   │   ├── leader_election_role.yaml
│   │   ├── role_binding.yaml
│   │   ├── service_account.yaml
│   │   ├── test2_editor_role.yaml
│   │   ├── test2_viewer_role.yaml
│   │   ├── test_editor_role.yaml
│   │   └── test_viewer_role.yaml
│   └── samples
│       ├── ship_v1beta1_demo.yaml
│       ├── ship_v1beta1_test2.yaml
│       └── ship_v1_test.yaml
├── controllers
│   ├── demo_controller.go
│   ├── suite_test.go
│   ├── test2_controller.go
│   └── test_controller.go
├── Dockerfile
├── go.mod
├── go.sum
├── hack
│   └── boilerplate.go.txt
├── main.go
├── Makefile
├── PROJECT
└── README.md

14 directories, 54 files

{kind}controller.go,字段生成的Reconciler对象名称时{kind}Reconsiler,它的主要方法是Reconcile(),即通过在这个函数的空白处填入逻辑完成对应的CRD构造工作。 SetupWithManager方法的作用是 

//+kubebuilder:rbac:groups=ship.demo.domain,resources=tests,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=ship.demo.domain,resources=tests/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=ship.demo.domain,resources=tests/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Test object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.1/pkg/reconcile
func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	_ = log.FromContext(ctx)

	// TODO(user): your logic here

	return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *TestReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&shipv1.Test{}).
		Complete(r)
}

Manager初始化

 

标签:--,demo,yaml,Kubebuilder,role,模块,go,ship
来源: https://www.cnblogs.com/i0day/p/16412340.html