@@ -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.hashKey)",message="hashKey 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+ // HashKey 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+ HashKey * HashMethodKey `json:"hashKey,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,97 @@ 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 for NGINX OSS.
135+
136+ // LoadBalancingTypeRoundRobin enables round-robin load balancing,
137+ // distributing requests evenly across all upstream servers.
138+ // NGINX defaults to this method if no load balancing method is specified.
139+ LoadBalancingTypeRoundRobin LoadBalancingType = "round_robin"
140+
141+ // LoadBalancingTypeLeastConn enables least-connections load balancing,
142+ // routing requests to the upstream server with the fewest active connections.
143+ LoadBalancingTypeLeastConnection LoadBalancingType = "least_conn"
144+
115145 // LoadBalancingTypeIPHash enables IP hash-based load balancing,
116146 // 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
118147 LoadBalancingTypeIPHash LoadBalancingType = "ip_hash"
119148
149+ // LoadBalancingTypeHash enables generic hash-based load balancing,
150+ // routing requests to upstream servers based on a hash of a specified key
151+ // HashKey field must be set when this method is selected.
152+ // Example configuration: hash $binary_remote_addr;.
153+ LoadBalancingTypeHash LoadBalancingType = "hash"
154+
155+ // LoadBalancingTypeHashConsistent enables consistent hash-based load balancing,
156+ // which minimizes the number of keys remapped when a server is added or removed.
157+ // HashKey field must be set when this method is selected.
158+ // Example configuration: hash $binary_remote_addr consistent;.
159+ LoadBalancingTypeHashConsistent LoadBalancingType = "hash consistent"
160+
161+ // LoadBalancingTypeRandom enables random load balancing,
162+ // routing requests to upstream servers in a random manner.
163+ LoadBalancingTypeRandom LoadBalancingType = "random"
164+
165+ // LoadBalancingTypeRandomTwo enables a variation of random load balancing
166+ // that randomly selects two servers and forwards traffic to one of them.
167+ // The default method is least_conn which passes a request to a server with the least number of active connections.
168+ LoadBalancingTypeRandomTwo LoadBalancingType = "random two"
169+
120170 // LoadBalancingTypeRandomTwoLeastConnection enables a variation of least-connections
121171 // balancing that randomly selects two servers and forwards traffic to the one with
122172 // 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
125173 LoadBalancingTypeRandomTwoLeastConnection LoadBalancingType = "random two least_conn"
174+
175+ // LoadBalancingMethods for NGINX Plus along with OSS methods.
176+
177+ // LoadBalancingTypeRandomTwoLeastTimeHeader enables a variation of least-time load balancing
178+ // that randomly selects two servers and forwards traffic to the one with the least
179+ // time to receive the response header.
180+ LoadBalancingTypeRandomTwoLeastTimeHeader LoadBalancingType = "random two least_time=header"
181+
182+ // LoadBalancingTypeRandomTwoLeastTimeLastByte enables a variation of least-time load balancing
183+ // that randomly selects two servers and forwards traffic to the one with the least time
184+ // to receive the full response.
185+ LoadBalancingTypeRandomTwoLeastTimeLastByte LoadBalancingType = "random two least_time=last_byte"
186+
187+ // LoadBalancingTypeLeastTimeHeader enables least-time load balancing,
188+ // routing requests to the upstream server with the least time to receive the response header.
189+ LoadBalancingTypeLeastTimeHeader LoadBalancingType = "least_time header"
190+
191+ // LoadBalancingTypeLeastTimeLastByte enables least-time load balancing,
192+ // routing requests to the upstream server with the least time to receive the full response.
193+ LoadBalancingTypeLeastTimeLastByte LoadBalancingType = "least_time last_byte"
194+
195+ // LoadBalancingTypeLeastTimeHeaderInflight enables least-time load balancing,
196+ // routing requests to the upstream server with the least time to receive the response header,
197+ // considering the incomplete requests.
198+ LoadBalancingTypeLeastTimeHeaderInflight LoadBalancingType = "least_time header inflight"
199+
200+ // LoadBalancingTypeLeastTimeLastByteInflight enables least-time load balancing,
201+ // routing requests to the upstream server with the least time to receive the full response,
202+ // considering the incomplete requests.
203+ LoadBalancingTypeLeastTimeLastByteInflight LoadBalancingType = "least_time last_byte inflight"
126204)
205+
206+ // HashMethodKey defines the key used for hash-based load balancing methods.
207+ // The key must be a valid NGINX variable name starting with '$' followed by lowercase
208+ // letters and underscores only.
209+ // For a full list of NGINX variables,
210+ // refer to: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables
211+ //
212+ // +kubebuilder:validation:Pattern=`^\$[a-z_]+$`
213+ type HashMethodKey string
0 commit comments