forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
152 lines (119 loc) · 4.16 KB
/
validation.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
package validation
import (
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/validation"
)
type RuntimeObjectValidator interface {
Validate(obj runtime.Object) field.ErrorList
ValidateUpdate(obj, old runtime.Object) field.ErrorList
}
var Validator = &RuntimeObjectsValidator{map[reflect.Type]RuntimeObjectValidatorInfo{}}
type RuntimeObjectsValidator struct {
typeToValidator map[reflect.Type]RuntimeObjectValidatorInfo
}
type RuntimeObjectValidatorInfo struct {
Validator RuntimeObjectValidator
IsNamespaced bool
HasObjectMeta bool
UpdateAllowed bool
}
func (v *RuntimeObjectsValidator) GetInfo(obj runtime.Object) (RuntimeObjectValidatorInfo, bool) {
ret, ok := v.typeToValidator[reflect.TypeOf(obj)]
return ret, ok
}
func (v *RuntimeObjectsValidator) MustRegister(obj runtime.Object, validateFunction interface{}, validateUpdateFunction interface{}) {
if err := v.Register(obj, validateFunction, validateUpdateFunction); err != nil {
panic(err)
}
}
func (v *RuntimeObjectsValidator) Register(obj runtime.Object, validateFunction interface{}, validateUpdateFunction interface{}) error {
objType := reflect.TypeOf(obj)
if oldValidator, exists := v.typeToValidator[objType]; exists {
panic(fmt.Sprintf("%v is already registered with %v", objType, oldValidator))
}
validator, err := NewValidationWrapper(validateFunction, validateUpdateFunction)
if err != nil {
return err
}
isNamespaced, err := GetRequiresNamespace(obj)
if err != nil {
return err
}
updateAllowed := validateUpdateFunction != nil
v.typeToValidator[objType] = RuntimeObjectValidatorInfo{validator, isNamespaced, HasObjectMeta(obj), updateAllowed}
return nil
}
func (v *RuntimeObjectsValidator) Validate(obj runtime.Object) field.ErrorList {
if obj == nil {
return field.ErrorList{}
}
allErrs := field.ErrorList{}
specificValidationInfo, err := v.getSpecificValidationInfo(obj)
if err != nil {
allErrs = append(allErrs, field.InternalError(nil, err))
return allErrs
}
allErrs = append(allErrs, specificValidationInfo.Validator.Validate(obj)...)
return allErrs
}
func (v *RuntimeObjectsValidator) ValidateUpdate(obj, old runtime.Object) field.ErrorList {
if obj == nil && old == nil {
return field.ErrorList{}
}
if newType, oldType := reflect.TypeOf(obj), reflect.TypeOf(old); newType != oldType {
return field.ErrorList{field.Invalid(field.NewPath("kind"), newType.Kind(), validation.NewInvalidTypeError(oldType.Kind(), newType.Kind(), "runtime.Object").Error())}
}
allErrs := field.ErrorList{}
specificValidationInfo, err := v.getSpecificValidationInfo(obj)
if err != nil {
if fieldErr, ok := err.(*field.Error); ok {
allErrs = append(allErrs, fieldErr)
} else {
allErrs = append(allErrs, field.InternalError(nil, err))
}
return allErrs
}
allErrs = append(allErrs, specificValidationInfo.Validator.ValidateUpdate(obj, old)...)
// no errors so far, make sure that the new object is actually valid against the original validator
if len(allErrs) == 0 {
allErrs = append(allErrs, specificValidationInfo.Validator.Validate(obj)...)
}
return allErrs
}
func (v *RuntimeObjectsValidator) getSpecificValidationInfo(obj runtime.Object) (RuntimeObjectValidatorInfo, error) {
objType := reflect.TypeOf(obj)
specificValidationInfo, exists := v.typeToValidator[objType]
if !exists {
return RuntimeObjectValidatorInfo{}, fmt.Errorf("no validator registered for %v", objType)
}
return specificValidationInfo, nil
}
func GetRequiresNamespace(obj runtime.Object) (bool, error) {
groupVersionKinds, _, err := kapi.Scheme.ObjectKinds(obj)
if err != nil {
return false, err
}
for _, gvk := range groupVersionKinds {
restMapping, err := kapi.Registry.RESTMapper().RESTMapping(gvk.GroupKind())
if err != nil {
return false, err
}
if restMapping.Scope.Name() == meta.RESTScopeNameNamespace {
return true, nil
}
}
return false, nil
}
func HasObjectMeta(obj runtime.Object) bool {
objValue := reflect.ValueOf(obj).Elem()
field := objValue.FieldByName("ObjectMeta")
if !field.IsValid() {
return false
}
return true
}