@@ -36,6 +36,9 @@ type UpstreamSettingsPolicyList struct {
3636}
3737
3838// UpstreamSettingsPolicySpec defines the desired state of the UpstreamSettingsPolicy.
39+ // +kubebuilder:validation:XValidation:rule="!(has(self.loadBalancingMethod) && (self.loadBalancingMethod == 'hash' || self.loadBalancingMethod == 'hash consistent')) || has(self.hashMethodKey)",message="hashMethodKey is required when loadBalancingMethod is 'hash' or 'hash consistent'"
40+ //
41+ //nolint:lll
3942type UpstreamSettingsPolicySpec struct {
4043 // ZoneSize is the size of the shared memory zone used by the upstream. This memory zone is used to share
4144 // the upstream configuration between nginx worker processes. The more servers that an upstream has,
@@ -58,6 +61,12 @@ type UpstreamSettingsPolicySpec struct {
5861 // +optional
5962 LoadBalancingMethod * LoadBalancingType `json:"loadBalancingMethod,omitempty"`
6063
64+ // HashMethodKey defines the key used for hash-based load balancing methods.
65+ // This field is required when `LoadBalancingMethod` is set to `hash` or `hash consistent`.
66+ //
67+ // +optional
68+ HashMethodKey * HashMethodKey `json:"hashMethodKey,omitempty"`
69+
6170 // TargetRefs identifies API object(s) to apply the policy to.
6271 // Objects must be in the same namespace as the policy.
6372 // Support: Service
@@ -108,19 +117,96 @@ type UpstreamKeepAlive struct {
108117
109118// LoadBalancingType defines the supported load balancing methods.
110119//
111- // +kubebuilder:validation:Enum=ip_hash;random two least_conn
120+ // +kubebuilder:validation:Enum=round_robin;least_conn;ip_hash;hash;hash consistent;random;random two;random two least_conn;random two least_time=header;random two least_time=last_byte;least_time header;least_time last_byte;least_time header inflight;least_time last_byte inflight
121+ //
122+ //nolint:lll
112123type LoadBalancingType string
113124
114125const (
126+ // Combination of NGINX directive
127+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#random
128+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#least_conn
129+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#least_time
130+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
131+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash
132+ // - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#hash
133+
134+ // LoadBalancingMethods supported by NGINX OSS and NGINX Plus.
135+
136+ // LoadBalancingTypeRoundRobin enables round-robin load balancing,
137+ // distributing requests evenly across all upstream servers.
138+ LoadBalancingTypeRoundRobin LoadBalancingType = "round_robin"
139+
140+ // LoadBalancingTypeLeastConnection enables least-connections load balancing,
141+ // routing requests to the upstream server with the fewest active connections.
142+ LoadBalancingTypeLeastConnection LoadBalancingType = "least_conn"
143+
115144 // LoadBalancingTypeIPHash enables IP hash-based load balancing,
116145 // 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
118146 LoadBalancingTypeIPHash LoadBalancingType = "ip_hash"
119147
148+ // LoadBalancingTypeHash enables generic hash-based load balancing,
149+ // routing requests to upstream servers based on a hash of a specified key
150+ // HashMethodKey field must be set when this method is selected.
151+ // Example configuration: hash $binary_remote_addr;.
152+ LoadBalancingTypeHash LoadBalancingType = "hash"
153+
154+ // LoadBalancingTypeHashConsistent enables consistent hash-based load balancing,
155+ // which minimizes the number of keys remapped when a server is added or removed.
156+ // HashMethodKey field must be set when this method is selected.
157+ // Example configuration: hash $binary_remote_addr consistent;.
158+ LoadBalancingTypeHashConsistent LoadBalancingType = "hash consistent"
159+
160+ // LoadBalancingTypeRandom enables random load balancing,
161+ // routing requests to upstream servers in a random manner.
162+ LoadBalancingTypeRandom LoadBalancingType = "random"
163+
164+ // LoadBalancingTypeRandomTwo enables a variation of random load balancing
165+ // that randomly selects two servers and forwards traffic to one of them.
166+ // The default method is least_conn which passes a request to a server with the least number of active connections.
167+ LoadBalancingTypeRandomTwo LoadBalancingType = "random two"
168+
120169 // LoadBalancingTypeRandomTwoLeastConnection enables a variation of least-connections
121170 // balancing that randomly selects two servers and forwards traffic to the one with
122171 // 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
125172 LoadBalancingTypeRandomTwoLeastConnection LoadBalancingType = "random two least_conn"
173+
174+ // LoadBalancingMethods supported by NGINX Plus.
175+
176+ // LoadBalancingTypeRandomTwoLeastTimeHeader enables a variation of least-time load balancing
177+ // that randomly selects two servers and forwards traffic to the one with the least
178+ // time to receive the response header.
179+ LoadBalancingTypeRandomTwoLeastTimeHeader LoadBalancingType = "random two least_time=header"
180+
181+ // LoadBalancingTypeRandomTwoLeastTimeLastByte enables a variation of least-time load balancing
182+ // that randomly selects two servers and forwards traffic to the one with the least time
183+ // to receive the full response.
184+ LoadBalancingTypeRandomTwoLeastTimeLastByte LoadBalancingType = "random two least_time=last_byte"
185+
186+ // LoadBalancingTypeLeastTimeHeader enables least-time load balancing,
187+ // routing requests to the upstream server with the least time to receive the response header.
188+ LoadBalancingTypeLeastTimeHeader LoadBalancingType = "least_time header"
189+
190+ // LoadBalancingTypeLeastTimeLastByte enables least-time load balancing,
191+ // routing requests to the upstream server with the least time to receive the full response.
192+ LoadBalancingTypeLeastTimeLastByte LoadBalancingType = "least_time last_byte"
193+
194+ // LoadBalancingTypeLeastTimeHeaderInflight enables least-time load balancing,
195+ // routing requests to the upstream server with the least time to receive the response header,
196+ // considering the incomplete requests.
197+ LoadBalancingTypeLeastTimeHeaderInflight LoadBalancingType = "least_time header inflight"
198+
199+ // LoadBalancingTypeLeastTimeLastByteInflight enables least-time load balancing,
200+ // routing requests to the upstream server with the least time to receive the full response,
201+ // considering the incomplete requests.
202+ LoadBalancingTypeLeastTimeLastByteInflight LoadBalancingType = "least_time last_byte inflight"
126203)
204+
205+ // HashMethodKey defines the key used for hash-based load balancing methods.
206+ // The key must be a valid NGINX variable name starting with '$' followed by lowercase
207+ // letters and underscores only.
208+ // For a full list of NGINX variables,
209+ // refer to: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables
210+ //
211+ // +kubebuilder:validation:Pattern=`^\$[a-z_]+$`
212+ type HashMethodKey string
0 commit comments