/
webhook.go
234 lines (209 loc) · 6.74 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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2021 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package admission
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
v1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/server/mux"
klog "k8s.io/klog/v2"
etcdv1alpha1 "tkestack.io/kstone-etcd-operator/pkg/apis/etcd/v1alpha1"
"tkestack.io/kstone-etcd-operator/pkg/validation"
)
// InstallHandler register handler for admission controller
func InstallHandler(mux *mux.PathRecorderMux) {
mux.Handle("/mutating-etcd", &admissionHandler{
delegate: &etcdMutatingHandler{},
})
mux.Handle("/validating-etcd", &admissionHandler{
delegate: &etcdValidateHandler{},
})
}
// Admit makes an admission decision based on the request attributes
type Admit interface {
Admit(ar *v1.AdmissionReview) *v1.AdmissionResponse
}
type admissionHandler struct {
delegate Admit
}
func (handler *admissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
klog.Errorf("contentType=%s, expect application/json", contentType)
return
}
if r.Body == nil {
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("Request could not be read: %v", err)
klog.Error(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
klog.V(3).Info(fmt.Sprintf("handling request: %s", body))
requestAdmissionReview := &v1.AdmissionReview{}
deserializer := codecs.UniversalDeserializer()
_, gvk, err := deserializer.Decode(body, nil, requestAdmissionReview)
if err != nil {
msg := fmt.Sprintf("Request could not be decoded: %v", err)
klog.Error(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
responseAdmissionReview := &v1.AdmissionReview{}
responseAdmissionReview.SetGroupVersionKind(*gvk)
responseAdmissionReview.Response = handler.delegate.Admit(requestAdmissionReview)
responseAdmissionReview.Response.UID = requestAdmissionReview.Request.UID
respBytes, err := json.Marshal(responseAdmissionReview)
if err != nil {
klog.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
klog.V(3).Info(fmt.Sprintf("sending response: %v", string(respBytes)))
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(respBytes); err != nil {
klog.Error(err)
}
}
type etcdMutatingHandler struct{}
// Admit mutate the etcd cluster's status and conditions
func (h *etcdMutatingHandler) Admit(ar *v1.AdmissionReview) *v1.AdmissionResponse {
switch ar.Request.Operation {
case v1.Create:
var e etcdv1alpha1.EtcdCluster
_, _, err := codecs.UniversalDeserializer().Decode(ar.Request.Object.Raw, nil, &e)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
patch, err := h.getCreatePatch(&e)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
return getPatchResponse(patch)
case v1.Update:
var newE, oldE etcdv1alpha1.EtcdCluster
_, _, err := codecs.UniversalDeserializer().Decode(ar.Request.Object.Raw, nil, &newE)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
_, _, err = codecs.UniversalDeserializer().Decode(ar.Request.OldObject.Raw, nil, &oldE)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
patch, err := h.getUpdatePatch(&oldE, &newE)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
return getPatchResponse(patch)
default:
}
return &v1.AdmissionResponse{Allowed: true}
}
// Patch define json patch type
type Patch struct {
Op string `json:"op,inline"`
Path string `json:"path,inline"`
Value interface{} `json:"value"`
}
func (h *etcdMutatingHandler) getCreatePatch(e *etcdv1alpha1.EtcdCluster) ([]byte, error) {
patches := make([]Patch, 0)
out := e.DeepCopy()
scheme.Default(out)
if !reflect.DeepEqual(out.Spec, e.Spec) {
patches = append(patches, Patch{
Op: "replace",
Path: "/spec",
Value: out.Spec,
})
}
patches = append(patches,
Patch{
Op: "add",
Path: "/status",
Value: struct{}{},
},
Patch{
Op: "add",
Path: "/status/phase",
Value: etcdv1alpha1.ClusterCreating,
})
return json.Marshal(patches)
}
func (h *etcdMutatingHandler) getUpdatePatch(oldE, newE *etcdv1alpha1.EtcdCluster) ([]byte, error) {
patches := make([]Patch, 0)
out := newE.DeepCopy()
scheme.Default(out)
if !reflect.DeepEqual(out.Spec, newE.Spec) {
patches = append(patches, Patch{
Op: "replace",
Path: "/spec",
Value: out.Spec,
})
}
if len(patches) == 0 {
return nil, nil
}
return json.Marshal(patches)
}
type etcdValidateHandler struct{}
// Admit validate the etcdculster's specification
func (v *etcdValidateHandler) Admit(ar *v1.AdmissionReview) *v1.AdmissionResponse {
switch ar.Request.Operation {
case v1.Create:
var e etcdv1alpha1.EtcdCluster
_, _, err := codecs.UniversalDeserializer().Decode(ar.Request.Object.Raw, nil, &e)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
if errs := validation.Validate(&e); len(errs) > 0 {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: errs.ToAggregate().Error()}}
}
case v1.Update:
var newE, oldE etcdv1alpha1.EtcdCluster
_, _, err := codecs.UniversalDeserializer().Decode(ar.Request.Object.Raw, nil, &newE)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
_, _, err = codecs.UniversalDeserializer().Decode(ar.Request.OldObject.Raw, nil, &oldE)
if err != nil {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
if errs := validation.ValidateUpdate(&oldE, &newE); len(errs) > 0 {
return &v1.AdmissionResponse{Result: &metav1.Status{Message: errs.ToAggregate().Error()}}
}
default:
}
return &v1.AdmissionResponse{Allowed: true}
}
func getPatchResponse(patch []byte) *v1.AdmissionResponse {
rsp := &v1.AdmissionResponse{Allowed: true}
if len(patch) > 0 {
rsp.Patch = patch
pt := v1.PatchTypeJSONPatch
rsp.PatchType = &pt
}
return rsp
}