Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes rook-client usage #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions addons/token-exchange/rook_secret_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,7 +23,6 @@ import (
type rookSecretHandler struct {
spokeClient client.Client
hubClient client.Client
rookClient rookclient.Interface
}

const (
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 12 additions & 17 deletions addons/token-exchange/rook_secret_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
8 changes: 1 addition & 7 deletions addons/token-exchange/secret_exchange_handler_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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,
}
Expand Down