master
dongming 2 years ago
parent 0d77e58a2b
commit aeced93953

@ -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

@ -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

@ -9,4 +9,5 @@ metadata:
app.kubernetes.io/created-by: demo
name: app-sample
spec:
# TODO(user): Add fields here
action: Study
object: Golang

@ -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
}

Loading…
Cancel
Save