/
registry.go
241 lines (206 loc) · 8.57 KB
/
registry.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
/*
Copyright 2020 The cert-manager Authors.
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
http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package validation allows a caller to automatically register, lookup and
// call API validation functions.
// It is similar to runtime.Scheme and is designed to make writing and
// consuming API validation functions easier.
package validation
import (
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// Registry is used to store and lookup references to validation functions for
// given Kubernetes API types.
type Registry struct {
scheme *runtime.Scheme
validateRegister map[schema.GroupVersionKind]ValidateFunc
validateUpdateRegister map[schema.GroupVersionKind]ValidateUpdateFunc
}
// ValidateFunc is a function type that implements validation for an admission
// request associated with a creation of an object of a particular type.
type ValidateFunc func(req *admissionv1.AdmissionRequest, obj runtime.Object) (field.ErrorList, WarningList)
// ValidateUpdateFunc is a function type that implements validation for an
// admission request associated with update of an object of a particular type.
type ValidateUpdateFunc func(req *admissionv1.AdmissionRequest, oldObj, obj runtime.Object) (field.ErrorList, WarningList)
// NewRegistry creates a new empty registry, backed by the provided Scheme.
func NewRegistry(scheme *runtime.Scheme) *Registry {
return &Registry{
scheme: scheme,
validateRegister: make(map[schema.GroupVersionKind]ValidateFunc),
validateUpdateRegister: make(map[schema.GroupVersionKind]ValidateUpdateFunc),
}
}
// AddValidateFunc will add a new validation function to the register.
// The function will be run whenever Validate is called with a requestVersion
// set to any recognised GroupVersionKinds for this object.
// If obj is part of an internal API version, the validation function will be
// called on all calls to Validate regardless of version.
// If obj cannot be recognised using the registry's scheme, an error will be
// returned.
func (r *Registry) AddValidateFunc(obj runtime.Object, fn ValidateFunc) error {
gvks, _, err := r.scheme.ObjectKinds(obj)
if err != nil {
return err
}
for _, gvk := range gvks {
r.appendValidate(gvk, fn)
}
return nil
}
// AddValidateUpdateFunc will add a new validation function to the register.
// The function will be run whenever ValidateUpdate is called with a
// requestVersion set to any recognised GroupVersionKinds for this object.
// If obj is part of an internal API version, the validation function will be
// called on all calls to Validate regardless of version.
// If obj cannot be recognised using the registry's scheme, an error will be
// returned.
func (r *Registry) AddValidateUpdateFunc(obj runtime.Object, fn ValidateUpdateFunc) error {
gvks, _, err := r.scheme.ObjectKinds(obj)
if err != nil {
return err
}
for _, gvk := range gvks {
r.appendValidateUpdate(gvk, fn)
}
return nil
}
// Validate will run all validation functions registered for the given object.
// If the passed obj is *not* of the same version as the provided
// requestVersion, the registry will attempt to convert the object before
// calling the validation functions.
// Any validation functions registered for the objects internal API version
// will be run against the object regardless of version.
func (r *Registry) Validate(req *admissionv1.AdmissionRequest, obj runtime.Object, requestVersion schema.GroupVersionKind) (field.ErrorList, WarningList) {
versioned, internal := r.lookupValidateFuncs(requestVersion)
if versioned == nil && internal == nil {
return nil, nil
}
targetObj, internalObj, err := r.convert(obj, requestVersion)
if err != nil {
return internalError(err), nil
}
el := field.ErrorList{}
warnings := WarningList{}
if versioned != nil {
e, w := versioned(req, targetObj)
el, warnings = append(el, e...), append(warnings, w...)
}
if internal != nil {
e, w := internal(req, internalObj)
el, warnings = append(el, e...), append(warnings, w...)
}
return el, warnings
}
// ValidateUpdate will run all update validation functions registered for the
// given object.
// If the passed objects are *not* of the same version as the provided
// requestVersion, the registry will attempt to convert the objects before
// calling the validation functions.
// Any validation functions registered for the objects internal API version
// will be run against the object regardless of version.
func (r *Registry) ValidateUpdate(req *admissionv1.AdmissionRequest, oldObj, obj runtime.Object, requestVersion schema.GroupVersionKind) (field.ErrorList, []string) {
versioned, internal := r.lookupValidateUpdateFuncs(requestVersion)
if versioned == nil && internal == nil {
return nil, nil
}
targetOldObj, internalOldObj, err := r.convert(oldObj, requestVersion)
if err != nil {
return internalError(err), nil
}
targetObj, internalObj, err := r.convert(obj, requestVersion)
if err != nil {
return internalError(err), nil
}
el := field.ErrorList{}
warnings := WarningList{}
if versioned != nil {
e, w := versioned(req, targetOldObj, targetObj)
el, warnings = append(el, e...), append(warnings, w...)
}
if internal != nil {
e, w := internal(req, internalOldObj, internalObj)
el, warnings = append(el, e...), append(warnings, w...)
}
return el, warnings
}
func (r *Registry) lookupValidateFuncs(gvk schema.GroupVersionKind) (versioned ValidateFunc, internal ValidateFunc) {
versioned = r.validateRegister[gvk]
gvk.Version = runtime.APIVersionInternal
internal = r.validateRegister[gvk]
return versioned, internal
}
func (r *Registry) lookupValidateUpdateFuncs(gvk schema.GroupVersionKind) (versioned ValidateUpdateFunc, internal ValidateUpdateFunc) {
versioned = r.validateUpdateRegister[gvk]
gvk.Version = runtime.APIVersionInternal
internal = r.validateUpdateRegister[gvk]
return versioned, internal
}
func (r *Registry) appendValidate(gvk schema.GroupVersionKind, fn ValidateFunc) {
existing, ok := r.validateRegister[gvk]
if !ok {
r.validateRegister[gvk] = fn
return
}
// If a ValidateFunc for GVK already exists, build a new ValidateFunc that
// will return both the results of the new and old ValidateFunc.
r.validateRegister[gvk] = func(req *admissionv1.AdmissionRequest, obj runtime.Object) (field.ErrorList, WarningList) {
e, w := existing(req, obj)
newE, newW := fn(req, obj)
return append(e, newE...), append(w, newW...)
}
}
func (r *Registry) appendValidateUpdate(gvk schema.GroupVersionKind, fn ValidateUpdateFunc) {
existing, ok := r.validateUpdateRegister[gvk]
if !ok {
r.validateUpdateRegister[gvk] = fn
return
}
// If a ValidateUpdateFunc for GVK already exists, build a new
// ValidateUpdateFunc that will return both the results of the new and old
// ValidateUpdateFunc.
r.validateUpdateRegister[gvk] = func(req *admissionv1.AdmissionRequest, oldObj, obj runtime.Object) (field.ErrorList, WarningList) {
e, w := existing(req, oldObj, obj)
newE, newW := fn(req, oldObj, obj)
return append(e, newE...), append(w, newW...)
}
}
// convert will convert the given obj into the requestVersion as well as
// returning the internal representation of the object.
func (r *Registry) convert(obj runtime.Object, requestVersion schema.GroupVersionKind) (targetObj, internalObj runtime.Object, err error) {
// create a new object in the desired version
targetObj, err = r.scheme.New(requestVersion)
if err != nil {
return nil, nil, err
}
// create a new object in the 'internal' version
internalVersion := requestVersion
internalVersion.Version = runtime.APIVersionInternal
internalObj, err = r.scheme.New(internalVersion)
if err != nil {
return nil, nil, err
}
// convert the obj into the internalVersion first
if err := r.scheme.Convert(obj, internalObj, nil); err != nil {
return nil, nil, err
}
// convert the internalObj into the requestVersion
if err := r.scheme.Convert(internalObj, targetObj, nil); err != nil {
return nil, nil, err
}
return targetObj, internalObj, nil
}
func internalError(err error) field.ErrorList {
return field.ErrorList{field.InternalError(nil, err)}
}