Skip to content

Commit

Permalink
api: support buffer limit in clientTrafficPolicy (#2805)
Browse files Browse the repository at this point in the history
* api: support buffer limit in clientTrafficPolicy

Signed-off-by: Yael Shechter <[email protected]>

* run make commands

Signed-off-by: Yael Shechter <[email protected]>

* run gen-check

Signed-off-by: Yael Shechter <[email protected]>

* fix cr comment

Signed-off-by: Yael Shechter <[email protected]>

* add default

Signed-off-by: Yael Shechter <[email protected]>

* add examples

Signed-off-by: Yael Shechter <[email protected]>

* use uint32 for buffer limit

Signed-off-by: Yael Shechter <[email protected]>

* use quantity and add a schema validation

Signed-off-by: Yael Shechter <[email protected]>

* add a type check

Signed-off-by: Yael Shechter <[email protected]>

---------

Signed-off-by: Yael Shechter <[email protected]>
  • Loading branch information
yaelSchechter authored Mar 29, 2024
1 parent 50d10b5 commit deea895
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/v1alpha1/connection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@

package v1alpha1

import gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
import (
"k8s.io/apimachinery/pkg/api/resource"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// Connection allows users to configure connection-level settings
type Connection struct {
// ConnectionLimit defines limits related to connections
//
// +optional
ConnectionLimit *ConnectionLimit `json:"connectionLimit,omitempty"`
// BufferLimit provides configuration for the maximum buffer size in bytes for each incoming connection.
// For example, 20Mi, 1Gi, 256Ki etc.
// Note that when the suffix is not provided, the value is interpreted as bytes.
// Default: 32768 bytes.
//
// +kubebuilder:validation:XValidation:rule="type(self) == string ? self.matches(r\"^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$\") : type(self) == int",message="bufferLimit must be of the format \"^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$\""
// +optional
BufferLimit *resource.Quantity `json:"bufferLimit,omitempty"`
}

type ConnectionLimit struct {
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ spec:
connection:
description: Connection includes client connection settings.
properties:
bufferLimit:
anyOf:
- type: integer
- type: string
description: 'BufferLimit provides configuration for the maximum
buffer size in bytes for each incoming connection. For example,
20Mi, 1Gi, 256Ki etc. Note that when the suffix is not provided,
the value is interpreted as bytes. Default: 32768 bytes.'
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
x-kubernetes-validations:
- message: bufferLimit must be of the format "^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$"
rule: 'type(self) == string ? self.matches(r"^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$")
: type(self) == int'
connectionLimit:
description: ConnectionLimit defines limits related to connections
properties:
Expand Down
1 change: 1 addition & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `connectionLimit` | _[ConnectionLimit](#connectionlimit)_ | false | ConnectionLimit defines limits related to connections |
| `bufferLimit` | _[Quantity](#quantity)_ | false | BufferLimit provides configuration for the maximum buffer size in bytes for each incoming connection. For example, 20Mi, 1Gi, 256Ki etc. Note that when the suffix is not provided, the value is interpreted as bytes. Default: 32768 bytes. |


#### ConnectionLimit
Expand Down
21 changes: 21 additions & 0 deletions test/cel-validation/clienttrafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package celvalidation
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -263,6 +264,26 @@ func TestClientTrafficPolicyTarget(t *testing.T) {
},
wantErrors: []string{},
},
{
desc: "invalid bufferLimit format",
mutate: func(ctp *egv1a1.ClientTrafficPolicy) {
ctp.Spec = egv1a1.ClientTrafficPolicySpec{
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: gwapiv1a2.Group("gateway.networking.k8s.io"),
Kind: gwapiv1a2.Kind("Gateway"),
Name: gwapiv1a2.ObjectName("eg"),
},
},
Connection: &egv1a1.Connection{
BufferLimit: ptr.To(resource.MustParse("15m")),
},
}
},
wantErrors: []string{
"spec.connection.bufferLimit: Invalid value: \"\": bufferLimit must be of the format \"^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$\"",
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit deea895

Please sign in to comment.