Skip to content

Commit fef5236

Browse files
committed
update upstream settings policy API
1 parent e1e2e73 commit fef5236

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

apis/v1alpha1/upstreamsettingspolicy_types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type UpstreamSettingsPolicySpec struct {
5151
// +optional
5252
KeepAlive *UpstreamKeepAlive `json:"keepAlive,omitempty"`
5353

54+
// LoadBalancingMethod specifies the load balancing algorithm to be used for the upstream.
55+
//
56+
// +optional
57+
// +kubebuilder:default:=random two least_conn
58+
LoadBalancingMethod *LoadBalancingType `json:"loadBalancingMethod,omitempty"`
59+
5460
// TargetRefs identifies API object(s) to apply the policy to.
5561
// Objects must be in the same namespace as the policy.
5662
// Support: Service
@@ -98,3 +104,22 @@ type UpstreamKeepAlive struct {
98104
// +optional
99105
Timeout *Duration `json:"timeout,omitempty"`
100106
}
107+
108+
// LoadBalancingType defines the supported load balancing methods.
109+
//
110+
// +kubebuilder:validation:Enum=ip_hash;random two least_conn
111+
type LoadBalancingType string
112+
113+
const (
114+
// LoadBalancingTypeIPHash enables IP hash-based load balancing,
115+
// ensuring requests from the same client IP are routed to the same upstream server.
116+
// NGINX directive: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash
117+
LoadBalancingTypeIPHash LoadBalancingType = "ip_hash"
118+
119+
// LoadBalancingTypeRandomTwoLeastConnection enables a variation of least-connections
120+
// balancing that randomly selects two servers and forwards traffic to the one with
121+
// fewer active connections.
122+
// NGINX directive least_conn: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#least_conn
123+
// NGINX directive random: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#random
124+
LoadBalancingTypeRandomTwoLeastConnection LoadBalancingType = "random two least_conn"
125+
)

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ spec:
8585
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
8686
type: string
8787
type: object
88+
loadBalancingMethod:
89+
default: random two least_conn
90+
description: LoadBalancingMethod specifies the load balancing algorithm
91+
to be used for the upstream.
92+
enum:
93+
- ip_hash
94+
- random two least_conn
95+
type: string
8896
targetRefs:
8997
description: |-
9098
TargetRefs identifies API object(s) to apply the policy to.

deploy/crds.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9595,6 +9595,14 @@ spec:
95959595
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
95969596
type: string
95979597
type: object
9598+
loadBalancingMethod:
9599+
default: random two least_conn
9600+
description: LoadBalancingMethod specifies the load balancing algorithm
9601+
to be used for the upstream.
9602+
enum:
9603+
- ip_hash
9604+
- random two least_conn
9605+
type: string
95989606
targetRefs:
95999607
description: |-
96009608
TargetRefs identifies API object(s) to apply the policy to.

tests/cel/upstreamsettingspolicy_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,64 @@ func TestUpstreamSettingsPolicyTargetRefNameUniqueness(t *testing.T) {
372372
})
373373
}
374374
}
375+
376+
func TestUpstreamSettingsPolicy_LoadBalancingMethodTypes(t *testing.T) {
377+
t.Parallel()
378+
k8sClient := getKubernetesClient(t)
379+
380+
convertLBType := func(s ngfAPIv1alpha1.LoadBalancingType) *ngfAPIv1alpha1.LoadBalancingType {
381+
return &s
382+
}
383+
384+
tests := []struct {
385+
spec ngfAPIv1alpha1.UpstreamSettingsPolicySpec
386+
name string
387+
wantErrors []string
388+
}{
389+
{
390+
name: "Valid LoadBalancingType 'random two least_conn' is allowed",
391+
spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
392+
LoadBalancingMethod: convertLBType(ngfAPIv1alpha1.LoadBalancingTypeRandomTwoLeastConnection),
393+
},
394+
},
395+
{
396+
name: "Valid LoadBalancingType 'ip_hash' is allowed",
397+
spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
398+
LoadBalancingMethod: convertLBType(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
399+
},
400+
},
401+
{
402+
name: "Invalid LoadBalancingType 'invalid_type' is not allowed",
403+
wantErrors: []string{
404+
"spec.loadBalancingMethod: Unsupported value: " +
405+
"\"invalid_type\": supported values: \"ip_hash\", \"random two least_conn\"",
406+
},
407+
spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
408+
LoadBalancingMethod: convertLBType("invalid_type"),
409+
},
410+
},
411+
}
412+
413+
for _, tt := range tests {
414+
t.Run(tt.name, func(t *testing.T) {
415+
t.Parallel()
416+
417+
tt.spec.TargetRefs = []gatewayv1alpha2.LocalPolicyTargetReference{
418+
{
419+
Kind: serviceKind,
420+
Group: coreGroup,
421+
Name: gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName)),
422+
},
423+
}
424+
425+
upstreamSettingsPolicy := &ngfAPIv1alpha1.UpstreamSettingsPolicy{
426+
ObjectMeta: controllerruntime.ObjectMeta{
427+
Name: uniqueResourceName(testResourceName),
428+
Namespace: defaultNamespace,
429+
},
430+
Spec: tt.spec,
431+
}
432+
validateCrd(t, tt.wantErrors, upstreamSettingsPolicy, k8sClient)
433+
})
434+
}
435+
}

0 commit comments

Comments
 (0)