Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
christinaexyou committed Jan 15, 2025
1 parent 4da3baf commit 42572c8
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 33 deletions.
4 changes: 3 additions & 1 deletion api/gorch/v1alpha1/guardrailsorchestrator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type ChunkerSpec struct {
}

type DetectorSpec struct {
Name string `json:"name"`
Type string `json:"type"`
Service ServiceSpec `json:"service"`
ChunkerName string `json:"chunkerName"`
Expand All @@ -63,6 +64,7 @@ type GuardrailsOrchestratorSpec struct {

// Number of replicas
Replicas int32 `json:"replicas"`
// CongigMapName string `json:"configMapName"`
// Generator configuration
Generator GeneratorSpec `json:"generator"`
// Chunker configuration
Expand Down Expand Up @@ -108,7 +110,7 @@ type GuardrailsOrchestratorCondition struct {
Type string `json:"type"`
Status metav1.ConditionStatus `json:"status"`
// +optional
Reason string `json:"reason,omitmempty"`
Reason string `json:"reason,omitempty"`
// +optional
Message string `json:"message,omitempty"`
// +optional
Expand Down
2 changes: 1 addition & 1 deletion config/base/params.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ lmes-image-pull-policy=Always
lmes-max-batch-size=24
lmes-default-batch-size=8
lmes-detect-device=true
guardrails-orchestrator-image=quay.io/csantiago/orchestrator-xu:latest
guardrails-orchestrator-image=quay.io/rh-ee-mmisiura/fms-orchestr8-nlp:0.7.0
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ spec:
type: string
defaultThreshold:
type: string
name:
type: string
service:
properties:
hostname:
Expand Down Expand Up @@ -119,12 +121,15 @@ spec:
required:
- chunkerName
- defaultThreshold
- name
- service
- type
type: object
type: array
generator:
description: Generator configuration
description: |-
CongigMapName string `json:"configMapName"`
Generator configuration
properties:
provider:
type: string
Expand Down
2 changes: 1 addition & 1 deletion config/overlays/odh/params.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ lmes-image-pull-policy=Always
lmes-max-batch-size=24
lmes-default-batch-size=8
lmes-detect-device=true
guardrails-orchestrator-image=quay.io/csantiago/orchestrator-xu:latest
guardrails-orchestrator-image=quay.io/rh-ee-mmisiura/fms-orchestr8-nlp:0.7.0
2 changes: 1 addition & 1 deletion config/overlays/rhoai/params.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ lmes-image-pull-policy=Always
lmes-max-batch-size=24
lmes-default-batch-size=8
lmes-detect-device=true
guardrails-orchestrator-image=quay.io/csantiago/orchestrator-xu:latest
guardrails-orchestrator-image=quay.io/rh-ee-mmisiura/fms-orchestr8-nlp:0.7.0
29 changes: 29 additions & 0 deletions controllers/gorch/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@ package gorch
import (
"context"
"fmt"
"reflect"

gorchv1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/gorch/v1alpha1"
"github.com/trustyai-explainability/trustyai-service-operator/controllers/constants"
templateParser "github.com/trustyai-explainability/trustyai-service-operator/controllers/gorch/templates"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

const configMapTemplatePath = "configmap.tmpl.yaml"

type configMapConfig struct {
Orchestrator *gorchv1alpha1.GuardrailsOrchestrator
// ContainerImage string
// Version string
}

func (r *GuardrailsOrchestratorReconciler) createConfigMap(ctx context.Context, orchestrator *gorchv1alpha1.GuardrailsOrchestrator) *corev1.ConfigMap {
configMapConfig := configMapConfig{
Orchestrator: orchestrator,
}

var configMap *corev1.ConfigMap
configMap, err := templateParser.ParseResource[corev1.ConfigMap](configMapTemplatePath, configMapConfig, reflect.TypeOf(&corev1.ConfigMap{}))
if err != nil {
log.FromContext(ctx).Error(err, "Failed to parse configmap template")
}
if err := controllerutil.SetControllerReference(orchestrator, configMap, r.Scheme); err != nil {
log.FromContext(ctx).Error(err, "Failed to set controller reference for configmap")
}
return configMap
}

func (r *GuardrailsOrchestratorReconciler) getImageFromConfigMap(ctx context.Context, configMapKey string, defaultContainerImage string) (string, error) {
if r.Namespace != "" {
configMap := &corev1.ConfigMap{}
Expand Down
15 changes: 15 additions & 0 deletions controllers/gorch/guardrailsorchestrator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
return ctrl.Result{}, nil
}

existingConfigMap := &corev1.ConfigMap{}
err = r.Get(ctx, types.NamespacedName{Name: orchestrator.Name + "-config", Namespace: orchestrator.Namespace}, existingConfigMap)
if err != nil && errors.IsNotFound(err) {
configMap := r.createConfigMap(ctx, orchestrator)
log.Info("Creating a new ConfigMap", "ConfigMap.Namespace", configMap.Namespace, "ConfigMap.Name", configMap.Name)
err = r.Create(ctx, configMap)
if err != nil {
log.Error(err, "Failed to create new ConfigMap", "ConfigMap.Namespace", configMap.Namespace, "ConfigMap.Name", configMap.Name)
return ctrl.Result{}, err
}
} else if err != nil {
log.Error(err, "Failed to get ConfigMap")
return ctrl.Result{}, err
}

existingServiceAccount := &corev1.ServiceAccount{}
err = r.Get(ctx, types.NamespacedName{Name: orchestrator.Name, Namespace: orchestrator.Namespace}, existingServiceAccount)
if err != nil && errors.IsNotFound(err) {
Expand Down
12 changes: 12 additions & 0 deletions controllers/gorch/guardrailsorchestrator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func testCreateDeleteGuardrailsOrchestrator(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: orchestratorName + "-config", Namespace: namespaceName}, configMap); err != nil {
return err
}
Expect(configMap.Name).Should(Equal(orchestratorName + "-config"))
Expect(configMap.Namespace).Should(Equal(namespaceName))
Expect(configMap.Data["config.yaml"]).ShouldNot(BeEmpty())

serviceAccount := &corev1.ServiceAccount{}
if err := k8sClient.Get(ctx, types.NamespacedName{Name: orchestratorName + "-serviceaccount", Namespace: namespaceName}, serviceAccount); err != nil {
return err
Expand All @@ -123,7 +131,11 @@ func testCreateDeleteGuardrailsOrchestrator(namespaceName string) {
Expect(deployment.Namespace).Should(Equal(namespaceName))
Expect(deployment.Name).Should(Equal(orchestratorName))
Expect(deployment.Labels["app"]).Should(Equal(orchestratorName))
Expect(deployment.Spec.Template.Spec.Volumes[0].Name).Should(Equal(orchestratorName + "-config"))
Expect(deployment.Spec.Template.Spec.Volumes[0].ConfigMap.Name).Should(Equal(orchestratorName + "-config"))
Expect(deployment.Spec.Template.Spec.Containers[0].Image).Should(Equal(defaultContainerImage))
Expect(deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name).Should(Equal(orchestratorName + "-config"))
Expect()

service := &corev1.Service{}
if err := k8sClient.Get(ctx, types.NamespacedName{Name: orchestratorName + "-service", Namespace: namespaceName}, service); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions controllers/gorch/templates/configmap.tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app:
component: fms-orchestr8-nlp
deploy-name: fms-orchestr8-nlp
name: {{.Orchestrator.Name}}-config
namespace: {{.Orchestrator.Namespace}}
data:
config.yaml: |
generation:
provider: {{.Orchestrator.Spec.Generator.Provider}}
service:
hostname: {{.Orchestrator.Spec.Generator.Service.Hostname}}
port: {{.Orchestrator.Spec.Generator.Service.Port}}
{{- range .Orchestrator.Spec.Detectors }}
detectors:
{{.Name}}:
type: {{.Type}}
service:
hostname: {{.Service.Hostname}}
port: {{.Service.Port}}
chunker_id: {{.ChunkerName}}
default_threshold: {{.DefaultThreshold}}
{{- end }}
{{ if .Orchestrator.Spec.Chunkers}}
{{- range .Orchestrator.Spec.Chunkers }}
chunkers:
{{.ChunkerName}}:
service:
hostname: {{.Service.Hostname}}
port: {{.Service.Port}}
{{- end }}
{{- end }}
40 changes: 12 additions & 28 deletions controllers/gorch/templates/deployment.tmpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,29 @@ spec:
annotations:
sidecar.istio.io/inject: "true"
spec:
volumes:
- name: {{.Orchestrator.Name}}-config
configMap:
name: {{.Orchestrator.Name}}-config
defualtMode: 420
serviceAccountName: {{.Orchestrator.Name}}-serviceaccount
containers:
- name: {{.Orchestrator.Name}}
image: {{.ContainerImage}}
env:
- name: ORCHESTRATOR_CONFIG
value: /config/config.yaml
- name: HTTP_PORT
value: '8033'
- name: RUST_BACKTRACE
value: 'full'
- name: RUST_LOG
value: 'fms_guardrails_orchestr8=trace,tower_grpc=debug'
- name: GENERATOR_PROVIDER
value: {{.Orchestrator.Spec.Generator.Provider}}
- name: GENERATOR_HOSTNAME
value: {{.Orchestrator.Spec.Generator.Service.Hostname}}
- name: GENERATOR_PORT
value: {{.Orchestrator.Spec.Generator.Service.Port}}
{{- range .Orchestrator.Spec.Detectors }}
- name: DETECTOR_TYPE
value: {{.Type}}
- name: DETECTOR_HOSTNAME
value: {{.Service.Hostname}}
- name: DETECTOR_PORT
value: {{.Service.Port}}
- name: DETECTOR_CHUNKERNAME
value: {{.ChunkerName}}
- name: DETECTOR_DEFAULT_THRESHOLD
value: {{.DefaultThreshold}}
{{- end }}
{{- if .Orchestrator.Spec.Chunkers}}
{{- range .Orchestrator.Spec.Chunkers }}
- name: CHUNKER_NAME
value: {{.ChunkerName}}
- name: CHUNKER_HOSTNAME
value: {{.Service.Hostname}}
- name: CHUNKER_PORT
value: {{.Service.Port}}
{{- end }}
{{- end }}
volumeMounts:
- name: {{.Orchestrator.Name}}-config
readOnly: true
mountPath: /config
subPath: config.yaml
ports:
- name: http
containerPort: 8033
Expand Down

0 comments on commit 42572c8

Please sign in to comment.