diff --git a/pkg/operator/ceph/controller/controller_utils.go b/pkg/operator/ceph/controller/controller_utils.go index a80f0ab90..24e10d849 100644 --- a/pkg/operator/ceph/controller/controller_utils.go +++ b/pkg/operator/ceph/controller/controller_utils.go @@ -24,7 +24,6 @@ import ( cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/pkg/clusterd" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" - "github.com/rook/rook/pkg/operator/k8sutil" kerrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -65,24 +64,26 @@ func IsReadyToReconcile(client client.Client, clustercontext *clusterd.Context, logger.Debugf("CephCluster resource %q found in namespace %q", namespacedName.Name, namespacedName.Namespace) - // If the cluster is ready - if cephCluster.Status.Phase == k8sutil.ReadyStatus { - // Test a Ceph command to verify the Operator is ready - // This is done to silence errors when the operator just started and cannot reconcile yet - _, err = cephclient.Status(clustercontext, namespacedName.Namespace) - if err != nil { - if strings.Contains(err.Error(), "error calling conf_read_file") { - logger.Info("operator is not ready to run ceph command, cannot reconcile yet.") - return cephCluster.Spec, false, cephClusterExists, WaitForRequeueIfCephClusterNotReady - } - // We should not arrive there - logger.Errorf("ceph command error %v", err) - return cephCluster.Spec, false, cephClusterExists, ImmediateRetryResult + // If the cluster is healthy + // Test a Ceph command to verify the Operator is ready + // This is done to silence errors when the operator just started and cannot reconcile yet + status, err := cephclient.Status(clustercontext, namespacedName.Namespace) + if err != nil { + if strings.Contains(err.Error(), "error calling conf_read_file") { + logger.Info("operator is not ready to run ceph command, cannot reconcile yet.") + return cephCluster.Spec, false, cephClusterExists, WaitForRequeueIfCephClusterNotReady } - logger.Debugf("operator is ready to run ceph command, reconciling") + // We should not arrive there + logger.Errorf("ceph command error %v", err) + return cephCluster.Spec, false, cephClusterExists, ImmediateRetryResult + } + + // If Ceph status is ok we can reconcile + if status.Health.Status == "HEALTH_OK" || status.Health.Status == "HEALTH_WARN" { + logger.Debugf("ceph status is %q, operator is ready to run ceph command, reconciling", status.Health.Status) return cephCluster.Spec, true, cephClusterExists, reconcile.Result{} } - logger.Debugf("CephCluster resource %q found in namespace %q but not ready yet", namespacedName.Name, namespacedName.Namespace) + logger.Debugf("CephCluster resource %q found in namespace %q but cluster state is not appropriate %q", namespacedName.Name, namespacedName.Namespace, status.Health.Status) return cephCluster.Spec, false, cephClusterExists, WaitForRequeueIfCephClusterNotReady } diff --git a/pkg/operator/ceph/file/controller_test.go b/pkg/operator/ceph/file/controller_test.go index 1a27a78f5..ac324d2a1 100644 --- a/pkg/operator/ceph/file/controller_test.go +++ b/pkg/operator/ceph/file/controller_test.go @@ -179,13 +179,7 @@ func TestCephObjectStoreController(t *testing.T) { executor := &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { if args[0] == "status" { - return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil - } - if args[0] == "fs" && args[1] == "get" { - return fsGet, nil - } - if args[0] == "auth" && args[1] == "get-or-create-key" { - return mdsCephAuthGetOrCreateKey, nil + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_ERR"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } if args[0] == "versions" { return dummyVersionsRaw, nil @@ -193,6 +187,7 @@ func TestCephObjectStoreController(t *testing.T) { return "", nil }, } + clientset := test.New(t, 3) c := &clusterd.Context{ Executor: executor, @@ -279,6 +274,25 @@ func TestCephObjectStoreController(t *testing.T) { // Create a fake client to mock API calls. cl = fake.NewFakeClientWithScheme(s, object...) + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { + if args[0] == "status" { + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_OK"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + } + if args[0] == "fs" && args[1] == "get" { + return fsGet, nil + } + if args[0] == "auth" && args[1] == "get-or-create-key" { + return mdsCephAuthGetOrCreateKey, nil + } + if args[0] == "versions" { + return dummyVersionsRaw, nil + } + return "", nil + }, + } + c.Executor = executor + // Create a ReconcileCephFilesystem object with the scheme and fake client. r = &ReconcileCephFilesystem{client: cl, scheme: s, context: c} diff --git a/pkg/operator/ceph/nfs/controller_test.go b/pkg/operator/ceph/nfs/controller_test.go index 4a6a370de..b6e309dfa 100644 --- a/pkg/operator/ceph/nfs/controller_test.go +++ b/pkg/operator/ceph/nfs/controller_test.go @@ -107,32 +107,12 @@ func TestCephNFSController(t *testing.T) { executor := &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { if args[0] == "status" { - return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_ERR"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } if args[0] == "versions" { return dummyVersionsRaw, nil } - if args[0] == "osd" && args[1] == "pool" && args[2] == "get" { - return poolDetails, nil - } - return "", errors.New("unknown command") - }, - MockExecuteCommand: func(command string, args ...string) error { - if command == "rados" { - logger.Infof("mock execute. %s. %s", command, args) - assert.Equal(t, "stat", args[6]) - return nil - } - return errors.New("unknown command") - }, - MockExecuteCommandWithEnv: func(env []string, command string, args ...string) error { - if command == "ganesha-rados-grace" { - logger.Infof("mock execute. %s. %s", command, args) - assert.Equal(t, "add", args[4]) - assert.Len(t, env, 1) - return nil - } - return errors.New("unknown command") + return "", nil }, } clientset := test.New(t, 3) @@ -221,6 +201,39 @@ func TestCephNFSController(t *testing.T) { // Create a fake client to mock API calls. cl = fake.NewFakeClientWithScheme(s, object...) + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { + if args[0] == "status" { + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_OK"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + } + if args[0] == "versions" { + return dummyVersionsRaw, nil + } + if args[0] == "osd" && args[1] == "pool" && args[2] == "get" { + return poolDetails, nil + } + return "", errors.New("unknown command") + }, + MockExecuteCommand: func(command string, args ...string) error { + if command == "rados" { + logger.Infof("mock execute. %s. %s", command, args) + assert.Equal(t, "stat", args[6]) + return nil + } + return errors.New("unknown command") + }, + MockExecuteCommandWithEnv: func(env []string, command string, args ...string) error { + if command == "ganesha-rados-grace" { + logger.Infof("mock execute. %s. %s", command, args) + assert.Equal(t, "add", args[4]) + assert.Len(t, env, 1) + return nil + } + return errors.New("unknown command") + }, + } + c.Executor = executor + // Create a ReconcileCephNFS object with the scheme and fake client. r = &ReconcileCephNFS{client: cl, scheme: s, context: c} diff --git a/pkg/operator/ceph/object/controller_test.go b/pkg/operator/ceph/object/controller_test.go index 292892156..b775e54cf 100644 --- a/pkg/operator/ceph/object/controller_test.go +++ b/pkg/operator/ceph/object/controller_test.go @@ -174,31 +174,13 @@ func TestCephObjectStoreController(t *testing.T) { executor := &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { if args[0] == "status" { - return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil - } - if args[0] == "auth" && args[1] == "get-or-create-key" { - return rgwCephAuthGetOrCreateKey, nil + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_ERR"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } if args[0] == "versions" { return dummyVersionsRaw, nil } return "", nil }, - MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { - if args[0] == "realm" && args[1] == "list" { - return realmListJSON, nil - } - if args[0] == "realm" && args[1] == "get" { - return realmGetJSON, nil - } - if args[0] == "zonegroup" && args[1] == "get" { - return zoneGroupGetJSON, nil - } - if args[0] == "zone" && args[1] == "get" { - return zoneGetJSON, nil - } - return "", nil - }, } clientset := test.New(t, 3) c := &clusterd.Context{ @@ -280,12 +262,41 @@ func TestCephObjectStoreController(t *testing.T) { _, err = c.Clientset.CoreV1().Secrets(namespace).Create(secret) assert.NoError(t, err) - // Add ready status to the CephCluster - cephCluster.Status.Phase = k8sutil.ReadyStatus - // Create a fake client to mock API calls. cl = fake.NewFakeClientWithScheme(s, object...) + // Override executor with the new ceph status and more content + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { + if args[0] == "status" { + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_OK"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + } + if args[0] == "auth" && args[1] == "get-or-create-key" { + return rgwCephAuthGetOrCreateKey, nil + } + if args[0] == "versions" { + return dummyVersionsRaw, nil + } + return "", nil + }, + MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { + if args[0] == "realm" && args[1] == "list" { + return realmListJSON, nil + } + if args[0] == "realm" && args[1] == "get" { + return realmGetJSON, nil + } + if args[0] == "zonegroup" && args[1] == "get" { + return zoneGroupGetJSON, nil + } + if args[0] == "zone" && args[1] == "get" { + return zoneGetJSON, nil + } + return "", nil + }, + } + c.Executor = executor + // Create a ReconcileCephObjectStore object with the scheme and fake client. r = &ReconcileCephObjectStore{client: cl, scheme: s, context: c} diff --git a/pkg/operator/ceph/object/user/controller_test.go b/pkg/operator/ceph/object/user/controller_test.go index 0feb70839..a0a6556e4 100644 --- a/pkg/operator/ceph/object/user/controller_test.go +++ b/pkg/operator/ceph/object/user/controller_test.go @@ -118,13 +118,7 @@ func TestCephObjectStoreUserController(t *testing.T) { executor := &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { if args[0] == "status" { - return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil - } - return "", nil - }, - MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { - if args[0] == "user" { - return userCreateJSON, nil + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_ERR"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } return "", nil }, @@ -190,6 +184,23 @@ func TestCephObjectStoreUserController(t *testing.T) { cephCluster.Status.Phase = k8sutil.ReadyStatus // Create a fake client to mock API calls. cl = fake.NewFakeClientWithScheme(s, object...) + + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { + if args[0] == "status" { + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_OK"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + } + return "", nil + }, + MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { + if args[0] == "user" { + return userCreateJSON, nil + } + return "", nil + }, + } + c.Executor = executor + // Create a ReconcileObjectStoreUser object with the scheme and fake client. r = &ReconcileObjectStoreUser{client: cl, scheme: s, context: c} diff --git a/pkg/operator/ceph/pool/controller_test.go b/pkg/operator/ceph/pool/controller_test.go index 6c66b05d2..2df0830e4 100644 --- a/pkg/operator/ceph/pool/controller_test.go +++ b/pkg/operator/ceph/pool/controller_test.go @@ -238,7 +238,7 @@ func TestCephBlockPoolController(t *testing.T) { executor := &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { if args[0] == "status" { - return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_ERR"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } return "", nil @@ -313,6 +313,18 @@ func TestCephBlockPoolController(t *testing.T) { } // Create a fake client to mock API calls. cl = fake.NewFakeClient(objects...) + + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(command, outfile string, args ...string) (string, error) { + if args[0] == "status" { + return `{"fsid":"c47cac40-9bee-4d52-823b-ccd803ba5bfe","health":{"checks":{},"status":"HEALTH_OK"},"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil + } + + return "", nil + }, + } + c.Executor = executor + // Create a ReconcileCephBlockPool object with the scheme and fake client. r = &ReconcileCephBlockPool{client: cl, scheme: s, context: c}