Skip to content

Commit

Permalink
feat(api): Add support for the header hash policy (#3342)
Browse files Browse the repository at this point in the history
* Make API changes

Signed-off-by: Harry Turton <harryt@canva.com>

* Add tests

Signed-off-by: Harry Turton <harryt@canva.com>

* Fixups

Signed-off-by: Harry Turton <harryt@canva.com>

* Fixup test compilation issues

Signed-off-by: Harry Turton <harryt@canva.com>

* Make ConsistentHash a union

Signed-off-by: Harry Turton <harryt@canva.com>

* Remove redundant TargetRef from tests

Signed-off-by: Harry Turton <harryt@canva.com>

* Revert test change

Signed-off-by: Harry Turton <harryt@canva.com>

* Add target ref back

Signed-off-by: Harry Turton <harryt@canva.com>

* Code review and fix tests

Signed-off-by: Harry Turton <harryt@canva.com>

* Regen charts

Signed-off-by: Harry Turton <harryt@canva.com>

* Propagate HeaderName to Header change to the json field

Signed-off-by: Harry Turton <harryt@canva.com>

* Generate code

Signed-off-by: Harry Turton <harryt@canva.com>

* Regen manifests

Signed-off-by: Harry Turton <harryt@canva.com>

* Add +notImplementedHide on Header

Signed-off-by: Harry Turton <harryt@canva.com>
  • Loading branch information
harrisonturton authored May 8, 2024
1 parent 4b22373 commit b90f81c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 10 deletions.
27 changes: 24 additions & 3 deletions api/v1alpha1/loadbalancer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type LoadBalancer struct {
// "ConsistentHash",
// "LeastRequest",
// "Random",
// "RoundRobin",
// "RoundRobin".
//
// +unionDiscriminator
Type LoadBalancerType `json:"type"`
Expand Down Expand Up @@ -52,18 +52,39 @@ const (
)

// ConsistentHash defines the configuration related to the consistent hash
// load balancer policy
// load balancer policy.
// +union
//
// +kubebuilder:validation:XValidation:rule="self.type == 'Header' ? has(self.header) : !has(self.header)",message="If consistent hash type is header, the header field must be set."
type ConsistentHash struct {
// Valid Type values are "SourceIP".
//
// +unionDiscriminator
Type ConsistentHashType `json:"type"`

// Header configures the header hash policy when the consistent hash type is set to Header.
//
// +optional
// +notImplementedHide
Header *Header `json:"header,omitempty"`
}

// Header defines the header hashing configuration for consistent hash based
// load balancing.
type Header struct {
// Name of the header to hash.
Name string `json:"name"`
}

// ConsistentHashType defines the type of input to hash on.
// +kubebuilder:validation:Enum=SourceIP
// +kubebuilder:validation:Enum=SourceIP;Header
type ConsistentHashType string

const (
// SourceIPConsistentHashType hashes based on the source IP address.
SourceIPConsistentHashType ConsistentHashType = "SourceIP"
// HeaderConsistentHashType hashes based on a request header.
HeaderConsistentHashType ConsistentHashType = "Header"
)

// SlowStart defines the configuration related to the slow start load balancer policy.
Expand Down
22 changes: 21 additions & 1 deletion 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 @@ -412,15 +412,29 @@ spec:
ConsistentHash defines the configuration when the load balancer type is
set to ConsistentHash
properties:
header:
description: Header configures the header hash policy when
the consistent hash type is set to Header.
properties:
name:
description: Name of the header to hash.
type: string
required:
- name
type: object
type:
description: ConsistentHashType defines the type of input
to hash on.
description: Valid Type values are "SourceIP".
enum:
- SourceIP
- Header
type: string
required:
- type
type: object
x-kubernetes-validations:
- message: If consistent hash type is header, the header field
must be set.
rule: 'self.type == ''Header'' ? has(self.header) : !has(self.header)'
slowStart:
description: |-
SlowStart defines the configuration related to the slow start load balancer policy.
Expand All @@ -444,7 +458,7 @@ spec:
"ConsistentHash",
"LeastRequest",
"Random",
"RoundRobin",
"RoundRobin".
enum:
- ConsistentHash
- LeastRequest
Expand Down
22 changes: 19 additions & 3 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,14 @@ _Appears in:_


ConsistentHash defines the configuration related to the consistent hash
load balancer policy
load balancer policy.

_Appears in:_
- [LoadBalancer](#loadbalancer)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `type` | _[ConsistentHashType](#consistenthashtype)_ | true | |
| `type` | _[ConsistentHashType](#consistenthashtype)_ | true | Valid Type values are "SourceIP". |


#### ConsistentHashType
Expand All @@ -577,6 +577,7 @@ _Appears in:_
| Value | Description |
| ----- | ----------- |
| `SourceIP` | SourceIPConsistentHashType hashes based on the source IP address.<br /> |
| `Header` | HeaderConsistentHashType hashes based on a request header.<br /> |


#### CustomHeaderExtensionSettings
Expand Down Expand Up @@ -1624,6 +1625,21 @@ _Appears in:_
| `url` | _string_ | true | URL is the URL containing the wasm code. |


#### Header



Header defines the header hashing configuration for consistent hash based
load balancing.

_Appears in:_
- [ConsistentHash](#consistenthash)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | _string_ | true | Name of the header to hash. |




#### HeaderMatchType
Expand Down Expand Up @@ -2018,7 +2034,7 @@ _Appears in:_

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `type` | _[LoadBalancerType](#loadbalancertype)_ | true | Type decides the type of Load Balancer policy.<br />Valid LoadBalancerType values are<br />"ConsistentHash",<br />"LeastRequest",<br />"Random",<br />"RoundRobin", |
| `type` | _[LoadBalancerType](#loadbalancertype)_ | true | Type decides the type of Load Balancer policy.<br />Valid LoadBalancerType values are<br />"ConsistentHash",<br />"LeastRequest",<br />"Random",<br />"RoundRobin". |
| `consistentHash` | _[ConsistentHash](#consistenthash)_ | false | ConsistentHash defines the configuration when the load balancer type is<br />set to ConsistentHash |
| `slowStart` | _[SlowStart](#slowstart)_ | false | SlowStart defines the configuration related to the slow start load balancer policy.<br />If set, during slow start window, traffic sent to the newly added hosts will gradually increase.<br />Currently this is only supported for RoundRobin and LeastRequest load balancers |

Expand Down
47 changes: 47 additions & 0 deletions test/cel-validation/backendtrafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,53 @@ func TestBackendTrafficPolicyTarget(t *testing.T) {
"spec.loadBalancer: Invalid value: \"object\": If LoadBalancer type is consistentHash, consistentHash field needs to be set",
},
},
{
desc: "consistentHash header field not nil when consistentHashType is header",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
btp.Spec = egv1a1.BackendTrafficPolicySpec{
TargetRef: gwapiv1a2.LocalPolicyTargetReferenceWithSectionName{
LocalPolicyTargetReference: gwapiv1a2.LocalPolicyTargetReference{
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: "Header",
Header: &egv1a1.Header{
Name: "name",
},
},
},
}
},
wantErrors: []string{},
},
{
desc: "consistentHash header field nil when consistentHashType is header",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
btp.Spec = egv1a1.BackendTrafficPolicySpec{
TargetRef: gwapiv1a2.LocalPolicyTargetReferenceWithSectionName{
LocalPolicyTargetReference: gwapiv1a2.LocalPolicyTargetReference{
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: "Header",
},
},
}
},
wantErrors: []string{
"spec.loadBalancer.consistentHash: Invalid value: \"object\": If consistent hash type is header, the header field must be set",
},
},
{
desc: "leastRequest with ConsistentHash nil",
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
Expand Down

0 comments on commit b90f81c

Please sign in to comment.