diff --git a/controllers/trace/informer_manager.go b/controllers/trace/informer_manager.go index fe53a308634..bc85c9327eb 100644 --- a/controllers/trace/informer_manager.go +++ b/controllers/trace/informer_manager.go @@ -126,7 +126,7 @@ func (m *informerManager) processNextWorkItem() bool { } // get involved object if 'object' is an Event if evt, ok := object.(*corev1.Event); ok { - ro, err := m.scheme.New(evt.InvolvedObject.GroupVersionKind()) + ro, err := m.scheme.New(getGVK(&evt.InvolvedObject)) if err != nil { m.logger.Error(err, "new an event involved object failed") return true @@ -145,6 +145,22 @@ func (m *informerManager) processNextWorkItem() bool { return true } +func getGVK(ref *corev1.ObjectReference) schema.GroupVersionKind { + if ref == nil { + return schema.GroupVersionKind{} + } + // handle core group + if ref.APIVersion == "" { + return schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: ref.Kind, + } + } + // handle other group + return schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind) +} + func (m *informerManager) createInformer(gvk schema.GroupVersionKind) error { o, err := m.scheme.New(gvk) if err != nil { diff --git a/controllers/trace/reconciler_tree.go b/controllers/trace/reconciler_tree.go index 608fc49732a..a6eaf6e4806 100644 --- a/controllers/trace/reconciler_tree.go +++ b/controllers/trace/reconciler_tree.go @@ -71,7 +71,6 @@ var reconcilerFuncMap = map[tracev1.ObjectType]reconcilerFunc{ objectType(workloadsAPI.SchemeGroupVersion.String(), workloadsAPI.InstanceSetKind): newInstanceSetReconciler, objectType(corev1.SchemeGroupVersion.String(), constant.ConfigMapKind): newConfigMapReconciler, objectType(corev1.SchemeGroupVersion.String(), constant.PersistentVolumeClaimKind): newPVCReconciler, - objectType(rbacv1.SchemeGroupVersion.String(), constant.ClusterRoleBindingKind): newClusterRoleBindingReconciler, objectType(rbacv1.SchemeGroupVersion.String(), constant.RoleBindingKind): newRoleBindingReconciler, objectType(corev1.SchemeGroupVersion.String(), constant.ServiceAccountKind): newSAReconciler, objectType(batchv1.SchemeGroupVersion.String(), constant.JobKind): newJobReconciler, @@ -700,10 +699,6 @@ func newRoleBindingReconciler(c client.Client, recorder record.EventRecorder) re return newDoNothingReconciler() } -func newClusterRoleBindingReconciler(c client.Client, recorder record.EventRecorder) reconcile.Reconciler { - return newDoNothingReconciler() -} - func newConfigMapReconciler(c client.Client, recorder record.EventRecorder) reconcile.Reconciler { return newDoNothingReconciler() } diff --git a/controllers/trace/resources_validator.go b/controllers/trace/resources_validator.go index dd12b9a1d54..411d3b32816 100644 --- a/controllers/trace/resources_validator.go +++ b/controllers/trace/resources_validator.go @@ -27,6 +27,7 @@ import ( kbappsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" tracev1 "github.com/apecloud/kubeblocks/apis/trace/v1" "github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx" + "github.com/apecloud/kubeblocks/pkg/controller/model" ) type resourcesValidator struct { @@ -46,6 +47,9 @@ func (r *resourcesValidator) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuild if tree.GetRoot() == nil { return kubebuilderx.Commit, nil } + if model.IsObjectDeleting(tree.GetRoot()) { + return kubebuilderx.Continue, nil + } // target object should exist v, _ := tree.GetRoot().(*tracev1.ReconciliationTrace) @@ -55,7 +59,7 @@ func (r *resourcesValidator) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuild objectKey.Name = v.Spec.TargetObject.Name } if err := r.reader.Get(r.ctx, objectKey, &kbappsv1.Cluster{}); err != nil { - return kubebuilderx.Commit, err + return kubebuilderx.Commit, client.IgnoreNotFound(err) } return kubebuilderx.Continue, nil diff --git a/controllers/trace/suite_test.go b/controllers/trace/suite_test.go index 4ae6be02890..35023e8c896 100644 --- a/controllers/trace/suite_test.go +++ b/controllers/trace/suite_test.go @@ -173,7 +173,6 @@ func mockObjects(k8sMock *mocks.MockClient) (*kbappsv1.Cluster, []kbappsv1.Compo &corev1.SecretList{}, &corev1.ConfigMapList{}, &corev1.PersistentVolumeClaimList{}, - &rbacv1.ClusterRoleBindingList{}, &rbacv1.RoleBindingList{}, &corev1.ServiceAccountList{}, &batchv1.JobList{}, diff --git a/controllers/trace/type.go b/controllers/trace/type.go index 4c2d44a85b4..146bdc618f6 100644 --- a/controllers/trace/type.go +++ b/controllers/trace/type.go @@ -138,10 +138,6 @@ var ( Secondary: objectType(corev1.SchemeGroupVersion.String(), constant.PersistentVolumeClaimKind), Criteria: componentCriteria, }, - { - Secondary: objectType(rbacv1.SchemeGroupVersion.String(), constant.ClusterRoleBindingKind), - Criteria: componentCriteria, - }, { Secondary: objectType(rbacv1.SchemeGroupVersion.String(), constant.RoleBindingKind), Criteria: componentCriteria, diff --git a/pkg/constant/const.go b/pkg/constant/const.go index fa807c7d55e..66269fafd23 100644 --- a/pkg/constant/const.go +++ b/pkg/constant/const.go @@ -44,7 +44,6 @@ const ( PersistentVolumeClaimKind = "PersistentVolumeClaim" PersistentVolumeKind = "PersistentVolume" ConfigurationKind = "Configuration" - ClusterRoleBindingKind = "ClusterRoleBinding" RoleBindingKind = "RoleBinding" ServiceAccountKind = "ServiceAccount" EventKind = "Event" diff --git a/pkg/controller/kubebuilderx/plan_builder.go b/pkg/controller/kubebuilderx/plan_builder.go index 510091a5738..6dea6bb2643 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 75c4c7c3869..31033c649dd 100644 --- a/pkg/controller/kubebuilderx/plan_builder_test.go +++ b/pkg/controller/kubebuilderx/plan_builder_test.go @@ -221,7 +221,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())) @@ -229,7 +231,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)