From aeced939530c89f5d03b9eb24d554a3719f6875c Mon Sep 17 00:00:00 2001 From: dongming Date: Fri, 9 Dec 2022 13:50:45 +0800 Subject: [PATCH] add code --- api/v1/app_types.go | 14 ++++++--- config/crd/bases/demo.mashibing.com_apps.yaml | 12 +++++-- config/samples/demo_v1_app.yaml | 3 +- controllers/app_controller.go | 31 +++++++++++++++++-- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/api/v1/app_types.go b/api/v1/app_types.go index b649461..0d286eb 100644 --- a/api/v1/app_types.go +++ b/api/v1/app_types.go @@ -28,14 +28,20 @@ type AppSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file - // Foo is an example field of App. Edit app_types.go to remove/update - Foo string `json:"foo,omitempty"` + // Action 对兑现做什么动作,例如给一个 Hello + //+optional + Action string `json:"action,omitempty"` + + // Object 对什么操作,例如给一个 World + //+optional + Object string `json:"object,omitempty"` } // AppStatus defines the observed state of App type AppStatus struct { - // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster - // Important: Run "make" to regenerate code after modifying this file + // Result 显示 action+obeject + //+optional + Result string `json:"result,omitempty"` } //+kubebuilder:object:root=true diff --git a/config/crd/bases/demo.mashibing.com_apps.yaml b/config/crd/bases/demo.mashibing.com_apps.yaml index 632831c..de638eb 100644 --- a/config/crd/bases/demo.mashibing.com_apps.yaml +++ b/config/crd/bases/demo.mashibing.com_apps.yaml @@ -35,13 +35,19 @@ spec: spec: description: AppSpec defines the desired state of App properties: - foo: - description: Foo is an example field of App. Edit app_types.go to - remove/update + action: + description: Action 对兑现做什么动作,例如给一个 Hello + type: string + object: + description: Object 对什么操作,例如给一个 World type: string type: object status: description: AppStatus defines the observed state of App + properties: + result: + description: Result 显示 action+obeject + type: string type: object type: object served: true diff --git a/config/samples/demo_v1_app.yaml b/config/samples/demo_v1_app.yaml index fa8c1de..588a7c8 100644 --- a/config/samples/demo_v1_app.yaml +++ b/config/samples/demo_v1_app.yaml @@ -9,4 +9,5 @@ metadata: app.kubernetes.io/created-by: demo name: app-sample spec: - # TODO(user): Add fields here + action: Study + object: Golang diff --git a/controllers/app_controller.go b/controllers/app_controller.go index 3e9d581..ed62d2a 100644 --- a/controllers/app_controller.go +++ b/controllers/app_controller.go @@ -18,7 +18,7 @@ package controllers import ( "context" - + "fmt" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -49,10 +49,35 @@ type AppReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *AppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - _ = log.FromContext(ctx) + logger := log.FromContext(ctx) + logger.Info("Start reconcile") - // TODO(user): your logic here // 实现你的业务逻辑 + // 1. 获取去资源对象 + app := new(demov1.App) + if err := r.Client.Get(ctx, req.NamespacedName, app); err != nil { + logger.Error(err, " Get resource") + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + // 2. 处理数据 + // 2.1 获取对应的字段 + action := app.Spec.Action + object := app.Spec.Object + + // 2.2 拼接数据 + result := fmt.Sprintf("%s,%s", action, object) + + // 3. 创建结果 + appCopy := app.DeepCopy() + appCopy.Status.Result = result + + // 4. 更新 status + if err := r.Client.Status().Update(ctx, appCopy); err != nil { + return ctrl.Result{}, err + } + + logger.Info("End reconcile") return ctrl.Result{}, nil }