Skip to content

Commit caf722e

Browse files
committed
Add session persistence support for NGINX Plus users using the sticky cookie directive (#4305)
Problem: Users want to be able to specify session persistence for their upstreams. Solution: Add support for session persistence using sticky cookie directives which is only available for NGINX Plus users.
1 parent 1923ab5 commit caf722e

26 files changed

+2050
-272
lines changed

internal/controller/manager.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ func StartManager(cfg config.Config) error {
140140
GenericValidator: genericValidator,
141141
PolicyValidator: policyManager,
142142
},
143-
EventRecorder: recorder,
144-
MustExtractGVK: mustExtractGVK,
145-
PlusSecrets: plusSecrets,
146-
ExperimentalFeatures: cfg.ExperimentalFeatures,
143+
EventRecorder: recorder,
144+
MustExtractGVK: mustExtractGVK,
145+
PlusSecrets: plusSecrets,
146+
FeatureFlags: graph.FeatureFlags{
147+
Plus: cfg.Plus,
148+
Experimental: cfg.ExperimentalFeatures,
149+
},
147150
})
148151

149152
var handlerCollector handlerMetricsCollector = collectors.NewControllerNoopCollector()

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const (
120120

121121
// Upstream holds all configuration for an HTTP upstream.
122122
type Upstream struct {
123+
SessionPersistence UpstreamSessionPersistence
123124
Name string
124125
ZoneSize string // format: 512k, 1m
125126
StateFile string
@@ -129,6 +130,14 @@ type Upstream struct {
129130
Servers []UpstreamServer
130131
}
131132

133+
// UpstreamSessionPersistence holds the session persistence configuration for an upstream.
134+
type UpstreamSessionPersistence struct {
135+
Name string
136+
Expiry string
137+
Path string
138+
SessionType string
139+
}
140+
132141
// UpstreamKeepAlive holds the keepalive configuration for an HTTP upstream.
133142
type UpstreamKeepAlive struct {
134143
Time string

internal/controller/nginx/config/upstreams.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (g GeneratorImpl) createUpstream(
147147
processor upstreamsettings.Processor,
148148
) http.Upstream {
149149
var stateFile string
150+
var sp http.UpstreamSessionPersistence
150151
upstreamPolicySettings := processor.Process(up.Policies)
151152

152153
zoneSize := ossZoneSize
@@ -155,8 +156,14 @@ func (g GeneratorImpl) createUpstream(
155156
// Only set state file if the upstream doesn't have resolve servers
156157
// Upstreams with resolve servers can't be managed via NGINX Plus API
157158
if !upstreamHasResolveServers(up) {
158-
stateFile = fmt.Sprintf("%s/%s.conf", stateDir, up.Name)
159+
base := up.StateFileKey
160+
if base == "" {
161+
base = up.Name
162+
}
163+
stateFile = fmt.Sprintf("%s/%s.conf", stateDir, base)
159164
}
165+
166+
sp = getSessionPersistenceConfiguration(up.SessionPersistence)
160167
}
161168

162169
if upstreamPolicySettings.ZoneSize != "" {
@@ -212,6 +219,7 @@ func (g GeneratorImpl) createUpstream(
212219
Servers: upstreamServers,
213220
KeepAlive: upstreamPolicySettings.KeepAlive,
214221
LoadBalancingMethod: chosenLBMethod,
222+
SessionPersistence: sp,
215223
}
216224
}
217225

@@ -236,3 +244,17 @@ func upstreamHasResolveServers(upstream dataplane.Upstream) bool {
236244
}
237245
return false
238246
}
247+
248+
// getSessionPersistenceConfiguration gets the session persistence configuration for an upstream.
249+
// Supported only for NGINX Plus and cookie-based type.
250+
func getSessionPersistenceConfiguration(sp dataplane.SessionPersistenceConfig) http.UpstreamSessionPersistence {
251+
if sp.Name == "" {
252+
return http.UpstreamSessionPersistence{}
253+
}
254+
return http.UpstreamSessionPersistence{
255+
Name: sp.Name,
256+
Expiry: sp.Expiry,
257+
Path: sp.Path,
258+
SessionType: string(sp.SessionType),
259+
}
260+
}

internal/controller/nginx/config/upstreams_template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ upstream {{ $u.Name }} {
1717
zone {{ $u.Name }} {{ $u.ZoneSize }};
1818
{{ end -}}
1919
20+
{{ if $u.SessionPersistence.Name -}}
21+
sticky {{ $u.SessionPersistence.SessionType }} {{ $u.SessionPersistence.Name }}
22+
{{- if $u.SessionPersistence.Expiry }} expires={{ $u.SessionPersistence.Expiry }}{{- end }}
23+
{{- if $u.SessionPersistence.Path }} path={{ $u.SessionPersistence.Path }}{{- end }};
24+
{{ end -}}
25+
2026
{{- if $u.StateFile }}
2127
state {{ $u.StateFile }};
2228
{{- else }}

0 commit comments

Comments
 (0)