This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
validate.go
153 lines (136 loc) · 5.03 KB
/
validate.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
// Copyright 2019 Istio 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 validate
import (
"fmt"
"net/url"
"reflect"
"istio.io/api/operator/v1alpha1"
"istio.io/operator/pkg/util"
)
var (
// defaultValidations maps a data path to a validation function.
defaultValidations = map[string]ValidatorFunc{
"Hub": validateHub,
"Tag": validateTag,
"InstallPackagePath": validateInstallPackagePath,
}
// requiredValues lists all the values that must be non-empty.
requiredValues = map[string]bool{}
)
// CheckIstioOperatorSpec validates the values in the given Installer spec, using the field map defaultValidations to
// call the appropriate validation function.
func CheckIstioOperatorSpec(is *v1alpha1.IstioOperatorSpec, checkRequired bool) (errs util.Errors) {
errs = CheckValues(is.Values)
return util.AppendErrs(errs, validate(defaultValidations, is, nil, checkRequired))
}
func validate(validations map[string]ValidatorFunc, structPtr interface{}, path util.Path, checkRequired bool) (errs util.Errors) {
scope.Debugf("validate with path %s, %v (%T)", path, structPtr, structPtr)
if structPtr == nil {
return nil
}
if util.IsStruct(structPtr) {
scope.Debugf("validate path %s, skipping struct type %T", path, structPtr)
return nil
}
if !util.IsPtr(structPtr) {
return util.NewErrs(fmt.Errorf("validate path %s, value: %v, expected ptr, got %T", path, structPtr, structPtr))
}
structElems := reflect.ValueOf(structPtr).Elem()
if !util.IsStruct(structElems) {
return util.NewErrs(fmt.Errorf("validate path %s, value: %v, expected struct, got %T", path, structElems, structElems))
}
if util.IsNilOrInvalidValue(structElems) {
return
}
for i := 0; i < structElems.NumField(); i++ {
fieldName := structElems.Type().Field(i).Name
fieldValue := structElems.Field(i)
kind := structElems.Type().Field(i).Type.Kind()
if a, ok := structElems.Type().Field(i).Tag.Lookup("json"); ok && a == "-" {
continue
}
scope.Debugf("Checking field %s", fieldName)
switch kind {
case reflect.Struct:
errs = util.AppendErrs(errs, validate(validations, fieldValue.Addr().Interface(), append(path, fieldName), checkRequired))
case reflect.Map:
newPath := append(path, fieldName)
for _, key := range fieldValue.MapKeys() {
nnp := append(newPath, key.String())
errs = util.AppendErrs(errs, validateLeaf(validations, nnp, fieldValue.MapIndex(key), checkRequired))
}
case reflect.Slice:
for i := 0; i < fieldValue.Len(); i++ {
errs = util.AppendErrs(errs, validate(validations, fieldValue.Index(i).Interface(), path, checkRequired))
}
case reflect.Ptr:
if util.IsNilOrInvalidValue(fieldValue.Elem()) {
continue
}
newPath := append(path, fieldName)
if fieldValue.Elem().Kind() == reflect.Struct {
errs = util.AppendErrs(errs, validate(validations, fieldValue.Interface(), newPath, checkRequired))
} else {
errs = util.AppendErrs(errs, validateLeaf(validations, newPath, fieldValue, checkRequired))
}
default:
if structElems.Field(i).CanInterface() {
errs = util.AppendErrs(errs, validateLeaf(validations, append(path, fieldName), fieldValue.Interface(), checkRequired))
}
}
}
return errs
}
func validateLeaf(validations map[string]ValidatorFunc, path util.Path, val interface{}, checkRequired bool) util.Errors {
pstr := path.String()
msg := fmt.Sprintf("validate %s:%v(%T) ", pstr, val, val)
if util.IsValueNil(val) || util.IsEmptyString(val) {
if checkRequired && requiredValues[pstr] {
return util.NewErrs(fmt.Errorf("field %s is required but not set", util.ToYAMLPathString(pstr)))
}
msg += fmt.Sprintf("validate %s: OK (empty value)", pstr)
scope.Debug(msg)
return nil
}
vf, ok := validations[pstr]
if !ok {
msg += fmt.Sprintf("validate %s: OK (no validation)", pstr)
scope.Debug(msg)
// No validation defined.
return nil
}
scope.Debug(msg)
return vf(path, val)
}
func validateHub(path util.Path, val interface{}) util.Errors {
return validateWithRegex(path, val, ReferenceRegexp)
}
func validateTag(path util.Path, val interface{}) util.Errors {
return validateWithRegex(path, val, TagRegexp)
}
func validateInstallPackagePath(path util.Path, val interface{}) util.Errors {
valStr, ok := val.(string)
if !ok {
return util.NewErrs(fmt.Errorf("validateInstallPackagePath(%s) bad type %T, want string", path, val))
}
if valStr == "" {
// compiled-in charts
return nil
}
if _, err := url.ParseRequestURI(val.(string)); err != nil {
return util.NewErrs(fmt.Errorf("invalid value %s: %s", path, valStr))
}
return nil
}