From c7e0392c1c269fea45ff65585e5db4c9f837b9d2 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 14 Oct 2020 21:45:53 -0700 Subject: [PATCH] refactor --- Makefile | 7 +++++-- controllers/controller_filer.go | 22 +++++++++++----------- controllers/controller_master.go | 22 +++++++++++----------- controllers/controller_s3.go | 22 +++++++++++----------- controllers/controller_volume.go | 22 +++++++++++----------- controllers/helper.go | 10 ++++++++++ 6 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 controllers/helper.go diff --git a/Makefile b/Makefile index bea67d7..1b1c494 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ endif BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) # Image URL to use all building/pushing image targets -IMG ?= controller:latest +IMG ?= seaweedfs/operator:latest # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) CRD_OPTIONS ?= "crd:trivialVersions=true" @@ -26,8 +26,11 @@ endif all: manager # Run tests +ENVTEST_ASSETS_DIR=$(shell pwd)/testbin test: generate fmt vet manifests - go test ./... -coverprofile cover.out + mkdir -p ${ENVTEST_ASSETS_DIR} + test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh + source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out # Build manager binary manager: generate fmt vet diff --git a/controllers/controller_filer.go b/controllers/controller_filer.go index 059e385..409dfc7 100644 --- a/controllers/controller_filer.go +++ b/controllers/controller_filer.go @@ -17,14 +17,14 @@ func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (do _ = r.Log.WithValues("seaweed", seaweedCR.Name) if done, result, err = r.ensureFilerStatefulSet(seaweedCR); done { - return done, result, err + return } if done, result, err = r.ensureFilerService(seaweedCR); done { - return done, result, err + return } - return false, ctrl.Result{}, nil + return } func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -40,16 +40,16 @@ func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new filer statefulset", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get filer statefulset") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get filer stateful set " + filerStatefulSet.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -65,16 +65,16 @@ func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bo err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new filer service", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get filer server service") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get filer service " + volumeServerService.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func labelsForFiler(name string) map[string]string { diff --git a/controllers/controller_master.go b/controllers/controller_master.go index 53a68f6..fbcb19d 100644 --- a/controllers/controller_master.go +++ b/controllers/controller_master.go @@ -22,14 +22,14 @@ func (r *SeaweedReconciler) ensureMaster(seaweedCR *seaweedv1.Seaweed) (done boo _ = r.Log.WithValues("seaweed", seaweedCR.Name) if done, result, err = r.ensureMasterService(seaweedCR); done { - return done, result, err + return } if done, result, err = r.ensureMasterStatefulSet(seaweedCR); done { - return done, result, err + return } - return false, ctrl.Result{}, nil + return } func (r *SeaweedReconciler) ensureMasterStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -45,18 +45,18 @@ func (r *SeaweedReconciler) ensureMasterStatefulSet(seaweedCR *seaweedv1.Seaweed err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new statefulset", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // sleep 60 seconds for DNS to have pod IP addresses ready time.Sleep(time.Minute) // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get Deployment") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get master stateful set " + masterStatefulSet.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func (r *SeaweedReconciler) ensureMasterService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -72,16 +72,16 @@ func (r *SeaweedReconciler) ensureMasterService(seaweedCR *seaweedv1.Seaweed) (b err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create master service", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get master service", "Namespace", seaweedCR.Namespace, "Name", seaweedCR.Name+"-master") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get master service " + masterService.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } diff --git a/controllers/controller_s3.go b/controllers/controller_s3.go index 224d474..9995af8 100644 --- a/controllers/controller_s3.go +++ b/controllers/controller_s3.go @@ -17,14 +17,14 @@ func (r *SeaweedReconciler) ensureS3Servers(seaweedCR *seaweedv1.Seaweed) (done _ = r.Log.WithValues("seaweed", seaweedCR.Name) if done, result, err = r.ensureS3Deployment(seaweedCR); done { - return done, result, err + return } if done, result, err = r.ensureS3Service(seaweedCR); done { - return done, result, err + return } - return false, ctrl.Result{}, nil + return } func (r *SeaweedReconciler) ensureS3Deployment(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -40,16 +40,16 @@ func (r *SeaweedReconciler) ensureS3Deployment(seaweedCR *seaweedv1.Seaweed) (bo err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new s3 statefulset", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get s3 statefulset") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get s3 stateful set " + s3Deployment.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func (r *SeaweedReconciler) ensureS3Service(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -65,16 +65,16 @@ func (r *SeaweedReconciler) ensureS3Service(seaweedCR *seaweedv1.Seaweed) (bool, err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new s3 service", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get s3 server service") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get s3 service " + s3Service.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func labelsForS3(name string) map[string]string { diff --git a/controllers/controller_volume.go b/controllers/controller_volume.go index 5d0cc28..f92bd99 100644 --- a/controllers/controller_volume.go +++ b/controllers/controller_volume.go @@ -17,14 +17,14 @@ func (r *SeaweedReconciler) ensureVolumeServers(seaweedCR *seaweedv1.Seaweed) (d _ = r.Log.WithValues("seaweed", seaweedCR.Name) if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done { - return done, result, err + return } if done, result, err = r.ensureVolumeServerService(seaweedCR); done { - return done, result, err + return } - return false, ctrl.Result{}, nil + return } func (r *SeaweedReconciler) ensureVolumeServerStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -40,16 +40,16 @@ func (r *SeaweedReconciler) ensureVolumeServerStatefulSet(seaweedCR *seaweedv1.S err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new volume statefulset", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get volume server statefulset") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get volume stateful set " + volumeServerStatefulSet.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { @@ -65,16 +65,16 @@ func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seawe err = r.Create(ctx, dep) if err != nil { log.Error(err, "Failed to create new volume service", "Namespace", dep.Namespace, "Name", dep.Name) - return true, ctrl.Result{}, err + return ReconcileResult(err) } // Deployment created successfully - return and requeue - return false, ctrl.Result{}, nil + return ReconcileResult(err) } else if err != nil { log.Error(err, "Failed to get volume server service") - return true, ctrl.Result{}, err + return ReconcileResult(err) } log.Info("Get volume service " + volumeServerService.Name) - return false, ctrl.Result{}, nil + return ReconcileResult(err) } func labelsForVolumeServer(name string) map[string]string { diff --git a/controllers/helper.go b/controllers/helper.go new file mode 100644 index 0000000..d57c68f --- /dev/null +++ b/controllers/helper.go @@ -0,0 +1,10 @@ +package controllers + +import ctrl "sigs.k8s.io/controller-runtime" + +func ReconcileResult(err error) (bool, ctrl.Result, error) { + if err != nil { + return true, ctrl.Result{}, err + } + return false, ctrl.Result{}, nil +}