diff --git a/apis/v1alpha1/upstreamsettingspolicy_types.go b/apis/v1alpha1/upstreamsettingspolicy_types.go index 8504c9717d..a8f6c09a29 100644 --- a/apis/v1alpha1/upstreamsettingspolicy_types.go +++ b/apis/v1alpha1/upstreamsettingspolicy_types.go @@ -51,6 +51,13 @@ type UpstreamSettingsPolicySpec struct { // +optional KeepAlive *UpstreamKeepAlive `json:"keepAlive,omitempty"` + // LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream. + // If not specified, NGINX Gateway Fabric defaults to `random two least_conn`, + // which differs from the standard NGINX default `round-robin`. + // + // +optional + LoadBalancingMethod *LoadBalancingType `json:"loadBalancingMethod,omitempty"` + // TargetRefs identifies API object(s) to apply the policy to. // Objects must be in the same namespace as the policy. // Support: Service @@ -98,3 +105,22 @@ type UpstreamKeepAlive struct { // +optional Timeout *Duration `json:"timeout,omitempty"` } + +// LoadBalancingType defines the supported load balancing methods. +// +// +kubebuilder:validation:Enum=ip_hash;random two least_conn +type LoadBalancingType string + +const ( + // LoadBalancingTypeIPHash enables IP hash-based load balancing, + // ensuring requests from the same client IP are routed to the same upstream server. + // NGINX directive: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash + LoadBalancingTypeIPHash LoadBalancingType = "ip_hash" + + // LoadBalancingTypeRandomTwoLeastConnection enables a variation of least-connections + // balancing that randomly selects two servers and forwards traffic to the one with + // fewer active connections. + // NGINX directive least_conn: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#least_conn + // NGINX directive random: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#random + LoadBalancingTypeRandomTwoLeastConnection LoadBalancingType = "random two least_conn" +) diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index d07825de2d..4f0c2bebc4 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -556,6 +556,11 @@ func (in *UpstreamSettingsPolicySpec) DeepCopyInto(out *UpstreamSettingsPolicySp *out = new(UpstreamKeepAlive) (*in).DeepCopyInto(*out) } + if in.LoadBalancingMethod != nil { + in, out := &in.LoadBalancingMethod, &out.LoadBalancingMethod + *out = new(LoadBalancingType) + **out = **in + } if in.TargetRefs != nil { in, out := &in.TargetRefs, &out.TargetRefs *out = make([]apisv1.LocalPolicyTargetReference, len(*in)) diff --git a/config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml b/config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml index ce6b9603ed..6fa9de4104 100644 --- a/config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml +++ b/config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml @@ -85,6 +85,15 @@ spec: pattern: ^[0-9]{1,4}(ms|s|m|h)?$ type: string type: object + loadBalancingMethod: + description: |- + LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream. + If not specified, NGINX Gateway Fabric defaults to `random two least_conn`, + which differs from the standard NGINX default `round-robin`. + enum: + - ip_hash + - random two least_conn + type: string targetRefs: description: |- TargetRefs identifies API object(s) to apply the policy to. diff --git a/deploy/crds.yaml b/deploy/crds.yaml index 2a526a961f..ce16d35823 100644 --- a/deploy/crds.yaml +++ b/deploy/crds.yaml @@ -9612,6 +9612,15 @@ spec: pattern: ^[0-9]{1,4}(ms|s|m|h)?$ type: string type: object + loadBalancingMethod: + description: |- + LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream. + If not specified, NGINX Gateway Fabric defaults to `random two least_conn`, + which differs from the standard NGINX default `round-robin`. + enum: + - ip_hash + - random two least_conn + type: string targetRefs: description: |- TargetRefs identifies API object(s) to apply the policy to.