@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/apimachinery/pkg/util/sets"
26
26
27
27
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
28
+ "sigs.k8s.io/karpenter/pkg/test"
28
29
)
29
30
30
31
var _ = Describe ("CEL/Validation" , func () {
@@ -65,23 +66,65 @@ var _ = Describe("CEL/Validation", func() {
65
66
{NodeSelectorRequirement : corev1.NodeSelectorRequirement {Key : label + "/test" , Operator : corev1 .NodeSelectorOpIn , Values : []string {"test" }}},
66
67
}
67
68
Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
68
- Expect (nodePool .RuntimeValidate ()).To (Succeed ())
69
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
69
70
Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
70
71
nodePool = oldNodePool .DeepCopy ()
71
72
}
72
73
})
73
74
It ("should allow well known label exceptions" , func () {
74
75
oldNodePool := nodePool .DeepCopy ()
75
- for label := range karpv1 .WellKnownLabels .Difference (sets .New (karpv1 .NodePoolLabelKey )) {
76
+ for label := range karpv1 .WellKnownLabels .Difference (sets .New (karpv1 .NodePoolLabelKey , karpv1 . CapacityTypeLabelKey )) {
76
77
nodePool .Spec .Template .Spec .Requirements = []karpv1.NodeSelectorRequirementWithMinValues {
77
78
{NodeSelectorRequirement : corev1.NodeSelectorRequirement {Key : label , Operator : corev1 .NodeSelectorOpIn , Values : []string {"test" }}},
78
79
}
79
80
Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
80
- Expect (nodePool .RuntimeValidate ()).To (Succeed ())
81
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
81
82
Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
82
83
nodePool = oldNodePool .DeepCopy ()
83
84
}
84
85
})
86
+ It ("should fail validation with only invalid capacity types" , func () {
87
+ oldNodePool := nodePool .DeepCopy ()
88
+ test .ReplaceRequirements (nodePool , karpv1.NodeSelectorRequirementWithMinValues {
89
+ NodeSelectorRequirement : corev1.NodeSelectorRequirement {
90
+ Key : karpv1 .CapacityTypeLabelKey ,
91
+ Operator : corev1 .NodeSelectorOpIn ,
92
+ Values : []string {"xspot" }, // Invalid value
93
+ },
94
+ })
95
+ Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
96
+ Expect (nodePool .RuntimeValidate (ctx )).ToNot (Succeed ())
97
+ Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
98
+ nodePool = oldNodePool .DeepCopy ()
99
+ })
100
+ It ("should pass validation with valid capacity types" , func () {
101
+ oldNodePool := nodePool .DeepCopy ()
102
+ test .ReplaceRequirements (nodePool , karpv1.NodeSelectorRequirementWithMinValues {
103
+ NodeSelectorRequirement : corev1.NodeSelectorRequirement {
104
+ Key : karpv1 .CapacityTypeLabelKey ,
105
+ Operator : corev1 .NodeSelectorOpIn ,
106
+ Values : []string {karpv1 .CapacityTypeOnDemand }, // Valid value
107
+ },
108
+ })
109
+ Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
110
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
111
+ Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
112
+ nodePool = oldNodePool .DeepCopy ()
113
+ })
114
+ It ("should fail open if invalid and valid capacity types are present" , func () {
115
+ oldNodePool := nodePool .DeepCopy ()
116
+ test .ReplaceRequirements (nodePool , karpv1.NodeSelectorRequirementWithMinValues {
117
+ NodeSelectorRequirement : corev1.NodeSelectorRequirement {
118
+ Key : karpv1 .CapacityTypeLabelKey ,
119
+ Operator : corev1 .NodeSelectorOpIn ,
120
+ Values : []string {karpv1 .CapacityTypeOnDemand , "xspot" }, // Valid and invalid value
121
+ },
122
+ })
123
+ Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
124
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
125
+ Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
126
+ nodePool = oldNodePool .DeepCopy ()
127
+ })
85
128
})
86
129
Context ("Labels" , func () {
87
130
It ("should allow restricted domains exceptions" , func () {
@@ -91,7 +134,7 @@ var _ = Describe("CEL/Validation", func() {
91
134
label : "test" ,
92
135
}
93
136
Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
94
- Expect (nodePool .RuntimeValidate ()).To (Succeed ())
137
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
95
138
Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
96
139
nodePool = oldNodePool .DeepCopy ()
97
140
}
@@ -103,7 +146,7 @@ var _ = Describe("CEL/Validation", func() {
103
146
label : "test" ,
104
147
}
105
148
Expect (env .Client .Create (ctx , nodePool )).To (Succeed ())
106
- Expect (nodePool .RuntimeValidate ()).To (Succeed ())
149
+ Expect (nodePool .RuntimeValidate (ctx )).To (Succeed ())
107
150
Expect (env .Client .Delete (ctx , nodePool )).To (Succeed ())
108
151
nodePool = oldNodePool .DeepCopy ()
109
152
}
0 commit comments