Set ownerReference for GC

Signed-off-by: Howard Lau <howardlau1999@hotmail.com>
This commit is contained in:
Howard Lau 2020-10-29 14:31:06 +00:00
parent c06204b5b6
commit 0d53ed3402
No known key found for this signature in database
GPG Key ID: 5CCC55849E3CF8E2
3 changed files with 28 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" 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) log := r.Log.WithValues("sw-filer-statefulset", seaweedCR.Name)
filerStatefulSet := r.createFilerStatefulSet(seaweedCR) 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 { _, err := r.CreateOrUpdate(filerStatefulSet, func(existing, desired runtime.Object) error {
existingStatefulSet := existing.(*appsv1.StatefulSet) existingStatefulSet := existing.(*appsv1.StatefulSet)
desiredStatefulSet := desired.(*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) log := r.Log.WithValues("sw-filer-headless-service", seaweedCR.Name)
filerHeadlessService := r.createFilerHeadlessService(seaweedCR) filerHeadlessService := r.createFilerHeadlessService(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, filerHeadlessService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateService(filerHeadlessService) _, err := r.CreateOrUpdateService(filerHeadlessService)
log.Info("ensure filer headless service " + filerHeadlessService.Name) 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) log := r.Log.WithValues("sw-filer-service", seaweedCR.Name)
filerService := r.createFilerService(seaweedCR) filerService := r.createFilerService(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, filerService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateService(filerService) _, err := r.CreateOrUpdateService(filerService)
log.Info("ensure filer service " + filerService.Name) log.Info("ensure filer service " + filerService.Name)

View File

@ -8,6 +8,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" 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) { if err != nil && errors.IsNotFound(err) {
// Define a new deployment // Define a new deployment
dep := r.createMasterStatefulSet(seaweedCR) 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) log.Info("Creating a new master statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
err = r.Create(ctx, dep) err = r.Create(ctx, dep)
if err != nil { if err != nil {
@ -70,6 +74,9 @@ func (r *SeaweedReconciler) ensureMasterService(seaweedCR *seaweedv1.Seaweed) (b
log := r.Log.WithValues("sw-master-service", seaweedCR.Name) log := r.Log.WithValues("sw-master-service", seaweedCR.Name)
masterService := r.createMasterService(seaweedCR) masterService := r.createMasterService(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, masterService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateService(masterService) _, err := r.CreateOrUpdateService(masterService)
log.Info("Get master service " + masterService.Name) log.Info("Get master service " + masterService.Name)

View File

@ -2,10 +2,12 @@ package controllers
import ( import (
"context" "context"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
) )
@ -14,11 +16,11 @@ func (r *SeaweedReconciler) ensureVolumeServers(seaweedCR *seaweedv1.Seaweed) (d
_ = context.Background() _ = context.Background()
_ = r.Log.WithValues("seaweed", seaweedCR.Name) _ = r.Log.WithValues("seaweed", seaweedCR.Name)
if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done { if done, result, err = r.ensureVolumeServerService(seaweedCR); done {
return return
} }
if done, result, err = r.ensureVolumeServerService(seaweedCR); done { if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done {
return return
} }
@ -29,6 +31,9 @@ func (r *SeaweedReconciler) ensureVolumeServerStatefulSet(seaweedCR *seaweedv1.S
log := r.Log.WithValues("sw-volume-statefulset", seaweedCR.Name) log := r.Log.WithValues("sw-volume-statefulset", seaweedCR.Name)
volumeServerStatefulSet := r.createVolumeServerStatefulSet(seaweedCR) 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 { _, err := r.CreateOrUpdate(volumeServerStatefulSet, func(existing, desired runtime.Object) error {
existingStatefulSet := existing.(*appsv1.StatefulSet) existingStatefulSet := existing.(*appsv1.StatefulSet)
desiredStatefulSet := desired.(*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) log := r.Log.WithValues("sw-volume-service", seaweedCR.Name)
volumeServerService := r.createVolumeServerService(seaweedCR) volumeServerService := r.createVolumeServerService(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, volumeServerService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateService(volumeServerService) _, err := r.CreateOrUpdateService(volumeServerService)
log.Info("ensure volume service " + volumeServerService.Name) log.Info("ensure volume service " + volumeServerService.Name)