From e19de4cd2900431524fb87678f9eafd3d975d080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Tue, 23 Nov 2021 11:25:11 +0100 Subject: [PATCH] nfs: always run default pool creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, if the pool was present we would not run the pool creation again. This is a problem if the pool spec changes, the new settings will never be applied. Signed-off-by: Sébastien Han (cherry picked from commit f3142847d391e075b59ab3a100227e74f57f2bcc) # Conflicts: # pkg/operator/ceph/nfs/controller_test.go --- pkg/operator/ceph/nfs/controller.go | 7 ++- pkg/operator/ceph/nfs/controller_test.go | 60 +++++++++++++++++++++++- pkg/operator/ceph/nfs/nfs.go | 17 ------- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/pkg/operator/ceph/nfs/controller.go b/pkg/operator/ceph/nfs/controller.go index fbbe57d9c8a2f..eb7335368f702 100644 --- a/pkg/operator/ceph/nfs/controller.go +++ b/pkg/operator/ceph/nfs/controller.go @@ -252,8 +252,11 @@ func (r *ReconcileCephNFS) reconcile(request reconcile.Request) (reconcile.Resul if err := validateGanesha(r.context, r.clusterInfo, cephNFS); err != nil { return reconcile.Result{}, errors.Wrapf(err, "invalid ceph nfs %q arguments", cephNFS.Name) } - if err := r.fetchOrCreatePool(cephNFS); err != nil { - return reconcile.Result{}, errors.Wrap(err, "failed to fetch or create RADOS pool") + + // Always create the default pool + err = r.createDefaultNFSRADOSPool(cephNFS) + if err != nil { + return reconcile.Result{}, errors.Wrapf(err, "failed to create default pool %q", cephNFS.Spec.RADOS.Pool) } // CREATE/UPDATE diff --git a/pkg/operator/ceph/nfs/controller_test.go b/pkg/operator/ceph/nfs/controller_test.go index c013e26d5d47b..b10f2d38abdff 100644 --- a/pkg/operator/ceph/nfs/controller_test.go +++ b/pkg/operator/ceph/nfs/controller_test.go @@ -19,11 +19,11 @@ package nfs import ( "context" - "errors" "os" "testing" "github.com/coreos/pkg/capnslog" + "github.com/pkg/errors" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake" "github.com/rook/rook/pkg/client/clientset/versioned/scheme" @@ -45,6 +45,7 @@ var ( name = "my-nfs" namespace = "rook-ceph" nfsCephAuthGetOrCreateKey = `{"key":"AQCvzWBeIV9lFRAAninzm+8XFxbSfTiPwoX50g=="}` +<<<<<<< HEAD dummyVersionsRaw = ` { "mon": { @@ -70,6 +71,8 @@ var ( "fast_read": 0, "pg_autoscale_mode": "on" }` +======= +>>>>>>> f3142847d (nfs: always run default pool creation) ) func TestCephNFSController(t *testing.T) { @@ -246,6 +249,7 @@ func TestCephNFSController(t *testing.T) { // Create a ReconcileCephNFS object with the scheme and fake client. r = &ReconcileCephNFS{client: cl, scheme: s, context: c} +<<<<<<< HEAD logger.Info("STARTING PHASE 3") res, err = r.Reconcile(ctx, req) assert.NoError(t, err) @@ -254,6 +258,60 @@ func TestCephNFSController(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "Ready", cephNFS.Status.Phase, cephNFS) logger.Info("PHASE 3 DONE") +======= + // Create a fake client to mock API calls. + cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build() + + executor = &exectest.MockExecutor{ + MockExecuteCommandWithOutput: func(command 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 nfsCephAuthGetOrCreateKey, nil + } + if args[0] == "osd" && args[1] == "pool" && args[2] == "create" { + return "", nil + } + if args[0] == "osd" && args[1] == "crush" && args[2] == "rule" { + return "", nil + } + if args[0] == "osd" && args[1] == "pool" && args[2] == "application" { + return "", nil + } + return "", errors.Errorf("unknown command %q %v", command, args) + }, + MockExecuteCommand: func(command string, args ...string) error { + if command == "rados" { + logger.Infof("mock execute. %s. %s", command, args) + assert.Equal(t, "stat", args[6]) + assert.Equal(t, "conf-nfs.my-nfs", args[7]) + 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, opManagerContext: ctx} + res, err := r.Reconcile(ctx, req) + assert.NoError(t, err) + assert.False(t, res.Requeue) + err = r.client.Get(context.TODO(), req.NamespacedName, cephNFS) + assert.NoError(t, err) + assert.Equal(t, "Ready", cephNFS.Status.Phase, cephNFS) + }) +>>>>>>> f3142847d (nfs: always run default pool creation) } func TestGetGaneshaConfigObject(t *testing.T) { diff --git a/pkg/operator/ceph/nfs/nfs.go b/pkg/operator/ceph/nfs/nfs.go index c41ef8e391da3..197eb70c87033 100644 --- a/pkg/operator/ceph/nfs/nfs.go +++ b/pkg/operator/ceph/nfs/nfs.go @@ -20,7 +20,6 @@ package nfs import ( "context" "fmt" - "strings" "github.com/banzaicloud/k8s-objectmatcher/patch" "github.com/pkg/errors" @@ -300,19 +299,3 @@ func (r *ReconcileCephNFS) createDefaultNFSRADOSPool(n *cephv1.CephNFS) error { return nil } - -func (r *ReconcileCephNFS) fetchOrCreatePool(n *cephv1.CephNFS) error { - // The existence of the pool provided in n.Spec.RADOS.Pool is necessary otherwise addRADOSConfigFile() will fail - _, err := cephclient.GetPoolDetails(r.context, r.clusterInfo, n.Spec.RADOS.Pool) - if err != nil { - if strings.Contains(err.Error(), "unrecognized pool") && r.clusterInfo.CephVersion.IsAtLeastPacific() { - err := r.createDefaultNFSRADOSPool(n) - if err != nil { - return errors.Wrapf(err, "failed to find %q pool and unable to create it", n.Spec.RADOS.Pool) - } - return nil - } - return errors.Wrapf(err, "pool %q not found", n.Spec.RADOS.Pool) - } - return nil -}