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

Ratelimit validation #1508

Merged
merged 21 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ resources:
namespaced: true
controller: true
domain: kyma-project.io
group: ratelimit
group: gateway
kind: RateLimit
path: github.com/kyma-project/api-gateway/apis/ratelimit/v1alpha1
path: github.com/kyma-project/api-gateway/apis/gateway/ratelimit/v1alpha1
version: v1alpha1
version: "3"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

// Package v1alpha1 contains API Schema definitions for the ratelimit v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=ratelimit.kyma-project.io
// +groupName=gateway.kyma-project.io
package v1alpha1

import (
Expand All @@ -26,7 +26,7 @@ import (

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "ratelimit.kyma-project.io", Version: "v1alpha1"}
GroupVersion = schema.GroupVersion{Group: "gateway.kyma-project.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,42 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// Bucket 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"
type Bucket struct {
Path string `json:"path,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
// +kubebuilder:validation:Required
DefaultBucket BucketTokenSpec `json:"bucket"`
}

// BucketTokenSpec defines the token bucket specification.
type BucketTokenSpec struct {
// +kubebuilder:validation:Required
MaxTokens int64 `json:"maxTokens"`
// +kubebuilder:validation:Required
TokensPerFill int64 `json:"tokensPerFill"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:Format=duration
FillInterval *metav1.Duration `json:"fillInterval"`
}

// Local represents the local rate limit configuration.
type Local struct {
// +kubebuilder:validation:Required
DefaultBucket BucketTokenSpec `json:"defaultBucket"`
Buckets []Bucket `json:"buckets,omitempty"`
}

// RateLimitSpec defines the desired state of RateLimit
type RateLimitSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of RateLimit. Edit ratelimit_types.go to remove/update
Foo string `json:"foo,omitempty"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinProperties=1
SelectorLabels map[string]string `json:"selectorLabels"`
// +kubebuilder:validation:Required
Local Local `json:"local"`
EnableResponseHeaders bool `json:"enableResponseHeaders,omitempty"`
Enforce bool `json:"enforce,omitempty"`
}

// RateLimitStatus defines the observed state of RateLimit
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions config/crd/bases/gateway.kyma-project.io_ratelimits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
name: ratelimits.gateway.kyma-project.io
spec:
group: gateway.kyma-project.io
names:
kind: RateLimit
listKind: RateLimitList
plural: ratelimits
singular: ratelimit
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: RateLimit is the Schema for the ratelimits API
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: RateLimitSpec defines the desired state of RateLimit
properties:
enableResponseHeaders:
type: boolean
enforce:
type: boolean
local:
description: Local represents the local rate limit configuration.
properties:
buckets:
items:
description: Bucket represents a rate limit bucket configuration.
properties:
bucket:
description: BucketTokenSpec defines the token bucket specification.
properties:
fillInterval:
format: duration
type: string
maxTokens:
format: int64
type: integer
tokensPerFill:
format: int64
type: integer
required:
- fillInterval
- maxTokens
- tokensPerFill
type: object
headers:
additionalProperties:
type: string
type: object
path:
type: string
required:
- 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
type: array
defaultBucket:
description: BucketTokenSpec defines the token bucket specification.
properties:
fillInterval:
format: duration
type: string
maxTokens:
format: int64
type: integer
tokensPerFill:
format: int64
type: integer
required:
- fillInterval
- maxTokens
- tokensPerFill
type: object
required:
- defaultBucket
type: object
selectorLabels:
additionalProperties:
type: string
minProperties: 1
type: object
required:
- local
- selectorLabels
type: object
status:
description: RateLimitStatus defines the observed state of RateLimit
type: object
type: object
served: true
storage: true
subresources:
status: {}
54 changes: 0 additions & 54 deletions config/crd/bases/ratelimit.kyma-project.io_ratelimits.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
resources:
- bases/gateway.kyma-project.io_apirules.yaml
- bases/operator.kyma-project.io_apigateways.yaml
- bases/ratelimit.kyma-project.io_ratelimits.yaml
- bases/gateway.kyma-project.io_ratelimits.yaml
#+kubebuilder:scaffold:crdkustomizeresource

labels:
Expand Down
6 changes: 3 additions & 3 deletions config/dev/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ patches:
path: /rules/-
value:
apiGroups:
- ratelimit.kyma-project.io
- gateway.kyma-project.io
resources:
- ratelimits
verbs:
Expand All @@ -31,7 +31,7 @@ patches:
path: /rules/-
value:
apiGroups:
- ratelimit.kyma-project.io
- gateway.kyma-project.io
resources:
- ratelimits/finalizers
verbs:
Expand All @@ -46,7 +46,7 @@ patches:
path: /rules/-
value:
apiGroups:
- ratelimit.kyma-project.io
- gateway.kyma-project.io
resources:
- ratelimits/status
verbs:
Expand Down
2 changes: 1 addition & 1 deletion config/prod/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ patchesStrategicMerge:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ratelimits.ratelimit.kyma-project.io
name: ratelimits.gateway.kyma-project.io
$patch: delete
Loading
Loading