Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidecar not removed #1508

Merged
merged 7 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions pkg/controller/deployment/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ func (r *ReconcileDeployment) Reconcile(request reconcile.Request) (reconcile.Re
span.AddEvent(msg, trace.WithAttributes(attribute.String("error", err.Error())))
}

if !inject.Desired(dep, ns) {
// sidecar isn't desired for this deployment, skip remaining of the reconciliation
return reconcile.Result{}, nil
}

jaegers := &v1.JaegerList{}
opts := []client.ListOption{}

Expand All @@ -132,23 +127,23 @@ func (r *ReconcileDeployment) Reconcile(request reconcile.Request) (reconcile.Re
return reconcile.Result{}, tracing.HandleError(err, span)
}

jaeger := inject.Select(dep, ns, jaegers)
if jaeger != nil && jaeger.GetDeletionTimestamp() == nil {
logger := logger.WithFields(log.Fields{
"jaeger": jaeger.Name,
"jaeger-namespace": jaeger.Namespace,
})
if jaeger.Namespace != dep.Namespace {
if err := r.reconcileConfigMaps(ctx, jaeger, dep); err != nil {
msg := "failed to reconcile config maps for the namespace"
logger.WithError(err).Error(msg)
span.AddEvent(msg)
if inject.Needed(dep, ns) {
jaeger := inject.Select(dep, ns, jaegers)
if jaeger != nil && jaeger.GetDeletionTimestamp() == nil {
logger := logger.WithFields(log.Fields{
"jaeger": jaeger.Name,
"jaeger-namespace": jaeger.Namespace,
})
if jaeger.Namespace != dep.Namespace {
if err := r.reconcileConfigMaps(ctx, jaeger, dep); err != nil {
msg := "failed to reconcile config maps for the namespace"
logger.WithError(err).Error(msg)
span.AddEvent(msg)
}
}
}

// a suitable jaeger instance was found! let's inject a sidecar pointing to it then
// Verified that jaeger instance was found and is not marked for deletion.
if inject.Needed(dep, ns) {
// a suitable jaeger instance was found! let's inject a sidecar pointing to it then
// Verified that jaeger instance was found and is not marked for deletion.
{
msg := "injecting Jaeger Agent sidecar"
logger.Info(msg)
Expand All @@ -160,16 +155,45 @@ func (r *ReconcileDeployment) Reconcile(request reconcile.Request) (reconcile.Re
logger.WithError(err).Error("failed to update deployment with sidecar")
return reconcile.Result{}, tracing.HandleError(err, span)
}

} else {
msg := "no suitable Jaeger instances found to inject a sidecar"
span.AddEvent(msg)
logger.Debug(msg)
}

} else {
msg := "no suitable Jaeger instances found to inject a sidecar"
span.AddEvent(msg)
logger.Debug(msg)
hasAgent, _ := inject.HasJaegerAgent(dep)
if hasAgent {
_, hasLabel := dep.Labels[inject.Label]
if hasLabel {
r.removeSidecar(ctx, dep)
}

}
return reconcile.Result{}, nil
}

return reconcile.Result{}, nil
}

func (r *ReconcileDeployment) removeSidecar(ctx context.Context, dep *appsv1.Deployment) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same logic that we used for the namespace case?

Is there a way to do a single function and reuse it for both case? Not need to be part of this PR, but we can have another PR with a re-factoring of this logic.

jaegerInstance := dep.Labels[inject.Label]
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.Patch(ctx, dep, patch); err != nil {
log.WithFields(log.Fields{
"deploymentName": dep.Name,
"deploymentNamespace": dep.Namespace,
}).WithError(err).Error("error cleaning orphaned deployment")
}
}

func (r *ReconcileDeployment) syncOnJaegerChanges(event handler.MapObject) []reconcile.Request {
reconciliations := []reconcile.Request{}
nss := map[string]corev1.Namespace{} // namespace cache
Expand Down
12 changes: 6 additions & 6 deletions pkg/inject/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ func Sidecar(jaeger *v1.Jaeger, dep *appsv1.Deployment) *appsv1.Deployment {
}

// Desired determines whether a sidecar is desired, based on the annotation from both the deployment and the namespace
func Desired(dep *appsv1.Deployment, ns *corev1.Namespace) bool {
func desired(dep *appsv1.Deployment, ns *corev1.Namespace) bool {
logger := log.WithFields(log.Fields{
"namespace": dep.Namespace,
"deployment": dep.Name,
})
_, depExist := dep.Annotations[Annotation]
_, nsExist := ns.Annotations[Annotation]
depAnnotationValue, depExist := dep.Annotations[Annotation]
nsAnnotationValue, nsExist := ns.Annotations[Annotation]

if depExist {
if depExist && !strings.EqualFold(depAnnotationValue, "false") {
logger.Debug("annotation present on deployment")
return true
}

if nsExist {
if nsExist && !strings.EqualFold(nsAnnotationValue, "false") {
logger.Debug("annotation present on namespace")
return true
}
Expand All @@ -98,7 +98,7 @@ func Desired(dep *appsv1.Deployment, ns *corev1.Namespace) bool {

// Needed determines whether a pod needs to get a sidecar injected or not
func Needed(dep *appsv1.Deployment, ns *corev1.Namespace) bool {
if !Desired(dep, ns) {
if !desired(dep, ns) {
return false
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/inject/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ func TestSidecarNeeded(t *testing.T) {
ns: ns(map[string]string{Annotation: "true"}),
needed: false,
},
{
dep: dep(map[string]string{Annotation: "false"}, map[string]string{}),
ns: ns(map[string]string{}),
needed: false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("dep:%s, ns: %s", test.dep.Annotations, test.ns.Annotations), func(t *testing.T) {
Expand Down