Repository: rook/rook
Pull request #12769 (2023-08-22 13:42:16)

csi: set ceph cluster as ControllerRef for holder ds (backport #12724)
This is an automatic backport of pull request #12724 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>


Patch:
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/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


Test patch:
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_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()},
 			},
 		}