Skip to content

Commit eabc8c9

Browse files
authored
Add NGINX configuration for UpstreamSettingsPolicy (#2877)
Translate data plane intermediary UpstreamSettingsPolicy configuration into NGINX configuration. Problem: I want the data plane configuration generated from my UpstreamSettingsPolicy to be translated into NGINX Configuration. Solution: Translate the data plane UpstreamSettingsPolicy configuration into NGINX configuration. Testing: Unit tests.
1 parent fe8b4dc commit eabc8c9

File tree

12 files changed

+1398
-274
lines changed

12 files changed

+1398
-274
lines changed

apis/v1alpha1/policy_methods.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ func (p *ObservabilityPolicy) GetPolicyStatus() v1alpha2.PolicyStatus {
3131
func (p *ObservabilityPolicy) SetPolicyStatus(status v1alpha2.PolicyStatus) {
3232
p.Status = status
3333
}
34+
35+
func (p *UpstreamSettingsPolicy) GetTargetRefs() []v1alpha2.LocalPolicyTargetReference {
36+
return p.Spec.TargetRefs
37+
}
38+
39+
func (p *UpstreamSettingsPolicy) GetPolicyStatus() v1alpha2.PolicyStatus {
40+
return p.Status
41+
}
42+
43+
func (p *UpstreamSettingsPolicy) SetPolicyStatus(status v1alpha2.PolicyStatus) {
44+
p.Status = status
45+
}

config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.16.2
6+
controller-gen.kubebuilder.io/version: v0.16.5
77
labels:
88
gateway.networking.k8s.io/policy: direct
99
name: upstreamsettingspolicies.gateway.nginx.org
@@ -76,13 +76,13 @@ spec:
7676
Time defines the maximum time during which requests can be processed through one keep-alive connection.
7777
After this time is reached, the connection is closed following the subsequent request processing.
7878
Directive: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_time
79-
pattern: ^\d{1,4}(ms|s)?$
79+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
8080
type: string
8181
timeout:
8282
description: |-
8383
Timeout defines the keep-alive timeout for upstreams.
8484
Directive: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_timeout
85-
pattern: ^\d{1,4}(ms|s)?$
85+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
8686
type: string
8787
type: object
8888
targetRefs:

internal/mode/static/nginx/config/generator.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"github.com/go-logr/logr"
77

88
ngfConfig "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
9+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/http"
910
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies"
1011
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/clientsettings"
1112
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/observability"
13+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/upstreamsettings"
1214
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
1315
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
1416
)
@@ -131,7 +133,10 @@ func (g GeneratorImpl) executeConfigTemplates(
131133
) []file.File {
132134
fileBytes := make(map[string][]byte)
133135

134-
for _, execute := range g.getExecuteFuncs(generator) {
136+
httpUpstreams := g.createUpstreams(conf.Upstreams, upstreamsettings.NewProcessor())
137+
keepAliveCheck := newKeepAliveChecker(httpUpstreams)
138+
139+
for _, execute := range g.getExecuteFuncs(generator, httpUpstreams, keepAliveCheck) {
135140
results := execute(conf)
136141
for _, res := range results {
137142
fileBytes[res.dest] = append(fileBytes[res.dest], res.data...)
@@ -156,12 +161,16 @@ func (g GeneratorImpl) executeConfigTemplates(
156161
return files
157162
}
158163

159-
func (g GeneratorImpl) getExecuteFuncs(generator policies.Generator) []executeFunc {
164+
func (g GeneratorImpl) getExecuteFuncs(
165+
generator policies.Generator,
166+
upstreams []http.Upstream,
167+
keepAliveCheck keepAliveChecker,
168+
) []executeFunc {
160169
return []executeFunc{
161170
executeMainConfig,
162171
executeBaseHTTPConfig,
163-
g.newExecuteServersFunc(generator),
164-
g.executeUpstreams,
172+
g.newExecuteServersFunc(generator, keepAliveCheck),
173+
newExecuteUpstreamsFunc(upstreams),
165174
executeSplitClients,
166175
executeMaps,
167176
executeTelemetry,

internal/mode/static/nginx/config/http/config.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package http
22

3-
import "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/shared"
3+
import (
4+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/shared"
5+
)
46

57
const (
68
InternalRoutePathPrefix = "/_ngf-internal"
@@ -82,9 +84,18 @@ const (
8284

8385
// Upstream holds all configuration for an HTTP upstream.
8486
type Upstream struct {
85-
Name string
86-
ZoneSize string // format: 512k, 1m
87-
Servers []UpstreamServer
87+
Name string
88+
ZoneSize string // format: 512k, 1m
89+
KeepAlive UpstreamKeepAlive
90+
Servers []UpstreamServer
91+
}
92+
93+
// UpstreamKeepAlive holds the keepalive configuration for an HTTP upstream.
94+
type UpstreamKeepAlive struct {
95+
Time string
96+
Timeout string
97+
Connections int32
98+
Requests int32
8899
}
89100

90101
// UpstreamServer holds all configuration for an HTTP upstream server.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package upstreamsettings
2+
3+
import (
4+
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
5+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/http"
6+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies"
7+
)
8+
9+
// Processor processes UpstreamSettingsPolicies.
10+
type Processor struct{}
11+
12+
// UpstreamSettings contains settings from UpstreamSettingsPolicy.
13+
type UpstreamSettings struct {
14+
// ZoneSize is the zone size setting.
15+
ZoneSize string
16+
// KeepAlive contains the keepalive settings.
17+
KeepAlive http.UpstreamKeepAlive
18+
}
19+
20+
// NewProcessor returns a new Processor.
21+
func NewProcessor() Processor {
22+
return Processor{}
23+
}
24+
25+
// Process processes policies into an UpstreamSettings object. The policies are already validated and are guaranteed
26+
// to not contain overlapping settings. This method merges all fields in the policies into a single UpstreamSettings
27+
// object.
28+
func (g Processor) Process(pols []policies.Policy) UpstreamSettings {
29+
return processPolicies(pols)
30+
}
31+
32+
func processPolicies(pols []policies.Policy) UpstreamSettings {
33+
upstreamSettings := UpstreamSettings{}
34+
35+
for _, pol := range pols {
36+
usp, ok := pol.(*ngfAPI.UpstreamSettingsPolicy)
37+
if !ok {
38+
continue
39+
}
40+
41+
// we can assume that there will be no instance of two or more policies setting the same
42+
// field for the same service
43+
if usp.Spec.ZoneSize != nil {
44+
upstreamSettings.ZoneSize = string(*usp.Spec.ZoneSize)
45+
}
46+
47+
if usp.Spec.KeepAlive != nil {
48+
if usp.Spec.KeepAlive.Connections != nil {
49+
upstreamSettings.KeepAlive.Connections = *usp.Spec.KeepAlive.Connections
50+
}
51+
52+
if usp.Spec.KeepAlive.Requests != nil {
53+
upstreamSettings.KeepAlive.Requests = *usp.Spec.KeepAlive.Requests
54+
}
55+
56+
if usp.Spec.KeepAlive.Time != nil {
57+
upstreamSettings.KeepAlive.Time = string(*usp.Spec.KeepAlive.Time)
58+
}
59+
60+
if usp.Spec.KeepAlive.Timeout != nil {
61+
upstreamSettings.KeepAlive.Timeout = string(*usp.Spec.KeepAlive.Timeout)
62+
}
63+
}
64+
}
65+
66+
return upstreamSettings
67+
}

0 commit comments

Comments
 (0)