Skip to content

Commit 7e3303c

Browse files
Merge pull request #20 from sharifelgamal/be-smarter
don't remount volumes if they already exist
2 parents 1b44e85 + 75a928f commit 7e3303c

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
REGISTRY?=gcr.io/k8s-minikube
2-
VERSION=v0.0.7
2+
VERSION=v0.0.8
33
GOOS?=$(shell go env GOOS)
44

55
build: ## Build the gcp-auth-webhook binary
66
CGO_ENABLED=0 GOOS=linux go build -o out/gcp-auth-webhook server.go
77

88
.PHONY: image
99
image: ## Create and push multiarch manifest and images
10+
@read -p "This will build and push $(REGISTRY)/gcp-auth-webhook:$(VERSION). Do you want to proceed? (Y/N): " confirm && echo $$confirm | grep -iq "^[yY]" || exit 1;
1011
curl -L https://github.com/google/ko/releases/download/v0.8.3/ko_0.8.3_$(GOOS)_x86_64.tar.gz | tar xzf - ko && chmod +x ./ko
1112
KO_DOCKER_REPO=$(REGISTRY) ./ko publish -B . --platform all -t $(VERSION)
1213
rm ./ko

server.go

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,20 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
129129
envVars = append(envVars, e)
130130

131131
// add the volume in the list of patches
132-
patch = append(patch, patchOperation{
133-
Op: "add",
134-
Path: "/spec/volumes",
135-
Value: append(pod.Spec.Volumes, v),
136-
})
132+
addVolume := true
133+
for _, vl := range pod.Spec.Volumes {
134+
if vl.Name == v.Name {
135+
addVolume = false
136+
break
137+
}
138+
}
139+
if addVolume {
140+
patch = append(patch, patchOperation{
141+
Op: "add",
142+
Path: "/spec/volumes",
143+
Value: append(pod.Spec.Volumes, v),
144+
})
145+
}
137146
}
138147

139148
// If GOOGLE_CLOUD_PROJECT is set in the VM, set it for all GCP apps.
@@ -162,11 +171,20 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
162171
Value: []corev1.VolumeMount{mount},
163172
})
164173
} else {
165-
patch = append(patch, patchOperation{
166-
Op: "add",
167-
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
168-
Value: append(c.VolumeMounts, mount),
169-
})
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 {
182+
patch = append(patch, patchOperation{
183+
Op: "add",
184+
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
185+
Value: append(c.VolumeMounts, mount),
186+
})
187+
}
170188
}
171189
}
172190
if len(c.Env) == 0 {
@@ -269,21 +287,32 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
269287
}
270288
}
271289

290+
// Make sure the gcp-auth secret exists before adding it as a pull secret
291+
hasSecret := false
292+
for _, s := range sa.Secrets {
293+
if s.Name == "gcp-auth" {
294+
hasSecret = true
295+
break
296+
}
297+
}
298+
272299
var patch []patchOperation
273300

274-
ips := corev1.LocalObjectReference{Name: "gcp-auth"}
275-
if len(sa.ImagePullSecrets) == 0 {
276-
patch = []patchOperation{{
277-
Op: "add",
278-
Path: "/imagePullSecrets",
279-
Value: []corev1.LocalObjectReference{ips},
280-
}}
281-
} else {
282-
patch = []patchOperation{{
283-
Op: "add",
284-
Path: "/imagePullSecrets",
285-
Value: append(sa.ImagePullSecrets, ips),
286-
}}
301+
if hasSecret {
302+
ips := corev1.LocalObjectReference{Name: "gcp-auth"}
303+
if len(sa.ImagePullSecrets) == 0 {
304+
patch = []patchOperation{{
305+
Op: "add",
306+
Path: "/imagePullSecrets",
307+
Value: []corev1.LocalObjectReference{ips},
308+
}}
309+
} else {
310+
patch = []patchOperation{{
311+
Op: "add",
312+
Path: "/imagePullSecrets",
313+
Value: append(sa.ImagePullSecrets, ips),
314+
}}
315+
}
287316
}
288317

289318
patchBytes, err := json.Marshal(patch)

0 commit comments

Comments
 (0)