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

bug: fix bucket validation in ratelimit_types.go #1633

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion apis/gateway/ratelimit/v1alpha1/ratelimit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
)

// BucketConfig represents a rate limit bucket configuration.
// +kubebuilder:validation:XValidation:rule="((has(self.path)?1:0)+(has(self.headers)?1:0))==1",message="path or headers must be set"
// +kubebuilder:validation:XValidation:rule="has(self.path) || has(self.headers)",message="At least one of 'path' or 'headers' must be set"
type BucketConfig struct {
Path string `json:"path,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/gateway.kyma-project.io_ratelimits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ spec:
- bucket
type: object
x-kubernetes-validations:
- message: path or headers must be set
rule: ((has(self.path)?1:0)+(has(self.headers)?1:0))==1
- message: At least one of 'path' or 'headers' must be set
rule: has(self.path) || has(self.headers)
type: array
defaultBucket:
description: BucketSpec defines the token bucket specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,165 @@ var _ = Describe("Rate Limit Controller", func() {
return ef.Generation > observedGeneration
}).Should(BeTrue())
})
Context("RateLimit CRD validation", func() {
It("should fail if path and headers are empty", func() {
namespace := ns.Name
By("Creating test pod")
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: namespace,
Annotations: map[string]string{
"sidecar.istio.io/status": "",
},
Labels: map[string]string{
"app": "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
Image: "busybox",
},
}}}
Expect(c.Create(ctx, pod)).Should(Succeed())
By("Creating RateLimit resource")
rl := &ratelimitv1alpha1.RateLimit{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "rate-limit-",
Namespace: ns.Name,
},
Spec: ratelimitv1alpha1.RateLimitSpec{
EnableResponseHeaders: false,
SelectorLabels: map[string]string{"app": "test"},
Local: ratelimitv1alpha1.LocalConfig{
DefaultBucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
Buckets: []ratelimitv1alpha1.BucketConfig{
{
Bucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
},
},
},
}}
err := c.Create(ctx, rl)
Expect(err).ShouldNot(Succeed())
Expect(err.Error()).To(ContainSubstring("At least one of 'path' or 'headers' must be set"))
})
It("should pass if path and headers are defined", func() {
namespace := ns.Name
By("Creating test pod")
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: namespace,
Annotations: map[string]string{
"sidecar.istio.io/status": "",
},
Labels: map[string]string{
"app": "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
Image: "busybox",
},
}}}
Expect(c.Create(ctx, pod)).Should(Succeed())
By("Creating RateLimit resource")
rl := &ratelimitv1alpha1.RateLimit{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "rate-limit-",
Namespace: ns.Name,
},
Spec: ratelimitv1alpha1.RateLimitSpec{
EnableResponseHeaders: false,
SelectorLabels: map[string]string{"app": "test"},
Local: ratelimitv1alpha1.LocalConfig{
DefaultBucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
Buckets: []ratelimitv1alpha1.BucketConfig{
{
Path: "/anything",
Headers: map[string]string{
"X-Foo": "bar",
},
Bucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
},
},
},
}}
Expect(c.Create(ctx, rl)).Should(Succeed())
})
It("should pass if only path is defined", func() {
namespace := ns.Name
By("Creating test pod")
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: namespace,
Annotations: map[string]string{
"sidecar.istio.io/status": "",
},
Labels: map[string]string{
"app": "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
Image: "busybox",
},
}}}
Expect(c.Create(ctx, pod)).Should(Succeed())
By("Creating RateLimit resource")
rl := &ratelimitv1alpha1.RateLimit{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "rate-limit-",
Namespace: ns.Name,
},
Spec: ratelimitv1alpha1.RateLimitSpec{
EnableResponseHeaders: false,
SelectorLabels: map[string]string{"app": "test"},
Local: ratelimitv1alpha1.LocalConfig{
DefaultBucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
Buckets: []ratelimitv1alpha1.BucketConfig{
{
Path: "/anything",
Bucket: ratelimitv1alpha1.BucketSpec{
MaxTokens: 20,
TokensPerFill: 20,
FillInterval: &metav1.Duration{Duration: time.Minute * 5},
},
},
},
},
}}
Expect(c.Create(ctx, rl)).Should(Succeed())
})
})
})

func getTestScheme() *runtime.Scheme {
Expand Down
Loading