Skip to content

Commit 5153d86

Browse files
committed
Refactor read and write functions for new handlers
1 parent 7e3303c commit 5153d86

File tree

1 file changed

+82
-119
lines changed

1 file changed

+82
-119
lines changed

server.go

Lines changed: 82 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ import (
2525
admissionv1 "k8s.io/api/admission/v1"
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
2829
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/runtime/serializer"
3031
)
3132

33+
const gcpAuth = "gcp-auth"
34+
3235
var (
3336
runtimeScheme = runtime.NewScheme()
3437
codecs = serializer.NewCodecFactory(runtimeScheme)
@@ -49,44 +52,20 @@ type patchOperation struct {
4952
Value interface{} `json:"value,omitempty"`
5053
}
5154

52-
// Mount in the volumes and add the appropriate env vars to new pods
55+
// mutateHandler mounts in the volumes and adds the appropriate env vars to new pods
5356
func mutateHandler(w http.ResponseWriter, r *http.Request) {
54-
log.Printf("%v\n", r)
55-
56-
var body []byte
57-
if r.Body != nil {
58-
if data, err := ioutil.ReadAll(r.Body); err == nil {
59-
body = data
60-
}
61-
}
62-
63-
if len(body) == 0 {
64-
log.Print("request body was empty, returning")
65-
http.Error(w, "empty body", http.StatusBadRequest)
66-
return
67-
}
68-
69-
var admissionResponse *admissionv1.AdmissionResponse
70-
71-
ar := admissionv1.AdmissionReview{}
72-
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
73-
log.Printf("Can't decode body: %v", err)
74-
admissionResponse = &admissionv1.AdmissionResponse{
75-
Result: &metav1.Status{
76-
Message: err.Error(),
77-
},
78-
}
79-
}
57+
ar := getAdmissionReview(w, r)
8058

8159
req := ar.Request
8260
var pod corev1.Pod
8361
if err := json.Unmarshal(req.Object.Raw, &pod); err != nil {
8462
log.Printf("Could not unmarshal raw object: %v", err)
85-
admissionResponse = &admissionv1.AdmissionResponse{
63+
writeError(w, &admissionv1.AdmissionResponse{
8664
Result: &metav1.Status{
8765
Message: err.Error(),
8866
},
89-
}
67+
})
68+
return
9069
}
9170

9271
var patch []patchOperation
@@ -204,93 +183,29 @@ func mutateHandler(w http.ResponseWriter, r *http.Request) {
204183
}
205184
}
206185

207-
patchBytes, err := json.Marshal(patch)
208-
if err != nil {
209-
admissionResponse = &admissionv1.AdmissionResponse{
210-
Result: &metav1.Status{
211-
Message: err.Error(),
212-
},
213-
}
214-
}
215-
216-
if admissionResponse == nil {
217-
admissionResponse = &admissionv1.AdmissionResponse{
218-
Allowed: true,
219-
Patch: patchBytes,
220-
PatchType: func() *admissionv1.PatchType {
221-
pt := admissionv1.PatchTypeJSONPatch
222-
return &pt
223-
}(),
224-
}
225-
}
226-
227-
admissionReview := admissionv1.AdmissionReview{}
228-
if admissionResponse != nil {
229-
admissionReview.Response = admissionResponse
230-
if ar.Request != nil {
231-
admissionReview.Response.UID = ar.Request.UID
232-
}
233-
}
234-
admissionReview.Kind = "AdmissionReview"
235-
admissionReview.APIVersion = "admission.k8s.io/v1"
236-
237-
resp, err := json.Marshal(admissionReview)
238-
if err != nil {
239-
log.Printf("Can't encode response: %v", err)
240-
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
241-
}
242-
log.Printf("Ready to write reponse ...")
243-
if _, err := w.Write(resp); err != nil {
244-
log.Printf("Can't write response: %v", err)
245-
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
246-
}
247-
186+
writePatch(w, ar, patch)
248187
}
249188

250-
// Add image pull secret to new service accounts
189+
// serviceaccountHandler adds image pull secret to new service accounts
251190
func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
252-
log.Printf("%v\n", r)
253-
254-
var body []byte
255-
if r.Body != nil {
256-
if data, err := ioutil.ReadAll(r.Body); err == nil {
257-
body = data
258-
}
259-
}
260-
261-
if len(body) == 0 {
262-
log.Print("request body was empty, returning")
263-
http.Error(w, "empty body", http.StatusBadRequest)
264-
return
265-
}
266-
267-
var admissionResponse *admissionv1.AdmissionResponse
268-
269-
ar := admissionv1.AdmissionReview{}
270-
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
271-
log.Printf("Can't decode body: %v", err)
272-
admissionResponse = &admissionv1.AdmissionResponse{
273-
Result: &metav1.Status{
274-
Message: err.Error(),
275-
},
276-
}
277-
}
191+
ar := getAdmissionReview(w, r)
278192

279193
req := ar.Request
280194
var sa corev1.ServiceAccount
281195
if err := json.Unmarshal(req.Object.Raw, &sa); err != nil {
282196
log.Printf("Could not unmarshal raw object: %v", err)
283-
admissionResponse = &admissionv1.AdmissionResponse{
197+
writeError(w, &admissionv1.AdmissionResponse{
284198
Result: &metav1.Status{
285199
Message: err.Error(),
286200
},
287-
}
201+
})
202+
return
288203
}
289204

290205
// Make sure the gcp-auth secret exists before adding it as a pull secret
291206
hasSecret := false
292207
for _, s := range sa.Secrets {
293-
if s.Name == "gcp-auth" {
208+
if s.Name == gcpAuth {
294209
hasSecret = true
295210
break
296211
}
@@ -299,7 +214,7 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
299214
var patch []patchOperation
300215

301216
if hasSecret {
302-
ips := corev1.LocalObjectReference{Name: "gcp-auth"}
217+
ips := corev1.LocalObjectReference{Name: gcpAuth}
303218
if len(sa.ImagePullSecrets) == 0 {
304219
patch = []patchOperation{{
305220
Op: "add",
@@ -315,42 +230,90 @@ func serviceaccountHandler(w http.ResponseWriter, r *http.Request) {
315230
}
316231
}
317232

233+
writePatch(w, ar, patch)
234+
}
235+
236+
// getAdmissionReview reads and validates an inbound request and returns an admissionReview
237+
func getAdmissionReview(w http.ResponseWriter, r *http.Request) *admissionv1.AdmissionReview {
238+
log.Printf("%v\n", r)
239+
240+
var body []byte
241+
if r.Body != nil {
242+
if data, err := ioutil.ReadAll(r.Body); err == nil {
243+
body = data
244+
}
245+
}
246+
247+
if len(body) == 0 {
248+
log.Print("request body was empty, returning")
249+
http.Error(w, "empty body", http.StatusBadRequest)
250+
return nil
251+
}
252+
253+
ar := admissionv1.AdmissionReview{}
254+
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
255+
log.Printf("Can't decode body: %v", err)
256+
writeError(w, &admissionv1.AdmissionResponse{
257+
Result: &metav1.Status{
258+
Message: err.Error(),
259+
},
260+
})
261+
return nil
262+
}
263+
return &ar
264+
}
265+
266+
// writeError writes an error response
267+
func writeError(w http.ResponseWriter, admissionResp *admissionv1.AdmissionResponse) {
268+
admissionReview := admissionv1.AdmissionReview{
269+
Response: admissionResp,
270+
}
271+
writeResp(w, admissionReview)
272+
}
273+
274+
// writePatch writes a patch response
275+
func writePatch(w http.ResponseWriter, ar *admissionv1.AdmissionReview, patch []patchOperation) {
318276
patchBytes, err := json.Marshal(patch)
319277
if err != nil {
320-
admissionResponse = &admissionv1.AdmissionResponse{
278+
writeError(w, &admissionv1.AdmissionResponse{
321279
Result: &metav1.Status{
322280
Message: err.Error(),
323281
},
324-
}
282+
})
283+
return
325284
}
326285

327-
if admissionResponse == nil {
328-
admissionResponse = &admissionv1.AdmissionResponse{
329-
Allowed: true,
330-
Patch: patchBytes,
331-
PatchType: func() *admissionv1.PatchType {
332-
pt := admissionv1.PatchTypeJSONPatch
333-
return &pt
334-
}(),
335-
}
286+
admissionResp := &admissionv1.AdmissionResponse{
287+
Allowed: true,
288+
Patch: patchBytes,
289+
PatchType: func() *admissionv1.PatchType {
290+
pt := admissionv1.PatchTypeJSONPatch
291+
return &pt
292+
}(),
336293
}
337294

338-
admissionReview := admissionv1.AdmissionReview{}
339-
if admissionResponse != nil {
340-
admissionReview.Response = admissionResponse
341-
if ar.Request != nil {
342-
admissionReview.Response.UID = ar.Request.UID
343-
}
295+
admissionReview := admissionv1.AdmissionReview{
296+
Response: admissionResp,
297+
}
298+
if ar.Request != nil {
299+
admissionReview.Response.UID = ar.Request.UID
344300
}
301+
302+
writeResp(w, admissionReview)
303+
}
304+
305+
// writeResp writes an admissionReview response
306+
func writeResp(w http.ResponseWriter, admissionReview admissionv1.AdmissionReview) {
345307
admissionReview.Kind = "AdmissionReview"
346308
admissionReview.APIVersion = "admission.k8s.io/v1"
347309

310+
log.Printf("Ready to marshal response ...")
348311
resp, err := json.Marshal(admissionReview)
349312
if err != nil {
350313
log.Printf("Can't encode response: %v", err)
351314
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
352315
}
353-
log.Printf("Ready to write reponse ...")
316+
log.Printf("Ready to write response ...")
354317
if _, err := w.Write(resp); err != nil {
355318
log.Printf("Can't write response: %v", err)
356319
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)

0 commit comments

Comments
 (0)