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

Remove internal mode code #278

Open
wants to merge 6 commits 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
151 changes: 0 additions & 151 deletions addons/agent_mirrorpeer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import (

multiclusterv1alpha1 "github.com/red-hat-storage/odf-multicluster-orchestrator/api/v1alpha1"
"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
rookv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -177,17 +175,6 @@ func (r *MirrorPeerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

logger.Info("Labeled the default VolumeSnapshotClasses successfully")

if mirrorPeer.Spec.Type == multiclusterv1alpha1.Async {
logger.Info("Enabling async mode dependencies")
storageId1 := clusterStorageIds[mirrorPeer.Spec.Items[0].ClusterName]["rbd"]
storageId2 := clusterStorageIds[mirrorPeer.Spec.Items[1].ClusterName]["rbd"]
err = r.labelCephClusters(ctx, scr, storageId1, storageId2)
if err != nil {
logger.Error("Failed to label cephcluster", "error", err)
return ctrl.Result{}, err
}
}
}

if mirrorPeer.Spec.Type == multiclusterv1alpha1.Async && hasStorageClientRef {
Expand Down Expand Up @@ -301,14 +288,6 @@ func (r *MirrorPeerReconciler) fetchClusterStorageIds(ctx context.Context, mp *m
logger := r.Logger.With("clusterName", pr.ClusterName,
"namespace", pr.StorageClusterRef.Namespace)

// Get cluster type (converged/external)
clusterType, err := utils.GetClusterType(pr.StorageClusterRef.Name,
pr.StorageClusterRef.Namespace, r.SpokeClient)
if err != nil {
logger.Error("Failed to get cluster type", "error", err)
return nil, fmt.Errorf("failed to get cluster type for %s: %v", pr.ClusterName, err)
}

var currentPeerStorageIds = make(map[string]string)

// Handle local cluster vs remote cluster
Expand All @@ -327,29 +306,6 @@ func (r *MirrorPeerReconciler) fetchClusterStorageIds(ctx context.Context, mp *m
for k, v := range storageIds {
currentPeerStorageIds[string(k)] = v
}
} else {
// For remote cluster, get storage IDs from secret
secretName := r.getSecretNameByType(clusterType, pr)
logger.Info("Checking secret",
"secretName", secretName,
"mode", clusterType)

secret, err := utils.FetchSecretWithName(ctx, r.SpokeClient,
types.NamespacedName{
Name: secretName,
Namespace: pr.StorageClusterRef.Namespace,
})
if err != nil {
logger.Error("Failed to fetch secret", "error", err)
return nil, fmt.Errorf("failed to fetch secret %s: %v", secretName, err)
}

currentPeerStorageIds, err = utils.GetStorageIdsFromGreenSecret(secret)
if err != nil {
logger.Error("Failed to extract storage IDs from secret", "error", err)
return nil, fmt.Errorf("failed to get storage IDs from secret %s: %v",
secretName, err)
}
}

clusterStorageIds[pr.ClusterName] = currentPeerStorageIds
Expand All @@ -363,16 +319,6 @@ func (r *MirrorPeerReconciler) fetchClusterStorageIds(ctx context.Context, mp *m
return clusterStorageIds, nil
}

func (r *MirrorPeerReconciler) getSecretNameByType(clusterType utils.ClusterType, pr multiclusterv1alpha1.PeerRef) string {
var secretName string
if clusterType == utils.CONVERGED {
secretName = utils.GetSecretNameByPeerRef(pr)
} else if clusterType == utils.EXTERNAL {
secretName = DefaultExternalSecretName
}
return secretName
}

func (r *MirrorPeerReconciler) createS3(ctx context.Context, mirrorPeer multiclusterv1alpha1.MirrorPeer, scNamespace string, hasStorageClientRef bool) error {
bucketNamespace := utils.GetEnv("ODR_NAMESPACE", scNamespace)
bucketName := utils.GenerateBucketName(mirrorPeer)
Expand All @@ -395,16 +341,6 @@ func (r *MirrorPeerReconciler) createS3(ctx context.Context, mirrorPeer multiclu
return nil
}

func getOppositePeerRefs(mp *multiclusterv1alpha1.MirrorPeer, spokeClusterName string) []multiclusterv1alpha1.PeerRef {
peerRefs := make([]multiclusterv1alpha1.PeerRef, 0)
for _, v := range mp.Spec.Items {
if v.ClusterName != spokeClusterName {
peerRefs = append(peerRefs, v)
}
}
return peerRefs
}

func (r *MirrorPeerReconciler) hasSpokeCluster(obj client.Object) bool {
mp, ok := obj.(*multiclusterv1alpha1.MirrorPeer)
if !ok {
Expand Down Expand Up @@ -463,32 +399,6 @@ func (r *MirrorPeerReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// deleteGreenSecret deletes the exchanged secret present in the namespace of the storage cluster
func (r *MirrorPeerReconciler) deleteGreenSecret(ctx context.Context, spokeClusterName string, scrNamespace string, mirrorPeer *multiclusterv1alpha1.MirrorPeer) error {
for _, peerRef := range mirrorPeer.Spec.Items {
if peerRef.ClusterName != spokeClusterName {
secretName := utils.GetSecretNameByPeerRef(peerRef)
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: scrNamespace,
},
}
err := r.SpokeClient.Delete(ctx, &secret)
if err != nil {
if errors.IsNotFound(err) {
r.Logger.Info("Green secret not found, no action needed", "secretName", secretName, "namespace", scrNamespace)
return nil
}
r.Logger.Error("Failed to delete green secret", "secretName", secretName, "namespace", scrNamespace, "error", err)
return err
}
r.Logger.Info("Successfully deleted green secret", "secretName", secretName, "namespace", scrNamespace)
}
}
return nil
}

// deleteS3 deletes the S3 bucket in the storage cluster namespace, each new mirrorpeer generates
// a new bucket, so we do not need to check if the bucket is being used by another mirrorpeer
func (r *MirrorPeerReconciler) deleteS3(ctx context.Context, mirrorPeer multiclusterv1alpha1.MirrorPeer, scNamespace string) error {
Expand Down Expand Up @@ -516,11 +426,6 @@ func (r *MirrorPeerReconciler) deleteS3(ctx context.Context, mirrorPeer multiclu
func (r *MirrorPeerReconciler) deleteMirrorPeer(ctx context.Context, mirrorPeer multiclusterv1alpha1.MirrorPeer, scr *multiclusterv1alpha1.StorageClusterRef) (ctrl.Result, error) {
r.Logger.Info("MirrorPeer is being deleted", "MirrorPeer", mirrorPeer.Name)

if err := r.deleteGreenSecret(ctx, r.SpokeClusterName, scr.Namespace, &mirrorPeer); err != nil {
r.Logger.Error("Failed to delete green secrets", "namespace", scr.Namespace, "error", err)
return ctrl.Result{}, fmt.Errorf("failed to delete green secrets: %v", err)
}

if err := r.deleteS3(ctx, mirrorPeer, scr.Namespace); err != nil {
r.Logger.Error("Failed to delete S3 buckets", "namespace", scr.Namespace, "error", err)
return ctrl.Result{}, fmt.Errorf("failed to delete S3 buckets")
Expand All @@ -529,59 +434,3 @@ func (r *MirrorPeerReconciler) deleteMirrorPeer(ctx context.Context, mirrorPeer
r.Logger.Info("Successfully completed the deletion of MirrorPeer resources", "MirrorPeer", mirrorPeer.Name)
return ctrl.Result{}, nil
}

func (r *MirrorPeerReconciler) labelCephClusters(ctx context.Context, scr *multiclusterv1alpha1.StorageClusterRef, storageId1, storageId2 string) error {
r.Logger.Info("Labelling CephClusters with replication ID")
cephClusters, err := utils.FetchAllCephClusters(ctx, r.SpokeClient)
if err != nil {
r.Logger.Error("Failed to fetch all CephClusters", "error", err)
return err
}
if cephClusters == nil || len(cephClusters.Items) == 0 {
r.Logger.Info("No CephClusters found to label")
return nil
}

var found rookv1.CephCluster
foundFlag := false
for _, cc := range cephClusters.Items {
for _, ref := range cc.OwnerReferences {
if ref.Kind == "StorageCluster" && ref.Name == scr.Name {
found = cc
foundFlag = true
break
}
}
if foundFlag {
break
}
}
if !foundFlag {
r.Logger.Info("No CephCluster matched the StorageCluster reference", "StorageClusterRef", scr.Name)
return nil
}

if found.Labels == nil {
found.Labels = make(map[string]string)
}

replicationId, err := utils.CreateUniqueReplicationId(storageId1, storageId2)
if err != nil {
r.Logger.Error("Failed to create unique replication ID", "error", err)
return err
}

if found.Labels[utils.CephClusterReplicationIdLabel] != replicationId {
r.Logger.Info("Adding label to CephCluster", "label", utils.CephClusterReplicationIdLabel, "replicationId", replicationId, "CephCluster", found.Name)
found.Labels[utils.CephClusterReplicationIdLabel] = replicationId
err = r.SpokeClient.Update(ctx, &found)
if err != nil {
r.Logger.Error("Failed to update CephCluster with new label", "CephCluster", found.Name, "label", utils.CephClusterReplicationIdLabel, "error", err)
return err
}
} else {
r.Logger.Info("CephCluster already labeled with replication ID", "CephCluster", found.Name, "replicationId", replicationId)
}

return nil
}
78 changes: 12 additions & 66 deletions addons/agent_mirrorpeer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ func GetTestCephBlockPool() *rookv1.CephBlockPool {
}
}

func GetOppositePeerRefs(mp *multiclusterv1alpha1.MirrorPeer, spokeClusterName string) []multiclusterv1alpha1.PeerRef {
peerRefs := make([]multiclusterv1alpha1.PeerRef, 0)
for _, v := range mp.Spec.Items {
if v.ClusterName != spokeClusterName {
peerRefs = append(peerRefs, v)
}
}
return peerRefs
}

func TestMirrorPeerReconcile(t *testing.T) {
ctx := context.TODO()
scheme := mgrScheme
Expand All @@ -261,7 +271,7 @@ func TestMirrorPeerReconcile(t *testing.T) {
oppositePeerRefsArray := make([][]multiclusterv1alpha1.PeerRef, 0)
// Quick iteration to get peer refs
for _, pr := range mirrorpeer1.Spec.Items {
peerRefs := getOppositePeerRefs(&mirrorpeer1, pr.ClusterName)
peerRefs := GetOppositePeerRefs(&mirrorpeer1, pr.ClusterName)
oppositePeerRefsArray = append(oppositePeerRefsArray, peerRefs)
}

Expand All @@ -284,18 +294,7 @@ func TestMirrorPeerReconcile(t *testing.T) {
}
cephCluster := GetTestCephCluster()

rcm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: RookConfigMapName,
Namespace: pr.StorageClusterRef.Namespace,
},
}

// Need to initialize this map otherwise it panics during reconcile
rcm.Data = make(map[string]string)
rcm.Data[RookCSIEnableKey] = "false"

fakeSpokeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&storageCluster, &rcm, &clusterPeerToken, &exchangedSecret1, &exchangedSecret2, cephCluster, rbdVirtStorageClass, cephblockpool, rbdStorageClass, cephfsStorageClass, &odfInfoConfigMap, rbdVolumeSnapshotClass, cephfsVolumeSnapshotClass).Build()
fakeSpokeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&storageCluster, &clusterPeerToken, &exchangedSecret1, &exchangedSecret2, cephCluster, rbdVirtStorageClass, cephblockpool, rbdStorageClass, cephfsStorageClass, &odfInfoConfigMap, rbdVolumeSnapshotClass, cephfsVolumeSnapshotClass).Build()

r := MirrorPeerReconciler{
HubClient: fakeHubClient,
Expand Down Expand Up @@ -407,59 +406,6 @@ func TestMirrorPeerReconcile(t *testing.T) {

}

func TestDeleteGreenSecret(t *testing.T) {
ctx := context.TODO()
scheme := mgrScheme
secretData := map[string][]byte{
utils.StorageClusterNameKey: []byte("test-storagecluster"),
utils.NamespaceKey: []byte("test-namespace"),
utils.SecretOriginKey: []byte(""),
utils.SecretDataKey: []byte(""),
}
fakeHubClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&mirrorpeer1).Build()
for _, pr := range mirrorpeer1.Spec.Items {
fakeSpokeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects().Build()
oppositePeerRefs := getOppositePeerRefs(&mirrorpeer1, pr.ClusterName)
for _, oppPeer := range oppositePeerRefs {
greenSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: utils.GetSecretNameByPeerRef(oppPeer),
Namespace: pr.StorageClusterRef.Namespace,
},
Data: secretData,
}
if err := fakeSpokeClient.Create(ctx, &greenSecret); err != nil {
t.Error("failed to create an exchanged secret", greenSecret.Name)
}
}

r := MirrorPeerReconciler{
HubClient: fakeHubClient,
SpokeClient: fakeSpokeClient,
Scheme: scheme,
SpokeClusterName: pr.ClusterName,
Logger: utils.GetLogger(utils.GetZapLogger(true)),
}

if err := r.deleteGreenSecret(ctx, pr.ClusterName, pr.StorageClusterRef.Namespace, &mirrorpeer1); err != nil {
t.Errorf("failed to delete green secret from namespace %s, err: %v", pr.StorageClusterRef.Namespace, err)
}
for _, oppPeer := range oppositePeerRefs {
var greenSecret corev1.Secret
if err := r.SpokeClient.Get(ctx, types.NamespacedName{
Name: utils.GetSecretNameByPeerRef(oppPeer),
Namespace: pr.StorageClusterRef.Namespace,
}, &greenSecret); err != nil {
if !errors.IsNotFound(err) {
t.Error(err, "Green Secret did not get deleted")
}
} else {
t.Error("Green secret did not get deleted")
}
}
}
}

func TestDeleteS3(t *testing.T) {
bucketName := utils.GenerateBucketName(mirrorPeer)
ctx := context.TODO()
Expand Down
Loading