/
validation.go
185 lines (164 loc) · 4.94 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
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
package v1alpha1
import (
"fmt"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func validateSemanticVersion(version string, path *field.Path) (allErrs field.ErrorList) {
v, err := semver.NewVersion(version)
if err != nil {
allErrs = append(
allErrs,
field.Invalid(path, version, err.Error()),
)
return
}
if v.Major != etcdSupportedVersionMajor {
allErrs = append(
allErrs,
field.Invalid(path, version, "only Etcd version 3 is supported"),
)
}
return
}
var _ webhook.Validator = &EtcdCluster{}
// ValidateCreate validates that all required fields are present and valid.
func (o *EtcdCluster) ValidateCreate() error {
path := field.NewPath("spec")
var allErrs field.ErrorList
allErrs = append(
allErrs,
validateSemanticVersion(o.Spec.Version, path.Child("version"))...,
)
allErrs = append(
allErrs,
o.Spec.Storage.validate(path.Child("storage"))...)
allErrs = append(
allErrs,
o.Spec.PodTemplate.validate(path.Child("podTemplate"))...)
return allErrs.ToAggregate()
}
// ValidateCreate validates that deletion is allowed
// TODO: Not yet implemented
func (o *EtcdCluster) ValidateDelete() error {
var allErrs field.ErrorList
return allErrs.ToAggregate()
}
// ValidateUpdate validates that only supported fields are changed
func (o *EtcdCluster) ValidateUpdate(old runtime.Object) error {
oldO, ok := old.(*EtcdCluster)
if !ok {
return fmt.Errorf("Unexpected type for old: %#v", old)
}
oldO = oldO.DeepCopy()
// Overwrite the fields which are allowed to change
oldO.Spec.Replicas = o.Spec.Replicas
oldO.Spec.Version = o.Spec.Version
if diff := cmp.Diff(oldO.Spec, o.Spec); diff != "" {
return fmt.Errorf("Unsupported changes: (- current, + new) %s", diff)
}
err := o.ValidateCreate()
if err != nil {
err = fmt.Errorf("Unsupported changes: %s", err)
}
return err
}
var _ webhook.Validator = &EtcdPeer{}
// ValidateCreate validates that all required fields are present and valid.
func (o *EtcdPeer) ValidateCreate() error {
path := field.NewPath("spec")
var allErrs field.ErrorList
allErrs = append(
allErrs,
validateSemanticVersion(o.Spec.Version, path.Child("version"))...,
)
allErrs = append(
allErrs,
o.Spec.Storage.validate(path.Child("storage"))...,
)
allErrs = append(
allErrs,
o.Spec.PodTemplate.validate(path.Child("podTemplate"))...)
return allErrs.ToAggregate()
}
// ValidateCreate validates that deletion is allowed
// TODO: Not yet implemented
func (o *EtcdPeer) ValidateDelete() error {
var allErrs field.ErrorList
return allErrs.ToAggregate()
}
// ValidateUpdate validates that only supported fields are changed
func (o *EtcdPeer) ValidateUpdate(old runtime.Object) error {
oldO, ok := old.(*EtcdPeer)
if !ok {
return fmt.Errorf("Unexpected type for old: %#v", old)
}
oldO = oldO.DeepCopy()
// Overwrite any the fields which are allowed to change
// oldO.Spec.Foo = o.Spec.Foo
if diff := cmp.Diff(oldO.Spec, o.Spec); diff != "" {
return fmt.Errorf("Unsupported changes: (- current, + new) %s", diff)
}
return nil
}
func validatePersistentVolumeClaimSpec(path *field.Path, o *corev1.PersistentVolumeClaimSpec) field.ErrorList {
var allErrs field.ErrorList
if o == nil {
allErrs = append(allErrs, field.Required(path, ""))
return allErrs
}
if o.StorageClassName == nil {
allErrs = append(allErrs, field.Required(path.Child("storageClassName"), ""))
return allErrs
}
if _, ok := o.Resources.Requests["storage"]; !ok {
allErrs = append(allErrs, field.Required(path.Child("resources", "requests", "storage"), ""))
return allErrs
}
return allErrs
}
func (o *EtcdPeerStorage) validate(path *field.Path) field.ErrorList {
var allErrs field.ErrorList
if o == nil {
allErrs = append(allErrs, field.Required(path, ""))
return allErrs
}
allErrs = append(
allErrs,
validatePersistentVolumeClaimSpec(path.Child("volumeClaimTemplate"), o.VolumeClaimTemplate)...,
)
return allErrs
}
// IsInvalidUserProvidedAnnotation tests to see if the given annotation name is one reserved by the operator
func IsInvalidUserProvidedAnnotationName(annotationName string) bool {
return strings.HasPrefix(annotationName, "etcd.improbable.io/")
}
func (o *EtcdPodTemplateObjectMeta) validate(path *field.Path) field.ErrorList {
var allErrs field.ErrorList
if o != nil {
for name := range o.Annotations {
if IsInvalidUserProvidedAnnotationName(name) {
allErrs = append(
allErrs,
field.Invalid(path, name, "Annotation name is a reserved name ('etcd.improbable.io' prefix)"))
}
}
} else {
// We can be nil, that's fine.
}
return allErrs
}
func (o *EtcdPodTemplateSpec) validate(path *field.Path) field.ErrorList {
var allErrs field.ErrorList
if o != nil {
allErrs = append(allErrs, o.Metadata.validate(path.Child("metadata"))...)
} else {
// Not mandatory, being nil is fine.
}
return allErrs
}