Skip to content

Commit

Permalink
exclude kube-system from getting patches
Browse files Browse the repository at this point in the history
  • Loading branch information
sharifelgamal committed Oct 2, 2020
1 parent 4a3da3d commit 7a0eabf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
REGISTRY?=gcr.io/k8s-minikube
VERSION=v0.0.2
VERSION=v0.0.3-snapshot

build:
CGO_ENABLED=0 GOOS=linux go build -o out/gcp-auth-webhook -ldflags=$(PROVISIONER_LDFLAGS) server.go
Expand Down
147 changes: 75 additions & 72 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,83 +90,86 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {

var patch []patchOperation

// Define the volume to mount in
v := corev1.Volume{
Name: "gcp-creds",
VolumeSource: corev1.VolumeSource{
HostPath: func() *corev1.HostPathVolumeSource {
h := corev1.HostPathVolumeSource{
Path: "/var/lib/minikube/google_application_credentials.json",
Type: func() *corev1.HostPathType {
hpt := corev1.HostPathFile
return &hpt
}(),
}
return &h
}(),
},
}
// Explicitly and silently exclude the kube-system namespace
if pod.ObjectMeta.Namespace != metav1.NamespaceSystem {
// Define the volume to mount in
v := corev1.Volume{
Name: "gcp-creds",
VolumeSource: corev1.VolumeSource{
HostPath: func() *corev1.HostPathVolumeSource {
h := corev1.HostPathVolumeSource{
Path: "/var/lib/minikube/google_application_credentials.json",
Type: func() *corev1.HostPathType {
hpt := corev1.HostPathFile
return &hpt
}(),
}
return &h
}(),
},
}

// Mount the volume in
mount := corev1.VolumeMount{
Name: "gcp-creds",
MountPath: "/google-app-creds.json",
ReadOnly: true,
}
// Mount the volume in
mount := corev1.VolumeMount{
Name: "gcp-creds",
MountPath: "/google-app-creds.json",
ReadOnly: true,
}

// Define the env var
e := corev1.EnvVar{
Name: "GOOGLE_APPLICATION_CREDENTIALS",
Value: "/google-app-creds.json",
}
envVars := []corev1.EnvVar{e}

// If GOOGLE_CLOUD_PROJECT is set in the VM, set it for all GCP apps.
if _, err := os.Stat("/var/lib/minikube/google_cloud_project"); err == nil {
project, err := ioutil.ReadFile("/var/lib/minikube/google_cloud_project")
if err == nil {
// Set the project name for every variant of the project env var
for _, a := range projectAliases {
envVars = append(envVars, corev1.EnvVar{
Name: a,
Value: string(project),
})
// Define the env var
e := corev1.EnvVar{
Name: "GOOGLE_APPLICATION_CREDENTIALS",
Value: "/google-app-creds.json",
}
envVars := []corev1.EnvVar{e}

// If GOOGLE_CLOUD_PROJECT is set in the VM, set it for all GCP apps.
if _, err := os.Stat("/var/lib/minikube/google_cloud_project"); err == nil {
project, err := ioutil.ReadFile("/var/lib/minikube/google_cloud_project")
if err == nil {
// Set the project name for every variant of the project env var
for _, a := range projectAliases {
envVars = append(envVars, corev1.EnvVar{
Name: a,
Value: string(project),
})
}
}
}
}

patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/volumes",
Value: append(pod.Spec.Volumes, v),
})

for i, c := range pod.Spec.Containers {
if len(c.VolumeMounts) == 0 {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: []corev1.VolumeMount{mount},
})
} else {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: append(c.VolumeMounts, mount),
})
}
if len(c.Env) == 0 {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/env", i),
Value: envVars,
})
} else {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/env", i),
Value: append(c.Env, envVars...),
})
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/volumes",
Value: append(pod.Spec.Volumes, v),
})

for i, c := range pod.Spec.Containers {
if len(c.VolumeMounts) == 0 {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: []corev1.VolumeMount{mount},
})
} else {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/volumeMounts", i),
Value: append(c.VolumeMounts, mount),
})
}
if len(c.Env) == 0 {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/env", i),
Value: envVars,
})
} else {
patch = append(patch, patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/env", i),
Value: append(c.Env, envVars...),
})
}
}
}

Expand Down

0 comments on commit 7a0eabf

Please sign in to comment.