forked from jaegertracing/jaeger-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject sidecar in properly annotated pods (jaegertracing#58)
* Inject sidecar in properly annotated pods Signed-off-by: Juraci Paixão Kröhling <[email protected]>
- Loading branch information
1 parent
97ff4d6
commit a6f14b7
Showing
15 changed files
with
516 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package inject | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/deployment" | ||
"github.com/jaegertracing/jaeger-operator/pkg/service" | ||
) | ||
|
||
var ( | ||
// Annotation is the annotation name to look for when deciding whether or not to inject | ||
Annotation = "inject-jaeger-agent" | ||
) | ||
|
||
// Sidecar adds a new container to the deployment, connecting to the given jaeger instance | ||
func Sidecar(dep *appsv1.Deployment, jaeger *v1alpha1.Jaeger) { | ||
deployment.NewAgent(jaeger) // we need some initialization from that, but we don't actually need the agent's instance here | ||
|
||
if jaeger == nil || dep.Annotations[Annotation] != jaeger.Name { | ||
logrus.Debugf("Skipping sidecar injection for deployment %v", dep.Name) | ||
} else { | ||
logrus.Debugf("Injecting sidecar for pod %v", dep.Name) | ||
dep.Spec.Template.Spec.Containers = append(dep.Spec.Template.Spec.Containers, container(jaeger)) | ||
} | ||
} | ||
|
||
// Needed determines whether a pod needs to get a sidecar injected or not | ||
func Needed(dep *appsv1.Deployment) bool { | ||
if dep.Annotations[Annotation] == "" { | ||
logrus.Debugf("Not needed, annotation not present for %v", dep.Name) | ||
return false | ||
} | ||
|
||
// this pod is annotated, it should have a sidecar | ||
// but does it already have one? | ||
for _, container := range dep.Spec.Template.Spec.Containers { | ||
if container.Name == "jaeger-agent" { // we don't labels/annotations on containers, so, we rely on its name | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Select a suitable Jaeger from the JaegerList for the given Pod, or nil of none is suitable | ||
func Select(target *appsv1.Deployment, availableJaegerPods *v1alpha1.JaegerList) *v1alpha1.Jaeger { | ||
jaegerName := target.Annotations[Annotation] | ||
if strings.ToLower(jaegerName) == "true" && len(availableJaegerPods.Items) == 1 { | ||
// if there's only *one* jaeger within this namespace, then that's what | ||
// we'll use -- otherwise, we should just not inject, as it's not clear which | ||
// jaeger instance to use! | ||
// first, we make sure we normalize the name: | ||
jaeger := &availableJaegerPods.Items[0] | ||
target.Annotations[Annotation] = jaeger.Name | ||
return jaeger | ||
} | ||
|
||
for _, p := range availableJaegerPods.Items { | ||
if p.Name == jaegerName { | ||
// matched the name! | ||
return &p | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func container(jaeger *v1alpha1.Jaeger) v1.Container { | ||
args := append(jaeger.Spec.Agent.Options.ToArgs(), fmt.Sprintf("--collector.host-port=%s:14267", service.GetNameForCollectorService(jaeger))) | ||
return v1.Container{ | ||
Image: jaeger.Spec.Agent.Image, | ||
Name: "jaeger-agent", | ||
Args: args, | ||
Ports: []v1.ContainerPort{ | ||
{ | ||
ContainerPort: 5775, | ||
Name: "zk-compact-trft", | ||
}, | ||
{ | ||
ContainerPort: 5778, | ||
Name: "config-rest", | ||
}, | ||
{ | ||
ContainerPort: 6831, | ||
Name: "jg-compact-trft", | ||
}, | ||
{ | ||
ContainerPort: 6832, | ||
Name: "jg-binary-trft", | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.