Skip to content

Commit d27107e

Browse files
committed
Addressed PR comments
- inverted large if block and moved check to beginning of func - small readme fixes
1 parent 2935f4e commit d27107e

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# gcp-auth-webhook
22

33
A server that includes:
4-
* A mutating webhook that will patch any newly created pods in your kubernetes cluster with GCP credentials (whose location is currently hardcoded to /var/lib/minikube/google_application_credentials.json).
5-
* A mutating webhook that will patch any newly created service accounts in your kubernetes cluster with an image pull secret.
4+
* A mutating webhook that will patch any newly created pods in your Kubernetes cluster with GCP credentials (whose location is currently hardcoded to /var/lib/minikube/google_application_credentials.json).
5+
* A mutating webhook that will patch any newly created service accounts in your Kubernetes cluster with an image pull secret.
66
* A thread that monitors namespaces to make sure all namespaces include a image pull secret to be able to pull from GCR and AR.
77

88
## Deployment
9-
Use the image gcr.io/k8s-minikube/gcp-auth-webhook as the image for a Deployment in your Kubernetes manifest and add that to a MutatingWebhookConfiguration. See [minikube](https://github.com/kubernetes/minikube/blob/master/deploy/addons/gcp-auth/gcp-auth-webhook.yaml.tmpl) for details.
9+
Use the image `gcr.io/k8s-minikube/gcp-auth-webhook` as the image for a Deployment in your Kubernetes manifest and add that to a MutatingWebhookConfiguration. See [minikube](https://github.com/kubernetes/minikube/blob/master/deploy/addons/gcp-auth/gcp-auth-webhook.yaml.tmpl) for details.
1010

1111
## Running Locally
1212
The easiest way to run the server locally is:

server.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,28 @@ func createPullSecret(clientset *kubernetes.Clientset, ns *corev1.Namespace, cre
103103
return nil
104104
}
105105

106-
token, err := creds.TokenSource.Token()
106+
secrets := clientset.CoreV1().Secrets(ns.Name)
107+
108+
// check if gcp-auth secret already exists
109+
exists := false
110+
secList, err := secrets.List(context.TODO(), metav1.ListOptions{})
107111
if err != nil {
108112
return err
109113
}
114+
for _, s := range secList.Items {
115+
if s.Name == gcpAuth {
116+
exists = true
117+
break
118+
}
119+
}
120+
if exists {
121+
return nil
122+
}
110123

124+
token, err := creds.TokenSource.Token()
125+
if err != nil {
126+
return err
127+
}
111128
var dockercfg string
112129
registries := append(gcr_config.DefaultGCRRegistries[:], gcr_config.DefaultARRegistries[:]...)
113130
for _, reg := range registries {
@@ -117,36 +134,18 @@ func createPullSecret(clientset *kubernetes.Clientset, ns *corev1.Namespace, cre
117134
data := map[string][]byte{
118135
".dockercfg": []byte(fmt.Sprintf(`{%s}`, dockercfg)),
119136
}
120-
121-
secrets := clientset.CoreV1().Secrets(ns.Name)
122-
123-
exists := false
124-
secList, err := secrets.List(context.TODO(), metav1.ListOptions{})
137+
secretObj := &corev1.Secret{
138+
ObjectMeta: metav1.ObjectMeta{
139+
Name: gcpAuth,
140+
},
141+
Data: data,
142+
Type: "kubernetes.io/dockercfg",
143+
}
144+
_, err = secrets.Create(context.TODO(), secretObj, metav1.CreateOptions{})
125145
if err != nil {
126146
return err
127147
}
128-
for _, s := range secList.Items {
129-
if s.Name == gcpAuth {
130-
exists = true
131-
break
132-
}
133-
}
134148

135-
if !exists {
136-
secretObj := &corev1.Secret{
137-
ObjectMeta: metav1.ObjectMeta{
138-
Name: gcpAuth,
139-
},
140-
Data: data,
141-
Type: "kubernetes.io/dockercfg",
142-
}
143-
144-
_, err = secrets.Create(context.TODO(), secretObj, metav1.CreateOptions{})
145-
if err != nil {
146-
return err
147-
}
148-
149-
}
150149
return nil
151150
}
152151

0 commit comments

Comments
 (0)