From 5a650085a0b089307b870341677893ebc0cb8c20 Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Thu, 29 Sep 2022 01:54:54 +0530 Subject: [PATCH] Perform correct cleanup using ownerReferences Signed-off-by: Ashish Tiwari --- controllers/broker_controller.go | 2 +- controllers/meshsync_controller.go | 3 ++- main.go | 12 ------------ pkg/broker/resources.go | 13 +++++++++---- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/controllers/broker_controller.go b/controllers/broker_controller.go index 4fe0faf6..7a4d5718 100644 --- a/controllers/broker_controller.go +++ b/controllers/broker_controller.go @@ -130,11 +130,11 @@ func (r *BrokerReconciler) reconcileBroker(ctx context.Context, enable bool, bas object, ) if err != nil && kubeerror.IsNotFound(err) && enable { + _ = ctrl.SetControllerReference(baseResource, object, r.Scheme) er := r.Create(ctx, object) if er != nil { return ctrl.Result{}, ErrCreateMeshsync(er) } - _ = ctrl.SetControllerReference(baseResource, object, r.Scheme) return ctrl.Result{Requeue: true}, nil } else if err != nil && enable { return ctrl.Result{}, ErrGetMeshsync(err) diff --git a/controllers/meshsync_controller.go b/controllers/meshsync_controller.go index 2856e55b..05ef724c 100644 --- a/controllers/meshsync_controller.go +++ b/controllers/meshsync_controller.go @@ -26,6 +26,7 @@ import ( "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + util "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" mesheryv1alpha1 "github.com/layer5io/meshery-operator/api/v1alpha1" brokerpackage "github.com/layer5io/meshery-operator/pkg/broker" @@ -140,11 +141,11 @@ func (r *MeshSyncReconciler) reconcileMeshsync(ctx context.Context, enable bool, object, ) if err != nil && kubeerror.IsNotFound(err) && enable { + _ = util.SetControllerReference(baseResource, object, r.Scheme) er := r.Create(ctx, object) if er != nil { return ctrl.Result{}, ErrCreateMeshsync(er) } - _ = ctrl.SetControllerReference(baseResource, object, r.Scheme) return ctrl.Result{Requeue: true}, nil } else if err != nil && enable { return ctrl.Result{}, ErrGetMeshsync(err) diff --git a/main.go b/main.go index 76ec0ba6..7ec0f1b9 100644 --- a/main.go +++ b/main.go @@ -118,16 +118,4 @@ func main() { setupLog.Error(err, "problem running manager") os.Exit(1) } - - // Cleanup residual controllers - setupLog.Info("cleaning up residual controllers") - err = mReconciler.Cleanup() - if err != nil { - ctrl.Log.Error(err, "unable to delete controller", "MeshSync") - } - - err = bReconciler.Cleanup() - if err != nil { - ctrl.Log.Error(err, "unable to delete controller", "Broker") - } } diff --git a/pkg/broker/resources.go b/pkg/broker/resources.go index 3ba897bb..7f160c25 100644 --- a/pkg/broker/resources.go +++ b/pkg/broker/resources.go @@ -41,8 +41,9 @@ var ( NatsConfigMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "meshery-nats-config", - Labels: BrokerLabel, + Namespace: "meshery", + Name: "meshery-nats-config", + Labels: BrokerLabel, }, Data: map[string]string{ "nats.conf": ` @@ -59,8 +60,9 @@ include "accounts/resolver.conf"`, AccountsConfigMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "meshery-nats-accounts", - Labels: BrokerLabel, + Namespace: "meshery", + Name: "meshery-nats-accounts", + Labels: BrokerLabel, }, Data: map[string]string{ "resolver.conf": ` @@ -73,6 +75,7 @@ ACSU3Q6LTLBVLGAQUONAGXJHVNWGSKKAUA7IY5TB4Z7PLEKSR5O6JTGR: eyJ0eXAiOiJqd3QiLCJhbG Service = &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ + Namespace: "meshery", Name: "meshery-nats", Labels: BrokerLabel, Annotations: MesheryAnnotation, @@ -111,6 +114,7 @@ ACSU3Q6LTLBVLGAQUONAGXJHVNWGSKKAUA7IY5TB4Z7PLEKSR5O6JTGR: eyJ0eXAiOiJqd3QiLCJhbG StatefulSet = &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ + Namespace: "meshery", Name: "meshery-nats", Labels: BrokerLabel, Annotations: MesheryAnnotation, @@ -127,6 +131,7 @@ ACSU3Q6LTLBVLGAQUONAGXJHVNWGSKKAUA7IY5TB4Z7PLEKSR5O6JTGR: eyJ0eXAiOiJqd3QiLCJhbG PodTemplate = corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ + Namespace: "meshery", Name: "meshery-nats", Labels: BrokerLabel, Annotations: PrometheusAnnotation,