Skip to content

Commit f3bddb7

Browse files
committed
move load balancing checks to generic validator
1 parent fbfbd6c commit f3bddb7

29 files changed

+263
-425
lines changed

apis/v1alpha1/upstreamsettingspolicy_types.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ 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'"
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'"
4040
//
4141
//nolint:lll
4242
type UpstreamSettingsPolicySpec struct {
@@ -61,11 +61,11 @@ type UpstreamSettingsPolicySpec struct {
6161
// +optional
6262
LoadBalancingMethod *LoadBalancingType `json:"loadBalancingMethod,omitempty"`
6363

64-
// HashKey defines the key used for hash-based load balancing methods.
64+
// HashMethodKey defines the key used for hash-based load balancing methods.
6565
// This field is required when `LoadBalancingMethod` is set to `hash` or `hash consistent`.
6666
//
6767
// +optional
68-
HashKey *HashMethodKey `json:"hashKey,omitempty"`
68+
HashMethodKey *HashMethodKey `json:"hashMethodKey,omitempty"`
6969

7070
// TargetRefs identifies API object(s) to apply the policy to.
7171
// Objects must be in the same namespace as the policy.
@@ -135,7 +135,6 @@ const (
135135

136136
// LoadBalancingTypeRoundRobin enables round-robin load balancing,
137137
// distributing requests evenly across all upstream servers.
138-
// NGINX defaults to this method if no load balancing method is specified.
139138
LoadBalancingTypeRoundRobin LoadBalancingType = "round_robin"
140139

141140
// LoadBalancingTypeLeastConn enables least-connections load balancing,
@@ -148,13 +147,13 @@ const (
148147

149148
// LoadBalancingTypeHash enables generic hash-based load balancing,
150149
// routing requests to upstream servers based on a hash of a specified key
151-
// HashKey field must be set when this method is selected.
150+
// HashMethodKey field must be set when this method is selected.
152151
// Example configuration: hash $binary_remote_addr;.
153152
LoadBalancingTypeHash LoadBalancingType = "hash"
154153

155154
// LoadBalancingTypeHashConsistent enables consistent hash-based load balancing,
156155
// 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.
156+
// HashMethodKey field must be set when this method is selected.
158157
// Example configuration: hash $binary_remote_addr consistent;.
159158
LoadBalancingTypeHashConsistent LoadBalancingType = "hash consistent"
160159

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ spec:
5151
spec:
5252
description: Spec defines the desired state of the UpstreamSettingsPolicy.
5353
properties:
54-
hashKey:
54+
hashMethodKey:
5555
description: |-
56-
HashKey defines the key used for hash-based load balancing methods.
56+
HashMethodKey defines the key used for hash-based load balancing methods.
5757
This field is required when `LoadBalancingMethod` is set to `hash` or `hash consistent`.
5858
pattern: ^\$[a-z_]+$
5959
type: string
@@ -171,11 +171,11 @@ spec:
171171
- targetRefs
172172
type: object
173173
x-kubernetes-validations:
174-
- message: hashKey is required when loadBalancingMethod is 'hash' or 'hash
175-
consistent'
174+
- message: hashMethodKey is required when loadBalancingMethod is 'hash'
175+
or 'hash consistent'
176176
rule: '!(has(self.loadBalancingMethod) && (self.loadBalancingMethod
177177
== ''hash'' || self.loadBalancingMethod == ''hash consistent'')) ||
178-
has(self.hashKey)'
178+
has(self.hashMethodKey)'
179179
status:
180180
description: Status defines the state of the UpstreamSettingsPolicy.
181181
properties:

deploy/crds.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9578,9 +9578,9 @@ spec:
95789578
spec:
95799579
description: Spec defines the desired state of the UpstreamSettingsPolicy.
95809580
properties:
9581-
hashKey:
9581+
hashMethodKey:
95829582
description: |-
9583-
HashKey defines the key used for hash-based load balancing methods.
9583+
HashMethodKey defines the key used for hash-based load balancing methods.
95849584
This field is required when `LoadBalancingMethod` is set to `hash` or `hash consistent`.
95859585
pattern: ^\$[a-z_]+$
95869586
type: string
@@ -9698,11 +9698,11 @@ spec:
96989698
- targetRefs
96999699
type: object
97009700
x-kubernetes-validations:
9701-
- message: hashKey is required when loadBalancingMethod is 'hash' or 'hash
9702-
consistent'
9701+
- message: hashMethodKey is required when loadBalancingMethod is 'hash'
9702+
or 'hash consistent'
97039703
rule: '!(has(self.loadBalancingMethod) && (self.loadBalancingMethod
97049704
== ''hash'' || self.loadBalancingMethod == ''hash consistent'')) ||
9705-
has(self.hashKey)'
9705+
has(self.hashMethodKey)'
97069706
status:
97079707
description: Status defines the state of the UpstreamSettingsPolicy.
97089708
properties:

internal/controller/manager.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,13 @@ func StartManager(cfg config.Config) error {
124124
mustExtractGVK := kinds.NewMustExtractGKV(scheme)
125125

126126
genericValidator := ngxvalidation.GenericValidator{}
127-
policyManager := createPolicyManager(mustExtractGVK, genericValidator)
127+
policyManager := createPolicyManager(mustExtractGVK, cfg.Plus, genericValidator)
128128

129129
plusSecrets, err := createPlusSecretMetadata(cfg, mgr.GetAPIReader())
130130
if err != nil {
131131
return err
132132
}
133133

134-
flags := graph.Flags{
135-
Plus: cfg.Plus,
136-
Experimental: cfg.ExperimentalFeatures,
137-
}
138-
139134
processor := state.NewChangeProcessorImpl(state.ChangeProcessorConfig{
140135
GatewayCtlrName: cfg.GatewayCtlrName,
141136
GatewayClassName: cfg.GatewayClassName,
@@ -145,10 +140,10 @@ func StartManager(cfg config.Config) error {
145140
GenericValidator: genericValidator,
146141
PolicyValidator: policyManager,
147142
},
148-
EventRecorder: recorder,
149-
MustExtractGVK: mustExtractGVK,
150-
PlusSecrets: plusSecrets,
151-
Flags: flags,
143+
EventRecorder: recorder,
144+
MustExtractGVK: mustExtractGVK,
145+
PlusSecrets: plusSecrets,
146+
ExperimentalFeatures: cfg.ExperimentalFeatures,
152147
})
153148

154149
var handlerCollector handlerMetricsCollector = collectors.NewControllerNoopCollector()
@@ -327,6 +322,7 @@ func StartManager(cfg config.Config) error {
327322

328323
func createPolicyManager(
329324
mustExtractGVK kinds.MustExtractGVK,
325+
plusEnabled bool,
330326
validator validation.GenericValidator,
331327
) *policies.CompositeValidator {
332328
cfgs := []policies.ManagerConfig{
@@ -340,7 +336,7 @@ func createPolicyManager(
340336
},
341337
{
342338
GVK: mustExtractGVK(&ngfAPIv1alpha1.UpstreamSettingsPolicy{}),
343-
Validator: upstreamsettings.NewValidator(validator),
339+
Validator: upstreamsettings.NewValidator(validator, plusEnabled),
344340
},
345341
}
346342

internal/controller/nginx/config/http/config.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package http
22

33
import (
4-
ngfAPI "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
54
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config/shared"
65
)
76

@@ -124,7 +123,7 @@ type Upstream struct {
124123
ZoneSize string // format: 512k, 1m
125124
StateFile string
126125
LoadBalancingMethod string
127-
HashKey string
126+
HashMethodKey string
128127
KeepAlive UpstreamKeepAlive
129128
Servers []UpstreamServer
130129
}
@@ -169,33 +168,3 @@ type ServerConfig struct {
169168
Plus bool
170169
DisableSNIHostValidation bool
171170
}
172-
173-
var (
174-
PlusAllowedLBMethods = map[ngfAPI.LoadBalancingType]struct{}{
175-
ngfAPI.LoadBalancingTypeRoundRobin: {},
176-
ngfAPI.LoadBalancingTypeLeastConnection: {},
177-
ngfAPI.LoadBalancingTypeIPHash: {},
178-
ngfAPI.LoadBalancingTypeRandom: {},
179-
ngfAPI.LoadBalancingTypeHash: {},
180-
ngfAPI.LoadBalancingTypeHashConsistent: {},
181-
ngfAPI.LoadBalancingTypeRandomTwo: {},
182-
ngfAPI.LoadBalancingTypeRandomTwoLeastConnection: {},
183-
ngfAPI.LoadBalancingTypeLeastTimeHeader: {},
184-
ngfAPI.LoadBalancingTypeLeastTimeLastByte: {},
185-
ngfAPI.LoadBalancingTypeLeastTimeHeaderInflight: {},
186-
ngfAPI.LoadBalancingTypeLeastTimeLastByteInflight: {},
187-
ngfAPI.LoadBalancingTypeRandomTwoLeastTimeHeader: {},
188-
ngfAPI.LoadBalancingTypeRandomTwoLeastTimeLastByte: {},
189-
}
190-
191-
OSSAllowedLBMethods = map[ngfAPI.LoadBalancingType]struct{}{
192-
ngfAPI.LoadBalancingTypeRoundRobin: {},
193-
ngfAPI.LoadBalancingTypeLeastConnection: {},
194-
ngfAPI.LoadBalancingTypeIPHash: {},
195-
ngfAPI.LoadBalancingTypeRandom: {},
196-
ngfAPI.LoadBalancingTypeHash: {},
197-
ngfAPI.LoadBalancingTypeHashConsistent: {},
198-
ngfAPI.LoadBalancingTypeRandomTwo: {},
199-
ngfAPI.LoadBalancingTypeRandomTwoLeastConnection: {},
200-
}
201-
)

internal/controller/nginx/config/policies/clientsettings/validator.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ func (v *Validator) ValidateGlobalSettings(
5050
return nil
5151
}
5252

53-
// ValidateLoadBalancingMethod validates the load balancing method for upstream servers.
54-
func (v *Validator) ValidateLoadBalancingMethod(
55-
_ policies.Policy,
56-
_ bool,
57-
) []conditions.Condition {
58-
return nil
59-
}
60-
6153
// Conflicts returns true if the two ClientSettingsPolicies conflict.
6254
func (v *Validator) Conflicts(polA, polB policies.Policy) bool {
6355
cspA := helpers.MustCastObject[*ngfAPI.ClientSettingsPolicy](polA)

internal/controller/nginx/config/policies/observability/validator.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,3 @@ func (v *Validator) validateSettings(spec ngfAPIv1alpha2.ObservabilityPolicySpec
141141

142142
return allErrs.ToAggregate()
143143
}
144-
145-
// ValidateLoadBalancingMethod validates the load balancing method for upstream servers.
146-
func (v *Validator) ValidateLoadBalancingMethod(
147-
_ policies.Policy,
148-
_ bool,
149-
) []conditions.Condition {
150-
return nil
151-
}

internal/controller/nginx/config/policies/policiesfakes/fake_validator.go

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

internal/controller/nginx/config/policies/upstreamsettings/processor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type UpstreamSettings struct {
1515
ZoneSize string
1616
// LoadBalancingMethod is the load balancing method setting.
1717
LoadBalancingMethod string
18-
// HashKey is the key to be used for hash-based load balancing methods.
19-
HashKey string
18+
// HashMethodKey is the key to be used for hash-based load balancing methods.
19+
HashMethodKey string
2020
// KeepAlive contains the keepalive settings.
2121
KeepAlive http.UpstreamKeepAlive
2222
}
@@ -70,8 +70,8 @@ func processPolicies(pols []policies.Policy) UpstreamSettings {
7070
upstreamSettings.LoadBalancingMethod = string(*usp.Spec.LoadBalancingMethod)
7171
}
7272

73-
if usp.Spec.HashKey != nil {
74-
upstreamSettings.HashKey = string(*usp.Spec.HashKey)
73+
if usp.Spec.HashMethodKey != nil {
74+
upstreamSettings.HashMethodKey = string(*usp.Spec.HashMethodKey)
7575
}
7676
}
7777

0 commit comments

Comments
 (0)