Skip to content

Pass opentelemetry environment variables to v1alpha ETOS #402

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 34 additions & 14 deletions internal/controller/environment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,39 @@ func (r EnvironmentReconciler) releaseJob(environment *etosv1alpha1.Environment,
}
clusterName = cluster.Name
}
traceparent, ok := environmentRequest.Annotations["etos.eiffel-community.github.io/traceparent"]
if !ok {
traceparent = ""
}

envList := []corev1.EnvVar{
{
Name: "REQUEST",
Value: environmentRequest.Name,
},
{
Name: "ENVIRONMENT",
Value: environment.Name,
},
{
Name: "ETOS_ETCD_HOST",
Value: databaseHost,
},
{
Name: "OTEL_CONTEXT",
Value: traceparent,
},
}
if cluster != nil && cluster.Spec.OpenTelemetry.Enabled {
envList = append(envList, corev1.EnvVar{
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
Value: cluster.Spec.OpenTelemetry.Endpoint,
})
envList = append(envList, corev1.EnvVar{
Name: "OTEL_EXPORTER_OTLP_INSECURE",
Value: cluster.Spec.OpenTelemetry.Insecure,
})
}

return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -303,20 +336,7 @@ func (r EnvironmentReconciler) releaseJob(environment *etosv1alpha1.Environment,
},
Command: []string{"python", "-u", "-m", "environment_provider.environment"},
Args: []string{environment.Name},
Env: []corev1.EnvVar{
{
Name: "REQUEST",
Value: environmentRequest.Name,
},
{
Name: "ENVIRONMENT",
Value: environment.Name,
},
{
Name: "ETOS_ETCD_HOST",
Value: databaseHost,
},
},
Env: envList,
},
},
},
Expand Down
47 changes: 38 additions & 9 deletions internal/controller/environmentrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (r *EnvironmentRequestReconciler) reconcileEnvironmentProvider(ctx context.
}

// envVarListFrom creates a list of EnvVar key-value pairs from an EnvironmentRequest instance
func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, environmentrequest *etosv1alpha1.EnvironmentRequest) ([]corev1.EnvVar, error) {
func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, environmentrequest *etosv1alpha1.EnvironmentRequest, cluster *etosv1alpha1.Cluster) ([]corev1.EnvVar, error) {
etosEncryptionKey, err := environmentrequest.Spec.Config.EncryptionKey.Get(ctx, r.Client, environmentrequest.Namespace)
if err != nil {
return nil, err
Expand All @@ -277,6 +277,10 @@ func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, enviro
if err != nil {
return nil, err
}
traceparent, ok := environmentrequest.Annotations["etos.eiffel-community.github.io/traceparent"]
if !ok {
traceparent = ""
}
envList := []corev1.EnvVar{
{
Name: "REQUEST",
Expand Down Expand Up @@ -410,6 +414,20 @@ func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, enviro
Name: "ETOS_ROUTING_KEY_TAG",
Value: environmentrequest.Spec.Config.RoutingKeyTag,
},
{
Name: "OTEL_CONTEXT",
Value: traceparent,
},
}
if cluster != nil && cluster.Spec.OpenTelemetry.Enabled {
envList = append(envList, corev1.EnvVar{
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
Value: cluster.Spec.OpenTelemetry.Endpoint,
})
envList = append(envList, corev1.EnvVar{
Name: "OTEL_EXPORTER_OTLP_INSECURE",
Value: cluster.Spec.OpenTelemetry.Insecure,
})
}
return envList, nil
}
Expand Down Expand Up @@ -473,19 +491,30 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(ctx context.Context
grace := int64(30)
backoff := int32(0)

envVarList, err := r.envVarListFrom(ctx, environmentrequest)
if err != nil {
logger.Error(err, "Failed to create environment variable list for environment provider")
return nil, err
}

labels := map[string]string{
"etos.eiffel-community.github.io/id": environmentrequest.Spec.Identifier, // TODO: omitempty
"app.kubernetes.io/name": "environment-provider",
"app.kubernetes.io/part-of": "etos",
}
if cluster := environmentrequest.Labels["etos.eiffel-community.github.io/cluster"]; cluster != "" {
labels["etos.eiffel-community.github.io/cluster"] = cluster

var cluster *etosv1alpha1.Cluster
if clusterName := environmentrequest.Labels["etos.eiffel-community.github.io/cluster"]; clusterName != "" {
labels["etos.eiffel-community.github.io/cluster"] = clusterName
clusterNamespacedName := types.NamespacedName{
Name: clusterName,
Namespace: environmentrequest.Namespace,
}
cluster = &etosv1alpha1.Cluster{}
if err := r.Get(ctx, clusterNamespacedName, cluster); err != nil {
logger.Info("Failed to get cluster resource!")
return nil, err
}
}

envVarList, err := r.envVarListFrom(ctx, environmentrequest, cluster)
if err != nil {
logger.Error(err, "Failed to create environment variable list for environment provider")
return nil, err
}

return &batchv1.Job{
Expand Down
17 changes: 15 additions & 2 deletions internal/controller/testrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type TestRunReconciler struct {
client.Client
Scheme *runtime.Scheme
Clock
Cluster *etosv1alpha1.Cluster
}

/*
Expand Down Expand Up @@ -455,6 +454,12 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes
databasePort = "2379"
}

annotations := make(map[string]string)
traceparent, ok := testrun.Annotations["etos.eiffel-community.github.io/traceparent"]
if ok {
annotations["etos.eiffel-community.github.io/traceparent"] = traceparent
}

return &etosv1alpha1.EnvironmentRequest{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand All @@ -463,7 +468,7 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes
"app.kubernetes.io/name": "suite-runner",
"app.kubernetes.io/part-of": "etos",
},
Annotations: make(map[string]string),
Annotations: annotations,
GenerateName: fmt.Sprintf("%s-", testrun.Name),
Namespace: testrun.Namespace,
},
Expand Down Expand Up @@ -516,6 +521,10 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes

// suiteRunnerJob is the job definition for an etos suite runner.
func (r TestRunReconciler) suiteRunnerJob(testrun *etosv1alpha1.TestRun) *batchv1.Job {
traceparent, ok := testrun.Annotations["etos.eiffel-community.github.io/traceparent"]
if !ok {
traceparent = ""
}
grace := int64(30)
backoff := int32(0)
return &batchv1.Job{
Expand Down Expand Up @@ -654,6 +663,10 @@ func (r TestRunReconciler) suiteRunnerJob(testrun *etosv1alpha1.TestRun) *batchv
Name: "IDENTIFIER",
Value: testrun.Spec.ID,
},
{
Name: "OTEL_CONTEXT",
Value: traceparent,
},
{
Name: "KUBEXIT_NAME",
Value: "esr",
Expand Down