@@ -13,6 +13,10 @@ import (
13
13
)
14
14
15
15
func (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
+
16
20
// Loop over the Deployments
17
21
for _ , deployment := range deployments {
18
22
@@ -23,8 +27,31 @@ func (r *TrustyAIServiceReconciler) patchEnvVarsForDeployments(ctx context.Conte
23
27
return false , nil
24
28
}
25
29
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
+
26
42
// Loop over all containers in the Deployment's Pod template
27
43
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
+
28
55
// Store the original environment variable list
29
56
// Get the existing env var
30
57
var envVar * corev1.EnvVar
@@ -50,14 +77,17 @@ func (r *TrustyAIServiceReconciler) patchEnvVarsForDeployments(ctx context.Conte
50
77
} else if envVar != nil {
51
78
// If the env var exists and already contains the value, don't do anything
52
79
existingValues := strings .Split (envVar .Value , " " )
80
+ valueExists := false
53
81
for _ , v := range existingValues {
54
82
if v == url {
55
- continue
83
+ valueExists = true
84
+ break
56
85
}
57
86
}
58
87
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
+ }
61
91
}
62
92
63
93
// 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
70
100
r .eventModelMeshConfigured (instance )
71
101
log .FromContext (ctx ).Info ("Updating Deployment " + deployment .Name + ", container spec " + deployment .Spec .Template .Spec .Containers [i ].Name + ", env var " + envVarName + " to " + url )
72
102
}
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
+ }
73
129
}
74
130
}
75
131
0 commit comments