Merge pull request #9 from howardlau1999/owner

Set ownerReference for GC
This commit is contained in:
Chris Lu 2020-10-29 08:51:33 -07:00 committed by GitHub
commit 9e229fc647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)