From e55f4637925e62c6901e0fff44ba83386d9d2ed1 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Tue, 22 Sep 2020 18:43:15 -0500 Subject: [PATCH 1/2] Delete injected sidecars of annotated namespaces when annotation is deleted Signed-off-by: Ruben Vargas --- .../namespace/namespace_controller.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/controller/namespace/namespace_controller.go b/pkg/controller/namespace/namespace_controller.go index ee9f9d648..75cf371ef 100644 --- a/pkg/controller/namespace/namespace_controller.go +++ b/pkg/controller/namespace/namespace_controller.go @@ -155,6 +155,29 @@ func (r *ReconcileNamespace) Reconcile(request reconcile.Request) (reconcile.Res } else { log.WithField("deployment", dep.Name).Info("No suitable Jaeger instances found to inject a sidecar") } + } else { + // Don't need injection, may be need to remove the sidecar? + // If deployment don't have the annotation and has an agent, this may be injected by the namespace + // we need to clean it. + agent, _ := inject.HasJaegerAgent(dep) + _, depAnnotation := dep.Annotations[inject.Annotation] + if agent && !depAnnotation{ + jaegerInstance, label := dep.Labels[inject.Label] + if label { + log.WithFields(log.Fields{ + "deployment": dep.Name, + "namespace": dep.Namespace, + "jaeger": jaegerInstance, + }).Info("Removing Jaeger Agent sidecar") + inject.CleanSidecar(jaegerInstance, dep) + if err := r.client.Update(ctx, dep); err != nil { + log.WithFields(log.Fields{ + "deploymentName": dep.Name, + "deploymentNamespace": dep.Namespace, + }).WithError(err).Error("error cleaning orphaned deployment") + } + } + } } } From 9b80ba7e04e5ee9e97ef18d2174702f73faabe79 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Wed, 23 Sep 2020 12:25:09 -0500 Subject: [PATCH 2/2] Rename some variables, change update call to patch Signed-off-by: Ruben Vargas --- pkg/controller/namespace/namespace_controller.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/controller/namespace/namespace_controller.go b/pkg/controller/namespace/namespace_controller.go index 75cf371ef..9e039c5f5 100644 --- a/pkg/controller/namespace/namespace_controller.go +++ b/pkg/controller/namespace/namespace_controller.go @@ -157,20 +157,21 @@ func (r *ReconcileNamespace) Reconcile(request reconcile.Request) (reconcile.Res } } else { // Don't need injection, may be need to remove the sidecar? - // If deployment don't have the annotation and has an agent, this may be injected by the namespace + // If deployment don't have the annotation and has an hasAgent, this may be injected by the namespace // we need to clean it. - agent, _ := inject.HasJaegerAgent(dep) - _, depAnnotation := dep.Annotations[inject.Annotation] - if agent && !depAnnotation{ - jaegerInstance, label := dep.Labels[inject.Label] - if label { + hasAgent, _ := inject.HasJaegerAgent(dep) + _, hasDepAnnotation := dep.Annotations[inject.Annotation] + if hasAgent && !hasDepAnnotation { + jaegerInstance, hasLabel := dep.Labels[inject.Label] + if hasLabel { log.WithFields(log.Fields{ "deployment": dep.Name, "namespace": dep.Namespace, "jaeger": jaegerInstance, }).Info("Removing Jaeger Agent sidecar") + patch := client.MergeFrom(dep.DeepCopy()) inject.CleanSidecar(jaegerInstance, dep) - if err := r.client.Update(ctx, dep); err != nil { + if err := r.client.Patch(ctx, dep, patch); err != nil { log.WithFields(log.Fields{ "deploymentName": dep.Name, "deploymentNamespace": dep.Namespace,