From 54579d199bdf5673b4a3c79a7a2d41b8957304c9 Mon Sep 17 00:00:00 2001 From: Gowtham Shanmugasundaram Date: Tue, 26 Nov 2024 14:24:21 +0530 Subject: [PATCH] Initialize Mirroring spec to avoid nil pointer exception Signed-off-by: Gowtham Shanmugasundaram --- addons/agent_mirrorpeer_controller.go | 5 ++++- addons/agent_mirrorpeer_controller_test.go | 2 +- addons/rook_secret_handler.go | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/addons/agent_mirrorpeer_controller.go b/addons/agent_mirrorpeer_controller.go index b24048f3..8aa3e15a 100644 --- a/addons/agent_mirrorpeer_controller.go +++ b/addons/agent_mirrorpeer_controller.go @@ -374,6 +374,9 @@ func (r *MirrorPeerReconciler) toggleMirroring(ctx context.Context, storageClust // Determine if mirroring should be enabled or disabled if enabled { + if sc.Spec.Mirroring == nil { + sc.Spec.Mirroring = &ocsv1.MirroringSpec{} + } oppPeers := getOppositePeerRefs(mp, r.SpokeClusterName) if hasRequiredSecret(sc.Spec.Mirroring.PeerSecretNames, oppPeers) { sc.Spec.Mirroring.Enabled = true @@ -382,7 +385,7 @@ func (r *MirrorPeerReconciler) toggleMirroring(ctx context.Context, storageClust return fmt.Errorf("StorageCluster %q does not have required PeerSecrets", storageClusterName) } } else { - sc.Spec.Mirroring.Enabled = false + sc.Spec.Mirroring = nil r.Logger.Info("Mirroring disabled on StorageCluster", "storageClusterName", storageClusterName) } diff --git a/addons/agent_mirrorpeer_controller_test.go b/addons/agent_mirrorpeer_controller_test.go index 38052a97..3a8847de 100644 --- a/addons/agent_mirrorpeer_controller_test.go +++ b/addons/agent_mirrorpeer_controller_test.go @@ -246,7 +246,7 @@ func TestDisableMirroring(t *testing.T) { t.Error("failed to get storage cluster", err) } - if sc.Spec.Mirroring.Enabled { + if sc.Spec.Mirroring != nil { t.Error("failed to disable mirroring") } } diff --git a/addons/rook_secret_handler.go b/addons/rook_secret_handler.go index b50ef29a..375eccd8 100644 --- a/addons/rook_secret_handler.go +++ b/addons/rook_secret_handler.go @@ -197,6 +197,9 @@ func updateStorageCluster(secretName, storageClusterName, storageClusterNamespac } // Update secret name + if sc.Spec.Mirroring == nil { + sc.Spec.Mirroring = &ocsv1.MirroringSpec{} + } if !utils.ContainsString(sc.Spec.Mirroring.PeerSecretNames, secretName) { sc.Spec.Mirroring.PeerSecretNames = append(sc.Spec.Mirroring.PeerSecretNames, secretName) err := spokeClient.Update(ctx, sc)