From d7a4f0038c3ebc23e8125c57b36ec11140abbc67 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Tue, 15 Aug 2023 12:41:18 +0200 Subject: [PATCH] csi: set ceph cluster as ControllerRef for holder Setting ceph cluster as the ControllerRef for the holder daemonset set so that when a ceph cluster is deleted the holder daemoset will also gets deleted. fixes: #12645 Signed-off-by: Madhu Rajanna --- pkg/operator/ceph/csi/controller.go | 4 ++++ pkg/operator/ceph/csi/controller_test.go | 1 + pkg/operator/ceph/csi/spec.go | 12 ++++++++++-- pkg/operator/ceph/csi/spec_test.go | 5 ++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/operator/ceph/csi/controller.go b/pkg/operator/ceph/csi/controller.go index 4af1e227e..18cd42873 100644 --- a/pkg/operator/ceph/csi/controller.go +++ b/pkg/operator/ceph/csi/controller.go @@ -38,6 +38,7 @@ import ( v1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ) @@ -47,6 +48,7 @@ const ( // ReconcileCSI reconciles a ceph-csi driver type ReconcileCSI struct { + scheme *runtime.Scheme client client.Client context *clusterd.Context opManagerContext context.Context @@ -70,6 +72,7 @@ func Add(mgr manager.Manager, context *clusterd.Context, opManagerContext contex // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager, context *clusterd.Context, opManagerContext context.Context, opConfig opcontroller.OperatorConfig) reconcile.Reconciler { return &ReconcileCSI{ + scheme: mgr.GetScheme(), client: mgr.GetClient(), context: context, opConfig: opConfig, @@ -207,6 +210,7 @@ func (r *ReconcileCSI) reconcile(request reconcile.Request) (reconcile.Result, e } return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "failed to load cluster info for cluster %q", cluster.Name) } + clusterInfo.OwnerInfo = k8sutil.NewOwnerInfo(&cephClusters.Items[i], r.scheme) logger.Debugf("cluster %q is running on multus or CSI_ENABLE_HOST_NETWORK is false, deploying the ceph-csi plugin holder", cluster.Name) diff --git a/pkg/operator/ceph/csi/controller_test.go b/pkg/operator/ceph/csi/controller_test.go index 767b4d922..693739345 100644 --- a/pkg/operator/ceph/csi/controller_test.go +++ b/pkg/operator/ceph/csi/controller_test.go @@ -215,6 +215,7 @@ func TestCephCSIController(t *testing.T) { c.Client = cl // // Create a ReconcileCSI object with the scheme and fake client. r := &ReconcileCSI{ + scheme: s, client: cl, context: c, opConfig: controller.OperatorConfig{ diff --git a/pkg/operator/ceph/csi/spec.go b/pkg/operator/ceph/csi/spec.go index ce7ef957b..3029d06b0 100644 --- a/pkg/operator/ceph/csi/spec.go +++ b/pkg/operator/ceph/csi/spec.go @@ -802,8 +802,16 @@ func (r *ReconcileCSI) configureHolder(driver driverDetails, c ClusterDetail, tp return errors.Wrapf(err, "failed to load ceph %q plugin holder template", driver.fullName) } - // DO NOT set owner reference on ceph plugin holder daemonset, this DS must never restart unless - // the entire node is rebooted + // As the plugin holder daemonset is created in the operator namespace, we + // need to set the owner reference to the cluster namespace only if the + // operator and cluster are created in same namespace so that the + // plugin holder daemonset is deleted when the cluster is deleted. + if r.opConfig.OperatorNamespace == c.cluster.Namespace { + err = c.clusterInfo.OwnerInfo.SetControllerReference(cephPluginHolder) + if err != nil { + return errors.Wrapf(err, "failed to set owner reference to plugin holder %q", driver.fullName) + } + } holderPluginTolerations := getToleration(r.opConfig.Parameters, driver.toleration, pluginTolerations) holderPluginNodeAffinity := getNodeAffinity(r.opConfig.Parameters, driver.nodeAffinity, pluginNodeAffinity) // apply driver's plugin tolerations and node affinity diff --git a/pkg/operator/ceph/csi/spec_test.go b/pkg/operator/ceph/csi/spec_test.go index 8eb3c7d81..d8747f483 100644 --- a/pkg/operator/ceph/csi/spec_test.go +++ b/pkg/operator/ceph/csi/spec_test.go @@ -24,6 +24,7 @@ import ( 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/clusterd" + "github.com/rook/rook/pkg/daemon/ceph/client" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" opcontroller "github.com/rook/rook/pkg/operator/ceph/controller" "github.com/rook/rook/pkg/operator/k8sutil" @@ -73,7 +74,9 @@ func TestReconcileCSI_configureHolders(t *testing.T) { ObjectMeta: metav1.ObjectMeta{Namespace: "rook-ceph", Name: "my-cluster"}, Spec: cephv1.ClusterSpec{}, }, - clusterInfo: &cephclient.ClusterInfo{Monitors: map[string]*cephclient.MonInfo{"a": {Name: "a", Endpoint: "10.0.0.1:6789"}}}, + clusterInfo: &cephclient.ClusterInfo{ + Monitors: map[string]*cephclient.MonInfo{"a": {Name: "a", Endpoint: "10.0.0.1:6789"}}, + OwnerInfo: client.NewMinimumOwnerInfoWithOwnerRef()}, }, }