Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't remount volumes if they already exist #20

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
REGISTRY?=gcr.io/k8s-minikube
VERSION=v0.0.7
VERSION=v0.0.8
GOOS?=$(shell go env GOOS)

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

.PHONY: image
image: ## Create and push multiarch manifest and images
@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;
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
KO_DOCKER_REPO=$(REGISTRY) ./ko publish -B . --platform all -t $(VERSION)
rm ./ko
Expand Down
75 changes: 52 additions & 23 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,20 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
envVars = append(envVars, e)

// add the volume in the list of patches
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/volumes",
Value: append(pod.Spec.Volumes, v),
})
addVolume := true
for _, vl := range pod.Spec.Volumes {
if vl.Name == v.Name {
addVolume = false
break
}
}
if addVolume {
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/volumes",
Value: append(pod.Spec.Volumes, v),
})
}
}

// If GOOGLE_CLOUD_PROJECT is set in the VM, set it for all GCP apps.
Expand Down Expand Up @@ -162,11 +171,20 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
Value: []corev1.VolumeMount{mount},
})
} else {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: append(c.VolumeMounts, mount),
})
addMount := true
for _, vm := range c.VolumeMounts {
if vm.Name == mount.Name {
addMount = false
break
}
}
if addMount {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: append(c.VolumeMounts, mount),
})
}
}
}
if len(c.Env) == 0 {
Expand Down Expand Up @@ -269,21 +287,32 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
}
}

// Make sure the gcp-auth secret exists before adding it as a pull secret
hasSecret := false
for _, s := range sa.Secrets {
if s.Name == "gcp-auth" {
hasSecret = true
break
}
}

var patch []patchOperation

ips := corev1.LocalObjectReference{Name: "gcp-auth"}
if len(sa.ImagePullSecrets) == 0 {
patch = []patchOperation{{
Op: "add",
Path: "/imagePullSecrets",
Value: []corev1.LocalObjectReference{ips},
}}
} else {
patch = []patchOperation{{
Op: "add",
Path: "/imagePullSecrets",
Value: append(sa.ImagePullSecrets, ips),
}}
if hasSecret {
ips := corev1.LocalObjectReference{Name: "gcp-auth"}
if len(sa.ImagePullSecrets) == 0 {
patch = []patchOperation{{
Op: "add",
Path: "/imagePullSecrets",
Value: []corev1.LocalObjectReference{ips},
}}
} else {
patch = []patchOperation{{
Op: "add",
Path: "/imagePullSecrets",
Value: append(sa.ImagePullSecrets, ips),
}}
}
}

patchBytes, err := json.Marshal(patch)
Expand Down