Skip to content

Commit

Permalink
feat: cookie based consistent hashing
Browse files Browse the repository at this point in the history
Fixes: #2624

Signed-off-by: Arko Dasgupta <[email protected]>
  • Loading branch information
arkodg committed Jun 27, 2024
1 parent d784c32 commit d44050a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
3 changes: 0 additions & 3 deletions api/v1alpha1/loadbalancer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ type ConsistentHash struct {
// Cookie configures the cookie hash policy when the consistent hash type is set to Cookie.
//
// +optional
// +notImplementedHide
Cookie *Cookie `json:"cookie,omitempty"`

// The table size for consistent hashing, must be prime number limited to 5000011.
Expand Down Expand Up @@ -103,12 +102,10 @@ type Cookie struct {
// Max-Age attribute value.
//
// +optional
// +notImplementedHide
TTL *metav1.Duration `json:"ttl,omitempty"`
// Additional Attributes to set for the generated cookie.
//
// +optional
// +notImplementedHide
Attributes map[string]string `json:"attributes,omitempty"`
}

Expand Down
2 changes: 2 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ func (t *Translator) buildConsistentHashLoadBalancer(policy *egv1a1.BackendTraff
consistentHash.Header = &ir.Header{
Name: policy.Spec.LoadBalancer.ConsistentHash.Header.Name,
}
case egv1a1.CookieConsistentHashType:
consistentHash.Cookie = policy.Spec.LoadBalancer.ConsistentHash.Cookie

Check warning on line 868 in internal/gatewayapi/backendtrafficpolicy.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/backendtrafficpolicy.go#L867-L868

Added lines #L867 - L868 were not covered by tests
}

return consistentHash, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,9 +1770,10 @@ type Random struct{}
// +k8s:deepcopy-gen=true
type ConsistentHash struct {
// Hash based on the Source IP Address
SourceIP *bool `json:"sourceIP,omitempty" yaml:"sourceIP,omitempty"`
Header *Header `json:"header,omitempty" yaml:"header,omitempty"`
TableSize *uint64 `json:"tableSize,omitempty" yaml:"tableSize,omitempty"`
SourceIP *bool `json:"sourceIP,omitempty" yaml:"sourceIP,omitempty"`
Header *Header `json:"header,omitempty" yaml:"header,omitempty"`
Cookie *egv1a1.Cookie `json:"cookie,omitempty" yaml:"cookie,omitempty"`
TableSize *uint64 `json:"tableSize,omitempty" yaml:"tableSize,omitempty"`
}

// Header consistent hash type settings
Expand Down
5 changes: 5 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

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

22 changes: 22 additions & 0 deletions internal/xds/translator/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,28 @@ func buildHashPolicy(httpRoute *ir.HTTPRoute) []*routev3.RouteAction_HashPolicy
},
}
return []*routev3.RouteAction_HashPolicy{hashPolicy}
case ch.Cookie != nil:
hashPolicy := &routev3.RouteAction_HashPolicy{
PolicySpecifier: &routev3.RouteAction_HashPolicy_Cookie_{
Cookie: &routev3.RouteAction_HashPolicy_Cookie{
Name: ch.Cookie.Name,
},
},

Check warning on line 501 in internal/xds/translator/route.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/route.go#L495-L501

Added lines #L495 - L501 were not covered by tests
}
if ch.Cookie.TTL != nil {
hashPolicy.GetCookie().Ttl = durationpb.New(ch.Cookie.TTL.Duration)

Check warning on line 504 in internal/xds/translator/route.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/route.go#L503-L504

Added lines #L503 - L504 were not covered by tests
}
if ch.Cookie.Attributes != nil {
attributes := make([]*routev3.RouteAction_HashPolicy_CookieAttribute, len(ch.Cookie.Attributes))
i := 0
for name, value := range ch.Cookie.Attributes {
attributes[i].Name = name
attributes[i].Value = value
i++

Check warning on line 512 in internal/xds/translator/route.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/route.go#L506-L512

Added lines #L506 - L512 were not covered by tests
}
hashPolicy.GetCookie().Attributes = attributes

Check warning on line 514 in internal/xds/translator/route.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/route.go#L514

Added line #L514 was not covered by tests
}
return []*routev3.RouteAction_HashPolicy{hashPolicy}

Check warning on line 516 in internal/xds/translator/route.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/route.go#L516

Added line #L516 was not covered by tests
case ch.SourceIP != nil:
if !*ch.SourceIP {
return nil
Expand Down
3 changes: 3 additions & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ _Appears in:_
| --- | --- | --- | --- |
| `type` | _[ConsistentHashType](#consistenthashtype)_ | true | ConsistentHashType defines the type of input to hash on. Valid Type values are "SourceIP" or "Header". |
| `header` | _[Header](#header)_ | false | Header configures the header hash policy when the consistent hash type is set to Header. |
| `cookie` | _[Cookie](#cookie)_ | false | Cookie configures the cookie hash policy when the consistent hash type is set to Cookie. |
| `tableSize` | _integer_ | false | The table size for consistent hashing, must be prime number limited to 5000011. |


Expand Down Expand Up @@ -787,6 +788,8 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | _string_ | true | Name of the cookie to hash.<br />If this cookie does not exist in the request, Envoy will generate a cookie and set<br />the TTL on the response back to the client based on Layer 4<br />attributes of the backend endpoint, to ensure that these future requests<br />go to the same backend endpoint. Make sure to set the TTL field for this case. |
| `ttl` | _[Duration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#duration-v1-meta)_ | false | TTL of the generated cookie if the cookie is not present. This value sets the<br />Max-Age attribute value. |
| `attributes` | _object (keys:string, values:string)_ | false | Additional Attributes to set for the generated cookie. |


#### CustomHeaderExtensionSettings
Expand Down
3 changes: 3 additions & 0 deletions site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ _Appears in:_
| --- | --- | --- | --- |
| `type` | _[ConsistentHashType](#consistenthashtype)_ | true | ConsistentHashType defines the type of input to hash on. Valid Type values are "SourceIP" or "Header". |
| `header` | _[Header](#header)_ | false | Header configures the header hash policy when the consistent hash type is set to Header. |
| `cookie` | _[Cookie](#cookie)_ | false | Cookie configures the cookie hash policy when the consistent hash type is set to Cookie. |
| `tableSize` | _integer_ | false | The table size for consistent hashing, must be prime number limited to 5000011. |


Expand Down Expand Up @@ -787,6 +788,8 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | _string_ | true | Name of the cookie to hash.<br />If this cookie does not exist in the request, Envoy will generate a cookie and set<br />the TTL on the response back to the client based on Layer 4<br />attributes of the backend endpoint, to ensure that these future requests<br />go to the same backend endpoint. Make sure to set the TTL field for this case. |
| `ttl` | _[Duration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#duration-v1-meta)_ | false | TTL of the generated cookie if the cookie is not present. This value sets the<br />Max-Age attribute value. |
| `attributes` | _object (keys:string, values:string)_ | false | Additional Attributes to set for the generated cookie. |


#### CustomHeaderExtensionSettings
Expand Down

0 comments on commit d44050a

Please sign in to comment.