Skip to content

Commit c752397

Browse files
committed
Pass opentelemetry environment variables to v1alpha ETOS
The Suite Runner job will get its environment variables automatically from the deployed ETOS cluster whereas for the environment requests we check if we have a cluster to fetch and get it from there when possible. I also make sure that we pass in the traceparents that can be set in the annotations field of our testruns and environmentrequests so that the opentelemetry trace graph is complete. This change requires an update of the ETOS API as well as the environment provider to be considered complete.
1 parent 2628a44 commit c752397

File tree

3 files changed

+86
-25
lines changed

3 files changed

+86
-25
lines changed

internal/controller/environment_controller.go

+34-14
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,39 @@ func (r EnvironmentReconciler) releaseJob(environment *etosv1alpha1.Environment,
261261
}
262262
clusterName = cluster.Name
263263
}
264+
traceparent, ok := environmentRequest.Annotations["etos.eiffel-community.github.io/traceparent"]
265+
if !ok {
266+
traceparent = ""
267+
}
268+
269+
envList := []corev1.EnvVar{
270+
{
271+
Name: "REQUEST",
272+
Value: environmentRequest.Name,
273+
},
274+
{
275+
Name: "ENVIRONMENT",
276+
Value: environment.Name,
277+
},
278+
{
279+
Name: "ETOS_ETCD_HOST",
280+
Value: databaseHost,
281+
},
282+
{
283+
Name: "OTEL_CONTEXT",
284+
Value: traceparent,
285+
},
286+
}
287+
if cluster != nil && cluster.Spec.OpenTelemetry.Enabled {
288+
envList = append(envList, corev1.EnvVar{
289+
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
290+
Value: cluster.Spec.OpenTelemetry.Endpoint,
291+
})
292+
envList = append(envList, corev1.EnvVar{
293+
Name: "OTEL_EXPORTER_OTLP_INSECURE",
294+
Value: cluster.Spec.OpenTelemetry.Insecure,
295+
})
296+
}
264297

265298
return &batchv1.Job{
266299
ObjectMeta: metav1.ObjectMeta{
@@ -303,20 +336,7 @@ func (r EnvironmentReconciler) releaseJob(environment *etosv1alpha1.Environment,
303336
},
304337
Command: []string{"python", "-u", "-m", "environment_provider.environment"},
305338
Args: []string{environment.Name},
306-
Env: []corev1.EnvVar{
307-
{
308-
Name: "REQUEST",
309-
Value: environmentRequest.Name,
310-
},
311-
{
312-
Name: "ENVIRONMENT",
313-
Value: environment.Name,
314-
},
315-
{
316-
Name: "ETOS_ETCD_HOST",
317-
Value: databaseHost,
318-
},
319-
},
339+
Env: envList,
320340
},
321341
},
322342
},

internal/controller/environmentrequest_controller.go

+37-9
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (r *EnvironmentRequestReconciler) reconcileEnvironmentProvider(ctx context.
264264
}
265265

266266
// envVarListFrom creates a list of EnvVar key-value pairs from an EnvironmentRequest instance
267-
func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, environmentrequest *etosv1alpha1.EnvironmentRequest) ([]corev1.EnvVar, error) {
267+
func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, environmentrequest *etosv1alpha1.EnvironmentRequest, cluster *etosv1alpha1.Cluster) ([]corev1.EnvVar, error) {
268268
etosEncryptionKey, err := environmentrequest.Spec.Config.EncryptionKey.Get(ctx, r.Client, environmentrequest.Namespace)
269269
if err != nil {
270270
return nil, err
@@ -277,6 +277,10 @@ func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, enviro
277277
if err != nil {
278278
return nil, err
279279
}
280+
traceparent, ok := environmentrequest.Annotations["etos.eiffel-community.github.io/traceparent"]
281+
if !ok {
282+
traceparent = ""
283+
}
280284
envList := []corev1.EnvVar{
281285
{
282286
Name: "REQUEST",
@@ -410,6 +414,20 @@ func (r EnvironmentRequestReconciler) envVarListFrom(ctx context.Context, enviro
410414
Name: "ETOS_ROUTING_KEY_TAG",
411415
Value: environmentrequest.Spec.Config.RoutingKeyTag,
412416
},
417+
{
418+
Name: "OTEL_CONTEXT",
419+
Value: traceparent,
420+
},
421+
}
422+
if cluster != nil && cluster.Spec.OpenTelemetry.Enabled {
423+
envList = append(envList, corev1.EnvVar{
424+
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
425+
Value: cluster.Spec.OpenTelemetry.Endpoint,
426+
})
427+
envList = append(envList, corev1.EnvVar{
428+
Name: "OTEL_EXPORTER_OTLP_INSECURE",
429+
Value: cluster.Spec.OpenTelemetry.Insecure,
430+
})
413431
}
414432
return envList, nil
415433
}
@@ -473,19 +491,29 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(ctx context.Context
473491
grace := int64(30)
474492
backoff := int32(0)
475493

476-
envVarList, err := r.envVarListFrom(ctx, environmentrequest)
477-
if err != nil {
478-
logger.Error(err, "Failed to create environment variable list for environment provider")
479-
return nil, err
480-
}
481-
482494
labels := map[string]string{
483495
"etos.eiffel-community.github.io/id": environmentrequest.Spec.Identifier, // TODO: omitempty
484496
"app.kubernetes.io/name": "environment-provider",
485497
"app.kubernetes.io/part-of": "etos",
486498
}
487-
if cluster := environmentrequest.Labels["etos.eiffel-community.github.io/cluster"]; cluster != "" {
488-
labels["etos.eiffel-community.github.io/cluster"] = cluster
499+
500+
var cluster *etosv1alpha1.Cluster
501+
if clusterName := environmentrequest.Labels["etos.eiffel-community.github.io/cluster"]; clusterName != "" {
502+
labels["etos.eiffel-community.github.io/cluster"] = clusterName
503+
clusterNamespacedName := types.NamespacedName{
504+
Name: clusterName,
505+
Namespace: environmentrequest.Namespace,
506+
}
507+
if err := r.Get(ctx, clusterNamespacedName, cluster); err != nil {
508+
logger.Info("Failed to get cluster resource!")
509+
return nil, err
510+
}
511+
}
512+
513+
envVarList, err := r.envVarListFrom(ctx, environmentrequest, cluster)
514+
if err != nil {
515+
logger.Error(err, "Failed to create environment variable list for environment provider")
516+
return nil, err
489517
}
490518

491519
return &batchv1.Job{

internal/controller/testrun_controller.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ type TestRunReconciler struct {
5959
client.Client
6060
Scheme *runtime.Scheme
6161
Clock
62-
Cluster *etosv1alpha1.Cluster
6362
}
6463

6564
/*
@@ -455,6 +454,12 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes
455454
databasePort = "2379"
456455
}
457456

457+
annotations := make(map[string]string)
458+
traceparent, ok := testrun.Annotations["etos.eiffel-community.github.io/traceparent"]
459+
if ok {
460+
annotations["etos.eiffel-community.github.io/traceparent"] = traceparent
461+
}
462+
458463
return &etosv1alpha1.EnvironmentRequest{
459464
ObjectMeta: metav1.ObjectMeta{
460465
Labels: map[string]string{
@@ -463,7 +468,7 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes
463468
"app.kubernetes.io/name": "suite-runner",
464469
"app.kubernetes.io/part-of": "etos",
465470
},
466-
Annotations: make(map[string]string),
471+
Annotations: annotations,
467472
GenerateName: fmt.Sprintf("%s-", testrun.Name),
468473
Namespace: testrun.Namespace,
469474
},
@@ -516,6 +521,10 @@ func (r TestRunReconciler) environmentRequest(cluster *etosv1alpha1.Cluster, tes
516521

517522
// suiteRunnerJob is the job definition for an etos suite runner.
518523
func (r TestRunReconciler) suiteRunnerJob(testrun *etosv1alpha1.TestRun) *batchv1.Job {
524+
traceparent, ok := testrun.Annotations["etos.eiffel-community.github.io/traceparent"]
525+
if !ok {
526+
traceparent = ""
527+
}
519528
grace := int64(30)
520529
backoff := int32(0)
521530
return &batchv1.Job{
@@ -654,6 +663,10 @@ func (r TestRunReconciler) suiteRunnerJob(testrun *etosv1alpha1.TestRun) *batchv
654663
Name: "IDENTIFIER",
655664
Value: testrun.Spec.ID,
656665
},
666+
{
667+
Name: "OTEL_CONTEXT",
668+
Value: traceparent,
669+
},
657670
{
658671
Name: "KUBEXIT_NAME",
659672
Value: "esr",

0 commit comments

Comments
 (0)