Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CEL Validation in BackendTrafficPolicy #2110

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)",message="If LoadBalancer type is consistentHash, consistentHash field needs to be set."
zirain marked this conversation as resolved.
Show resolved Hide resolved
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,10 @@ 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)
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
41 changes: 41 additions & 0 deletions test/cel-validation/backendtrafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,47 @@ func TestBackendTrafficPolicyTarget(t *testing.T) {
"spec.targetRef: Invalid value: \"object\": this policy does not yet support the sectionName field",
},
},
{
slayer321 marked this conversation as resolved.
Show resolved Hide resolved
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",
},
},
}

for _, tc := range cases {
Expand Down