From 0ccbfe8c2c3231f1270c1fc8d72f476a3128892f Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Wed, 27 Mar 2024 12:02:15 +0530 Subject: [PATCH 1/2] Revert "Use uncached client (#1682)" This reverts commit aa44df2ca0691638e6484cebd5ec75af9868f8cb. --- controllers/ibmpowervscluster_controller.go | 3 +-- controllers/ibmpowervscluster_controller_test.go | 9 +++------ main.go | 7 ------- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/controllers/ibmpowervscluster_controller.go b/controllers/ibmpowervscluster_controller.go index 1b9d1c03f..d451bf2e8 100644 --- a/controllers/ibmpowervscluster_controller.go +++ b/controllers/ibmpowervscluster_controller.go @@ -51,7 +51,6 @@ import ( // IBMPowerVSClusterReconciler reconciles a IBMPowerVSCluster object. type IBMPowerVSClusterReconciler struct { client.Client - UncachedClient client.Client Recorder record.EventRecorder ServiceEndpoint []endpoints.ServiceEndpoint Scheme *runtime.Scheme @@ -66,7 +65,7 @@ func (r *IBMPowerVSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re // Fetch the IBMPowerVSCluster instance. ibmCluster := &infrav1beta2.IBMPowerVSCluster{} - err := r.UncachedClient.Get(ctx, req.NamespacedName, ibmCluster) + err := r.Get(ctx, req.NamespacedName, ibmCluster) if err != nil { if apierrors.IsNotFound(err) { return ctrl.Result{}, nil diff --git a/controllers/ibmpowervscluster_controller_test.go b/controllers/ibmpowervscluster_controller_test.go index fa38a8dd1..c70f6f679 100644 --- a/controllers/ibmpowervscluster_controller_test.go +++ b/controllers/ibmpowervscluster_controller_test.go @@ -79,8 +79,7 @@ func TestIBMPowerVSClusterReconciler_Reconcile(t *testing.T) { t.Run(tc.name, func(t *testing.T) { g := NewWithT(t) reconciler := &IBMPowerVSClusterReconciler{ - Client: testEnv.Client, - UncachedClient: testEnv.Client, + Client: testEnv.Client, } ns, err := testEnv.CreateNamespace(ctx, fmt.Sprintf("namespace-%s", util.RandomString(5))) @@ -158,8 +157,7 @@ func TestIBMPowerVSClusterReconciler_reconcile(t *testing.T) { t.Run(tc.name, func(t *testing.T) { g := NewWithT(t) reconciler := &IBMPowerVSClusterReconciler{ - Client: testEnv.Client, - UncachedClient: testEnv.Client, + Client: testEnv.Client, } _, _ = reconciler.reconcile(tc.powervsClusterScope) g.Expect(tc.powervsClusterScope.IBMPowerVSCluster.Status.Ready).To(Equal(tc.clusterStatus)) @@ -174,8 +172,7 @@ func TestIBMPowerVSClusterReconciler_delete(t *testing.T) { clusterScope *scope.PowerVSClusterScope ) reconciler = IBMPowerVSClusterReconciler{ - Client: testEnv.Client, - UncachedClient: testEnv.Client, + Client: testEnv.Client, } t.Run("Reconciling delete IBMPowerVSCluster", func(t *testing.T) { t.Run("Should reconcile successfully if no descendants are found", func(t *testing.T) { diff --git a/main.go b/main.go index 4418000cf..1fa536bcd 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook" capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -243,14 +242,8 @@ func setupReconcilers(mgr ctrl.Manager, serviceEndpoint []endpoints.ServiceEndpo os.Exit(1) } - cfg := ctrl.GetConfigOrDie() - uncachedClient, err := client.New(cfg, client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()}) - if err != nil { - setupLog.Error(err, "unable to set up uncached client") - } if err := (&controllers.IBMPowerVSClusterReconciler{ Client: mgr.GetClient(), - UncachedClient: uncachedClient, Recorder: mgr.GetEventRecorderFor("ibmpowervscluster-controller"), ServiceEndpoint: serviceEndpoint, Scheme: mgr.GetScheme(), From a5c78ef8199e785990939bfa0813a0ea076fbbf7 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Wed, 27 Mar 2024 12:51:22 +0530 Subject: [PATCH 2/2] Explicitly set GVK for IBMPowerVSCluster object --- controllers/ibmpowervscluster_controller.go | 11 +++++++++++ main.go | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/controllers/ibmpowervscluster_controller.go b/controllers/ibmpowervscluster_controller.go index d451bf2e8..27b87646d 100644 --- a/controllers/ibmpowervscluster_controller.go +++ b/controllers/ibmpowervscluster_controller.go @@ -300,6 +300,17 @@ func (r *IBMPowerVSClusterReconciler) deleteIBMPowerVSImage(ctx context.Context, return reconcile.Result{}, err } + // since we are avoiding using cache for IBMPowerVSCluster the Type meta of the retrieved object will be empty + // explicitly setting here to filter children + if gvk := cluster.GetObjectKind().GroupVersionKind(); gvk.Empty() { + gvk, err := r.GroupVersionKindFor(cluster) + if err != nil { + log.Error(err, "Failed to get GVK of cluster") + return reconcile.Result{}, err + } + cluster.SetGroupVersionKind(gvk) + } + children, err := descendants.filterOwnedDescendants(cluster) if err != nil { log.Error(err, "Failed to extract direct descendants") diff --git a/main.go b/main.go index 1fa536bcd..5cf27ff48 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook" capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -130,6 +131,13 @@ func main() { Port: webhookPort, CertDir: webhookCertDir, }), + Client: client.Options{ + Cache: &client.CacheOptions{ + DisableFor: []client.Object{ + &infrav1beta2.IBMPowerVSCluster{}, + }, + }, + }, }) if err != nil { setupLog.Error(err, "unable to start manager")