From 0d53ed3402101b197dbbdb05530849b892cb6bb5 Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Thu, 29 Oct 2020 14:31:06 +0000 Subject: [PATCH] Set ownerReference for GC Signed-off-by: Howard Lau --- controllers/controller_filer.go | 11 +++++++++++ controllers/controller_master.go | 7 +++++++ controllers/controller_volume.go | 12 ++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/controllers/controller_filer.go b/controllers/controller_filer.go index 2d42f1a..4f1489a 100644 --- a/controllers/controller_filer.go +++ b/controllers/controller_filer.go @@ -7,6 +7,7 @@ import ( appsv1 "k8s.io/api/apps/v1" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" ) @@ -34,6 +35,9 @@ func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) log := r.Log.WithValues("sw-filer-statefulset", seaweedCR.Name) filerStatefulSet := r.createFilerStatefulSet(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, filerStatefulSet, r.Scheme); err != nil { + return ReconcileResult(err) + } _, err := r.CreateOrUpdate(filerStatefulSet, func(existing, desired runtime.Object) error { existingStatefulSet := existing.(*appsv1.StatefulSet) desiredStatefulSet := desired.(*appsv1.StatefulSet) @@ -51,6 +55,10 @@ func (r *SeaweedReconciler) ensureFilerHeadlessService(seaweedCR *seaweedv1.Seaw log := r.Log.WithValues("sw-filer-headless-service", seaweedCR.Name) filerHeadlessService := r.createFilerHeadlessService(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, filerHeadlessService, r.Scheme); err != nil { + return ReconcileResult(err) + } + _, err := r.CreateOrUpdateService(filerHeadlessService) log.Info("ensure filer headless service " + filerHeadlessService.Name) @@ -63,6 +71,9 @@ func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bo log := r.Log.WithValues("sw-filer-service", seaweedCR.Name) filerService := r.createFilerService(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, filerService, r.Scheme); err != nil { + return ReconcileResult(err) + } _, err := r.CreateOrUpdateService(filerService) log.Info("ensure filer service " + filerService.Name) diff --git a/controllers/controller_master.go b/controllers/controller_master.go index 4b4f7a5..192f2ee 100644 --- a/controllers/controller_master.go +++ b/controllers/controller_master.go @@ -8,6 +8,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" ) @@ -36,6 +37,9 @@ func (r *SeaweedReconciler) ensureMasterStatefulSet(seaweedCR *seaweedv1.Seaweed if err != nil && errors.IsNotFound(err) { // Define a new deployment dep := r.createMasterStatefulSet(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, dep, r.Scheme); err != nil { + return ReconcileResult(err) + } log.Info("Creating a new master statefulset", "Namespace", dep.Namespace, "Name", dep.Name) err = r.Create(ctx, dep) if err != nil { @@ -70,6 +74,9 @@ func (r *SeaweedReconciler) ensureMasterService(seaweedCR *seaweedv1.Seaweed) (b log := r.Log.WithValues("sw-master-service", seaweedCR.Name) masterService := r.createMasterService(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, masterService, r.Scheme); err != nil { + return ReconcileResult(err) + } _, err := r.CreateOrUpdateService(masterService) log.Info("Get master service " + masterService.Name) diff --git a/controllers/controller_volume.go b/controllers/controller_volume.go index cd77c6e..07d4537 100644 --- a/controllers/controller_volume.go +++ b/controllers/controller_volume.go @@ -2,10 +2,12 @@ package controllers import ( "context" + "k8s.io/apimachinery/pkg/runtime" appsv1 "k8s.io/api/apps/v1" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" ) @@ -14,11 +16,11 @@ func (r *SeaweedReconciler) ensureVolumeServers(seaweedCR *seaweedv1.Seaweed) (d _ = context.Background() _ = r.Log.WithValues("seaweed", seaweedCR.Name) - if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done { + if done, result, err = r.ensureVolumeServerService(seaweedCR); done { return } - if done, result, err = r.ensureVolumeServerService(seaweedCR); done { + if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done { return } @@ -29,6 +31,9 @@ func (r *SeaweedReconciler) ensureVolumeServerStatefulSet(seaweedCR *seaweedv1.S log := r.Log.WithValues("sw-volume-statefulset", seaweedCR.Name) volumeServerStatefulSet := r.createVolumeServerStatefulSet(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, volumeServerStatefulSet, r.Scheme); err != nil { + return ReconcileResult(err) + } _, err := r.CreateOrUpdate(volumeServerStatefulSet, func(existing, desired runtime.Object) error { existingStatefulSet := existing.(*appsv1.StatefulSet) desiredStatefulSet := desired.(*appsv1.StatefulSet) @@ -47,6 +52,9 @@ func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seawe log := r.Log.WithValues("sw-volume-service", seaweedCR.Name) volumeServerService := r.createVolumeServerService(seaweedCR) + if err := controllerutil.SetControllerReference(seaweedCR, volumeServerService, r.Scheme); err != nil { + return ReconcileResult(err) + } _, err := r.CreateOrUpdateService(volumeServerService) log.Info("ensure volume service " + volumeServerService.Name)