diff --git a/pkg/clientutil/predicates.go b/pkg/clientutil/predicates.go index c6db038ed..9b24e856d 100644 --- a/pkg/clientutil/predicates.go +++ b/pkg/clientutil/predicates.go @@ -8,6 +8,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/predicate" greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" @@ -69,6 +70,12 @@ func PredicateByName(name string) predicate.Predicate { }) } +func PredicateHasFinalizer(finalizer string) predicate.Predicate { + return predicate.NewPredicateFuncs(func(o client.Object) bool { + return controllerutil.ContainsFinalizer(o, finalizer) + }) +} + func PredicateHasOICDConfigured() predicate.Predicate { return predicate.NewPredicateFuncs(func(o client.Object) bool { org, ok := o.(*greenhousev1alpha1.Organization) diff --git a/pkg/controllers/propagation_reconciler.go b/pkg/controllers/propagation_reconciler.go index abe2f96be..f8fa226a5 100644 --- a/pkg/controllers/propagation_reconciler.go +++ b/pkg/controllers/propagation_reconciler.go @@ -50,7 +50,9 @@ func (r *PropagationReconciler) BaseSetupWithManager(name string, mgr ctrl.Manag // Watch the respective CRD and enqueue all objects. Watches(&apiextensionsv1.CustomResourceDefinition{}, handler.EnqueueRequestsFromMapFunc(r.HandlerFunc), - builder.WithPredicates(clientutil.PredicateByName(r.CRDName)), + builder.WithPredicates( + clientutil.PredicateByName(r.CRDName), + clientutil.PredicateHasFinalizer(greenhouseapis.FinalizerCleanupPropagatedResource)), ). Complete(r) } @@ -68,8 +70,9 @@ func (r *PropagationReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, client.IgnoreNotFound(err) } - if err := clientutil.EnsureFinalizer(ctx, r.Client, obj, greenhouseapis.FinalizerCleanupPropagatedResource); err != nil { - return ctrl.Result{}, err + if !controllerutil.ContainsFinalizer(obj, greenhouseapis.FinalizerCleanupPropagatedResource) { + fmt.Printf("Skip resource because it does not contain the cleanup finalizer") + return ctrl.Result{}, nil } clusterList := new(greenhousev1alpha1.ClusterList) @@ -95,10 +98,6 @@ func (r *PropagationReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, err } - if err := r.ReconcileCRD(ctx, remoteRestClient, cluster.GetName(), obj.GetNamespace()); err != nil { - return ctrl.Result{}, err - } - if err, removeFinalizer = r.reconcileObject(ctx, remoteRestClient, obj, cluster.GetName()); err != nil { return ctrl.Result{}, err } @@ -112,38 +111,6 @@ func (r *PropagationReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, nil } -func (r *PropagationReconciler) ReconcileCRD(ctx context.Context, remoteClient client.Client, clusterName, namespace string) error { - SrcCRD := &apiextensionsv1.CustomResourceDefinition{} - if err := r.Client.Get(ctx, types.NamespacedName{Namespace: "", Name: r.CRDName}, SrcCRD); err != nil { - return err - } - - var remoteNamespace = new(corev1.Namespace) - if err := remoteClient.Get(ctx, types.NamespacedName{Namespace: "", Name: namespace}, remoteNamespace); err != nil { - log.FromContext(ctx).Error(err, "failed getting remote namespace for CRD owner reference", "CRD", SrcCRD, "cluster", clusterName) - return err - } - - var remoteCRD = &apiextensionsv1.CustomResourceDefinition{} - remoteCRD.SetName(SrcCRD.GetName()) - - result, err := clientutil.CreateOrPatch(ctx, remoteClient, remoteCRD, func() error { - remoteCRD.Spec = SrcCRD.Spec - return controllerutil.SetOwnerReference(remoteNamespace, remoteCRD, remoteClient.Scheme()) - }) - if err != nil { - return err - } - message := fmt.Sprintf("%s CRD on target cluster", result) - switch result { - case clientutil.OperationResultCreated, clientutil.OperationResultUpdated: - log.FromContext(ctx).Info(message, "CRD", SrcCRD, "cluster", clusterName) - case clientutil.OperationResultNone: - log.FromContext(ctx).V(5).Info(message, "CRD", SrcCRD, "cluster", clusterName) - } - return nil -} - func (r *PropagationReconciler) reconcileObject(ctx context.Context, restClient client.Client, obj client.Object, clusterName string) (err error, removeFinalizer bool) { remoteObject := obj.DeepCopyObject().(client.Object) //nolint:errcheck remoteObjectExists := true @@ -169,27 +136,6 @@ func (r *PropagationReconciler) reconcileObject(ctx context.Context, restClient log.FromContext(ctx).Info("deleted object on target cluster", "object", obj, "cluster", clusterName) return nil, true } - - remoteObjectResource, err := r.StripObjectWrapper(obj) - if err != nil { - return err, false - } - - // update - if remoteObjectExists { - remoteObjectResource.SetResourceVersion(remoteObject.GetResourceVersion()) - if err = restClient.Update(ctx, remoteObjectResource); err != nil { - return err, false - } - log.FromContext(ctx).Info("updated object on target cluster", "object", obj, "cluster", clusterName) - return nil, false - } - - // create - if err = restClient.Create(ctx, remoteObjectResource); err != nil { - return err, false - } - log.FromContext(ctx).Info("created object on target cluster", "object", obj, "cluster", clusterName) return nil, false } diff --git a/pkg/controllers/propagation_reconciler_test.go b/pkg/controllers/propagation_reconciler_test.go index 81538fc9a..f52fd47ad 100644 --- a/pkg/controllers/propagation_reconciler_test.go +++ b/pkg/controllers/propagation_reconciler_test.go @@ -244,6 +244,7 @@ var _ = Describe("Propagation reconciler", Ordered, func() { ) It("should have created the crd on the remote clusters with owner reference", func() { + Skip("Skipped because propagation reconciler is deprecated.") CRDList := &apiextensionsv1.CustomResourceDefinitionList{} otherCRDList := &apiextensionsv1.CustomResourceDefinitionList{} Eventually(func(g Gomega) bool { @@ -263,6 +264,7 @@ var _ = Describe("Propagation reconciler", Ordered, func() { }) It("should have created the resource on the remote cluster", func() { + Skip("Skipped because propagation reconciler is deprecated.") remoteObject := &fixtures.Dummy{} otherRemoteObject := &fixtures.Dummy{} Eventually(func(g Gomega) bool { @@ -277,6 +279,7 @@ var _ = Describe("Propagation reconciler", Ordered, func() { }) It("should reconcile CRD and object after CRD update", func() { + Skip("Skipped because propagation reconciler is deprecated.") By("updating the crd in the local cluster") currentCRD := &apiextensionsv1.CustomResourceDefinition{} err := test.K8sClient.Get(test.Ctx, types.NamespacedName{Namespace: "", Name: dummyCRDName}, currentCRD) @@ -319,6 +322,7 @@ var _ = Describe("Propagation reconciler", Ordered, func() { }) It("should reconcile the remote resource after local resource update", func() { + Skip("Skipped because propagation reconciler is deprecated.") By("getting the local resource") localObject := dummy updateLabel := map[string]string{"test": "test"} @@ -347,6 +351,7 @@ var _ = Describe("Propagation reconciler", Ordered, func() { }) It("should delete the remote resources after deletion", func() { + Skip("Skipped because propagation reconciler is deprecated.") By("deleting the local resource") err := test.K8sClient.Delete(test.Ctx, dummy) Expect(err).ToNot(HaveOccurred(), "there should be no error deleting the resource")