From ce9b3ee2f2bdff485992f165e955ade515f9fa2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Thu, 27 Jan 2022 15:17:46 +0100 Subject: [PATCH] core: dereference pointer before trying to compare with deepequal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this, we were comparing a pointer (the memory address) with a struct. This was obviously always failing and returned false. We must dereference the pointer to access the data contained at that memory location. Closes: https://github.com/rook/rook/issues/9544 Signed-off-by: Sébastien Han (cherry picked from commit 5404ec13a29c09073a222986c4540d5f705a047c) --- pkg/operator/ceph/cluster/mon/predicate.go | 2 +- pkg/operator/ceph/cluster/osd/health.go | 8 ++-- pkg/operator/ceph/cluster/rbd/controller.go | 2 +- pkg/operator/ceph/controller/spec.go | 7 +++- pkg/operator/ceph/file/controller.go | 2 +- pkg/operator/ceph/file/mirror/controller.go | 2 +- pkg/operator/ceph/nfs/controller.go | 2 +- pkg/operator/ceph/object/controller.go | 2 +- pkg/operator/ceph/object/controller_test.go | 46 +++++++++++++++++++++ 9 files changed, 61 insertions(+), 12 deletions(-) diff --git a/pkg/operator/ceph/cluster/mon/predicate.go b/pkg/operator/ceph/cluster/mon/predicate.go index b858973ce..9a71666fe 100644 --- a/pkg/operator/ceph/cluster/mon/predicate.go +++ b/pkg/operator/ceph/cluster/mon/predicate.go @@ -96,7 +96,7 @@ func wereMonEndpointsUpdated(oldCMData, newCMData map[string]string) bool { // Iterate over the map and compare the values for _, v := range oldKeys { - if !reflect.DeepEqual(oldMappingToGo.Schedule[v], newMappingToGo.Schedule[v]) { + if !reflect.DeepEqual(*oldMappingToGo.Schedule[v], *newMappingToGo.Schedule[v]) { logger.Debugf("oldMappingToGo.Schedule[v] AND newMappingToGo.Schedule[v]: %v | %v", oldMappingToGo.Schedule[v], newMappingToGo.Schedule[v]) return true } diff --git a/pkg/operator/ceph/cluster/osd/health.go b/pkg/operator/ceph/cluster/osd/health.go index b8319bd0f..821c146b7 100644 --- a/pkg/operator/ceph/cluster/osd/health.go +++ b/pkg/operator/ceph/cluster/osd/health.go @@ -186,13 +186,13 @@ func (m *OSDHealthMonitor) removeOSDDeploymentIfSafeToDestroy(outOSDid int) erro // updateCephStorage updates the CR with deviceclass details func (m *OSDHealthMonitor) updateCephStatus(devices []string) { - cephCluster := &cephv1.CephCluster{} + cephCluster := cephv1.CephCluster{} cephClusterStorage := cephv1.CephStorage{} for _, device := range devices { cephClusterStorage.DeviceClasses = append(cephClusterStorage.DeviceClasses, cephv1.DeviceClasses{Name: device}) } - err := m.context.Client.Get(m.clusterInfo.Context, m.clusterInfo.NamespacedName(), cephCluster) + err := m.context.Client.Get(m.clusterInfo.Context, m.clusterInfo.NamespacedName(), &cephCluster) if err != nil { if kerrors.IsNotFound(err) { logger.Debug("CephCluster resource not found. Ignoring since object must be deleted.") @@ -201,9 +201,9 @@ func (m *OSDHealthMonitor) updateCephStatus(devices []string) { logger.Errorf("failed to retrieve ceph cluster %q to update ceph Storage. %v", m.clusterInfo.NamespacedName().Name, err) return } - if !reflect.DeepEqual(cephCluster.Status.CephStorage, &cephClusterStorage) { + if !reflect.DeepEqual(cephCluster.Status.CephStorage, cephClusterStorage) { cephCluster.Status.CephStorage = &cephClusterStorage - if err := reporting.UpdateStatus(m.context.Client, cephCluster); err != nil { + if err := reporting.UpdateStatus(m.context.Client, &cephCluster); err != nil { logger.Errorf("failed to update cluster %q Storage. %v", m.clusterInfo.NamespacedName().Name, err) return } diff --git a/pkg/operator/ceph/cluster/rbd/controller.go b/pkg/operator/ceph/cluster/rbd/controller.go index 601890eab..b9b42a12e 100644 --- a/pkg/operator/ceph/cluster/rbd/controller.go +++ b/pkg/operator/ceph/cluster/rbd/controller.go @@ -208,7 +208,7 @@ func (r *ReconcileCephRBDMirror) reconcile(request reconcile.Request) (reconcile // If the version of the Ceph monitor differs from the CephCluster CR image version we assume // the cluster is being upgraded. So the controller will just wait for the upgrade to finish and // then versions should match. Obviously using the cmd reporter job adds up to the deployment time - if !reflect.DeepEqual(runningCephVersion, desiredCephVersion) { + if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) { // Upgrade is in progress, let's wait for the mons to be done return opcontroller.WaitForRequeueIfCephClusterIsUpgrading, opcontroller.ErrorCephUpgradingRequeue(desiredCephVersion, runningCephVersion) diff --git a/pkg/operator/ceph/controller/spec.go b/pkg/operator/ceph/controller/spec.go index e7b9b51cb..d7e185f07 100644 --- a/pkg/operator/ceph/controller/spec.go +++ b/pkg/operator/ceph/controller/spec.go @@ -743,8 +743,11 @@ func ConfigureExternalMetricsEndpoint(ctx *clusterd.Context, monitoringSpec ceph } // If endpoints are identical there is nothing to do - if reflect.DeepEqual(currentEndpoints, endpoint) { - return nil + // First check for nil pointers otherwise dereferencing a nil pointer will cause a panic + if endpoint != nil && currentEndpoints != nil { + if reflect.DeepEqual(*currentEndpoints, *endpoint) { + return nil + } } logger.Debugf("diff between current endpoint and newly generated one: %v \n", cmp.Diff(currentEndpoints, endpoint, cmp.Comparer(func(x, y resource.Quantity) bool { return x.Cmp(y) == 0 }))) diff --git a/pkg/operator/ceph/file/controller.go b/pkg/operator/ceph/file/controller.go index afc0ec253..d15be7b48 100644 --- a/pkg/operator/ceph/file/controller.go +++ b/pkg/operator/ceph/file/controller.go @@ -302,7 +302,7 @@ func (r *ReconcileCephFilesystem) reconcile(request reconcile.Request) (reconcil // If the version of the Ceph monitor differs from the CephCluster CR image version we assume // the cluster is being upgraded. So the controller will just wait for the upgrade to finish and // then versions should match. Obviously using the cmd reporter job adds up to the deployment time - if !reflect.DeepEqual(runningCephVersion, desiredCephVersion) { + if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) { // Upgrade is in progress, let's wait for the mons to be done return opcontroller.WaitForRequeueIfCephClusterIsUpgrading, opcontroller.ErrorCephUpgradingRequeue(desiredCephVersion, runningCephVersion) diff --git a/pkg/operator/ceph/file/mirror/controller.go b/pkg/operator/ceph/file/mirror/controller.go index 001c7c838..ee595654c 100644 --- a/pkg/operator/ceph/file/mirror/controller.go +++ b/pkg/operator/ceph/file/mirror/controller.go @@ -195,7 +195,7 @@ func (r *ReconcileFilesystemMirror) reconcile(request reconcile.Request) (reconc // If the version of the Ceph monitor differs from the CephCluster CR image version we assume // the cluster is being upgraded. So the controller will just wait for the upgrade to finish and // then versions should match. Obviously using the cmd reporter job adds up to the deployment time - if !reflect.DeepEqual(runningCephVersion, desiredCephVersion) { + if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) { // Upgrade is in progress, let's wait for the mons to be done return opcontroller.WaitForRequeueIfCephClusterIsUpgrading, opcontroller.ErrorCephUpgradingRequeue(desiredCephVersion, runningCephVersion) diff --git a/pkg/operator/ceph/nfs/controller.go b/pkg/operator/ceph/nfs/controller.go index 96fea5f6a..1e70a3c86 100644 --- a/pkg/operator/ceph/nfs/controller.go +++ b/pkg/operator/ceph/nfs/controller.go @@ -245,7 +245,7 @@ func (r *ReconcileCephNFS) reconcile(request reconcile.Request) (reconcile.Resul // If the version of the Ceph monitor differs from the CephCluster CR image version we assume // the cluster is being upgraded. So the controller will just wait for the upgrade to finish and // then versions should match. Obviously using the cmd reporter job adds up to the deployment time - if !reflect.DeepEqual(runningCephVersion, desiredCephVersion) { + if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) { // Upgrade is in progress, let's wait for the mons to be done return opcontroller.WaitForRequeueIfCephClusterIsUpgrading, opcontroller.ErrorCephUpgradingRequeue(desiredCephVersion, runningCephVersion) diff --git a/pkg/operator/ceph/object/controller.go b/pkg/operator/ceph/object/controller.go index e155d376f..e8d527549 100644 --- a/pkg/operator/ceph/object/controller.go +++ b/pkg/operator/ceph/object/controller.go @@ -312,7 +312,7 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci // If the version of the Ceph monitor differs from the CephCluster CR image version we assume // the cluster is being upgraded. So the controller will just wait for the upgrade to finish and // then versions should match. Obviously using the cmd reporter job adds up to the deployment time - if !reflect.DeepEqual(runningCephVersion, desiredCephVersion) { + if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) { // Upgrade is in progress, let's wait for the mons to be done return opcontroller.WaitForRequeueIfCephClusterIsUpgrading, cephObjectStore, diff --git a/pkg/operator/ceph/object/controller_test.go b/pkg/operator/ceph/object/controller_test.go index 06f0ca247..aba42c08e 100644 --- a/pkg/operator/ceph/object/controller_test.go +++ b/pkg/operator/ceph/object/controller_test.go @@ -21,6 +21,7 @@ import ( "context" "net/http" "os" + "reflect" "testing" "time" @@ -745,3 +746,48 @@ func TestCephObjectStoreControllerMultisite(t *testing.T) { assert.True(t, calledCommitConfigChanges) }) } + +func TestDiffVersions(t *testing.T) { + executor := &exectest.MockExecutor{ + MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { + if args[0] == "versions" { + return `{ + "mon": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 3 + }, + "mgr": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 1 + }, + "osd": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 3 + }, + "mds": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 2 + }, + "rgw": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 1 + }, + "overall": { + "ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)": 10 + } +}`, nil + } + return "", errors.Errorf("unknown command %s %v", command, args) + }, + } + c := &clusterd.Context{Executor: executor} + + // desiredCephVersion comes from DetectCephVersion() (ceph --version) which uses ExtractCephVersion() + desiredCephVersion, err := cephver.ExtractCephVersion("ceph version 17.0.0-9718-g4ff72306 (4ff723061fc15c803dcf6556d02f56bdf56de5fa) quincy (dev)") + assert.NoError(t, err) + + // runningCephVersion comes from LeastUptodateDaemonVersion() + runningCephVersion, err := client.LeastUptodateDaemonVersion(c, &client.ClusterInfo{Context: context.TODO()}, "mon") + assert.NoError(t, err) + + // Compares the pointer's address with the struct so it's wrong + assert.False(t, reflect.DeepEqual(runningCephVersion, desiredCephVersion)) + + // Compares the actual value of the pointer by dereferencing the pointer + assert.True(t, reflect.DeepEqual(runningCephVersion, *desiredCephVersion)) +}