@@ -13,6 +13,10 @@ import (
1313)
1414
1515func (r * TrustyAIServiceReconciler ) patchEnvVarsForDeployments (ctx context.Context , instance * trustyaiopendatahubiov1alpha1.TrustyAIService , deployments []appsv1.Deployment , envVarName string , url string , remove bool ) (bool , error ) {
16+ // Create volume and volume mount for this intance's TLS secrets
17+ certVolumes := TLSCertVolumes {}
18+ certVolumes .createFor (instance )
19+
1620 // Loop over the Deployments
1721 for _ , deployment := range deployments {
1822
@@ -23,8 +27,31 @@ func (r *TrustyAIServiceReconciler) patchEnvVarsForDeployments(ctx context.Conte
2327 return false , nil
2428 }
2529
30+ // If the secret volume doesn't exist, add it
31+ volumeExists := false
32+ for _ , vol := range deployment .Spec .Template .Spec .Volumes {
33+ if vol .Name == instance .Name + "-internal" {
34+ volumeExists = true
35+ break
36+ }
37+ }
38+ if ! volumeExists {
39+ deployment .Spec .Template .Spec .Volumes = append (deployment .Spec .Template .Spec .Volumes , certVolumes .volume )
40+ }
41+
2642 // Loop over all containers in the Deployment's Pod template
2743 for i := range deployment .Spec .Template .Spec .Containers {
44+ mountExists := false
45+ for _ , mount := range deployment .Spec .Template .Spec .Containers [i ].VolumeMounts {
46+ if mount .Name == instance .Name + "-internal" {
47+ mountExists = true
48+ break
49+ }
50+ }
51+ if ! mountExists {
52+ deployment .Spec .Template .Spec .Containers [i ].VolumeMounts = append (deployment .Spec .Template .Spec .Containers [i ].VolumeMounts , certVolumes .volumeMount )
53+ }
54+
2855 // Store the original environment variable list
2956 // Get the existing env var
3057 var envVar * corev1.EnvVar
@@ -50,14 +77,17 @@ func (r *TrustyAIServiceReconciler) patchEnvVarsForDeployments(ctx context.Conte
5077 } else if envVar != nil {
5178 // If the env var exists and already contains the value, don't do anything
5279 existingValues := strings .Split (envVar .Value , " " )
80+ valueExists := false
5381 for _ , v := range existingValues {
5482 if v == url {
55- continue
83+ valueExists = true
84+ break
5685 }
5786 }
5887
59- // Modify the existing env var based on the remove flag and current value
60- envVar .Value = generateEnvVarValue (envVar .Value , url , remove )
88+ if ! valueExists {
89+ envVar .Value = generateEnvVarValue (envVar .Value , url , remove )
90+ }
6191 }
6292
6393 // Only update the deployment if the var value has to change, or we are removing it
@@ -70,6 +100,32 @@ func (r *TrustyAIServiceReconciler) patchEnvVarsForDeployments(ctx context.Conte
70100 r .eventModelMeshConfigured (instance )
71101 log .FromContext (ctx ).Info ("Updating Deployment " + deployment .Name + ", container spec " + deployment .Spec .Template .Spec .Containers [i ].Name + ", env var " + envVarName + " to " + url )
72102 }
103+
104+ // Check TLS environment variable on ModelMesh
105+ if deployment .Spec .Template .Spec .Containers [i ].Name == mmContainerName {
106+ tlsKeyCertPathEnvValue := tlsMountPath + "/tls.crt"
107+ tlsKeyCertPathExists := false
108+ for _ , envVar := range deployment .Spec .Template .Spec .Containers [i ].Env {
109+ if envVar .Name == tlsKeyCertPathName {
110+ tlsKeyCertPathExists = true
111+ break
112+ }
113+ }
114+
115+ // Doesn't exist, so we can add
116+ if ! tlsKeyCertPathExists {
117+ deployment .Spec .Template .Spec .Containers [i ].Env = append (deployment .Spec .Template .Spec .Containers [i ].Env , corev1.EnvVar {
118+ Name : tlsKeyCertPathName ,
119+ Value : tlsKeyCertPathEnvValue ,
120+ })
121+
122+ if err := r .Update (ctx , & deployment ); err != nil {
123+ log .FromContext (ctx ).Error (err , "Could not update Deployment" , "Deployment" , deployment .Name )
124+ return false , err
125+ }
126+ log .FromContext (ctx ).Info ("Added environment variable " + tlsKeyCertPathName + " to deployment " + deployment .Name + " for container " + mmContainerName )
127+ }
128+ }
73129 }
74130 }
75131
0 commit comments