-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathnodeclaim_validation_cel_test.go
256 lines (246 loc) · 12 KB
/
nodeclaim_validation_cel_test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright The Kubernetes 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 v1_test
import (
"strconv"
"strings"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/awslabs/operatorpkg/object"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
. "sigs.k8s.io/karpenter/pkg/apis/v1"
"sigs.k8s.io/karpenter/pkg/test"
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
)
var _ = Describe("Validation", func() {
var nodeClaim *NodeClaim
BeforeEach(func() {
if env.Version.Minor() < 25 {
Skip("CEL Validation is for 1.25>")
}
nodeClaim = &NodeClaim{
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(randomdata.SillyName())},
Spec: NodeClaimSpec{
NodeClassRef: &NodeClassReference{
Group: "karpenter.test.sh",
Kind: "TestNodeClaim",
Name: "default",
},
Requirements: []NodeSelectorRequirementWithMinValues{
{
NodeSelectorRequirement: v1.NodeSelectorRequirement{
Key: CapacityTypeLabelKey,
Operator: v1.NodeSelectorOpExists,
},
},
},
},
}
})
Context("Taints", func() {
It("should succeed for valid taints", func() {
nodeClaim.Spec.Taints = []v1.Taint{
{Key: "a", Value: "b", Effect: v1.TaintEffectNoSchedule},
{Key: "c", Value: "d", Effect: v1.TaintEffectNoExecute},
{Key: "e", Value: "f", Effect: v1.TaintEffectPreferNoSchedule},
{Key: "key-only", Effect: v1.TaintEffectNoExecute},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail for invalid taint keys", func() {
nodeClaim.Spec.Taints = []v1.Taint{{Key: "???"}}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should fail for missing taint key", func() {
nodeClaim.Spec.Taints = []v1.Taint{{Effect: v1.TaintEffectNoSchedule}}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should fail for invalid taint value", func() {
nodeClaim.Spec.Taints = []v1.Taint{{Key: "invalid-value", Effect: v1.TaintEffectNoSchedule, Value: "???"}}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should fail for invalid taint effect", func() {
nodeClaim.Spec.Taints = []v1.Taint{{Key: "invalid-effect", Effect: "???"}}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should not fail for same key with different effects", func() {
nodeClaim.Spec.Taints = []v1.Taint{
{Key: "a", Effect: v1.TaintEffectNoSchedule},
{Key: "a", Effect: v1.TaintEffectNoExecute},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
})
Context("NodeClassRef", func() {
It("should succeed for valid group", func() {
nodeClaim.Spec.NodeClassRef = &NodeClassReference{
Kind: object.GVK(&v1alpha1.TestNodeClass{}).Kind,
Name: "nodeclass-test",
Group: object.GVK(&v1alpha1.TestNodeClass{}).Group,
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail for invalid group", func() {
nodeClaim.Spec.NodeClassRef = &NodeClassReference{
Kind: object.GVK(&v1alpha1.TestNodeClass{}).Kind,
Name: "nodeclass-test",
Group: "karpenter.test.sh/v1",
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
})
Context("Requirements", func() {
It("should allow supported ops", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpGt, Values: []string{"1"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpLt, Values: []string{"1"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpNotIn}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpExists}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail for unsupported ops", func() {
for _, op := range []v1.NodeSelectorOperator{"unknown"} {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: op, Values: []string{"test"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
}
})
It("should fail for restricted domains", func() {
for label := range RestrictedLabelDomains {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: label + "/test", Operator: v1.NodeSelectorOpIn, Values: []string{"test"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
}
})
It("should allow kubernetes domains", func() {
oldNodeClaim := nodeClaim.DeepCopy()
for label := range K8sLabelDomains {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: label + "/test", Operator: v1.NodeSelectorOpIn, Values: []string{"test"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
Expect(env.Client.Delete(ctx, nodeClaim)).To(Succeed())
nodeClaim = oldNodeClaim.DeepCopy()
}
})
It("should allow kubernetes subdomains", func() {
oldNodeClaim := nodeClaim.DeepCopy()
for label := range K8sLabelDomains {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: "subdomain." + label + "/test", Operator: v1.NodeSelectorOpIn, Values: []string{"test"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
Expect(env.Client.Delete(ctx, nodeClaim)).To(Succeed())
nodeClaim = oldNodeClaim.DeepCopy()
}
})
It("should allow well known label exceptions", func() {
oldNodeClaim := nodeClaim.DeepCopy()
for label := range WellKnownLabels.Difference(sets.New(NodePoolLabelKey)) {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: label, Operator: v1.NodeSelectorOpIn, Values: []string{"test"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
Expect(env.Client.Delete(ctx, nodeClaim)).To(Succeed())
nodeClaim = oldNodeClaim.DeepCopy()
}
})
It("should allow non-empty set after removing overlapped value", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test", "foo"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpNotIn, Values: []string{"test", "bar"}}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should allow empty requirements", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail with invalid GT or LT values", func() {
for _, requirement := range []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpGt, Values: []string{}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpGt, Values: []string{"1", "2"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpGt, Values: []string{"a"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpGt, Values: []string{"-1"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpLt, Values: []string{}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpLt, Values: []string{"1", "2"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpLt, Values: []string{"a"}}},
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpLt, Values: []string{"-1"}}},
} {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{requirement}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
}
})
It("should error when minValues is negative", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelInstanceTypeStable, Operator: v1.NodeSelectorOpIn, Values: []string{"insance-type-1"}}, MinValues: lo.ToPtr(-1)},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should error when minValues is zero", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelInstanceTypeStable, Operator: v1.NodeSelectorOpIn, Values: []string{"insance-type-1"}}, MinValues: lo.ToPtr(0)},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should error when minValues is more than 50", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelInstanceTypeStable, Operator: v1.NodeSelectorOpExists}, MinValues: lo.ToPtr(51)},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should allow more than 50 values if minValues is not specified.", func() {
var instanceTypes []string
for i := 0; i < 90; i++ {
instanceTypes = append(instanceTypes, "instance"+strconv.Itoa(i))
}
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelInstanceTypeStable, Operator: v1.NodeSelectorOpIn, Values: instanceTypes}},
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should error when minValues is greater than the number of unique values specified within In operator", func() {
nodeClaim.Spec.Requirements = []NodeSelectorRequirementWithMinValues{
{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: v1.LabelInstanceTypeStable, Operator: v1.NodeSelectorOpIn, Values: []string{"insance-type-1"}}, MinValues: lo.ToPtr(2)},
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should error when requirements is greater than 100", func() {
var req []NodeSelectorRequirementWithMinValues
for i := 0; i < 101; i++ {
req = append(req, NodeSelectorRequirementWithMinValues{NodeSelectorRequirement: v1.NodeSelectorRequirement{Key: test.RandomName(), Operator: v1.NodeSelectorOpIn, Values: []string{test.RandomName()}}})
}
nodeClaim.Spec.Requirements = req
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
})
Context("TerminationGracePeriod", func() {
It("should succeed on a positive terminationGracePeriod duration", func() {
nodeClaim.Spec.TerminationGracePeriod = &metav1.Duration{Duration: time.Second * 300}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail on a negative terminationGracePeriod duration", func() {
nodeClaim.Spec.TerminationGracePeriod = &metav1.Duration{Duration: time.Second * -30}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
})
})