Skip to content

Commit 69b7214

Browse files
committed
add google creds to both containers and initContainers
1 parent 66a2a35 commit 69b7214

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

server.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,45 +162,50 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
162162
}
163163

164164
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 {
182169
patch = append(patch, patchOperation{
183170
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},
186173
})
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+
}
187189
}
188190
}
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+
}
202204
}
203205
}
206+
207+
addCredsToContainer(pod.Spec.Containers, "containers")
208+
addCredsToContainer(pod.Spec.InitContainers, "initContainers")
204209
}
205210
}
206211

0 commit comments

Comments
 (0)