Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/controller/nginx/config/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ const (

// Upstream holds all configuration for an HTTP upstream.
type Upstream struct {
Name string
ZoneSize string // format: 512k, 1m
StateFile string
KeepAlive UpstreamKeepAlive
Servers []UpstreamServer
Name string
ZoneSize string // format: 512k, 1m
StateFile string
LoadBalancingMethod string
KeepAlive UpstreamKeepAlive
Servers []UpstreamServer
}

// UpstreamKeepAlive holds the keepalive configuration for an HTTP upstream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Processor struct{}
type UpstreamSettings struct {
// ZoneSize is the zone size setting.
ZoneSize string
// LoadBalancingMethod is the load balancing method setting.
LoadBalancingMethod string
// KeepAlive contains the keepalive settings.
KeepAlive http.UpstreamKeepAlive
}
Expand Down Expand Up @@ -61,6 +63,10 @@ func processPolicies(pols []policies.Policy) UpstreamSettings {
upstreamSettings.KeepAlive.Timeout = string(*usp.Spec.KeepAlive.Timeout)
}
}

if usp.Spec.LoadBalancingMethod != nil {
upstreamSettings.LoadBalancingMethod = string(*usp.Spec.LoadBalancingMethod)
}
}

return upstreamSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestProcess(t *testing.T) {
Time: helpers.GetPointer[ngfAPIv1alpha1.Duration]("5s"),
Timeout: helpers.GetPointer[ngfAPIv1alpha1.Duration]("10s"),
}),
LoadBalancingMethod: helpers.GetPointer(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
},
Expand All @@ -48,6 +49,24 @@ func TestProcess(t *testing.T) {
Time: "5s",
Timeout: "10s",
},
LoadBalancingMethod: string(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
{
name: "load balancing method set",
policies: []policies.Policy{
&ngfAPIv1alpha1.UpstreamSettingsPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "usp",
Namespace: "test",
},
Spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
LoadBalancingMethod: helpers.GetPointer(ngfAPIv1alpha1.LoadBalancingTypeRandomTwoLeastConnection),
},
},
},
expUpstreamSettings: UpstreamSettings{
LoadBalancingMethod: string(ngfAPIv1alpha1.LoadBalancingTypeRandomTwoLeastConnection),
},
},
{
Expand Down Expand Up @@ -220,6 +239,15 @@ func TestProcess(t *testing.T) {
}),
},
},
&ngfAPIv1alpha1.UpstreamSettingsPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "usp-loadBalancingMethod",
Namespace: "test",
},
Spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
LoadBalancingMethod: helpers.GetPointer(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
},
expUpstreamSettings: UpstreamSettings{
ZoneSize: "2m",
Expand All @@ -229,6 +257,7 @@ func TestProcess(t *testing.T) {
Time: "5s",
Timeout: "10s",
},
LoadBalancingMethod: string(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
{
Expand Down Expand Up @@ -310,6 +339,15 @@ func TestProcess(t *testing.T) {
},
},
},
&ngfAPIv1alpha1.UpstreamSettingsPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "usp-lb-method",
Namespace: "test",
},
Spec: ngfAPIv1alpha1.UpstreamSettingsPolicySpec{
LoadBalancingMethod: helpers.GetPointer(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
},
expUpstreamSettings: UpstreamSettings{
ZoneSize: "2m",
Expand All @@ -319,6 +357,7 @@ func TestProcess(t *testing.T) {
Time: "5s",
Timeout: "10s",
},
LoadBalancingMethod: string(ngfAPIv1alpha1.LoadBalancingTypeIPHash),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func conflicts(a, b ngfAPI.UpstreamSettingsPolicySpec) bool {
}
}

if a.LoadBalancingMethod != nil && b.LoadBalancingMethod != nil {
return true
}

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func createValidPolicy() *ngfAPI.UpstreamSettingsPolicy {
Timeout: helpers.GetPointer[ngfAPI.Duration]("30s"),
Connections: helpers.GetPointer[int32](100),
},
LoadBalancingMethod: helpers.GetPointer(ngfAPI.LoadBalancingTypeRandomTwoLeastConnection),
},
Status: v1.PolicyStatus{},
}
Expand Down Expand Up @@ -176,6 +177,7 @@ func TestValidator_Conflicts(t *testing.T) {
Requests: helpers.GetPointer[int32](900),
Time: helpers.GetPointer[ngfAPI.Duration]("50s"),
},
LoadBalancingMethod: helpers.GetPointer(ngfAPI.LoadBalancingTypeRandomTwoLeastConnection),
},
},
polB: &ngfAPI.UpstreamSettingsPolicy{
Expand Down Expand Up @@ -246,6 +248,16 @@ func TestValidator_Conflicts(t *testing.T) {
},
conflicts: true,
},
{
name: "load balancing method conflicts",
polA: createValidPolicy(),
polB: &ngfAPI.UpstreamSettingsPolicy{
Spec: ngfAPI.UpstreamSettingsPolicySpec{
LoadBalancingMethod: helpers.GetPointer(ngfAPI.LoadBalancingTypeIPHash),
},
},
conflicts: true,
},
}

v := upstreamsettings.NewValidator(nil)
Expand Down
18 changes: 13 additions & 5 deletions internal/controller/nginx/config/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
plusZoneSizeStream = "1m"
// stateDir is the directory for storing state files.
stateDir = "/var/lib/nginx/state"
// default load balancing method.
defaultLBMethod = "random two least_conn"
)

// keepAliveChecker takes an upstream name and returns if it has keep alive settings enabled.
Expand Down Expand Up @@ -185,12 +187,18 @@ func (g GeneratorImpl) createUpstream(
}
}

chosenLBMethod := defaultLBMethod
if upstreamPolicySettings.LoadBalancingMethod != "" {
chosenLBMethod = upstreamPolicySettings.LoadBalancingMethod
}

return http.Upstream{
Name: up.Name,
ZoneSize: zoneSize,
StateFile: stateFile,
Servers: upstreamServers,
KeepAlive: upstreamPolicySettings.KeepAlive,
Name: up.Name,
ZoneSize: zoneSize,
StateFile: stateFile,
Servers: upstreamServers,
KeepAlive: upstreamPolicySettings.KeepAlive,
LoadBalancingMethod: chosenLBMethod,
}
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/nginx/config/upstreams_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ package config
const upstreamsTemplateText = `
{{ range $u := . }}
upstream {{ $u.Name }} {
random two least_conn;
{{ if $u.LoadBalancingMethod -}}
{{ $u.LoadBalancingMethod }};
{{- end }}
{{ if $u.ZoneSize -}}
zone {{ $u.Name }} {{ $u.ZoneSize }};
{{ end -}}
Expand Down
Loading
Loading