-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
198 lines (175 loc) · 4.26 KB
/
type.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
package kubehook
import (
"encoding/json"
"gomodules.xyz/jsonpatch/v2"
admissv1 "k8s.io/api/admission/v1"
v12 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
)
const (
CREATE = "CREATE"
UPDATE = "UPDATE"
DELETE = "DELETE"
)
type UrlParams map[string]string
type index map[v1.GroupVersionKind]runtime.Object
func (i index) Registry(gvk v1.GroupVersionKind, obj runtime.Object) {
i[gvk] = obj
}
func (i index) Get(gvk v1.GroupVersionKind) (runtime.Object, error) {
if v, ok := i[gvk]; ok {
return v, nil
} else {
return nil, &TypeError{"sd"}
}
}
var Index = &index{
v1.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Pod",
}: &v12.Pod{},
}
type RST struct {
Code int32
Message string
Result bool
}
type Mutatingfun func(obj runtime.Object) runtime.Object
type ValidateFun struct {
ValidateUpdate func(obj, old_obj runtime.Object) error
ValidateDelete func(obj runtime.Object) error
ValidateCreate func(obj runtime.Object) error
}
type WebHook struct {
HandlerFun func(ctx *Ctx)
}
func (w *WebHook) do(ctx *Ctx) {
w.HandlerFun(ctx)
}
func (f *WebHook) GetObject() runtime.Object {
return nil
}
func (f *WebHook) GetGVK() string {
return ""
}
type ValidateWebhook struct {
metav1.GroupVersionKind
runtime.Object
ValidateUpdate func(obj, old_obj runtime.Object) error
ValidateDelete func(obj runtime.Object) error
ValidateCreate func(obj runtime.Object) error
}
func (f *ValidateWebhook) GetObject() runtime.Object {
return f.Object
}
func (f *ValidateWebhook) do(ctx *Ctx) {
adm_obj := admissv1.AdmissionReview{}
err := json.Unmarshal(ctx.Request.Data(), &adm_obj)
if err != nil {
klog.Error(err)
ctx.Response(400, []byte(err.Error()))
return
}
err = json.Unmarshal(adm_obj.Request.Object.Raw, f.Object)
if err != nil {
klog.Error(err)
ctx.Response(400, []byte(err.Error()))
return
}
ctx.Object = f.Object
var validateErr error
if adm_obj.Request.Operation == CREATE {
validateErr = f.ValidateCreate(ctx.Object)
} else if adm_obj.Request.Operation == UPDATE {
validateErr = f.ValidateUpdate(ctx.Object, ctx.Old_Object)
} else {
validateErr = f.ValidateDelete(ctx.Object)
}
if validateErr != nil {
ctx.Validate_result = RST{
Code: 400,
Message: validateErr.Error(),
Result: false,
}
} else {
ctx.Validate_result = RST{
Code: 200,
Message: "",
Result: true,
}
}
adm_return := admissv1.AdmissionReview{}
c := &admissv1.AdmissionResponse{
Allowed: ctx.Validate_result.Result,
Result: &metav1.Status{
Code: ctx.Validate_result.Code,
Message: ctx.Validate_result.Message,
},
}
adm_return.Response = c
adm_return_data, err := json.Marshal(adm_return)
if err != nil {
klog.Error(err)
// todo return false
ctx.Response(400, []byte(err.Error()))
} else {
ctx.Response(200, adm_return_data)
}
}
func (f *ValidateWebhook) GetGVK() string {
return f.GroupVersionKind.String()
}
type MutatingWebhook struct {
metav1.GroupVersionKind
runtime.Object
Mutatingfun func(obj runtime.Object) runtime.Object
}
func (f *MutatingWebhook) GetObject() runtime.Object {
return f.Object
}
func (f *MutatingWebhook) GetGVK() string {
return f.GroupVersionKind.String()
}
func (w *MutatingWebhook) do(ctx *Ctx) {
adm_obj := admissv1.AdmissionReview{}
err := json.Unmarshal(ctx.Request.Data(), &adm_obj)
if err != nil {
klog.Error(err)
ctx.Response(400, []byte(err.Error()))
return
}
err = json.Unmarshal(adm_obj.Request.Object.Raw, w.Object)
if err != nil {
ctx.Response(400, []byte(err.Error()))
return
}
ctx.Object = w.Object
obj := w.Mutatingfun(ctx.Object)
ctx.ChangeObject = obj
adm_return := admissv1.AdmissionReview{}
var PatchTypeJSONPatch admissv1.PatchType = "JsonPath"
data, err := json.Marshal(ctx.ChangeObject)
if err != nil {
klog.Error(err)
}
patch, e := jsonpatch.CreatePatch(adm_obj.Request.Object.Raw, data)
if e != nil {
klog.Error(e)
}
path_byte_data, _ := json.Marshal(patch)
adm_return.Response = &admissv1.AdmissionResponse{
Patch: path_byte_data,
PatchType: &PatchTypeJSONPatch,
UID: adm_obj.Request.UID,
Allowed: true,
}
data, err = json.Marshal(adm_return)
if err != nil {
klog.Error(err)
}
ctx.Response(200, data)
}