-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
251 lines (227 loc) · 7.22 KB
/
webhook.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/buger/jsonparser"
"k8s.io/api/admission/v1beta1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/kubernetes/pkg/apis/core/v1"
)
var (
runtimeScheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
// (https://github.com/kubernetes/kubernetes/issues/57982)
defaulter = runtime.ObjectDefaulter(runtimeScheme)
)
type WebhookServer struct {
server *http.Server
// client *kubernetes.ClientSet
}
var annotations StoredAnnotations = StoredAnnotations{}
// WhSvrParameters ...
// Webhook Server parameters
type WhSvrParameters struct {
port int // webhook server port
certFile string // path to the x509 certificate for https
keyFile string // path to the x509 private key matching `CertFile`
}
type patchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
type label struct {
key string
value string
}
func init() {
_ = corev1.AddToScheme(runtimeScheme)
_ = admissionregistrationv1beta1.AddToScheme(runtimeScheme)
// defaulting with webhooks:
// https://github.com/kubernetes/kubernetes/issues/57982
_ = v1.AddToScheme(runtimeScheme)
annotations.KindToEntry = make(map[string][]Entry, 0)
}
// main mutation process
func (whsvr *WebhookServer) mutate(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
req := ar.Request
fmt.Println(req.Kind.Kind)
allAnnotations, _, _, err := jsonparser.Get(req.Object.Raw, "metadata", "annotations")
fmt.Printf("Adding annotations: %s\n", string(allAnnotations))
var patchOperations []patchOperation
patchOperations = make([]patchOperation, 0)
var entry Entry
var kind string
name, err := jsonparser.GetUnsafeString(req.Object.Raw, "metadata", "name")
if err != nil {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
namespace, err := jsonparser.GetUnsafeString(req.Object.Raw, "metadata", "namespace")
if err != nil {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
kind, err = jsonparser.GetUnsafeString(req.Object.Raw, "kind")
if err != nil {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
jsonparser.ObjectEach(allAnnotations, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
entry = Entry{InstanceName: name, Namespace: namespace, Key: string(key), Value: string(value)}
var entryList []Entry
var kindExists bool
if entryList, kindExists = annotations.KindToEntry[kind]; !kindExists {
entryList = make([]Entry, 0)
}
if annotations.Exists(entry, kind) {
return nil
}
entryList = append(entryList, entry)
annotations.KindToEntry[kind] = entryList
return nil
})
fmt.Println("----- Stored data: -----")
fmt.Printf("Data: %v\n", annotations.KindToEntry)
fmt.Printf("Api Request!: %s\n", string(req.Object.Raw))
forResolve := ParseJson(req.Object.Raw)
fmt.Printf("Objects To Resolve: %v\n", forResolve)
for i := 0; i < len(forResolve); i++ {
var resolveObj ResolveData
resolveObj = forResolve[i]
if resolveObj.FunctionType == ImportValue {
annotationPath := resolveObj.AnnotationPath
fmt.Printf("Annotation Path: %s\n", annotationPath)
namespace1, kind1, instanceName1, key1, err := ParseAnnotationPath(annotationPath)
if err != nil {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
fmt.Printf("Trying to Resolve: %s %s %s %s\n", namespace1, kind1, instanceName1, key1)
value, err := searchAnnotation(annotations.KindToEntry[kind1], instanceName1, namespace1, key1)
if err != nil {
// Because we could not resolve one of the Fn::<path>
// we want to roll back our data structure by deleting the entry
// we just added. The store and resolve should be an atomic
// operation
deleted := annotations.Delete(entry, kind)
fmt.Printf("The data was deleted : %t", deleted)
fmt.Println(annotations)
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
patch := patchOperation{
Op: "replace",
Path: resolveObj.JSONTreePath,
Value: value,
}
patchOperations = append(patchOperations, patch)
} else if resolveObj.FunctionType == AddLabel {
patch := patchOperation{
Op: "replace",
Path: resolveObj.JSONTreePath,
Value: resolveObj.Value,
}
patchOperations = append(patchOperations, patch)
}
}
patchBytes, _ := json.Marshal(patchOperations)
// marshal the struct into bytes to pass into AdmissionResponse
return &v1beta1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
PatchType: func() *v1beta1.PatchType {
pt := v1beta1.PatchTypeJSONPatch
return &pt
}(),
}
}
func searchAnnotation(entries []Entry, instanceName, namespace, key string) (string, error) {
for i := 0; i < len(entries); i++ {
entry := entries[i]
if strings.EqualFold(entry.InstanceName, instanceName) &&
strings.EqualFold(entry.Key, key) &&
strings.EqualFold(entry.Namespace, namespace) {
return entry.Value, nil
}
}
// Could not find the data
fmt.Printf("instance name: %s key: %s \n", instanceName, key)
return "", fmt.Errorf("annotation data was not found. Check path")
}
// Serve method for webhook server
func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
if len(body) == 0 {
fmt.Println("empty body")
http.Error(w, "empty body", http.StatusBadRequest)
return
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
fmt.Printf("Content-Type=%s, expect application/json", contentType)
http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
return
}
var admissionResponse *v1beta1.AdmissionResponse
ar := v1beta1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
fmt.Printf("Can't decode body: %v", err)
admissionResponse = &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
} else {
fmt.Println(r.URL.Path)
if r.URL.Path == "/mutate" {
admissionResponse = whsvr.mutate(&ar)
}
}
admissionReview := v1beta1.AdmissionReview{}
if admissionResponse != nil {
admissionReview.Response = admissionResponse
if ar.Request != nil {
admissionReview.Response.UID = ar.Request.UID
}
}
resp, err := json.Marshal(admissionReview)
if err != nil {
fmt.Printf("Can't encode response: %v", err)
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
}
fmt.Println("Ready to write reponse ...")
if _, err := w.Write(resp); err != nil {
fmt.Printf("Can't write response: %v", err)
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
}
}