Skip to content

Commit 70bba4d

Browse files
authored
Add a loadBalancingMethod field to the Upstream Settings Policy API (#4251)
Problem: Users want to specify load balancing method via Upstream Settings Policy API Solution: Extend Upstream settings policy API to support load balancing method field.
1 parent 9d2bf35 commit 70bba4d

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

apis/v1alpha1/upstreamsettingspolicy_types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ type UpstreamSettingsPolicySpec struct {
5151
// +optional
5252
KeepAlive *UpstreamKeepAlive `json:"keepAlive,omitempty"`
5353

54+
// LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream.
55+
// If not specified, NGINX Gateway Fabric defaults to `random two least_conn`,
56+
// which differs from the standard NGINX default `round-robin`.
57+
//
58+
// +optional
59+
LoadBalancingMethod *LoadBalancingType `json:"loadBalancingMethod,omitempty"`
60+
5461
// TargetRefs identifies API object(s) to apply the policy to.
5562
// Objects must be in the same namespace as the policy.
5663
// Support: Service
@@ -98,3 +105,22 @@ type UpstreamKeepAlive struct {
98105
// +optional
99106
Timeout *Duration `json:"timeout,omitempty"`
100107
}
108+
109+
// LoadBalancingType defines the supported load balancing methods.
110+
//
111+
// +kubebuilder:validation:Enum=ip_hash;random two least_conn
112+
type LoadBalancingType string
113+
114+
const (
115+
// LoadBalancingTypeIPHash enables IP hash-based load balancing,
116+
// ensuring requests from the same client IP are routed to the same upstream server.
117+
// NGINX directive: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash
118+
LoadBalancingTypeIPHash LoadBalancingType = "ip_hash"
119+
120+
// LoadBalancingTypeRandomTwoLeastConnection enables a variation of least-connections
121+
// balancing that randomly selects two servers and forwards traffic to the one with
122+
// fewer active connections.
123+
// NGINX directive least_conn: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#least_conn
124+
// NGINX directive random: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#random
125+
LoadBalancingTypeRandomTwoLeastConnection LoadBalancingType = "random two least_conn"
126+
)

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ spec:
8585
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
8686
type: string
8787
type: object
88+
loadBalancingMethod:
89+
description: |-
90+
LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream.
91+
If not specified, NGINX Gateway Fabric defaults to `random two least_conn`,
92+
which differs from the standard NGINX default `round-robin`.
93+
enum:
94+
- ip_hash
95+
- random two least_conn
96+
type: string
8897
targetRefs:
8998
description: |-
9099
TargetRefs identifies API object(s) to apply the policy to.

deploy/crds.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9612,6 +9612,15 @@ spec:
96129612
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
96139613
type: string
96149614
type: object
9615+
loadBalancingMethod:
9616+
description: |-
9617+
LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream.
9618+
If not specified, NGINX Gateway Fabric defaults to `random two least_conn`,
9619+
which differs from the standard NGINX default `round-robin`.
9620+
enum:
9621+
- ip_hash
9622+
- random two least_conn
9623+
type: string
96159624
targetRefs:
96169625
description: |-
96179626
TargetRefs identifies API object(s) to apply the policy to.

0 commit comments

Comments
 (0)