/
prefix.go
227 lines (184 loc) · 8.12 KB
/
prefix.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
// Copyright 2022 OnMetal 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
import (
"fmt"
commonv1alpha1 "github.com/onmetal/onmetal-api/api/common/v1alpha1"
onmetalapivalidation "github.com/onmetal/onmetal-api/internal/api/validation"
commonvalidation "github.com/onmetal/onmetal-api/internal/apis/common/validation"
"github.com/onmetal/onmetal-api/internal/apis/ipam"
"go4.org/netipx"
corev1 "k8s.io/api/core/v1"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func ValidatePrefix(prefix *ipam.Prefix) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessor(prefix, true, apivalidation.NameIsDNSLabel, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidatePrefixSpec(&prefix.Spec, field.NewPath("spec"))...)
return allErrs
}
func validateIPFamilyAndOptionalPrefixAndLength(ipFamily corev1.IPFamily, prefix *commonv1alpha1.IPPrefix, prefixLength int32, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, onmetalapivalidation.ValidateIPFamily(ipFamily, fldPath.Child("ipFamily"))...)
if prefix != nil {
allErrs = append(allErrs, commonvalidation.ValidateIPPrefix(ipFamily, *prefix, fldPath.Child("prefix"))...)
}
if prefixLength != 0 {
allErrs = append(allErrs, commonvalidation.ValidatePrefixLength(ipFamily, prefixLength, fldPath.Child("prefixLength"))...)
}
return allErrs
}
func validateOptionalRef(ref *corev1.LocalObjectReference, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
if ref == nil {
return allErrs
}
if ref.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify name"))
}
for _, msg := range apivalidation.NameIsDNSLabel(ref.Name, false) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), ref.Name, msg))
}
return allErrs
}
func ValidatePrefixSpec(spec *ipam.PrefixSpec, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
if spec.Prefix != nil {
allErrs = append(allErrs, commonvalidation.ValidateIPPrefix(spec.IPFamily, *spec.Prefix, fldPath.Child("prefix"))...)
}
allErrs = append(allErrs, validateIPFamilyAndOptionalPrefixAndLength(spec.IPFamily, spec.Prefix, spec.PrefixLength, fldPath)...)
if spec.IsRoot() {
if spec.PrefixLength != 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("prefixLength"), spec.PrefixLength, "cannot specify prefixLength for a root prefix"))
}
if spec.Prefix == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("prefix"), "must specify for root prefix"))
}
} else {
var numDefs int
if spec.Prefix != nil {
numDefs++
}
if spec.PrefixLength > 0 {
numDefs++
if spec.Prefix.IsValid() && spec.PrefixLength != int32(spec.Prefix.Bits()) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("prefixLength"), spec.PrefixLength, fmt.Sprintf("does not match prefix %s", spec.Prefix)))
}
}
if numDefs == 0 {
allErrs = append(allErrs, field.Invalid(fldPath, spec, "must specify either prefix or prefixLength"))
}
allErrs = append(allErrs, validateOptionalRef(spec.ParentRef, fldPath.Child("parentRef"))...)
allErrs = append(allErrs, metav1validation.ValidateLabelSelector(spec.ParentSelector, metav1validation.LabelSelectorValidationOptions{}, fldPath.Child("parentSelector"))...)
}
return allErrs
}
func ValidatePrefixUpdate(newPrefix, oldPrefix *ipam.Prefix) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessorUpdate(newPrefix, oldPrefix, field.NewPath("metadata"))...)
allErrs = append(allErrs, validatePrefixSpecUpdate(&newPrefix.Spec, &oldPrefix.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidatePrefix(newPrefix)...)
return allErrs
}
func validatePrefixSpecUpdate(newSpec, oldSpec *ipam.PrefixSpec, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
newSpecCopy := newSpec.DeepCopy()
oldSpecCopy := oldSpec.DeepCopy()
// Allow setting Prefix, PrefixLength and ParentRef exactly once.
if oldSpec.Prefix == nil {
oldSpecCopy.Prefix = newSpecCopy.Prefix
}
// We allow setting PrefixLength for symmetry with setting Prefix.
// ValidatePrefixSpec will guard against any mismatch with prefix.
if oldSpec.PrefixLength == 0 {
oldSpecCopy.PrefixLength = newSpecCopy.PrefixLength
}
// ParentRef can only be set if the prefix is not a root prefix.
if oldSpec.ParentRef == nil && !oldSpec.IsRoot() {
oldSpecCopy.ParentRef = newSpecCopy.ParentRef
}
allErrs = append(allErrs, onmetalapivalidation.ValidateImmutableFieldWithDiff(newSpecCopy, oldSpecCopy, fldPath)...)
return allErrs
}
func ValidatePrefixStatus(status *ipam.PrefixStatus, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
phase := status.Phase
switch phase {
case ipam.PrefixPhaseAllocated:
var bldr netipx.IPSetBuilder
var (
seenFamilies = sets.NewString()
overlapSeen bool
)
for i, used := range status.Used {
usedField := fldPath.Child("used").Index(i)
allErrs = append(allErrs, commonvalidation.ValidateIPPrefix(used.IP().Family(), used, usedField)...)
seenFamilies.Insert(string(used.IP().Family()))
if !overlapSeen {
ipSet, _ := bldr.IPSet()
var usedBldr netipx.IPSetBuilder
usedBldr.AddPrefix(used.Prefix)
usedSet, _ := usedBldr.IPSet()
overlapSeen = ipSet.Overlaps(usedSet)
bldr.AddSet(usedSet)
}
}
if seenFamilies.Len() > 1 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("used"), status.Used, fmt.Sprintf("different bit lengths among prefixes: %v", seenFamilies.List())))
}
if overlapSeen {
allErrs = append(allErrs, field.Invalid(fldPath.Child("used"), status.Used, "overlapping used addresses"))
}
default:
if len(status.Used) > 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("used"), status.Used, "must not specify used if the prefix is not succeeded"))
}
}
return allErrs
}
func ValidatePrefixStatusUpdate(newPrefix, oldPrefix *ipam.Prefix) field.ErrorList {
var allErrs field.ErrorList
statusField := field.NewPath("status")
allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessorUpdate(newPrefix, oldPrefix, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidatePrefixStatus(&newPrefix.Status, statusField)...)
newPhase := newPrefix.Status.Phase
oldPhase := oldPrefix.Status.Phase
if oldPhase == ipam.PrefixPhaseAllocated && oldPhase != newPhase {
allErrs = append(allErrs, field.Forbidden(field.NewPath("status", "phase"), "may not update from allocated to non-allocated phase"))
}
if newPhase == ipam.PrefixPhaseAllocated {
if !newPrefix.Spec.Prefix.IsValid() {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "prefix"), "must specify prefix if allocated"))
}
if newPrefix.Spec.ParentSelector != nil && newPrefix.Spec.ParentRef == nil {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "parentRef"), "must specify parentRef when allocating sub-prefix"))
}
}
// We only have to validate that used / reserved prefixes are contained in the allocated prefix.
// ValidatePrefixStatus already validates that they're non-overlapping.
if prefix := newPrefix.Spec.Prefix; prefix.IsValid() {
var bldr netipx.IPSetBuilder
bldr.AddPrefix(prefix.Prefix)
set, _ := bldr.IPSet()
for i, used := range newPrefix.Status.Used {
if !set.ContainsPrefix(used.Prefix) {
allErrs = append(allErrs, field.Forbidden(statusField.Child("used").Index(i), fmt.Sprintf("not contained in prefix %s", prefix)))
}
}
}
return allErrs
}