This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
113 lines (93 loc) · 3.17 KB
/
handler.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"log"
"net/http"
)
var (
universalDeserializer = serializer.NewCodecFactory(runtime.NewScheme()).UniversalDeserializer()
jsonContentType = "application/json"
)
type PatchHandler struct{}
func NewPatchHandler() *PatchHandler {
return &PatchHandler{}
}
func (p *PatchHandler) generatePatchOperations() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Print("Handling webhook request ...")
var writeErr error
if bytes, err := doPatchOperationGeneratorFunc(w, r); err != nil {
log.Printf("Error handling webhook request: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, writeErr = w.Write([]byte(err.Error()))
} else {
log.Print("Webhook request handled successfully")
_, writeErr = w.Write(bytes)
}
if writeErr != nil {
log.Printf("Could not write response: %v", writeErr)
}
})
}
func doPatchOperationGeneratorFunc(w http.ResponseWriter, r *http.Request) ([]byte, error) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return nil, fmt.Errorf("invalid method %s, only POST requests are allowed", r.Method)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return nil, fmt.Errorf("could not read request body: %v", err)
}
if contentType := r.Header.Get("Content-Type"); contentType != jsonContentType {
w.WriteHeader(http.StatusBadRequest)
return nil, fmt.Errorf("unsupported content type %s, only %s is supported", contentType, jsonContentType)
}
admissionReviewReq := &v1beta1.AdmissionReview{}
if _, _, err := universalDeserializer.Decode(body, nil, admissionReviewReq); err != nil {
w.WriteHeader(http.StatusBadRequest)
return nil, fmt.Errorf("could not deserialize request: %v", err)
} else if admissionReviewReq.Request == nil {
w.WriteHeader(http.StatusBadRequest)
return nil, errors.New("malformed admission review: request is nil")
}
admissionReviewResp := &v1beta1.AdmissionReview{
Response: &v1beta1.AdmissionResponse{
Allowed: true,
UID: admissionReviewReq.Request.UID,
},
}
var patchOperations []patchOperation
if !isKubeNamespace(admissionReviewReq.Request.Namespace) {
patchOperations, err = generatePodPatches(admissionReviewReq.Request)
}
if err != nil {
admissionReviewResp.Response.Allowed = false
admissionReviewResp.Response.Result = &metav1.Status{
Message: err.Error(),
}
} else {
patchOperationsBytes, err := json.Marshal(patchOperations)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return nil, fmt.Errorf("could not marshal JSON patch: %v", err)
}
admissionReviewResp.Response.Allowed = true
admissionReviewResp.Response.Patch = patchOperationsBytes
}
bytes, err := json.Marshal(&admissionReviewResp)
if err != nil {
return nil, fmt.Errorf("marshaling response: %v", err)
}
return bytes, nil
}
func isKubeNamespace(ns string) bool {
return ns == metav1.NamespacePublic || ns == metav1.NamespaceSystem
}