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

Scan deployments for agent injection #454

Merged
merged 1 commit into from
Nov 15, 2019
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
49 changes: 49 additions & 0 deletions pkg/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,54 @@ func (b *Background) Stop() {
b.ticker.Stop()
}

func (b *Background) requireUpdates(deps *appsv1.DeploymentList) []*appsv1.Deployment {
instances := &v1.JaegerList{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you leave a comment in the func, saying that this is related to the Deployment controller, but that this here exists only because the Jaeger Operator might not be up and running when the deployment was created/updated/annotated?

if err := b.cl.List(context.Background(), instances); err != nil {
return nil
}
requireUpdates := make([]*appsv1.Deployment, 0)
for i := 0; i < len(deps.Items); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

The code within this loop is pretty much the same as the existing controller, isn't it:

if inject.Needed(instance) {
pods := &v1.JaegerList{}
opts := []client.ListOption{}
err := r.client.List(context.Background(), pods, opts...)
if err != nil {
log.WithError(err).Error("failed to get the available Jaeger pods")
return reconcile.Result{}, err
}
jaeger := inject.Select(instance, pods)
if jaeger != nil && jaeger.GetDeletionTimestamp() == nil {
// 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.
log.WithFields(log.Fields{
"deployment": instance.Name,
"namespace": instance.Namespace,
"jaeger": jaeger.Name,
"jaeger-namespace": jaeger.Namespace,
}).Info("Injecting Jaeger Agent sidecar")
instance = inject.Sidecar(jaeger, instance)
if err := r.client.Update(context.Background(), instance); err != nil {
log.WithField("deployment", instance).WithError(err).Error("failed to update")
return reconcile.Result{}, err
}
} else {
log.WithField("deployment", instance.Name).Info("No suitable Jaeger instances found to inject a sidecar")
}
}

In that case, it would probably make sense to externalize that into its own func, and call it from both places (this here and the controller shown above)

dep := &deps.Items[i]
if inject.Needed(dep) { // If sidecar is not present and should be
jaeger := inject.Select(dep, instances)
if jaeger != nil { // Instance exists.
jaeger.Logger().WithFields(log.Fields{
"deploymentName": dep.Name,
"deploymentNamespace": dep.Namespace,
}).Info("Injecting Jaeger Agent sidecar")
dep.Annotations[inject.Annotation] = jaeger.Name
newDep := inject.Sidecar(jaeger, dep)
requireUpdates = append(requireUpdates, newDep)
}
} else {
// Try to update the sidecar if is required
jaeger := inject.Select(dep, instances)
updated := inject.UpdateSideCar(jaeger, dep)
if updated {
if err := b.cl.Update(context.Background(), dep); err != nil {
return nil
}
}
}
}
return requireUpdates
}

func (b *Background) detectDeploymentUpdates() error {
deps := &appsv1.DeploymentList{}
if err := b.cl.List(context.Background(), deps); err != nil {
return err
}
injectedDeps := b.requireUpdates(deps)
for _, d := range injectedDeps {
if err := b.cl.Update(context.Background(), d); err != nil {
return err
}
}

return nil
}

func (b *Background) autoDetectCapabilities() {
apiList, err := b.availableAPIs()
if err != nil {
Expand All @@ -92,6 +140,7 @@ func (b *Background) autoDetectCapabilities() {

b.detectClusterRoles()
b.cleanDeployments()
b.detectDeploymentUpdates()

}

Expand Down
11 changes: 11 additions & 0 deletions pkg/inject/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func Sidecar(jaeger *v1.Jaeger, dep *appsv1.Deployment) *appsv1.Deployment {
return dep
}

// UpdateSideCar modify the deployment side car with the latest parameters if it's required.
func UpdateSideCar(jaeger *v1.Jaeger, dep *appsv1.Deployment) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

s/SideCar/Sidecar

for i := range dep.Spec.Template.Spec.Containers {
if dep.Spec.Template.Spec.Containers[i].Name == "jaeger-agent" {
dep.Spec.Template.Spec.Containers[i] = container(jaeger, dep)
return true
}
}
return false
}

// Needed determines whether a pod needs to get a sidecar injected or not
func Needed(dep *appsv1.Deployment) bool {
if dep.Annotations[Annotation] == "" {
Expand Down