From 48feb7d4aa49128fcb88f3be9cdfadf2c8266d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Thu, 17 Feb 2022 15:30:25 +0100 Subject: [PATCH] core: check for canceled context in external mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is fixing a potential race where the context is canceled and the external cluster is trying to fetch the cluster info. If the context is canceled then the loop will be stuck forever. Now we check for the canceled context and return appropriately. Signed-off-by: Sébastien Han (cherry picked from commit fdcad15b873bc711a24fed39c9b161e300b28104) --- pkg/daemon/ceph/client/info.go | 4 ++++ pkg/operator/ceph/cluster/cluster_external.go | 5 ++++- pkg/operator/ceph/cluster/mon/config.go | 16 +++++++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/daemon/ceph/client/info.go b/pkg/daemon/ceph/client/info.go index e0c28890d..b27bbbbf1 100644 --- a/pkg/daemon/ceph/client/info.go +++ b/pkg/daemon/ceph/client/info.go @@ -138,6 +138,10 @@ func (c *ClusterInfo) IsInitialized(logError bool) bool { if logError { logger.Error("nil context") } + } else if c.Context.Err() != nil { + if logError { + logger.Errorf("%v", c.Context.Err()) + } } else { isInitialized = true } diff --git a/pkg/operator/ceph/cluster/cluster_external.go b/pkg/operator/ceph/cluster/cluster_external.go index fb97a9adf..a77a6f184 100644 --- a/pkg/operator/ceph/cluster/cluster_external.go +++ b/pkg/operator/ceph/cluster/cluster_external.go @@ -49,7 +49,10 @@ func (c *ClusterController) configureExternalCephCluster(cluster *cluster) error // loop until we find the secret necessary to connect to the external cluster // then populate clusterInfo - cluster.ClusterInfo = mon.PopulateExternalClusterInfo(c.context, c.OpManagerCtx, c.namespacedName.Namespace, cluster.ownerInfo) + cluster.ClusterInfo, err = mon.PopulateExternalClusterInfo(c.context, c.OpManagerCtx, c.namespacedName.Namespace, cluster.ownerInfo) + if err != nil { + return errors.Wrap(err, "failed to populate external cluster info") + } cluster.ClusterInfo.SetName(c.namespacedName.Name) cluster.ClusterInfo.Context = c.OpManagerCtx diff --git a/pkg/operator/ceph/cluster/mon/config.go b/pkg/operator/ceph/cluster/mon/config.go index 56c9d23ac..b29f2fb30 100644 --- a/pkg/operator/ceph/cluster/mon/config.go +++ b/pkg/operator/ceph/cluster/mon/config.go @@ -373,18 +373,24 @@ func ExtractKey(contents string) (string, error) { return secret, nil } -// PopulateExternalClusterInfo Add validation in the code to fail if the external cluster has no OSDs keep waiting -func PopulateExternalClusterInfo(context *clusterd.Context, ctx context.Context, namespace string, ownerInfo *k8sutil.OwnerInfo) *cephclient.ClusterInfo { +// PopulateExternalClusterInfo Add validation in the code to fail if the external cluster has no +// OSDs keep waiting +func PopulateExternalClusterInfo(context *clusterd.Context, ctx context.Context, namespace string, ownerInfo *k8sutil.OwnerInfo) (*cephclient.ClusterInfo, error) { for { - clusterInfo, _, _, err := LoadClusterInfo(context, ctx, namespace) + // Checking for the context makes sure we don't loop forever with a canceled context + if ctx.Err() != nil { + return nil, ctx.Err() + } + clusterInfo, _, _, err := CreateOrLoadClusterInfo(context, ctx, namespace, nil) if err != nil { - logger.Warningf("waiting for the csi connection info of the external cluster. retrying in %s.", externalConnectionRetry.String()) + logger.Warningf("waiting for connection info of the external cluster. retrying in %s.", externalConnectionRetry.String()) logger.Debugf("%v", err) time.Sleep(externalConnectionRetry) continue } logger.Infof("found the cluster info to connect to the external cluster. will use %q to check health and monitor status. mons=%+v", clusterInfo.CephCred.Username, clusterInfo.Monitors) clusterInfo.OwnerInfo = ownerInfo - return clusterInfo + + return clusterInfo, nil } }