diff --git a/controllers/gorch/constants.go b/controllers/gorch/constants.go index 778481d1..2511b04d 100644 --- a/controllers/gorch/constants.go +++ b/controllers/gorch/constants.go @@ -4,7 +4,7 @@ const ( orchestratorName = "guardrails-orchestrator" finalizerName = "trustyai.opendatahub.io/gorch-finalizer" configMapName = "gorch-config" - orchestratorImageKey = "guardrailsOrchestratorImage" + orchestratorImageKey = "guardrails-orchestrator-image" vllmGatewayImageKey = "vllmGatewayImage" regexDetectorImageKey = "regexDetectorImage" ServiceName = "GORCH" diff --git a/controllers/gorch/deployment.go b/controllers/gorch/deployment.go index 7d017813..033c117e 100644 --- a/controllers/gorch/deployment.go +++ b/controllers/gorch/deployment.go @@ -37,7 +37,7 @@ type DeploymentConfig struct { func (r *GuardrailsOrchestratorReconciler) createDeployment(ctx context.Context, orchestrator *gorchv1alpha1.GuardrailsOrchestrator) *appsv1.Deployment { var containerImages ContainerImages // Get orchestrator image - orchestratorImage, err := r.getImageFromConfigMap(ctx, orchestratorImageKey, constants.ConfigMap, orchestrator.Namespace) + orchestratorImage, err := r.getImageFromConfigMap(ctx, orchestratorImageKey, constants.ConfigMap, r.Namespace) if orchestratorImage == "" || err != nil { log.FromContext(ctx).Error(err, "Error getting container image from ConfigMap.") } @@ -46,11 +46,11 @@ func (r *GuardrailsOrchestratorReconciler) createDeployment(ctx context.Context, // Check if the vLLM gateway is enabled if orchestrator.Spec.VLLMGatewayConfig != nil { // Get the gateway and regex detector container images - vllmGatewayImage, err := r.getImageFromConfigMap(ctx, vllmGatewayImageKey, constants.ConfigMap, orchestrator.Namespace) + vllmGatewayImage, err := r.getImageFromConfigMap(ctx, vllmGatewayImageKey, orchestratorName+"-config", orchestrator.Namespace) if vllmGatewayImage == "" || err != nil { log.FromContext(ctx).Error(err, "Error getting vLLM gateway image from ConfigMap.") } - regexDetectorImage, err := r.getImageFromConfigMap(ctx, regexDetectorImageKey, constants.ConfigMap, orchestrator.Namespace) + regexDetectorImage, err := r.getImageFromConfigMap(ctx, regexDetectorImageKey, orchestratorName+"-config", orchestrator.Namespace) if regexDetectorImage == "" || err != nil { log.FromContext(ctx).Error(err, "Error getting regex detectors image from ConfigMap.") } diff --git a/controllers/gorch/guardrailsorchestrator_controller_test.go b/controllers/gorch/guardrailsorchestrator_controller_test.go index 856081bf..671c0e05 100644 --- a/controllers/gorch/guardrailsorchestrator_controller_test.go +++ b/controllers/gorch/guardrailsorchestrator_controller_test.go @@ -126,51 +126,22 @@ func deleteGuardrailsOrchestrator(ctx context.Context, namespace string) error { func testCreateDeleteGuardrailsOrchestrator(namespaceName string) { It("Should sucessfully reconcile creating a custom resource for the GuardrailsOrchestrator", func() { - By("Creating an Orchestrator configmap") - configMap := &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: orchestratorName + "-config", - Namespace: namespaceName, - }, - } - err := k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) By("Creating a custom resource for the GuardrailsOrchestrator") ctx := context.Background() typedNamespacedName := types.NamespacedName{Name: orchestratorName, Namespace: namespaceName} - err = createGuardrailsOrchestrator(ctx, configMap.Name) + err := createGuardrailsOrchestrator(ctx, orchestratorName+"-config") Expect(err).ToNot(HaveOccurred()) By("Checking if the custom resource was successfully created") err = k8sClient.Get(ctx, typedNamespacedName, &gorchv1alpha1.GuardrailsOrchestrator{}) Expect(err).ToNot(HaveOccurred()) - By("Creating the TrustyAI configmap for testing") - configMap = &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: constants.ConfigMap, - Namespace: namespaceName, - }, - Data: map[string]string{ - orchestratorImageKey: "quay.io/trustyai/ta-guardrails-orchestrator:latest", - }, - } - err = k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) - By("Reconciling the custom resource that was created") reconciler := &GuardrailsOrchestratorReconciler{ - Client: k8sClient, - Scheme: k8sClient.Scheme(), + Client: k8sClient, + Scheme: k8sClient.Scheme(), + Namespace: namespaceName, } _, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typedNamespacedName}) @@ -243,22 +214,9 @@ func testCreateDeleteGuardrailsOrchestrator(namespaceName string) { func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) { It("Should sucessfully reconcile creating a custom resource for the GuardrailsOrchestrator", func() { - By("Creating an Orchestrator configmap") - configMap := &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: orchestratorName + "-config", - Namespace: namespaceName, - }, - } - err := k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) By("Creating an VLLM Gateway configmap") - configMap = &corev1.ConfigMap{ + configMap := &corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ Kind: "ConfigMap", APIVersion: "v1", @@ -268,7 +226,7 @@ func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) { Namespace: namespaceName, }, } - err = k8sClient.Create(ctx, configMap) + err := k8sClient.Create(ctx, configMap) Expect(err).ToNot(HaveOccurred()) By("Creating a custom resource for the GuardrailsOrchestrator") @@ -281,29 +239,11 @@ func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) { err = k8sClient.Get(ctx, typedNamespacedName, &gorchv1alpha1.GuardrailsOrchestrator{}) Expect(err).ToNot(HaveOccurred()) - By("Creating the TrustyAI configmap with sidecar images") - configMap = &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: constants.ConfigMap, - Namespace: namespaceName, - }, - Data: map[string]string{ - orchestratorImageKey: "quay.io/trustyai/ta-guardrails-orchestrator:latest", - vllmGatewayImageKey: "quay.io/trustyai/ta-guardrails-gateway:latest", - regexDetectorImageKey: "quay.io/trustyai/ta-guardrails-regex:latest", - }, - } - err = k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) - By("Reconciling the custom resource that was created") reconciler := &GuardrailsOrchestratorReconciler{ - Client: k8sClient, - Scheme: k8sClient.Scheme(), + Client: k8sClient, + Scheme: k8sClient.Scheme(), + Namespace: namespaceName, } _, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typedNamespacedName}) @@ -312,11 +252,11 @@ func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) { By("Checking if resources were successfully created in the reconcilation") Eventually(func() error { configMap := &corev1.ConfigMap{} - if err := k8sClient.Get(ctx, types.NamespacedName{Name: constants.ConfigMap, Namespace: namespaceName}, configMap); err != nil { + if err := k8sClient.Get(ctx, types.NamespacedName{Name: orchestratorName + "-config", Namespace: namespaceName}, configMap); err != nil { return err } Expect(configMap.Namespace).Should(Equal(namespaceName)) - Expect(configMap.Name).Should(Equal(constants.ConfigMap)) + Expect(configMap.Name).Should(Equal(orchestratorName + "-config")) serviceAccount := &corev1.ServiceAccount{} if err := k8sClient.Get(ctx, types.NamespacedName{Name: orchestratorName + "-serviceaccount", Namespace: namespaceName}, serviceAccount); err != nil { @@ -376,51 +316,22 @@ func testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName string) { func testCreateDeleteGuardrailsOrchestratorOtelExporter(namespaceName string) { It("Should sucessfully reconcile creating a custom resource for the GuardrailsOrchestrator", func() { - By("Creating an Orchestrator configmap") - configMap := &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: orchestratorName + "-config", - Namespace: namespaceName, - }, - } - err := k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) By("Creating a custom resource for the GuardrailsOrchestrator") ctx := context.Background() typedNamespacedName := types.NamespacedName{Name: orchestratorName, Namespace: namespaceName} - err = createGuardrailsOrchestratorOtelExporter(ctx, configMap.Name) + err := createGuardrailsOrchestratorOtelExporter(ctx, orchestratorName+"-config") Expect(err).ToNot(HaveOccurred()) By("Checking if the custom resource was successfully created") err = k8sClient.Get(ctx, typedNamespacedName, &gorchv1alpha1.GuardrailsOrchestrator{}) Expect(err).ToNot(HaveOccurred()) - By("Creating the TrustyAI configmap with sidecar images") - configMap = &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: constants.ConfigMap, - Namespace: namespaceName, - }, - Data: map[string]string{ - orchestratorImageKey: "quay.io/trustyai/ta-guardrails-orchestrator:latest", - }, - } - err = k8sClient.Create(ctx, configMap) - Expect(err).ToNot(HaveOccurred()) - By("Reconciling the custom resource that was created") reconciler := &GuardrailsOrchestratorReconciler{ - Client: k8sClient, - Scheme: k8sClient.Scheme(), + Client: k8sClient, + Scheme: k8sClient.Scheme(), + Namespace: namespaceName, } _, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typedNamespacedName}) @@ -502,6 +413,49 @@ func testCreateDeleteGuardrailsOrchestratorOtelExporter(namespaceName string) { } var _ = Describe("GuardrailsOrchestrator Controller", func() { + var ctx = context.Background() + BeforeEach(func() { + By("Creating the operator's ConfigMap") + configMap := &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: constants.ConfigMap, + Namespace: namespaceName, + }, + Data: map[string]string{ + orchestratorImageKey: "quay.io/trustyai/ta-guardrails-orchestrator:latest", + }, + } + err := k8sClient.Create(ctx, configMap) + + if err != nil && !errors.IsAlreadyExists(err) { + Expect(err).ToNot(HaveOccurred()) + } + + orchConfig := &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: orchestratorName + "-config", + Namespace: namespaceName, + }, + Data: map[string]string{ + vllmGatewayImageKey: "quay.io/trustyai/ta-guardrails-gateway:latest", + regexDetectorImageKey: "quay.io/trustyai/ta-guardrails-regex:latest", + }, + } + err = k8sClient.Create(ctx, orchConfig) + if err != nil && !errors.IsAlreadyExists(err) { + Expect(err).ToNot(HaveOccurred()) + } + + }) + Context("GuardrailsOrchestrator Controller Test", func() { testCreateDeleteGuardrailsOrchestrator(namespaceName) testCreateDeleteGuardrailsOrchestratorSidecar(namespaceName)