Skip to content

Commit

Permalink
feat: add APIPlans bucket support
Browse files Browse the repository at this point in the history
  • Loading branch information
jspdown authored Sep 26, 2024
1 parent 51d57f9 commit a0201d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pkg/apis/hub/v1alpha1/api_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ type APIPlanStatus struct {
Hash string `json:"hash,omitempty"`
}

// Bucket defines the scope of rate limit or quota.
// +kubebuilder:validation:Enum=user;access
type Bucket string

// List of supported buckets.
const (
BucketUser Bucket = "user"
BucketAccess Bucket = "access"
)

type RateLimit struct {
// Limit is the maximum number of token in the bucket.
// +kubebuilder:validation:XValidation:message="must be a positive number",rule="self >= 0"
Expand All @@ -73,6 +83,10 @@ type RateLimit struct {
// +optional
// +kubebuilder:validation:XValidation:message="must be between 1s and 1h",rule="self >= duration('1s') && self <= duration('1h')"
Period *Period `json:"period,omitempty"`

// Bucket defines the scope of the rate limit.
// +optional
Bucket Bucket `json:"bucket,omitempty"`
}

type Quota struct {
Expand All @@ -84,6 +98,10 @@ type Quota struct {
// +optional
// +kubebuilder:validation:XValidation:message="must be between 1s and 9999h",rule="self >= duration('1s') && self <= duration('9999h')"
Period *Period `json:"period,omitempty"`

// Bucket defines the scope of the quota.
// +optional
Bucket Bucket `json:"bucket,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/hub/v1alpha1/crd/hub.traefik.io_apiplans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ spec:
quota:
description: Quota defines the quota policy.
properties:
bucket:
description: Bucket defines the scope of the quota.
enum:
- user
- access
type: string
limit:
description: Limit is the maximum number of token in the bucket.
type: integer
Expand All @@ -64,6 +70,12 @@ spec:
rateLimit:
description: RateLimit defines the rate limit policy.
properties:
bucket:
description: Bucket defines the scope of the rate limit.
enum:
- user
- access
type: string
limit:
description: Limit is the maximum number of token in the bucket.
type: integer
Expand Down
20 changes: 19 additions & 1 deletion pkg/validation/v1alpha1/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ spec:
rateLimit:
limit: 1
period: 2s
bucket: user
quota:
limit: 1
period: 2s`),
period: 2s
bucket: access`),
},
{
desc: "missing resource namespace",
Expand Down Expand Up @@ -217,6 +219,22 @@ spec:
period: 0s`),
wantErrs: field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "spec.rateLimit.period", BadValue: "string", Detail: "must be between 1s and 1h"}},
},
{
desc: "unsupported ratelimit bucket",
manifest: []byte(`
apiVersion: hub.traefik.io/v1alpha1
kind: APIPlan
metadata:
name: my-plan
namespace: default
spec:
title: my-plan
rateLimit:
limit: 1
period: 1s
bucket: something`),
wantErrs: field.ErrorList{{Type: field.ErrorTypeNotSupported, Field: "spec.rateLimit.bucket", BadValue: "something", Detail: "supported values: \"user\", \"access\""}},
},
}

for _, test := range tests {
Expand Down

0 comments on commit a0201d9

Please sign in to comment.