@@ -90,6 +90,9 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
90
90
}
91
91
92
92
var patch []patchOperation
93
+ var envVars []corev1.EnvVar
94
+
95
+ needsCreds := needsEnvVar (pod .Spec .Containers [0 ], "GOOGLE_APPLICATION_CREDENTIALS" )
93
96
94
97
// Explicitly and silently exclude the kube-system namespace
95
98
if pod .ObjectMeta .Namespace != metav1 .NamespaceSystem {
@@ -117,59 +120,68 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
117
120
ReadOnly : true ,
118
121
}
119
122
120
- // Define the env var
121
- e := corev1.EnvVar {
122
- Name : "GOOGLE_APPLICATION_CREDENTIALS" ,
123
- Value : "/google-app-creds.json" ,
123
+ if needsCreds {
124
+ // Define the env var
125
+ e := corev1.EnvVar {
126
+ Name : "GOOGLE_APPLICATION_CREDENTIALS" ,
127
+ Value : "/google-app-creds.json" ,
128
+ }
129
+ envVars = append (envVars , e )
130
+
131
+ // 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
+ })
124
137
}
125
- envVars := []corev1.EnvVar {e }
126
138
127
139
// If GOOGLE_CLOUD_PROJECT is set in the VM, set it for all GCP apps.
128
140
if _ , err := os .Stat ("/var/lib/minikube/google_cloud_project" ); err == nil {
129
141
project , err := ioutil .ReadFile ("/var/lib/minikube/google_cloud_project" )
130
142
if err == nil {
131
143
// Set the project name for every variant of the project env var
132
144
for _ , a := range projectAliases {
133
- envVars = append (envVars , corev1.EnvVar {
134
- Name : a ,
135
- Value : string (project ),
136
- })
145
+ if needsEnvVar (pod .Spec .Containers [0 ], a ) {
146
+ envVars = append (envVars , corev1.EnvVar {
147
+ Name : a ,
148
+ Value : string (project ),
149
+ })
150
+ }
137
151
}
138
152
}
139
153
}
140
154
141
- patch = append (patch , patchOperation {
142
- Op : "add" ,
143
- Path : "/spec/volumes" ,
144
- Value : append (pod .Spec .Volumes , v ),
145
- })
146
-
147
- for i , c := range pod .Spec .Containers {
148
- if len (c .VolumeMounts ) == 0 {
149
- patch = append (patch , patchOperation {
150
- Op : "add" ,
151
- Path : fmt .Sprintf ("/spec/containers/%d/volumeMounts" , i ),
152
- Value : []corev1.VolumeMount {mount },
153
- })
154
- } else {
155
- patch = append (patch , patchOperation {
156
- Op : "add" ,
157
- Path : fmt .Sprintf ("/spec/containers/%d/volumeMounts" , i ),
158
- Value : append (c .VolumeMounts , mount ),
159
- })
160
- }
161
- if len (c .Env ) == 0 {
162
- patch = append (patch , patchOperation {
163
- Op : "add" ,
164
- Path : fmt .Sprintf ("/spec/containers/%d/env" , i ),
165
- Value : envVars ,
166
- })
167
- } else {
168
- patch = append (patch , patchOperation {
169
- Op : "add" ,
170
- Path : fmt .Sprintf ("/spec/containers/%d/env" , i ),
171
- Value : append (c .Env , envVars ... ),
172
- })
155
+ if len (envVars ) > 0 {
156
+ for i , c := range pod .Spec .Containers {
157
+ if needsCreds {
158
+ if len (c .VolumeMounts ) == 0 {
159
+ patch = append (patch , patchOperation {
160
+ Op : "add" ,
161
+ Path : fmt .Sprintf ("/spec/containers/%d/volumeMounts" , i ),
162
+ Value : []corev1.VolumeMount {mount },
163
+ })
164
+ } 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
+ })
170
+ }
171
+ }
172
+ if len (c .Env ) == 0 {
173
+ patch = append (patch , patchOperation {
174
+ Op : "add" ,
175
+ Path : fmt .Sprintf ("/spec/containers/%d/env" , i ),
176
+ Value : envVars ,
177
+ })
178
+ } else {
179
+ patch = append (patch , patchOperation {
180
+ Op : "add" ,
181
+ Path : fmt .Sprintf ("/spec/containers/%d/env" , i ),
182
+ Value : append (c .Env , envVars ... ),
183
+ })
184
+ }
173
185
}
174
186
}
175
187
}
@@ -261,13 +273,13 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
261
273
262
274
ips := corev1.LocalObjectReference {Name : "gcp-auth" }
263
275
if len (sa .ImagePullSecrets ) == 0 {
264
- patch = []patchOperation {patchOperation {
276
+ patch = []patchOperation {{
265
277
Op : "add" ,
266
278
Path : "/imagePullSecrets" ,
267
279
Value : []corev1.LocalObjectReference {ips },
268
280
}}
269
281
} else {
270
- patch = []patchOperation {patchOperation {
282
+ patch = []patchOperation {{
271
283
Op : "add" ,
272
284
Path : "/imagePullSecrets" ,
273
285
Value : append (sa .ImagePullSecrets , ips ),
@@ -316,6 +328,15 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
316
328
}
317
329
}
318
330
331
+ func needsEnvVar (c corev1.Container , name string ) bool {
332
+ for _ , e := range c .Env {
333
+ if e .Name == name {
334
+ return false
335
+ }
336
+ }
337
+ return true
338
+ }
339
+
319
340
func main () {
320
341
log .Print ("GCP Auth Webhook started!" )
321
342
0 commit comments