@@ -162,45 +162,50 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
162
162
}
163
163
164
164
if len (envVars ) > 0 {
165
- for i , c := range pod .Spec .Containers {
166
- if needsCreds {
167
- if len (c .VolumeMounts ) == 0 {
168
- patch = append (patch , patchOperation {
169
- Op : "add" ,
170
- Path : fmt .Sprintf ("/spec/containers/%d/volumeMounts" , i ),
171
- Value : []corev1.VolumeMount {mount },
172
- })
173
- } else {
174
- addMount := true
175
- for _ , vm := range c .VolumeMounts {
176
- if vm .Name == mount .Name {
177
- addMount = false
178
- break
179
- }
180
- }
181
- if addMount {
165
+ addCredsToContainer := func (containers []corev1.Container , container_uri string ) {
166
+ for i , c := range containers {
167
+ if needsCreds {
168
+ if len (c .VolumeMounts ) == 0 {
182
169
patch = append (patch , patchOperation {
183
170
Op : "add" ,
184
- Path : fmt .Sprintf ("/spec/containers /%d/volumeMounts" , i ),
185
- Value : append ( c . VolumeMounts , mount ) ,
171
+ Path : fmt .Sprintf ("/spec/%s /%d/volumeMounts" , container_uri , i ),
172
+ Value : []corev1. VolumeMount { mount } ,
186
173
})
174
+ } else {
175
+ addMount := true
176
+ for _ , vm := range c .VolumeMounts {
177
+ if vm .Name == mount .Name {
178
+ addMount = false
179
+ break
180
+ }
181
+ }
182
+ if addMount {
183
+ patch = append (patch , patchOperation {
184
+ Op : "add" ,
185
+ Path : fmt .Sprintf ("/spec/%s/%d/volumeMounts" , container_uri , i ),
186
+ Value : append (c .VolumeMounts , mount ),
187
+ })
188
+ }
187
189
}
188
190
}
189
- }
190
- if len ( c . Env ) == 0 {
191
- patch = append ( patch , patchOperation {
192
- Op : "add" ,
193
- Path : fmt . Sprintf ( "/spec/containers/%d/env" , i ) ,
194
- Value : envVars ,
195
- })
196
- } else {
197
- patch = append ( patch , patchOperation {
198
- Op : "add" ,
199
- Path : fmt . Sprintf ( "/spec/containers/%d/env" , i ),
200
- Value : append ( c . Env , envVars ... ),
201
- })
191
+ if len ( c . Env ) == 0 {
192
+ patch = append ( patch , patchOperation {
193
+ Op : "add" ,
194
+ Path : fmt . Sprintf ( "/spec/%s/%d/env" , container_uri , i ) ,
195
+ Value : envVars ,
196
+ })
197
+ } else {
198
+ patch = append ( patch , patchOperation {
199
+ Op : "add" ,
200
+ Path : fmt . Sprintf ( "/spec/%s/%d/env" , container_uri , i ) ,
201
+ Value : append ( c . Env , envVars ... ),
202
+ })
203
+ }
202
204
}
203
205
}
206
+
207
+ addCredsToContainer (pod .Spec .Containers , "containers" )
208
+ addCredsToContainer (pod .Spec .InitContainers , "initContainers" )
204
209
}
205
210
}
206
211
0 commit comments