Skip to content

Commit b45ae24

Browse files
authored
feat: CEL Validation in BackendTrafficPolicy (#2110)
* feat: CEL Validation in BackendTrafficPolicy Signed-off-by: slayer321 <sachin.maurya7666@gmail.com> * leastRequest with consistentHash nil test Signed-off-by: slayer321 <sachin.maurya7666@gmail.com> --------- Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
1 parent be86295 commit b45ae24

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

api/v1alpha1/loadbalancer_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package v1alpha1
77

88
// LoadBalancer defines the load balancer policy to be applied.
99
// +union
10+
//
11+
// +kubebuilder:validation:XValidation:rule="self.type == 'ConsistentHash' ? has(self.consistentHash) : !has(self.consistentHash)",message="If LoadBalancer type is consistentHash, consistentHash field needs to be set."
1012
type LoadBalancer struct {
1113
// Type decides the type of Load Balancer policy.
1214
// Valid RateLimitType values are

charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ spec:
7474
required:
7575
- type
7676
type: object
77+
x-kubernetes-validations:
78+
- message: If LoadBalancer type is consistentHash, consistentHash
79+
field needs to be set.
80+
rule: 'self.type == ''ConsistentHash'' ? has(self.consistentHash)
81+
: !has(self.consistentHash)'
7782
rateLimit:
7883
description: RateLimit allows the user to limit the number of incoming
7984
requests to a predefined value based on attributes within the traffic

test/cel-validation/backendtrafficpolicy_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,65 @@ func TestBackendTrafficPolicyTarget(t *testing.T) {
151151
"spec.targetRef: Invalid value: \"object\": this policy does not yet support the sectionName field",
152152
},
153153
},
154+
{
155+
desc: "consistentHash field not nil when type is consistentHash",
156+
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
157+
btp.Spec = egv1a1.BackendTrafficPolicySpec{
158+
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
159+
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
160+
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
161+
Kind: gwapiv1a2.Kind("Gateway"),
162+
Name: gwapiv1a2.ObjectName("eg"),
163+
},
164+
},
165+
LoadBalancer: &egv1a1.LoadBalancer{
166+
Type: egv1a1.ConsistentHashLoadBalancerType,
167+
ConsistentHash: &egv1a1.ConsistentHash{
168+
Type: "SourceIP",
169+
},
170+
},
171+
}
172+
},
173+
wantErrors: []string{},
174+
},
175+
{
176+
desc: "consistentHash field nil when type is consistentHash",
177+
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
178+
btp.Spec = egv1a1.BackendTrafficPolicySpec{
179+
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
180+
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
181+
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
182+
Kind: gwapiv1a2.Kind("Gateway"),
183+
Name: gwapiv1a2.ObjectName("eg"),
184+
},
185+
},
186+
LoadBalancer: &egv1a1.LoadBalancer{
187+
Type: egv1a1.ConsistentHashLoadBalancerType,
188+
},
189+
}
190+
},
191+
wantErrors: []string{
192+
"spec.loadBalancer: Invalid value: \"object\": If LoadBalancer type is consistentHash, consistentHash field needs to be set",
193+
},
194+
},
195+
{
196+
desc: "leastRequest with ConsistentHash nil",
197+
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
198+
btp.Spec = egv1a1.BackendTrafficPolicySpec{
199+
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
200+
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
201+
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
202+
Kind: gwapiv1a2.Kind("Gateway"),
203+
Name: gwapiv1a2.ObjectName("eg"),
204+
},
205+
},
206+
LoadBalancer: &egv1a1.LoadBalancer{
207+
Type: egv1a1.LeastRequestLoadBalancerType,
208+
},
209+
}
210+
},
211+
wantErrors: []string{},
212+
},
154213
}
155214

156215
for _, tc := range cases {

0 commit comments

Comments
 (0)