From b5981d13af050dbd6bafa202ad4add366bb283f4 Mon Sep 17 00:00:00 2001 From: wangyelei Date: Fri, 13 Dec 2024 14:25:53 +0800 Subject: [PATCH] fix: filter 0.9 api events bug (#8630) --- cmd/manager/main.go | 32 +++++++++---------- .../apps/componentdefinition_controller.go | 4 ++- controllers/apps/opsrequest_controller.go | 3 ++ controllers/apps/transformer_cluster_init.go | 3 +- .../apps/transformer_component_init.go | 1 + .../instanceset/reconciler_api_version.go | 6 ++-- pkg/controller/kubebuilderx/plan_builder.go | 20 ++++++++++-- .../kubebuilderx/plan_builder_test.go | 6 ++-- 8 files changed, 49 insertions(+), 26 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index e09e1b3e853..8d0fedde395 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -429,24 +429,22 @@ func main() { os.Exit(1) } - if !viper.GetBool(constant.DualOperatorsMode) { - if err = (&appscontrollers.OpsDefinitionReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Recorder: mgr.GetEventRecorderFor("ops-definition-controller"), - }).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "OpsDefinition") - os.Exit(1) - } + if err = (&appscontrollers.OpsDefinitionReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("ops-definition-controller"), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "OpsDefinition") + os.Exit(1) + } - if err = (&appscontrollers.OpsRequestReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Recorder: mgr.GetEventRecorderFor("ops-request-controller"), - }).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "OpsRequest") - os.Exit(1) - } + if err = (&appscontrollers.OpsRequestReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("ops-request-controller"), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "OpsRequest") + os.Exit(1) } if !viper.GetBool(constant.DualOperatorsMode) { diff --git a/controllers/apps/componentdefinition_controller.go b/controllers/apps/componentdefinition_controller.go index fb749d5996e..a6f03661e1e 100644 --- a/controllers/apps/componentdefinition_controller.go +++ b/controllers/apps/componentdefinition_controller.go @@ -79,7 +79,9 @@ func (r *ComponentDefinitionReconciler) Reconcile(ctx context.Context, req ctrl. if err := r.Client.Get(rctx.Ctx, rctx.Req.NamespacedName, cmpd); err != nil { return intctrlutil.CheckedRequeueWithError(err, rctx.Log, "") } - + if !intctrlutil.ObjectAPIVersionSupported(cmpd) { + return intctrlutil.Reconciled() + } return r.reconcile(rctx, cmpd) } diff --git a/controllers/apps/opsrequest_controller.go b/controllers/apps/opsrequest_controller.go index f7b8f5818ab..52a25877fdb 100644 --- a/controllers/apps/opsrequest_controller.go +++ b/controllers/apps/opsrequest_controller.go @@ -352,6 +352,9 @@ func (r *OpsRequestReconciler) handleOpsReqDeletedDuringRunning(reqCtx intctrlut return err } for _, cluster := range clusterList.Items { + if !intctrlutil.ObjectAPIVersionSupported(&cluster) { + continue + } if err := r.cleanupOpsAnnotationForCluster(reqCtx, &cluster); err != nil { return err } diff --git a/controllers/apps/transformer_cluster_init.go b/controllers/apps/transformer_cluster_init.go index 0a0b697fffb..ef76de0ba50 100644 --- a/controllers/apps/transformer_cluster_init.go +++ b/controllers/apps/transformer_cluster_init.go @@ -38,9 +38,10 @@ func (t *clusterInitTransformer) Transform(ctx graph.TransformContext, dag *grap graphCli, _ := transCtx.Client.(model.GraphClient) // init dag - graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionStatusPtr()) if !intctrlutil.ObjectAPIVersionSupported(transCtx.Cluster) { + graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionNoopPtr()) return graph.ErrPrematureStop } + graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionStatusPtr()) return nil } diff --git a/controllers/apps/transformer_component_init.go b/controllers/apps/transformer_component_init.go index 0564c45b9b1..d9f984fdd7a 100644 --- a/controllers/apps/transformer_component_init.go +++ b/controllers/apps/transformer_component_init.go @@ -40,6 +40,7 @@ func (t *componentInitTransformer) Transform(ctx graph.TransformContext, dag *gr transCtx.Context = intoContext(transCtx.Context, placement(transCtx.Component)) if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) { + rootVertex.Action = model.ActionNoopPtr() return graph.ErrPrematureStop } return nil diff --git a/pkg/controller/instanceset/reconciler_api_version.go b/pkg/controller/instanceset/reconciler_api_version.go index bab86795a80..ab1f62b2129 100644 --- a/pkg/controller/instanceset/reconciler_api_version.go +++ b/pkg/controller/instanceset/reconciler_api_version.go @@ -30,10 +30,10 @@ func (r *apiVersionReconciler) PreCondition(tree *kubebuilderx.ObjectTree) *kube } func (r *apiVersionReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilderx.Result, error) { - if intctrlutil.ObjectAPIVersionSupported(tree.GetRoot()) { - return kubebuilderx.Continue, nil + if !intctrlutil.ObjectAPIVersionSupported(tree.GetRoot()) { + return kubebuilderx.Commit, nil } - return kubebuilderx.Commit, nil + return kubebuilderx.Continue, nil } func NewAPIVersionReconciler() kubebuilderx.Reconciler { diff --git a/pkg/controller/kubebuilderx/plan_builder.go b/pkg/controller/kubebuilderx/plan_builder.go index befa285e526..556aaff4dcb 100644 --- a/pkg/controller/kubebuilderx/plan_builder.go +++ b/pkg/controller/kubebuilderx/plan_builder.go @@ -106,6 +106,18 @@ func (b *PlanBuilder) Build() (graph.Plan, error) { } func buildOrderedVertices(ctx context.Context, currentTree *ObjectTree, desiredTree *ObjectTree) []*model.ObjectVertex { + getStatusField := func(obj client.Object) interface{} { + objValue := reflect.ValueOf(obj) + if objValue.Kind() != reflect.Ptr || objValue.Elem().Kind() != reflect.Struct { + return nil + } + field := objValue.Elem().FieldByName("Status") + if !field.IsValid() { + return nil + } + return field.Interface() + } + var vertices []*model.ObjectVertex // handle root object @@ -113,8 +125,12 @@ func buildOrderedVertices(ctx context.Context, currentTree *ObjectTree, desiredT root := model.NewObjectVertex(currentTree.GetRoot(), currentTree.GetRoot(), model.ActionDeletePtr()) vertices = append(vertices, root) } else { - root := model.NewObjectVertex(currentTree.GetRoot(), desiredTree.GetRoot(), model.ActionStatusPtr()) - vertices = append(vertices, root) + currentStatus := getStatusField(currentTree.GetRoot()) + desiredStatus := getStatusField(desiredTree.GetRoot()) + if !reflect.DeepEqual(currentStatus, desiredStatus) { + root := model.NewObjectVertex(currentTree.GetRoot(), desiredTree.GetRoot(), model.ActionStatusPtr()) + vertices = append(vertices, root) + } // if annotations, labels or finalizers updated, do both meta patch and status update. if !reflect.DeepEqual(currentTree.GetRoot().GetAnnotations(), desiredTree.GetRoot().GetAnnotations()) || !reflect.DeepEqual(currentTree.GetRoot().GetLabels(), desiredTree.GetRoot().GetLabels()) || diff --git a/pkg/controller/kubebuilderx/plan_builder_test.go b/pkg/controller/kubebuilderx/plan_builder_test.go index c41cfa553ff..72cde49b7fa 100644 --- a/pkg/controller/kubebuilderx/plan_builder_test.go +++ b/pkg/controller/kubebuilderx/plan_builder_test.go @@ -253,7 +253,9 @@ var _ = Describe("plan builder test", func() { env := builder.NewConfigMapBuilder(namespace, name+"-env").GetObject() var verticesExpected []*model.ObjectVertex - verticesExpected = append(verticesExpected, newVertex(its.DeepCopy(), its, model.ActionStatusPtr())) + itsCopy := its.DeepCopy() + itsCopy.Status.Replicas = *itsCopy.Spec.Replicas + verticesExpected = append(verticesExpected, newVertex(its, itsCopy, model.ActionStatusPtr())) verticesExpected = append(verticesExpected, newVertex(nil, pod, model.ActionCreatePtr())) verticesExpected = append(verticesExpected, newVertex(nil, headlessSvc, model.ActionCreatePtr())) verticesExpected = append(verticesExpected, newVertex(nil, svc, model.ActionCreatePtr())) @@ -261,7 +263,7 @@ var _ = Describe("plan builder test", func() { // build ordered vertices currentTree.SetRoot(its) - desiredTree.SetRoot(its) + desiredTree.SetRoot(itsCopy) Expect(desiredTree.Add(pod, headlessSvc, svc, env)).Should(Succeed()) vertices := buildOrderedVertices(ctx, currentTree, desiredTree)