Skip to content

Commit

Permalink
Add tracing enabled annotation (#5643)
Browse files Browse the repository at this point in the history
This change adds the `jaeger.linkerd.io/tracing-enabled` annotation which is
automatically added by the Jaeger extension's `jaeger-injector`.

All pods that receive this annotation have also had the required environment
variables and volume/volume mounts add by the injector.

The purpose of this annotation is that it will allow `jaeger check` to check for
the presence of this annotation instead of needing to look at the proxy
containers directly. If this annotation is not present on pods, `jaeger check`
can warn users that tracing is not configured for those pods. This is similar to
`viz check` warning users that tap is not configured—recenlty added in #5602.

Closes #5632

Signed-off-by: Kevin Leimkuhler <kevin@kleimkuhler.com>
  • Loading branch information
kleimkuhler authored Feb 3, 2021
1 parent b521091 commit 228d8e9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion controller/tap-injector/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var tpl = fmt.Sprintf(`[
{
"op": "add",
"path": "/metadata/annotations/{{.Annotation}}",
"path": "/metadata/annotations/viz.linkerd.io~1tap-enabled",
"value": "true"
},
{
Expand Down
12 changes: 1 addition & 11 deletions controller/tap-injector/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"html/template"
"strings"

"github.com/ghodss/yaml"
"github.com/linkerd/linkerd2/controller/k8s"
Expand All @@ -19,7 +18,6 @@ import (

// Params holds the values used in the patch template.
type Params struct {
Annotation string
ProxyIndex int
ProxyTapSvcName string
}
Expand All @@ -43,19 +41,11 @@ func Mutate(tapSvcName string) webhook.Handler {
if err := yaml.Unmarshal(request.Object.Raw, &pod); err != nil {
return nil, err
}
// annotation is used in the patch as a JSON pointer, so '/' must be
// encoded as '~1' as stated in
// https://tools.ietf.org/html/rfc6901#section-3
annotation := strings.Replace(vizLabels.VizTapEnabled, "/", "~1", -1)
params := Params{
Annotation: annotation,
ProxyIndex: webhook.GetProxyContainerIndex(pod.Spec.Containers),
ProxyTapSvcName: tapSvcName,
}
if params.ProxyIndex < 0 {
return admissionResponse, nil
}
if _, contains := pod.GetAnnotations()[vizLabels.VizTapEnabled]; contains {
if params.ProxyIndex < 0 || vizLabels.IsTapEnabled(pod) {
return admissionResponse, nil
}
namespace, err := k8sAPI.NS().Lister().Get(request.Namespace)
Expand Down
5 changes: 5 additions & 0 deletions jaeger/injector/mutator/patch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package mutator

const tpl = `[
{
"op": "add",
"path": "/metadata/annotations/jaeger.linkerd.io~1tracing-enabled",
"value": "true"
},
{
"op": "add",
"path": "/spec/containers/{{.ProxyIndex}}/env/-",
Expand Down
31 changes: 5 additions & 26 deletions jaeger/injector/mutator/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"github.com/linkerd/linkerd2/controller/k8s"
"github.com/linkerd/linkerd2/controller/webhook"
labels "github.com/linkerd/linkerd2/pkg/k8s"
labels "github.com/linkerd/linkerd2/jaeger/pkg"
l5dLables "github.com/linkerd/linkerd2/pkg/k8s"
"github.com/prometheus/common/log"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -18,8 +19,8 @@ import (
)

const (
collectorSvcAddrAnnotation = labels.ProxyConfigAnnotationsPrefix + "/trace-collector"
collectorSvcAccountAnnotation = labels.ProxyConfigAnnotationsPrefixAlpha +
collectorSvcAddrAnnotation = l5dLables.ProxyConfigAnnotationsPrefix + "/trace-collector"
collectorSvcAccountAnnotation = l5dLables.ProxyConfigAnnotationsPrefixAlpha +
"/trace-collector-service-account"
)

Expand Down Expand Up @@ -54,13 +55,12 @@ func Mutate(collectorSvcAddr, collectorSvcAccount string) webhook.Handler {
if err := yaml.Unmarshal(request.Object.Raw, &pod); err != nil {
return nil, err
}

params := Params{
ProxyIndex: webhook.GetProxyContainerIndex(pod.Spec.Containers),
CollectorSvcAddr: collectorSvcAddr,
CollectorSvcAccount: collectorSvcAccount,
}
if params.ProxyIndex < 0 || alreadyMutated(pod, params.ProxyIndex) {
if params.ProxyIndex < 0 || labels.IsTracingEnabled(pod) {
return admissionResponse, nil
}

Expand Down Expand Up @@ -88,27 +88,6 @@ func Mutate(collectorSvcAddr, collectorSvcAccount string) webhook.Handler {
}
}

func alreadyMutated(pod *corev1.Pod, proxyIndex int) bool {
for _, v := range pod.Spec.Volumes {
if v.DownwardAPI != nil && v.Name == "podinfo" {
return true
}
}
for _, mount := range pod.Spec.Containers[proxyIndex].VolumeMounts {
if mount.Name == "podinfo" && mount.MountPath == "var/run/linkerd/podinfo" {
return true
}
}
for _, env := range pod.Spec.Containers[proxyIndex].Env {
if env.Name == "LINKERD2_PROXY_TRACE_ATTRIBUTES_PATH" ||
env.Name == "LINKERD2_PROXY_TRACE_COLLECTOR_SVC_ADDR" ||
env.Name == "LINKERD2_PROXY_TRACE_COLLECTOR_SVC_NAME" {
return true
}
}
return false
}

func applyOverrides(ns *corev1.Namespace, pod *corev1.Pod, params *Params) {
ann := ns.GetAnnotations()
if ann == nil {
Expand Down
29 changes: 29 additions & 0 deletions jaeger/pkg/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pkg

import (
"strconv"

corev1 "k8s.io/api/core/v1"
)

const (
// JaegerAnnotationsPrefix is the prefix of all jaeger-related annotations
JaegerAnnotationsPrefix = "jaeger.linkerd.io"

// JaegerTracingEnabled is set by the jaeger-injector component when
// tracing has been enabled on a pod.
JaegerTracingEnabled = JaegerAnnotationsPrefix + "/tracing-enabled"
)

// IsTracingEnabled returns true if a pod has an annotation indicating that
// tracing is enabled.
func IsTracingEnabled(pod *corev1.Pod) bool {
valStr := pod.GetAnnotations()[JaegerTracingEnabled]
if valStr != "" {
valBool, err := strconv.ParseBool(valStr)
if err == nil && valBool {
return true
}
}
return false
}

0 comments on commit 228d8e9

Please sign in to comment.