@@ -77,6 +77,31 @@ func createGuardrailsOrchestratorSidecar(ctx context.Context, orchestratorConfig
77
77
return err
78
78
}
79
79
80
+ func createGuardrailsOrchestratorOtelExporter (ctx context.Context , orchestratorConfigMap string ) error {
81
+ typedNamespacedName := types.NamespacedName {Name : orchestratorName , Namespace : namespaceName }
82
+ otelExporter := gorchv1alpha1.OtelExporter {
83
+ Protocol : "grpc" ,
84
+ OTLPEndpoint : "localhost:4317" ,
85
+ OTLPExport : "traces" ,
86
+ }
87
+ err := k8sClient .Get (ctx , typedNamespacedName , & gorchv1alpha1.GuardrailsOrchestrator {})
88
+ if err != nil && errors .IsNotFound (err ) {
89
+ gorch := & gorchv1alpha1.GuardrailsOrchestrator {
90
+ ObjectMeta : metav1.ObjectMeta {
91
+ Name : typedNamespacedName .Name ,
92
+ Namespace : typedNamespacedName .Namespace ,
93
+ },
94
+ Spec : gorchv1alpha1.GuardrailsOrchestratorSpec {
95
+ Replicas : 1 ,
96
+ OrchestratorConfig : & orchestratorConfigMap ,
97
+ OtelExporter : otelExporter ,
98
+ },
99
+ }
100
+ err = k8sClient .Create (ctx , gorch )
101
+ }
102
+ return err
103
+ }
104
+
80
105
func deleteGuardrailsOrchestrator (ctx context.Context , namespace string ) error {
81
106
typedNamespacedName := types.NamespacedName {Name : orchestratorName , Namespace : namespace }
82
107
err := k8sClient .Get (ctx , typedNamespacedName , & gorchv1alpha1.GuardrailsOrchestrator {})
@@ -336,9 +361,134 @@ func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) {
336
361
})
337
362
}
338
363
364
+ func testCreateDeleteGuardrailsOrchestratorOtelExporter (namespaceName string ) {
365
+ It ("Should sucessfully reconcile creating a custom resource for the GuardrailsOrchestrator" , func () {
366
+ By ("Creating an Orchestrator configmap" )
367
+ configMap := & corev1.ConfigMap {
368
+ TypeMeta : metav1.TypeMeta {
369
+ Kind : "ConfigMap" ,
370
+ APIVersion : "v1" ,
371
+ },
372
+ ObjectMeta : metav1.ObjectMeta {
373
+ Name : orchestratorName + "-config" ,
374
+ Namespace : namespaceName ,
375
+ },
376
+ }
377
+ err := k8sClient .Create (ctx , configMap )
378
+ Expect (err ).ToNot (HaveOccurred ())
379
+
380
+ By ("Creating a custom resource for the GuardrailsOrchestrator" )
381
+ ctx := context .Background ()
382
+ typedNamespacedName := types.NamespacedName {Name : orchestratorName , Namespace : namespaceName }
383
+ err = createGuardrailsOrchestratorOtelExporter (ctx , configMap .Name )
384
+ Expect (err ).ToNot (HaveOccurred ())
385
+
386
+ By ("Checking if the custom resource was successfully created" )
387
+ err = k8sClient .Get (ctx , typedNamespacedName , & gorchv1alpha1.GuardrailsOrchestrator {})
388
+ Expect (err ).ToNot (HaveOccurred ())
389
+
390
+ By ("Creating the TrustyAI configmap with sidecar images" )
391
+ configMap = & corev1.ConfigMap {
392
+ TypeMeta : metav1.TypeMeta {
393
+ Kind : "ConfigMap" ,
394
+ APIVersion : "v1" ,
395
+ },
396
+ ObjectMeta : metav1.ObjectMeta {
397
+ Name : constants .ConfigMap ,
398
+ Namespace : namespaceName ,
399
+ },
400
+ Data : map [string ]string {
401
+ orchestratorImageKey : "quay.io/trustyai/ta-guardrails-orchestrator:latest" ,
402
+ },
403
+ }
404
+ err = k8sClient .Create (ctx , configMap )
405
+ Expect (err ).ToNot (HaveOccurred ())
406
+
407
+ By ("Reconciling the custom resource that was created" )
408
+ reconciler := & GuardrailsOrchestratorReconciler {
409
+ Client : k8sClient ,
410
+ Scheme : k8sClient .Scheme (),
411
+ }
412
+
413
+ _ , err = reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : typedNamespacedName })
414
+ Expect (err ).ToNot (HaveOccurred ())
415
+
416
+ By ("Checking if resources were successfully created in the reconcilation" )
417
+ Eventually (func () error {
418
+ configMap := & corev1.ConfigMap {}
419
+ if err := k8sClient .Get (ctx , types.NamespacedName {Name : constants .ConfigMap , Namespace : namespaceName }, configMap ); err != nil {
420
+ return err
421
+ }
422
+ Expect (configMap .Namespace ).Should (Equal (namespaceName ))
423
+ Expect (configMap .Name ).Should (Equal (constants .ConfigMap ))
424
+
425
+ serviceAccount := & corev1.ServiceAccount {}
426
+ if err := k8sClient .Get (ctx , types.NamespacedName {Name : orchestratorName + "-serviceaccount" , Namespace : namespaceName }, serviceAccount ); err != nil {
427
+ return err
428
+ }
429
+
430
+ deployment := & appsv1.Deployment {}
431
+ if err = k8sClient .Get (ctx , types.NamespacedName {Name : orchestratorName , Namespace : namespaceName }, deployment ); err != nil {
432
+ return err
433
+ }
434
+ var container * corev1.Container
435
+ var envVar * corev1.EnvVar
436
+ Expect (* deployment .Spec .Replicas ).Should (Equal (int32 (1 )))
437
+ Expect (deployment .Namespace ).Should (Equal (namespaceName ))
438
+ Expect (deployment .Name ).Should (Equal (orchestratorName ))
439
+ Expect (deployment .Labels ["app" ]).Should (Equal (orchestratorName ))
440
+ Expect (deployment .Spec .Template .Spec .Volumes [0 ].Name ).Should (Equal (orchestratorName + "-config" ))
441
+ container = getContainers (orchestratorName , deployment .Spec .Template .Spec .Containers )
442
+ Expect (container .Image ).Should (Equal ("quay.io/trustyai/ta-guardrails-orchestrator:latest" ))
443
+ Expect (container .VolumeMounts [0 ].Name ).Should (Equal (orchestratorName + "-config" ))
444
+ envVar = getEnvVar ("OTEL_EXPORTER_OTLP_PROTOCOL" , container .Env )
445
+ Expect (envVar ).ShouldNot (BeNil ())
446
+ Expect (envVar .Value ).To (Equal ("grpc" ))
447
+ envVar = getEnvVar ("OTEL_EXPORTER_OTLP_ENDPOINT" , container .Env )
448
+ Expect (envVar ).ShouldNot (BeNil ())
449
+ Expect (envVar .Value ).To (Equal ("localhost:4317" ))
450
+ envVar = getEnvVar ("OTLP_EXPORT" , container .Env )
451
+ Expect (envVar ).ShouldNot (BeNil ())
452
+ Expect (envVar .Value ).To (Equal ("traces" ))
453
+
454
+ service := & corev1.Service {}
455
+ if err := k8sClient .Get (ctx , types.NamespacedName {Name : orchestratorName + "-service" , Namespace : namespaceName }, service ); err != nil {
456
+ return err
457
+ }
458
+ Expect (service .Namespace ).Should (Equal (namespaceName ))
459
+
460
+ route := & routev1.Route {}
461
+ if err := routev1 .AddToScheme (scheme .Scheme ); err != nil {
462
+ return err
463
+ }
464
+ if err := k8sClient .Get (ctx , types.NamespacedName {Name : orchestratorName + "-route" , Namespace : namespaceName }, route ); err != nil {
465
+ return err
466
+ }
467
+ return nil
468
+ }, time .Second * 10 , time .Millisecond * 10 ).Should (Succeed ())
469
+
470
+ By ("Deleting the custom resource for the GuardrailsOrchestrator" )
471
+ err = deleteGuardrailsOrchestrator (ctx , namespaceName )
472
+ Expect (err ).ToNot (HaveOccurred ())
473
+
474
+ By ("Deleting the orchestrator configmap" )
475
+ err = k8sClient .Delete (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Name : orchestratorName + "-config" , Namespace : namespaceName }})
476
+ Expect (err ).ToNot (HaveOccurred ())
477
+
478
+ By ("Deleting the TrustyAI configmap" )
479
+ err = k8sClient .Delete (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Name : constants .ConfigMap , Namespace : namespaceName }})
480
+ Expect (err ).ToNot (HaveOccurred ())
481
+
482
+ By ("Reconciling the custom resource that was deleted" )
483
+ _ , err = reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : typedNamespacedName })
484
+ Expect (err ).ToNot (HaveOccurred ())
485
+ })
486
+ }
487
+
339
488
var _ = Describe ("GuardrailsOrchestrator Controller" , func () {
340
489
Context ("GuardrailsOrchestrator Controller Test" , func () {
341
490
testCreateDeleteGuardrailsOrchestrator (namespaceName )
342
491
testCreateDeleteGuardrailsOrchestratorSidecar (namespaceName )
492
+ testCreateDeleteGuardrailsOrchestratorOtelExporter (namespaceName )
343
493
})
344
494
})
0 commit comments