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

Add env support to instrumentation kind #674

Merged
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
25 changes: 25 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -36,6 +37,12 @@ type InstrumentationSpec struct {
// +optional
Sampler `json:"sampler,omitempty"`

// Env defines common env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`

// Java defines configuration for java auto-instrumentation.
// +optional
Java Java `json:"java,omitempty"`
Expand Down Expand Up @@ -88,20 +95,38 @@ type Java struct {
// Image is a container image with javaagent auto-instrumentation JAR.
// +optional
Image string `json:"image,omitempty"`

// Env defines java specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// NodeJS defines NodeJS SDK and instrumentation configuration.
type NodeJS struct {
// Image is a container image with NodeJS SDK and auto-instrumentation.
// +optional
Image string `json:"image,omitempty"`

// Env defines nodejs specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// Python defines Python SDK and instrumentation configuration.
type Python struct {
// Image is a container image with Python SDK and auto-instrumentation.
// +optional
Image string `json:"image,omitempty"`

// Env defines python specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// InstrumentationStatus defines status of the instrumentation.
Expand Down
27 changes: 27 additions & 0 deletions apis/v1alpha1/instrumentation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package v1alpha1
import (
"fmt"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -28,6 +30,7 @@ const (
AnnotationDefaultAutoInstrumentationJava = "instrumentation.opentelemetry.io/default-auto-instrumentation-java-image"
AnnotationDefaultAutoInstrumentationNodeJS = "instrumentation.opentelemetry.io/default-auto-instrumentation-nodejs-image"
AnnotationDefaultAutoInstrumentationPython = "instrumentation.opentelemetry.io/default-auto-instrumentation-python-image"
envPrefix = "OTEL_"
)

// log is for logging in this package.
Expand Down Expand Up @@ -107,5 +110,29 @@ func (in *Instrumentation) validate() error {
}
case AlwaysOn, AlwaysOff, JaegerRemote, ParentBasedAlwaysOn, ParentBasedAlwaysOff, XRaySampler:
}

// validate env vars
if err := in.validateEnv(in.Spec.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Java.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.NodeJS.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Python.Env); err != nil {
return err
}

return nil
}

func (in *Instrumentation) validateEnv(envs []corev1.EnvVar) error {
for _, env := range envs {
if !strings.HasPrefix(env.Name, envPrefix) {
return fmt.Errorf("env name should start with \"OTEL_\": %s", env.Name)
}
}
return nil
}
34 changes: 31 additions & 3 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading