Skip to content

Commit ad3e324

Browse files
committed
update backends and upstreams with a prefix
1 parent e044a07 commit ad3e324

File tree

11 files changed

+103
-89
lines changed

11 files changed

+103
-89
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,15 @@ func createProxyPass(
738738
}
739739

740740
backendName := backendGroupName(backendGroup)
741+
// The proxy_pass directive value starts with the namespace name of the backend group.
742+
// We prefix it with "upstream_" to avoid potential situation where the namespace name starts
743+
// with a number, which is not allowed in a variable name in nginx.conf
744+
updatedBackendName := shared.UpstreamPrefix + backendName
741745
if backendGroupNeedsSplit(backendGroup) {
742-
return protocol + "://$" + convertStringToSafeVariableName(backendName) + requestURI
746+
return protocol + "://$" + convertStringToSafeVariableName(updatedBackendName) + requestURI
743747
}
744748

745-
return protocol + "://" + backendName + requestURI
749+
return protocol + "://" + updatedBackendName + requestURI
746750
}
747751

748752
func createMatchLocation(path string, grpc bool) http.Location {

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,21 +1123,21 @@ func TestCreateServers(t *testing.T) {
11231123
},
11241124
{
11251125
Path: "/_ngf-internal-rule0-route0",
1126-
ProxyPass: "http://test_foo_80$request_uri",
1126+
ProxyPass: "http://upstream_test_foo_80$request_uri",
11271127
ProxySetHeaders: httpBaseHeaders,
11281128
Type: http.InternalLocationType,
11291129
Includes: internalIncludes,
11301130
},
11311131
{
11321132
Path: "/_ngf-internal-rule0-route1",
1133-
ProxyPass: "http://test_foo_80$request_uri",
1133+
ProxyPass: "http://upstream_test_foo_80$request_uri",
11341134
ProxySetHeaders: httpBaseHeaders,
11351135
Type: http.InternalLocationType,
11361136
Includes: internalIncludes,
11371137
},
11381138
{
11391139
Path: "/_ngf-internal-rule0-route2",
1140-
ProxyPass: "http://test_foo_80$request_uri",
1140+
ProxyPass: "http://upstream_test_foo_80$request_uri",
11411141
ProxySetHeaders: httpBaseHeaders,
11421142
Type: http.InternalLocationType,
11431143
Includes: internalIncludes,
@@ -1150,28 +1150,28 @@ func TestCreateServers(t *testing.T) {
11501150
},
11511151
{
11521152
Path: "/_ngf-internal-rule1-route0",
1153-
ProxyPass: "http://$test__route1_rule1$request_uri",
1153+
ProxyPass: "http://$upstream_test__route1_rule1$request_uri",
11541154
ProxySetHeaders: httpBaseHeaders,
11551155
Type: http.InternalLocationType,
11561156
Includes: internalIncludes,
11571157
},
11581158
{
11591159
Path: "/path-only/",
1160-
ProxyPass: "http://invalid-backend-ref$request_uri",
1160+
ProxyPass: "http://upstream_invalid-backend-ref$request_uri",
11611161
ProxySetHeaders: httpBaseHeaders,
11621162
Type: http.ExternalLocationType,
11631163
Includes: externalIncludes,
11641164
},
11651165
{
11661166
Path: "= /path-only",
1167-
ProxyPass: "http://invalid-backend-ref$request_uri",
1167+
ProxyPass: "http://upstream_invalid-backend-ref$request_uri",
11681168
ProxySetHeaders: httpBaseHeaders,
11691169
Type: http.ExternalLocationType,
11701170
Includes: externalIncludes,
11711171
},
11721172
{
11731173
Path: "/backend-tls-policy/",
1174-
ProxyPass: "https://test_btp_80$request_uri",
1174+
ProxyPass: "https://upstream_test_btp_80$request_uri",
11751175
ProxySetHeaders: httpBaseHeaders,
11761176
ProxySSLVerify: &http.ProxySSLVerify{
11771177
Name: "test-btp.example.com",
@@ -1182,7 +1182,7 @@ func TestCreateServers(t *testing.T) {
11821182
},
11831183
{
11841184
Path: "= /backend-tls-policy",
1185-
ProxyPass: "https://test_btp_80$request_uri",
1185+
ProxyPass: "https://upstream_test_btp_80$request_uri",
11861186
ProxySetHeaders: httpBaseHeaders,
11871187
ProxySSLVerify: &http.ProxySSLVerify{
11881188
Name: "test-btp.example.com",
@@ -1251,15 +1251,15 @@ func TestCreateServers(t *testing.T) {
12511251
{
12521252
Path: "/rewrite/",
12531253
Rewrites: []string{"^ /replacement break"},
1254-
ProxyPass: "http://test_foo_80",
1254+
ProxyPass: "http://upstream_test_foo_80",
12551255
ProxySetHeaders: rewriteProxySetHeaders,
12561256
Type: http.ExternalLocationType,
12571257
Includes: externalIncludes,
12581258
},
12591259
{
12601260
Path: "= /rewrite",
12611261
Rewrites: []string{"^ /replacement break"},
1262-
ProxyPass: "http://test_foo_80",
1262+
ProxyPass: "http://upstream_test_foo_80",
12631263
ProxySetHeaders: rewriteProxySetHeaders,
12641264
Type: http.ExternalLocationType,
12651265
Includes: externalIncludes,
@@ -1279,7 +1279,7 @@ func TestCreateServers(t *testing.T) {
12791279
{
12801280
Path: "/_ngf-internal-rule8-route0",
12811281
Rewrites: []string{"^ $request_uri", "^/rewrite-with-headers(.*)$ /prefix-replacement$1 break"},
1282-
ProxyPass: "http://test_foo_80",
1282+
ProxyPass: "http://upstream_test_foo_80",
12831283
ProxySetHeaders: rewriteProxySetHeaders,
12841284
Type: http.InternalLocationType,
12851285
Includes: internalIncludes,
@@ -1322,7 +1322,7 @@ func TestCreateServers(t *testing.T) {
13221322
},
13231323
{
13241324
Path: "= /exact",
1325-
ProxyPass: "http://test_foo_80$request_uri",
1325+
ProxyPass: "http://upstream_test_foo_80$request_uri",
13261326
ProxySetHeaders: httpBaseHeaders,
13271327
Type: http.ExternalLocationType,
13281328
Includes: externalIncludes,
@@ -1335,14 +1335,14 @@ func TestCreateServers(t *testing.T) {
13351335
},
13361336
{
13371337
Path: "/_ngf-internal-rule12-route0",
1338-
ProxyPass: "http://test_foo_80$request_uri",
1338+
ProxyPass: "http://upstream_test_foo_80$request_uri",
13391339
ProxySetHeaders: httpBaseHeaders,
13401340
Type: http.InternalLocationType,
13411341
Includes: internalIncludes,
13421342
},
13431343
{
13441344
Path: "/proxy-set-headers/",
1345-
ProxyPass: "http://test_foo_80$request_uri",
1345+
ProxyPass: "http://upstream_test_foo_80$request_uri",
13461346
ProxySetHeaders: []http.Header{
13471347
{
13481348
Name: "my-header",
@@ -1396,7 +1396,7 @@ func TestCreateServers(t *testing.T) {
13961396
},
13971397
{
13981398
Path: "= /proxy-set-headers",
1399-
ProxyPass: "http://test_foo_80$request_uri",
1399+
ProxyPass: "http://upstream_test_foo_80$request_uri",
14001400
ProxySetHeaders: []http.Header{
14011401
{
14021402
Name: "my-header",
@@ -1450,15 +1450,15 @@ func TestCreateServers(t *testing.T) {
14501450
},
14511451
{
14521452
Path: "= /grpc/method",
1453-
ProxyPass: "grpc://test_foo_80",
1453+
ProxyPass: "grpc://upstream_test_foo_80",
14541454
GRPC: true,
14551455
ProxySetHeaders: grpcBaseHeaders,
14561456
Type: http.ExternalLocationType,
14571457
Includes: externalIncludes,
14581458
},
14591459
{
14601460
Path: "= /grpc-with-backend-tls-policy/method",
1461-
ProxyPass: "grpcs://test_btp_80",
1461+
ProxyPass: "grpcs://upstream_test_btp_80",
14621462
ProxySSLVerify: &http.ProxySSLVerify{
14631463
Name: "test-btp.example.com",
14641464
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -1470,7 +1470,7 @@ func TestCreateServers(t *testing.T) {
14701470
},
14711471
{
14721472
Path: "= /include-path-only-match",
1473-
ProxyPass: "http://test_foo_80$request_uri",
1473+
ProxyPass: "http://upstream_test_foo_80$request_uri",
14741474
ProxySetHeaders: httpBaseHeaders,
14751475
Type: http.ExternalLocationType,
14761476
Includes: externalIncludes,
@@ -1483,7 +1483,7 @@ func TestCreateServers(t *testing.T) {
14831483
},
14841484
{
14851485
Path: "/_ngf-internal-rule17-route0",
1486-
ProxyPass: "http://test_foo_80$request_uri",
1486+
ProxyPass: "http://upstream_test_foo_80$request_uri",
14871487
ProxySetHeaders: httpBaseHeaders,
14881488
Rewrites: []string{"^ $request_uri break"},
14891489
Type: http.InternalLocationType,
@@ -1625,13 +1625,13 @@ func TestCreateServersConflicts(t *testing.T) {
16251625
expLocs: []http.Location{
16261626
{
16271627
Path: "/coffee/",
1628-
ProxyPass: "http://test_foo_80$request_uri",
1628+
ProxyPass: "http://upstream_test_foo_80$request_uri",
16291629
ProxySetHeaders: httpBaseHeaders,
16301630
Type: http.ExternalLocationType,
16311631
},
16321632
{
16331633
Path: "= /coffee",
1634-
ProxyPass: "http://test_bar_80$request_uri",
1634+
ProxyPass: "http://upstream_test_bar_80$request_uri",
16351635
ProxySetHeaders: httpBaseHeaders,
16361636
Type: http.ExternalLocationType,
16371637
},
@@ -1665,13 +1665,13 @@ func TestCreateServersConflicts(t *testing.T) {
16651665
expLocs: []http.Location{
16661666
{
16671667
Path: "= /coffee",
1668-
ProxyPass: "http://test_foo_80$request_uri",
1668+
ProxyPass: "http://upstream_test_foo_80$request_uri",
16691669
ProxySetHeaders: httpBaseHeaders,
16701670
Type: http.ExternalLocationType,
16711671
},
16721672
{
16731673
Path: "/coffee/",
1674-
ProxyPass: "http://test_bar_80$request_uri",
1674+
ProxyPass: "http://upstream_test_bar_80$request_uri",
16751675
ProxySetHeaders: httpBaseHeaders,
16761676
Type: http.ExternalLocationType,
16771677
},
@@ -1715,13 +1715,13 @@ func TestCreateServersConflicts(t *testing.T) {
17151715
expLocs: []http.Location{
17161716
{
17171717
Path: "/coffee/",
1718-
ProxyPass: "http://test_bar_80$request_uri",
1718+
ProxyPass: "http://upstream_test_bar_80$request_uri",
17191719
ProxySetHeaders: httpBaseHeaders,
17201720
Type: http.ExternalLocationType,
17211721
},
17221722
{
17231723
Path: "= /coffee",
1724-
ProxyPass: "http://test_baz_80$request_uri",
1724+
ProxyPass: "http://upstream_test_baz_80$request_uri",
17251725
ProxySetHeaders: httpBaseHeaders,
17261726
Type: http.ExternalLocationType,
17271727
},
@@ -2163,13 +2163,13 @@ func TestCreateLocationsRootPath(t *testing.T) {
21632163
expLocations: []http.Location{
21642164
{
21652165
Path: "/path-1",
2166-
ProxyPass: "http://test_foo_80$request_uri",
2166+
ProxyPass: "http://upstream_test_foo_80$request_uri",
21672167
ProxySetHeaders: httpBaseHeaders,
21682168
Type: http.ExternalLocationType,
21692169
},
21702170
{
21712171
Path: "/path-2",
2172-
ProxyPass: "http://test_foo_80$request_uri",
2172+
ProxyPass: "http://upstream_test_foo_80$request_uri",
21732173
ProxySetHeaders: httpBaseHeaders,
21742174
Type: http.ExternalLocationType,
21752175
},
@@ -2188,19 +2188,19 @@ func TestCreateLocationsRootPath(t *testing.T) {
21882188
expLocations: []http.Location{
21892189
{
21902190
Path: "/path-1",
2191-
ProxyPass: "http://test_foo_80$request_uri",
2191+
ProxyPass: "http://upstream_test_foo_80$request_uri",
21922192
ProxySetHeaders: httpBaseHeaders,
21932193
Type: http.ExternalLocationType,
21942194
},
21952195
{
21962196
Path: "/path-2",
2197-
ProxyPass: "http://test_foo_80$request_uri",
2197+
ProxyPass: "http://upstream_test_foo_80$request_uri",
21982198
ProxySetHeaders: httpBaseHeaders,
21992199
Type: http.ExternalLocationType,
22002200
},
22012201
{
22022202
Path: "/grpc",
2203-
ProxyPass: "grpc://test_foo_80",
2203+
ProxyPass: "grpc://upstream_test_foo_80",
22042204
GRPC: true,
22052205
ProxySetHeaders: grpcBaseHeaders,
22062206
Type: http.ExternalLocationType,
@@ -2219,19 +2219,19 @@ func TestCreateLocationsRootPath(t *testing.T) {
22192219
expLocations: []http.Location{
22202220
{
22212221
Path: "/path-1",
2222-
ProxyPass: "http://test_foo_80$request_uri",
2222+
ProxyPass: "http://upstream_test_foo_80$request_uri",
22232223
ProxySetHeaders: httpBaseHeaders,
22242224
Type: http.ExternalLocationType,
22252225
},
22262226
{
22272227
Path: "/path-2",
2228-
ProxyPass: "http://test_foo_80$request_uri",
2228+
ProxyPass: "http://upstream_test_foo_80$request_uri",
22292229
ProxySetHeaders: httpBaseHeaders,
22302230
Type: http.ExternalLocationType,
22312231
},
22322232
{
22332233
Path: "/",
2234-
ProxyPass: "http://test_foo_80$request_uri",
2234+
ProxyPass: "http://upstream_test_foo_80$request_uri",
22352235
ProxySetHeaders: httpBaseHeaders,
22362236
Type: http.ExternalLocationType,
22372237
},
@@ -2798,7 +2798,7 @@ func TestCreateProxyPass(t *testing.T) {
27982798
GRPC bool
27992799
}{
28002800
{
2801-
expected: "http://10.0.0.1:80$request_uri",
2801+
expected: "http://upstream_10.0.0.1:80$request_uri",
28022802
grp: dataplane.BackendGroup{
28032803
Backends: []dataplane.Backend{
28042804
{
@@ -2810,7 +2810,7 @@ func TestCreateProxyPass(t *testing.T) {
28102810
},
28112811
},
28122812
{
2813-
expected: "http://$ns1__bg_rule0$request_uri",
2813+
expected: "http://$upstream_ns1__bg_rule0$request_uri",
28142814
grp: dataplane.BackendGroup{
28152815
Source: types.NamespacedName{Namespace: "ns1", Name: "bg"},
28162816
Backends: []dataplane.Backend{
@@ -2828,7 +2828,7 @@ func TestCreateProxyPass(t *testing.T) {
28282828
},
28292829
},
28302830
{
2831-
expected: "http://10.0.0.1:80",
2831+
expected: "http://upstream_10.0.0.1:80",
28322832
rewrite: &dataplane.HTTPURLRewriteFilter{
28332833
Path: &dataplane.HTTPPathModifier{},
28342834
},
@@ -2843,7 +2843,7 @@ func TestCreateProxyPass(t *testing.T) {
28432843
},
28442844
},
28452845
{
2846-
expected: "grpc://10.0.0.1:80",
2846+
expected: "grpc://upstream_10.0.0.1:80",
28472847
grp: dataplane.BackendGroup{
28482848
Backends: []dataplane.Backend{
28492849
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type RewriteClientIPSettings struct {
3030

3131
const (
3232
ProxyProtocolDirective = " proxy_protocol"
33+
UpstreamPrefix = "upstream_"
3334
)
3435

3536
// Include defines a file that's included via the include directive.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
99
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/http"
10+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/shared"
1011
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
1112
)
1213

@@ -43,8 +44,10 @@ func createSplitClients(backendGroups []dataplane.BackendGroup) []http.SplitClie
4344
continue
4445
}
4546

47+
// prefixing the splitClients variable name with "upstream_"
48+
// to match the backend name in the proxy_pass directive.
4649
splitClients = append(splitClients, http.SplitClient{
47-
VariableName: convertStringToSafeVariableName(group.Name()),
50+
VariableName: shared.UpstreamPrefix + convertStringToSafeVariableName(group.Name()),
4851
Distributions: distributions,
4952
})
5053
}
@@ -88,7 +91,7 @@ func createSplitClientDistributions(group dataplane.BackendGroup) []http.SplitCl
8891

8992
distributions = append(distributions, http.SplitClientDistribution{
9093
Percent: fmt.Sprintf("%.2f", percentage),
91-
Value: getSplitClientValue(b),
94+
Value: shared.UpstreamPrefix + getSplitClientValue(b),
9295
})
9396
}
9497

@@ -98,7 +101,7 @@ func createSplitClientDistributions(group dataplane.BackendGroup) []http.SplitCl
98101

99102
distributions = append(distributions, http.SplitClientDistribution{
100103
Percent: fmt.Sprintf("%.2f", availablePercentage),
101-
Value: getSplitClientValue(lastBackend),
104+
Value: shared.UpstreamPrefix + getSplitClientValue(lastBackend),
102105
})
103106

104107
return distributions

0 commit comments

Comments
 (0)