kubebuilder提供的Makefile将构建和部署工作大幅度简化,执行以下命令会将最新构建的CRD部署在kubernetes上
# make install ...... customresourcedefinition.apiextensions.k8s.io/applications.apps.zise.feizhu created 编译和运行controller以体验基本流程为主,不深入研究源码,所以对代码仅做少量修改,用于验证是否能生效
main.go
./controllers/application_controller.go
type ApplicationReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme } func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) _ = r.Log.WithValues("application",req.NamespacedName) // your logic here r.Log.Info("1. %v",req) // 打印入参 r.Log.Info("2. %s",debug.Stack()) // 打印堆栈 return ctrl.Result{}, nil } ...执行以下命令,会编译并启动刚才修改的controller:
# make run /Users/zisefeizhu/linkun/goproject/kubedev/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases main.go:35:2: package kubedev/controllers imports github.com/go-logr/logr from implicitly required module; to add missing requirements, run: go get github.com/go-logr/logr@v0.3.0 Error: not all generators ran successfully run `controller-gen crd:trivialVersions=true,preserveUnknownFields=false rbac:roleName=manager-role webhook paths=./... output:crd:artifacts:config=config/crd/bases -w` to see all available markers, or `controller-gen crd:trivialVersions=true,preserveUnknownFields=false rbac:roleName=manager-role webhook paths=./... output:crd:artifacts:config=config/crd/bases -h` for usage make: *** [manifests] Error 1 # go get github.com/go-logr/logr@v0.3.0 # make run 创建Application资源的实例现在kubernetes已经部署了Application类型的CRD,而且对应的controller也已正在运行中,可以尝试创建Application类型的实例了(相当于有了pod的定义后,才可以创建pod);
kubebuilder已经自动创建了一个类型的部署文件 ./config/samples/apps_v1_application.yaml ,内容如下,很简单,接下来就用这个文件来创建Application实例:
apiVersion: apps.zise.feizhu/v1 kind: Application metadata: name: application-sample spec: # Add fields here foo: bar 执行 apply, 去controller所在控制台,可以看到新增和修改的操作都有日志输出,新增的日志都在里面,代码调用栈一目了然 make run /Users/zisefeizhu/linkun/goproject/kubedev/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases /Users/zisefeizhu/linkun/goproject/kubedev/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... go run ./main.go I0210 15:26:27.993087 31662 request.go:655] Throttling request took 1.031351708s, request: GET:https://192.168.101.45:6443/apis/storage.k8s.io/v1beta1?timeout=32s 2022-02-10T15:26:29.262+0800 INFO controller-runtime.metrics metrics server is starting to listen {"addr": ":8080"} 2022-02-10T15:26:29.263+0800 INFO setup starting manager 2022-02-10T15:26:29.263+0800 INFO controller-runtime.manager starting metrics server {"path": "/metrics"} 2022-02-10T15:26:29.263+0800 INFO controller-runtime.manager.controller.application Starting EventSource {"reconciler group": "apps.zise.feizhu", "reconciler kind": "Application", "source": "kind source: /, Kind="} 2022-02-10T15:26:29.368+0800 INFO controller-runtime.manager.controller.application Starting Controller {"reconciler group": "apps.zise.feizhu", "reconciler kind": "Application"} 2022-02-10T15:26:29.368+0800 INFO controller-runtime.manager.controller.application Starting workers {"reconciler group": "apps.zise.feizhu", "reconciler kind": "Application", "worker count": 1} 2022-02-10T15:26:36.742+0800 INFO controllers.Application 1. default/application-sample 2022-02-10T15:26:36.743+0800 INFO controllers.Application 2. goroutine 420 [running]: runtime/debug.Stack() /Users/zisefeizhu/go/go1.17.6/src/runtime/debug/stack.go:24 +0x88 kubedev/controllers.(*ApplicationReconciler).Reconcile(0x140007443c0, {0x10399f3d8, 0x140006c5680}, {{{0x140003ac869, 0x7}, {0x1400071cc30, 0x12}}}) /Users/zisefeizhu/linkun/goproject/kubedev/controllers/application_controller.go:58 +0x184 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0x1400055e140, {0x10399f330, 0x14000632600}, {0x10383dfc0, 0x14000443f20}) /Users/zisefeizhu/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.8.3/pkg/internal/controller/controller.go:298 +0x2ac sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0x1400055e140, {0x10399f330, 0x14000632600}) /Users/zisefeizhu/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.8.3/pkg/internal/controller/controller.go:253 +0x1d8 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1.2({0x10399f330, 0x14000632600}) /Users/zisefeizhu/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.8.3/pkg/internal/controller/controller.go:216 +0x44 k8s.io/apimachinery/pkg/util/wait.JitterUntilWithContext.func1() /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:185 +0x38 k8s.io/apimachinery/pkg/util/wait.BackoffUntil.func1(0x14000025748) /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:155 +0x68 k8s.io/apimachinery/pkg/util/wait.BackoffUntil(0x1400052ff48, {0x10397a2e0, 0x140005585a0}, 0x1, 0x14000026300) /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:156 +0x94 k8s.io/apimachinery/pkg/util/wait.JitterUntil(0x14000025748, 0x3b9aca00, 0x0, 0x1, 0x14000026300) /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:133 +0x88 k8s.io/apimachinery/pkg/util/wait.JitterUntilWithContext({0x10399f330, 0x14000632600}, 0x14000720140, 0x3b9aca00, 0x0, 0x1) /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:185 +0x88 k8s.io/apimachinery/pkg/util/wait.UntilWithContext({0x10399f330, 0x14000632600}, 0x14000720140, 0x3b9aca00) /Users/zisefeizhu/go/pkg/mod/k8s.io/apimachinery@v0.20.2/pkg/util/wait/wait.go:99 +0x50 created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1 /Users/zisefeizhu/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.8.3/pkg/internal/controller/controller.go:213 +0x308 删除实例并停止controller 不再需要Application实例的时候,执行以下命令即可删除: # ctl delete -f config/samples/ 不再需要controller的时候,去它的控制台使用Ctrl+c中断即可