-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
94 lines (75 loc) · 1.89 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
admissionV1 "k8s.io/api/admission/v1"
coreV1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func HandleHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
resp := make(map[string]string)
resp["message"] = "Status OK"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func HandlePodValidate(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
err = ioutil.WriteFile("/tmp/request", body, 0644)
if err != nil {
panic(err.Error())
}
ad := admission{
w: w,
}
ad.populateRequest(body)
pod := &coreV1.Pod{}
if err := json.Unmarshal(ad.review.Request.OldObject.Raw, pod); err != nil {
fmt.Errorf("could not unmarshal pod on admission request: %v", err)
}
admissionReviewResponse := admissionV1.AdmissionReview{
Response: &admissionV1.AdmissionResponse{
UID: ad.review.Request.UID,
Allowed: true,
// Warnings: warnings,
},
}
bytes, err := json.Marshal(&admissionReviewResponse)
if err != nil {
fmt.Errorf("marshaling response: %v", err)
}
if _, err := ad.w.Write(bytes); err != nil {
fmt.Errorf("Error (http):", err)
}
}
func HandlePodMutate(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
err = ioutil.WriteFile("/tmp/request", body, 0644)
if err != nil {
panic(err.Error())
}
ad := admission{
w: w,
}
ad.populateRequest(body)
// Default to permit admission webhook
var allowed bool = true
var status v1.Status
var patchBytes []byte
// warnings := []string{}
patchBytes, err = HandlePod(&ad)
if err != nil {
allowed = false
status = v1.Status{
Message: err.Error(),
}
}
ad.sendAdmissionResponse(allowed, patchBytes, &status)
}