From 247d725aac43f46dc5c823fc59f4c1bd476511e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Tue, 28 Jun 2022 14:22:10 +0200 Subject: [PATCH 1/2] core: let IsInitialized return errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's return an error instead of a bool so we can better parse it and decide what to do with it. Signed-off-by: Sébastien Han --- pkg/daemon/ceph/client/config_test.go | 4 +- pkg/daemon/ceph/client/info.go | 55 ++++++++----------- pkg/operator/ceph/cluster/cluster.go | 4 +- pkg/operator/ceph/cluster/cluster_external.go | 4 +- pkg/operator/ceph/cluster/mon/health.go | 4 +- pkg/operator/ceph/cluster/mon/mon_test.go | 8 +-- pkg/operator/ceph/cluster/version.go | 4 +- 7 files changed, 36 insertions(+), 47 deletions(-) diff --git a/pkg/daemon/ceph/client/config_test.go b/pkg/daemon/ceph/client/config_test.go index a240b47b9..c3e4496ae 100644 --- a/pkg/daemon/ceph/client/config_test.go +++ b/pkg/daemon/ceph/client/config_test.go @@ -99,8 +99,8 @@ func TestGenerateConfigFile(t *testing.T) { Context: ctx, } - isInitialized := clusterInfo.IsInitialized(true) - assert.True(t, isInitialized) + isInitialized := clusterInfo.IsInitialized() + assert.NoError(t, isInitialized) // generate the config file to disk now configFilePath, err := generateConfigFile(context, clusterInfo, configDir, filepath.Join(configDir, "mykeyring"), nil, nil) diff --git a/pkg/daemon/ceph/client/info.go b/pkg/daemon/ceph/client/info.go index b4a736675..86226378b 100644 --- a/pkg/daemon/ceph/client/info.go +++ b/pkg/daemon/ceph/client/info.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/pkg/errors" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" cephver "github.com/rook/rook/pkg/operator/ceph/version" "github.com/rook/rook/pkg/operator/k8sutil" @@ -112,42 +113,30 @@ func AdminTestClusterInfo(namespace string) *ClusterInfo { // in. This method exists less out of necessity than the desire to be explicit about the lifecycle // of the ClusterInfo struct during startup, specifically that it is expected to exist after the // Rook operator has started up or connected to the first components of the Ceph cluster. -func (c *ClusterInfo) IsInitialized(logError bool) bool { - var isInitialized bool - +func (c *ClusterInfo) IsInitialized() error { if c == nil { - if logError { - logger.Error("clusterInfo is nil") - } - } else if c.FSID == "" { - if logError { - logger.Error("cluster fsid is empty") - } - } else if c.MonitorSecret == "" { - if logError { - logger.Error("monitor secret is empty") - } - } else if c.CephCred.Username == "" { - if logError { - logger.Error("ceph username is empty") - } - } else if c.CephCred.Secret == "" { - if logError { - logger.Error("ceph secret is empty") - } - } else if c.Context == nil { - if logError { - logger.Error("nil context") - } - } else if c.Context.Err() != nil { - if logError { - logger.Errorf("%v", c.Context.Err()) - } - } else { - isInitialized = true + return errors.New("clusterInfo is nil") + } + if c.FSID == "" { + return errors.New("cluster fsid is empty") + } + if c.MonitorSecret == "" { + return errors.New("monitor secret is empty") + } + if c.CephCred.Username == "" { + return errors.New("ceph username is empty") + } + if c.CephCred.Secret == "" { + return errors.New("ceph secret is empty") + } + if c.Context == nil { + return errors.New("context is nil") + } + if c.Context.Err() != nil { + return c.Context.Err() } - return isInitialized + return nil } // NewMonInfo returns a new Ceph mon info struct from the given inputs. diff --git a/pkg/operator/ceph/cluster/cluster.go b/pkg/operator/ceph/cluster/cluster.go index 6593aa341..7922cdb94 100755 --- a/pkg/operator/ceph/cluster/cluster.go +++ b/pkg/operator/ceph/cluster/cluster.go @@ -118,8 +118,8 @@ func (c *cluster) reconcileCephDaemons(rookImage string, cephVersion cephver.Cep c.ClusterInfo.NetworkSpec = c.Spec.Network // The cluster Identity must be established at this point - if !c.ClusterInfo.IsInitialized(true) { - return errors.New("the cluster identity was not established") + if err := c.ClusterInfo.IsInitialized(); err != nil { + return errors.Wrap(err, "the cluster identity was not established") } if c.ClusterInfo.Context.Err() != nil { diff --git a/pkg/operator/ceph/cluster/cluster_external.go b/pkg/operator/ceph/cluster/cluster_external.go index 56fa7ce1f..4d71bcd42 100644 --- a/pkg/operator/ceph/cluster/cluster_external.go +++ b/pkg/operator/ceph/cluster/cluster_external.go @@ -94,8 +94,8 @@ func (c *ClusterController) configureExternalCephCluster(cluster *cluster) error } // The cluster Identity must be established at this point - if !cluster.ClusterInfo.IsInitialized(true) { - return errors.New("the cluster identity was not established") + if err := cluster.ClusterInfo.IsInitialized(); err != nil { + return errors.Wrap(err, "the cluster identity was not established") } logger.Info("external cluster identity established") diff --git a/pkg/operator/ceph/cluster/mon/health.go b/pkg/operator/ceph/cluster/mon/health.go index 79d434425..d30ac21b8 100644 --- a/pkg/operator/ceph/cluster/mon/health.go +++ b/pkg/operator/ceph/cluster/mon/health.go @@ -148,8 +148,8 @@ func (c *Cluster) checkHealth(ctx context.Context) error { defer c.releaseOrchestrationLock() // If cluster details are not initialized - if !c.ClusterInfo.IsInitialized(true) { - return errors.New("skipping mon health check since cluster details are not initialized") + if err := c.ClusterInfo.IsInitialized(); err != nil { + return errors.Wrap(err, "skipping mon health check since cluster details are not initialized") } // If the cluster is converged and no mons were specified diff --git a/pkg/operator/ceph/cluster/mon/mon_test.go b/pkg/operator/ceph/cluster/mon/mon_test.go index fb4004503..66f305c50 100644 --- a/pkg/operator/ceph/cluster/mon/mon_test.go +++ b/pkg/operator/ceph/cluster/mon/mon_test.go @@ -213,7 +213,7 @@ func TestOperatorRestart(t *testing.T) { // start a basic cluster info, err := c.Start(c.ClusterInfo, c.rookVersion, cephver.Quincy, c.spec) assert.NoError(t, err) - assert.True(t, info.IsInitialized(true)) + assert.NoError(t, info.IsInitialized()) validateStart(t, c) @@ -223,7 +223,7 @@ func TestOperatorRestart(t *testing.T) { // starting again should be a no-op, but will not result in an error info, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Quincy, c.spec) assert.NoError(t, err) - assert.True(t, info.IsInitialized(true)) + assert.NoError(t, info.IsInitialized()) validateStart(t, c) } @@ -241,7 +241,7 @@ func TestOperatorRestartHostNetwork(t *testing.T) { // start a basic cluster info, err := c.Start(c.ClusterInfo, c.rookVersion, cephver.Quincy, c.spec) assert.NoError(t, err) - assert.True(t, info.IsInitialized(true)) + assert.NoError(t, info.IsInitialized()) validateStart(t, c) @@ -253,7 +253,7 @@ func TestOperatorRestartHostNetwork(t *testing.T) { // starting again should be a no-op, but still results in an error info, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Quincy, c.spec) assert.NoError(t, err) - assert.True(t, info.IsInitialized(true), info) + assert.NoError(t, info.IsInitialized(), info) validateStart(t, c) } diff --git a/pkg/operator/ceph/cluster/version.go b/pkg/operator/ceph/cluster/version.go index 1fc7d1a9f..6e6ac2afc 100644 --- a/pkg/operator/ceph/cluster/version.go +++ b/pkg/operator/ceph/cluster/version.go @@ -153,9 +153,9 @@ func (c *cluster) validateCephVersion(version *cephver.CephVersion) error { } } - if !clusterInfo.IsInitialized(false) { + if err := clusterInfo.IsInitialized(); err != nil { // If not initialized, this is likely a new cluster so there is nothing to do - logger.Debug("cluster not initialized, nothing to validate") + logger.Debugf("cluster not initialized, nothing to validate. %v", err) return nil } From a6036074dbaab6e8c25b0eed13d4894454294c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Tue, 28 Jun 2022 17:13:34 +0200 Subject: [PATCH 2/2] core: more checkpoint for context cancelled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some context canceled were missed and not reported correctly. We had multiple reconcilers running in //. Since they all run in goroutine then will continue to run until they timeout (if timeout is available, otherwise would run forever until the op is restarted). Signed-off-by: Sébastien Han --- pkg/operator/ceph/cluster/controller.go | 12 ++++++++++++ pkg/operator/ceph/cluster/mon/health.go | 6 ++++++ pkg/operator/ceph/cluster/mon/mon.go | 5 +++++ pkg/operator/ceph/cluster/osd/status.go | 7 ++++--- .../ceph/disruption/clusterdisruption/osd.go | 3 +++ 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/operator/ceph/cluster/controller.go b/pkg/operator/ceph/cluster/controller.go index f252226fe..33267ab6e 100644 --- a/pkg/operator/ceph/cluster/controller.go +++ b/pkg/operator/ceph/cluster/controller.go @@ -256,6 +256,18 @@ func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.R // Do reconcile here! ownerInfo := k8sutil.NewOwnerInfo(cephCluster, r.scheme) if err := r.clusterController.reconcileCephCluster(cephCluster, ownerInfo); err != nil { + // If the error has a context cancelled let's return a success result so that the controller can + // exit gracefully and the goroutine (the one the manager runs in) won't block retrying even if the parent context has been + // cancelled. + // Normally this should not happen but in some circumstances if the context is cancelled we will + // error-loop (backoff) and the controller will never exit (even though a new one has been + // started). So we would get messages from the other go routine printing a message + // repeatedly that the context is cancelled. + if errors.Is(r.opManagerContext.Err(), context.Canceled) { + logger.Info("context cancelled, exiting reconcile") + return reconcile.Result{}, *cephCluster, nil + } + return reconcile.Result{}, *cephCluster, errors.Wrapf(err, "failed to reconcile cluster %q", cephCluster.Name) } diff --git a/pkg/operator/ceph/cluster/mon/health.go b/pkg/operator/ceph/cluster/mon/health.go index d30ac21b8..3cb0eb653 100644 --- a/pkg/operator/ceph/cluster/mon/health.go +++ b/pkg/operator/ceph/cluster/mon/health.go @@ -133,6 +133,12 @@ func (hc *HealthChecker) Check(monitoringRoutines map[string]*controller.Cluster delete(monitoringRoutines, daemon) return + // Since c.ClusterInfo.IsInitialized() below uses a different context, we need to check if the context is done + case <-hc.monCluster.ClusterInfo.Context.Done(): + logger.Infof("stopping monitoring of mons in namespace %q", hc.monCluster.Namespace) + delete(monitoringRoutines, daemon) + return + case <-time.After(hc.interval): logger.Debugf("checking health of mons") err := hc.monCluster.checkHealth(monitoringRoutines[daemon].InternalCtx) diff --git a/pkg/operator/ceph/cluster/mon/mon.go b/pkg/operator/ceph/cluster/mon/mon.go index 6fe6c5097..6f77bebaa 100644 --- a/pkg/operator/ceph/cluster/mon/mon.go +++ b/pkg/operator/ceph/cluster/mon/mon.go @@ -1320,6 +1320,11 @@ func waitForQuorumWithMons(context *clusterd.Context, clusterInfo *cephclient.Cl retryCount := 0 retryMax := 30 for { + // Return immediately if the context has been canceled + if clusterInfo.Context.Err() != nil { + return clusterInfo.Context.Err() + } + retryCount++ if retryCount > retryMax { return errors.New("exceeded max retry count waiting for monitors to reach quorum") diff --git a/pkg/operator/ceph/cluster/osd/status.go b/pkg/operator/ceph/cluster/osd/status.go index 3e0b3dc71..d2d9aaf5d 100644 --- a/pkg/operator/ceph/cluster/osd/status.go +++ b/pkg/operator/ceph/cluster/osd/status.go @@ -268,13 +268,14 @@ func (c *Cluster) updateAndCreateOSDsLoop( updateConfig.updateExistingOSDs(errs) case <-minuteTicker.C: - if c.clusterInfo.Context.Err() != nil { - return false, c.clusterInfo.Context.Err() - } // Log progress c, cExp := createConfig.progress() u, uExp := updateConfig.progress() logger.Infof("waiting... %d of %d OSD prepare jobs have finished processing and %d of %d OSDs have been updated", c, cExp, u, uExp) + + case <-c.clusterInfo.Context.Done(): + logger.Infof("context cancelled, exiting OSD update and create loop") + return false, c.clusterInfo.Context.Err() } } diff --git a/pkg/operator/ceph/disruption/clusterdisruption/osd.go b/pkg/operator/ceph/disruption/clusterdisruption/osd.go index 1359bf8d8..2180d05ee 100644 --- a/pkg/operator/ceph/disruption/clusterdisruption/osd.go +++ b/pkg/operator/ceph/disruption/clusterdisruption/osd.go @@ -400,6 +400,9 @@ func (r *ReconcileClusterDisruption) reconcilePDBsForOSDs( err = r.client.Update(clusterInfo.Context, pdbStateMap) if err != nil { + if errors.Is(err, context.Canceled) { + return reconcile.Result{}, nil + } return reconcile.Result{}, errors.Wrapf(err, "failed to update configMap %q in cluster %q", pdbStateMapName, request) }