From ce6b3545f760dafbd08db4c71dee51d79f25b998 Mon Sep 17 00:00:00 2001 From: vbadrina Date: Wed, 20 Dec 2023 17:20:01 +0530 Subject: [PATCH] Removes rook-client usage This commit removes the usages of rook-client and replaces them with controller runtime dependencies. Signed-off-by: vbadrina --- addons/token-exchange/rook_secret_handler.go | 13 +++++---- .../rook_secret_handler_test.go | 29 ++++++++----------- .../secret_exchange_handler_register.go | 8 +---- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/addons/token-exchange/rook_secret_handler.go b/addons/token-exchange/rook_secret_handler.go index e83a37ad..09d54e9b 100644 --- a/addons/token-exchange/rook_secret_handler.go +++ b/addons/token-exchange/rook_secret_handler.go @@ -4,14 +4,15 @@ import ( "context" "encoding/json" "fmt" - "github.com/red-hat-storage/odf-multicluster-orchestrator/addons/setup" "os" "strings" + "github.com/red-hat-storage/odf-multicluster-orchestrator/addons/setup" + "github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils" ocsv1 "github.com/red-hat-storage/ocs-operator/api/v1" - rookclient "github.com/rook/rook/pkg/client/clientset/versioned" + rookv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -22,7 +23,6 @@ import ( type rookSecretHandler struct { spokeClient client.Client hubClient client.Client - rookClient rookclient.Interface } const ( @@ -80,7 +80,7 @@ func (r rookSecretHandler) syncBlueSecret(name string, namespace string, c *blue klog.Infof("detected secret %s in %s namespace", name, namespace) // fetch storage cluster name - sc, err := getStorageClusterFromRookSecret(secret, r.rookClient) + sc, err := getStorageClusterFromRookSecret(secret, r.spokeClient) if err != nil { return fmt.Errorf("failed to get the storage cluster name from the secret %q in namespace %q in managed cluster. Error %v", name, namespace, err) } @@ -179,14 +179,15 @@ func (r rookSecretHandler) syncGreenSecret(name string, namespace string, c *gre return nil } -func getStorageClusterFromRookSecret(secret *corev1.Secret, rclient rookclient.Interface) (storageCluster string, err error) { +func getStorageClusterFromRookSecret(secret *corev1.Secret, rclient client.Client) (storageCluster string, err error) { for _, v := range secret.ObjectMeta.OwnerReferences { if v.Kind != "CephCluster" && v.Kind != "StorageCluster" { continue } if v.Kind == "CephCluster" { - found, err := rclient.CephV1().CephClusters(secret.Namespace).Get(context.TODO(), v.Name, metav1.GetOptions{}) + var found rookv1.CephCluster + err := rclient.Get(context.TODO(), types.NamespacedName{Name: v.Name, Namespace: secret.Namespace}, &found) if err != nil { return "", fmt.Errorf("unable to fetch ceph cluster err: %v", err) } diff --git a/addons/token-exchange/rook_secret_handler_test.go b/addons/token-exchange/rook_secret_handler_test.go index 4cfbf20f..c798764f 100644 --- a/addons/token-exchange/rook_secret_handler_test.go +++ b/addons/token-exchange/rook_secret_handler_test.go @@ -4,17 +4,17 @@ import ( "context" "encoding/json" "fmt" - "github.com/red-hat-storage/odf-multicluster-orchestrator/addons/setup" "reflect" "testing" "time" + "github.com/red-hat-storage/odf-multicluster-orchestrator/addons/setup" + "github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils" "github.com/openshift/library-go/pkg/operator/events/eventstesting" ocsv1 "github.com/red-hat-storage/ocs-operator/api/v1" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" - rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -63,17 +63,6 @@ func fakeSpokeClientForGreenSecret(t *testing.T) client.Client { }, }, }, - } - scheme := runtime.NewScheme() - err := ocsv1.AddToScheme(scheme) - if err != nil { - assert.Fail(t, "failed to add ocsv1 scheme") - } - return cfake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obj...).Build() -} - -func fakeRookClient(t *testing.T) *rookclient.Clientset { - obj := []runtime.Object{ &cephv1.CephCluster{ ObjectMeta: metav1.ObjectMeta{ Name: TestCephClusterName, @@ -88,8 +77,16 @@ func fakeRookClient(t *testing.T) *rookclient.Clientset { Spec: cephv1.ClusterSpec{}, }, } - rclient := rookclient.NewSimpleClientset(obj...) - return rclient + scheme := runtime.NewScheme() + err := ocsv1.AddToScheme(scheme) + if err != nil { + assert.Fail(t, "failed to add ocsv1 scheme") + } + err = cephv1.AddToScheme((scheme)) + if err != nil { + assert.Fail(t, "failed to add cephv1 scheme") + } + return cfake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obj...).Build() } func getFakeToken() map[string][]byte { @@ -222,7 +219,6 @@ func getFakeRookGreenSecretExchangeController(t *testing.T) *greenSecretTokenExc func TestRookGreenSecretSnyc(t *testing.T) { rookHandler := rookSecretHandler{ spokeClient: fakeSpokeClientForGreenSecret(t), - rookClient: fakeRookClient(t), } cases := []testCase{ { @@ -334,7 +330,6 @@ func getExpectedExternalClusterRookBlueSecret(t *testing.T) *corev1.Secret { func TestRookBlueSecretSnyc(t *testing.T) { rookHandler := rookSecretHandler{ spokeClient: fakeSpokeClientForGreenSecret(t), - rookClient: fakeRookClient(t), } cases := []testCase{ { diff --git a/addons/token-exchange/secret_exchange_handler_register.go b/addons/token-exchange/secret_exchange_handler_register.go index fd69d23f..afd381c3 100644 --- a/addons/token-exchange/secret_exchange_handler_register.go +++ b/addons/token-exchange/secret_exchange_handler_register.go @@ -2,9 +2,9 @@ package addons import ( "fmt" + multiclusterv1alpha1 "github.com/red-hat-storage/odf-multicluster-orchestrator/api/v1alpha1" "github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils" - rookclient "github.com/rook/rook/pkg/client/clientset/versioned" "k8s.io/client-go/rest" ) @@ -19,11 +19,6 @@ func registerHandler(mode multiclusterv1alpha1.DRType, spokeKubeConfig *rest.Con if mode != multiclusterv1alpha1.Sync && mode != multiclusterv1alpha1.Async { return fmt.Errorf("unknown mode %q detected, please check the mirrorpeer created", mode) } - // rook specific client - rookClient, err := rookclient.NewForConfig(spokeKubeConfig) - if err != nil { - return fmt.Errorf("failed to add rook client: %v", err) - } // a generic client which is common between all handlers genericSpokeClient, err := getClient(spokeKubeConfig) @@ -44,7 +39,6 @@ func registerHandler(mode multiclusterv1alpha1.DRType, spokeKubeConfig *rest.Con hubClient: genericHubClient, } secretExchangeHandler.RegisteredHandlers[utils.RookSecretHandlerName] = rookSecretHandler{ - rookClient: rookClient, spokeClient: genericSpokeClient, hubClient: genericHubClient, }