Skip to content

Commit 49b16b1

Browse files
committed
add plus method check back
1 parent c40f1f4 commit 49b16b1

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,32 @@ type ServerConfig struct {
170170
DisableSNIHostValidation bool
171171
}
172172

173-
var OSSAllowedLBMethods = map[ngfAPI.LoadBalancingType]struct{}{
174-
ngfAPI.LoadBalancingTypeRoundRobin: {},
175-
ngfAPI.LoadBalancingTypeLeastConnection: {},
176-
ngfAPI.LoadBalancingTypeIPHash: {},
177-
ngfAPI.LoadBalancingTypeRandom: {},
178-
ngfAPI.LoadBalancingTypeHash: {},
179-
ngfAPI.LoadBalancingTypeHashConsistent: {},
180-
ngfAPI.LoadBalancingTypeRandomTwo: {},
181-
ngfAPI.LoadBalancingTypeRandomTwoLeastConnection: {},
182-
}
173+
var (
174+
OSSAllowedLBMethods = 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+
}
184+
185+
PlusAllowedLBMethods = map[ngfAPI.LoadBalancingType]struct{}{
186+
ngfAPI.LoadBalancingTypeRoundRobin: {},
187+
ngfAPI.LoadBalancingTypeLeastConnection: {},
188+
ngfAPI.LoadBalancingTypeIPHash: {},
189+
ngfAPI.LoadBalancingTypeRandom: {},
190+
ngfAPI.LoadBalancingTypeHash: {},
191+
ngfAPI.LoadBalancingTypeHashConsistent: {},
192+
ngfAPI.LoadBalancingTypeRandomTwo: {},
193+
ngfAPI.LoadBalancingTypeRandomTwoLeastConnection: {},
194+
ngfAPI.LoadBalancingTypeLeastTimeHeader: {},
195+
ngfAPI.LoadBalancingTypeLeastTimeLastByte: {},
196+
ngfAPI.LoadBalancingTypeLeastTimeHeaderInflight: {},
197+
ngfAPI.LoadBalancingTypeLeastTimeLastByteInflight: {},
198+
ngfAPI.LoadBalancingTypeRandomTwoLeastTimeHeader: {},
199+
ngfAPI.LoadBalancingTypeRandomTwoLeastTimeLastByte: {},
200+
}
201+
)

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,31 +159,37 @@ func (v Validator) validateUpstreamKeepAlive(
159159

160160
// ValidateLoadBalancingMethod validates the load balancing method for upstream servers.
161161
func (v Validator) validateLoadBalancingMethod(spec ngfAPI.UpstreamSettingsPolicySpec) field.ErrorList {
162-
var allErrs field.ErrorList
163-
fieldPath := field.NewPath("spec")
164-
165162
if spec.LoadBalancingMethod == nil {
166163
return nil
167164
}
168165

169-
if !v.plusEnabled {
170-
if _, ok := httpConfig.OSSAllowedLBMethods[*spec.LoadBalancingMethod]; !ok {
171-
path := fieldPath.Child("loadBalancingMethod")
172-
allErrs = append(allErrs, field.Invalid(
173-
path,
174-
*spec.LoadBalancingMethod,
175-
fmt.Sprintf(
176-
"NGINX OSS only supports the following load balancing methods: %s",
177-
getLoadBalancingMethodList(httpConfig.OSSAllowedLBMethods),
178-
),
179-
))
180-
}
166+
var allErrs field.ErrorList
167+
path := field.NewPath("spec")
168+
lbPath := path.Child("loadBalancingMethod")
169+
170+
allowedMethods := httpConfig.OSSAllowedLBMethods
171+
nginxType := "NGINX OSS"
172+
if v.plusEnabled {
173+
allowedMethods = httpConfig.PlusAllowedLBMethods
174+
nginxType = "NGINX Plus"
175+
}
176+
177+
if _, ok := allowedMethods[*spec.LoadBalancingMethod]; !ok {
178+
allErrs = append(allErrs, field.Invalid(
179+
lbPath,
180+
*spec.LoadBalancingMethod,
181+
fmt.Sprintf(
182+
"%s supports the following load balancing methods: %s",
183+
nginxType,
184+
getLoadBalancingMethodList(allowedMethods),
185+
),
186+
))
181187
}
182188

183189
if spec.HashMethodKey != nil {
184190
hashMethodKey := *spec.HashMethodKey
185191
if err := v.genericValidator.ValidateNginxVariableName(string(hashMethodKey)); err != nil {
186-
path := fieldPath.Child("hashMethodKey")
192+
path := path.Child("hashMethodKey")
187193
allErrs = append(allErrs, field.Invalid(path, hashMethodKey, err.Error()))
188194
}
189195
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func TestValidate_ValidateLoadBalancingMethod(t *testing.T) {
335335
},
336336
expConditions: []conditions.Condition{
337337
conditions.NewPolicyInvalid("spec.loadBalancingMethod: Invalid value: \"least_time last_byte\": " +
338-
"NGINX OSS only supports the following load balancing methods: "),
338+
"NGINX OSS supports the following load balancing methods: "),
339339
},
340340
},
341341
{
@@ -357,9 +357,22 @@ func TestValidate_ValidateLoadBalancingMethod(t *testing.T) {
357357
},
358358
expConditions: []conditions.Condition{
359359
conditions.NewPolicyInvalid("spec.loadBalancingMethod: Invalid value: \"invalid-method\": " +
360-
"NGINX OSS only supports the following load balancing methods: "),
360+
"NGINX OSS supports the following load balancing methods: "),
361361
},
362362
},
363+
{
364+
name: "invalid load balancing method for NGINX Plus",
365+
policy: &ngfAPI.UpstreamSettingsPolicy{
366+
Spec: ngfAPI.UpstreamSettingsPolicySpec{
367+
LoadBalancingMethod: helpers.GetPointer(ngfAPI.LoadBalancingType("invalid-method")),
368+
},
369+
},
370+
expConditions: []conditions.Condition{
371+
conditions.NewPolicyInvalid("spec.loadBalancingMethod: Invalid value: \"invalid-method\": " +
372+
"NGINX Plus supports the following load balancing methods: "),
373+
},
374+
plusEnabled: true,
375+
},
363376
}
364377

365378
for _, test := range tests {

0 commit comments

Comments
 (0)