Skip to content

Commit

Permalink
feat: CEL Validation in BackendTrafficPolicy (#2110)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
slayer321 committed Oct 31, 2023
1 parent be86295 commit b45ae24
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha1/loadbalancer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package v1alpha1

// LoadBalancer defines the load balancer policy to be applied.
// +union
//
// +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."
type LoadBalancer struct {
// Type decides the type of Load Balancer policy.
// Valid RateLimitType values are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ spec:
required:
- type
type: object
x-kubernetes-validations:
- message: If LoadBalancer type is consistentHash, consistentHash
field needs to be set.
rule: 'self.type == ''ConsistentHash'' ? has(self.consistentHash)
: !has(self.consistentHash)'
rateLimit:
description: RateLimit allows the user to limit the number of incoming
requests to a predefined value based on attributes within the traffic
Expand Down
59 changes: 59 additions & 0 deletions test/cel-validation/backendtrafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,65 @@ func TestBackendTrafficPolicyTarget(t *testing.T) {
"spec.targetRef: Invalid value: \"object\": this policy does not yet support the sectionName field",
},
},
{
desc: "consistentHash field not nil when type is consistentHash",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
btp.Spec = egv1a1.BackendTrafficPolicySpec{
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
Kind: gwapiv1a2.Kind("Gateway"),
Name: gwapiv1a2.ObjectName("eg"),
},
},
LoadBalancer: &egv1a1.LoadBalancer{
Type: egv1a1.ConsistentHashLoadBalancerType,
ConsistentHash: &egv1a1.ConsistentHash{
Type: "SourceIP",
},
},
}
},
wantErrors: []string{},
},
{
desc: "consistentHash field nil when type is consistentHash",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
btp.Spec = egv1a1.BackendTrafficPolicySpec{
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
Kind: gwapiv1a2.Kind("Gateway"),
Name: gwapiv1a2.ObjectName("eg"),
},
},
LoadBalancer: &egv1a1.LoadBalancer{
Type: egv1a1.ConsistentHashLoadBalancerType,
},
}
},
wantErrors: []string{
"spec.loadBalancer: Invalid value: \"object\": If LoadBalancer type is consistentHash, consistentHash field needs to be set",
},
},
{
desc: "leastRequest with ConsistentHash nil",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
btp.Spec = egv1a1.BackendTrafficPolicySpec{
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
Kind: gwapiv1a2.Kind("Gateway"),
Name: gwapiv1a2.ObjectName("eg"),
},
},
LoadBalancer: &egv1a1.LoadBalancer{
Type: egv1a1.LeastRequestLoadBalancerType,
},
}
},
wantErrors: []string{},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit b45ae24

Please sign in to comment.