Skip to content

Commit 05ab72d

Browse files
committed
Do not generate nginx plus template code when running on oss
1 parent 19d3ea0 commit 05ab72d

File tree

6 files changed

+172
-33
lines changed

6 files changed

+172
-33
lines changed

internal/mode/static/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, logger logr.Log
174174
return
175175
case state.EndpointsOnlyChange:
176176
h.version++
177-
cfg := dataplane.BuildConfiguration(ctx, gr, h.cfg.serviceResolver, h.version)
177+
cfg := dataplane.BuildConfiguration(ctx, gr, h.cfg.serviceResolver, h.version, h.cfg.plus)
178178
depCtx, getErr := h.getDeploymentContext(ctx)
179179
if getErr != nil {
180180
logger.Error(getErr, "error getting deployment context for usage reporting")
@@ -190,7 +190,7 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, logger logr.Log
190190
}
191191
case state.ClusterStateChange:
192192
h.version++
193-
cfg := dataplane.BuildConfiguration(ctx, gr, h.cfg.serviceResolver, h.version)
193+
cfg := dataplane.BuildConfiguration(ctx, gr, h.cfg.serviceResolver, h.version, h.cfg.plus)
194194
depCtx, getErr := h.getDeploymentContext(ctx)
195195
if getErr != nil {
196196
logger.Error(getErr, "error getting deployment context for usage reporting")

internal/mode/static/handler_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ var _ = Describe("eventHandler", func() {
442442
handler.HandleEventBatch(context.Background(), ctlrZap.New(), batch)
443443

444444
dcfg := dataplane.GetDefaultConfiguration(&graph.Graph{}, 1)
445+
dcfg.NginxPlus = dataplane.NginxPlus{AllowedAddresses: []string{"127.0.0.1"}}
445446
Expect(helpers.Diff(handler.GetLatestConfiguration(), &dcfg)).To(BeEmpty())
446447

447448
Expect(fakeGenerator.GenerateCallCount()).To(Equal(0))

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ var plusAPITemplate = gotemplate.Must(gotemplate.New("plusAPI").Parse(plusAPITem
1212
func executePlusAPI(conf dataplane.Configuration) []executeResult {
1313
result := executeResult{
1414
dest: nginxPlusConfigFile,
15-
data: helpers.MustExecuteTemplate(plusAPITemplate, conf.NginxPlus),
15+
}
16+
// if AllowedAddresses is empty, it means that we are not running on nginx plus, and we don't want this generated
17+
if conf.NginxPlus.AllowedAddresses != nil {
18+
result = executeResult{
19+
dest: nginxPlusConfigFile,
20+
data: helpers.MustExecuteTemplate(plusAPITemplate, conf.NginxPlus),
21+
}
1622
}
1723

1824
return []executeResult{result}

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

+26
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,29 @@ func TestExecutePlusAPI(t *testing.T) {
3535
g.Expect(expCount).To(Equal(strings.Count(string(res[0].data), expSubStr)))
3636
}
3737
}
38+
39+
func TestExecutePlusAPI_EmptyNginxPlus(t *testing.T) {
40+
t.Parallel()
41+
conf := dataplane.Configuration{
42+
NginxPlus: dataplane.NginxPlus{},
43+
}
44+
45+
g := NewWithT(t)
46+
expSubStrings := map[string]int{
47+
"listen unix:/var/run/nginx/nginx-plus-api.sock;": 0,
48+
"access_log off;": 0,
49+
"api write=on;": 0,
50+
"listen 8765;": 0,
51+
"root /usr/share/nginx/html;": 0,
52+
"allow 127.0.0.1;": 0,
53+
"deny all;": 0,
54+
"location = /dashboard.html {}": 0,
55+
"api write=off;": 0,
56+
}
57+
58+
for expSubStr, expCount := range expSubStrings {
59+
res := executePlusAPI(conf)
60+
g.Expect(res).To(HaveLen(1))
61+
g.Expect(expCount).To(Equal(strings.Count(string(res[0].data), expSubStr)))
62+
}
63+
}

internal/mode/static/state/dataplane/configuration.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ func BuildConfiguration(
3333
g *graph.Graph,
3434
serviceResolver resolver.ServiceResolver,
3535
configVersion int,
36+
plus bool,
3637
) Configuration {
3738
if g.GatewayClass == nil || !g.GatewayClass.Valid || g.Gateway == nil {
38-
return GetDefaultConfiguration(g, configVersion)
39+
config := GetDefaultConfiguration(g, configVersion)
40+
if plus {
41+
config.NginxPlus = buildNginxPlus(g)
42+
}
43+
44+
return config
3945
}
4046

4147
baseHTTPConfig := buildBaseHTTPConfig(g)
@@ -50,6 +56,11 @@ func BuildConfiguration(
5056
baseHTTPConfig.IPFamily,
5157
)
5258

59+
var nginxPlus NginxPlus
60+
if plus {
61+
nginxPlus = buildNginxPlus(g)
62+
}
63+
5364
config := Configuration{
5465
HTTPServers: httpServers,
5566
SSLServers: sslServers,
@@ -63,7 +74,7 @@ func BuildConfiguration(
6374
Telemetry: buildTelemetry(g),
6475
BaseHTTPConfig: baseHTTPConfig,
6576
Logging: buildLogging(g),
66-
NginxPlus: buildNginxPlus(g),
77+
NginxPlus: nginxPlus,
6778
MainSnippets: buildSnippetsForContext(g.SnippetsFilters, ngfAPIv1alpha1.NginxContextMain),
6879
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
6980
}
@@ -1009,7 +1020,7 @@ func GetDefaultConfiguration(g *graph.Graph, configVersion int) Configuration {
10091020
return Configuration{
10101021
Version: configVersion,
10111022
Logging: buildLogging(g),
1012-
NginxPlus: buildNginxPlus(g),
1023+
NginxPlus: NginxPlus{},
10131024
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
10141025
}
10151026
}

internal/mode/static/state/dataplane/configuration_test.go

+122-27
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ func getExpectedConfiguration() Configuration {
6666
Logging: Logging{
6767
ErrorLevel: defaultErrorLogLevel,
6868
},
69-
NginxPlus: NginxPlus{
70-
AllowedAddresses: []string{"127.0.0.1"},
71-
},
69+
NginxPlus: NginxPlus{},
7270
}
7371
}
7472

@@ -870,7 +868,7 @@ func TestBuildConfiguration(t *testing.T) {
870868

871869
defaultConfig := Configuration{
872870
Logging: Logging{ErrorLevel: defaultErrorLogLevel},
873-
NginxPlus: NginxPlus{AllowedAddresses: []string{"127.0.0.1"}},
871+
NginxPlus: NginxPlus{},
874872
}
875873

876874
tests := []struct {
@@ -1559,36 +1557,14 @@ func TestBuildConfiguration(t *testing.T) {
15591557
{
15601558
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
15611559
g.GatewayClass.Valid = false
1562-
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1563-
Name: "listener-80-1",
1564-
Source: listener80,
1565-
Valid: true,
1566-
Routes: map[graph.RouteKey]*graph.L7Route{
1567-
graph.CreateRouteKey(hr1): routeHR1,
1568-
},
1569-
})
1570-
g.Routes = map[graph.RouteKey]*graph.L7Route{
1571-
graph.CreateRouteKey(hr1): routeHR1,
1572-
}
15731560
return g
15741561
}),
15751562
expConf: defaultConfig,
15761563
msg: "invalid gatewayclass",
15771564
},
15781565
{
15791566
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
1580-
g.GatewayClass.Valid = false
1581-
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1582-
Name: "listener-80-1",
1583-
Source: listener80,
1584-
Valid: true,
1585-
Routes: map[graph.RouteKey]*graph.L7Route{
1586-
graph.CreateRouteKey(hr1): routeHR1,
1587-
},
1588-
})
1589-
g.Routes = map[graph.RouteKey]*graph.L7Route{
1590-
graph.CreateRouteKey(hr1): routeHR1,
1591-
}
1567+
g.GatewayClass = nil
15921568
return g
15931569
}),
15941570
expConf: defaultConfig,
@@ -2374,6 +2350,100 @@ func TestBuildConfiguration(t *testing.T) {
23742350
}),
23752351
msg: "SnippetsFilters with main and http snippet",
23762352
},
2353+
{
2354+
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
2355+
g.Gateway.Source.ObjectMeta = metav1.ObjectMeta{
2356+
Name: "gw",
2357+
Namespace: "ns",
2358+
}
2359+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
2360+
Name: "listener-80-1",
2361+
Source: listener80,
2362+
Valid: true,
2363+
Routes: map[graph.RouteKey]*graph.L7Route{},
2364+
})
2365+
g.NginxProxy = &graph.NginxProxy{
2366+
Valid: true,
2367+
Source: &ngfAPIv1alpha1.NginxProxy{
2368+
Spec: ngfAPIv1alpha1.NginxProxySpec{
2369+
NginxPlus: &ngfAPIv1alpha1.NginxPlus{
2370+
AllowedAddresses: []ngfAPIv1alpha1.NginxPlusAllowAddress{
2371+
{Type: ngfAPIv1alpha1.NginxPlusAllowIPAddressType, Value: "127.0.0.3"},
2372+
{Type: ngfAPIv1alpha1.NginxPlusAllowIPAddressType, Value: "25.0.0.3"},
2373+
},
2374+
},
2375+
},
2376+
},
2377+
}
2378+
return g
2379+
}),
2380+
expConf: getModifiedExpectedConfiguration(func(conf Configuration) Configuration {
2381+
conf.SSLServers = []VirtualServer{}
2382+
conf.SSLKeyPairs = map[SSLKeyPairID]SSLKeyPair{}
2383+
return conf
2384+
}),
2385+
msg: "NginxProxy with NginxPlus allowed addresses configured but running on nginx oss",
2386+
},
2387+
}
2388+
2389+
for _, test := range tests {
2390+
t.Run(test.msg, func(t *testing.T) {
2391+
t.Parallel()
2392+
g := NewWithT(t)
2393+
2394+
result := BuildConfiguration(
2395+
context.TODO(),
2396+
test.graph,
2397+
fakeResolver,
2398+
1,
2399+
false,
2400+
)
2401+
2402+
g.Expect(result.BackendGroups).To(ConsistOf(test.expConf.BackendGroups))
2403+
g.Expect(result.Upstreams).To(ConsistOf(test.expConf.Upstreams))
2404+
g.Expect(result.HTTPServers).To(ConsistOf(test.expConf.HTTPServers))
2405+
g.Expect(result.SSLServers).To(ConsistOf(test.expConf.SSLServers))
2406+
g.Expect(result.TLSPassthroughServers).To(ConsistOf(test.expConf.TLSPassthroughServers))
2407+
g.Expect(result.SSLKeyPairs).To(Equal(test.expConf.SSLKeyPairs))
2408+
g.Expect(result.Version).To(Equal(1))
2409+
g.Expect(result.CertBundles).To(Equal(test.expConf.CertBundles))
2410+
g.Expect(result.Telemetry).To(Equal(test.expConf.Telemetry))
2411+
g.Expect(result.BaseHTTPConfig).To(Equal(test.expConf.BaseHTTPConfig))
2412+
g.Expect(result.Logging).To(Equal(test.expConf.Logging))
2413+
g.Expect(result.NginxPlus).To(Equal(test.expConf.NginxPlus))
2414+
})
2415+
}
2416+
}
2417+
2418+
func TestBuildConfiguration_Plus(t *testing.T) {
2419+
t.Parallel()
2420+
fooEndpoints := []resolver.Endpoint{
2421+
{
2422+
Address: "10.0.0.0",
2423+
Port: 8080,
2424+
},
2425+
}
2426+
2427+
fakeResolver := &resolverfakes.FakeServiceResolver{}
2428+
fakeResolver.ResolveReturns(fooEndpoints, nil)
2429+
2430+
listener80 := v1.Listener{
2431+
Name: "listener-80-1",
2432+
Hostname: nil,
2433+
Port: 80,
2434+
Protocol: v1.HTTPProtocolType,
2435+
}
2436+
2437+
defaultPlusConfig := Configuration{
2438+
Logging: Logging{ErrorLevel: defaultErrorLogLevel},
2439+
NginxPlus: NginxPlus{AllowedAddresses: []string{"127.0.0.1"}},
2440+
}
2441+
2442+
tests := []struct {
2443+
graph *graph.Graph
2444+
msg string
2445+
expConf Configuration
2446+
}{
23772447
{
23782448
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
23792449
g.Gateway.Source.ObjectMeta = metav1.ObjectMeta{
@@ -2409,6 +2479,30 @@ func TestBuildConfiguration(t *testing.T) {
24092479
}),
24102480
msg: "NginxProxy with NginxPlus allowed addresses configured",
24112481
},
2482+
{
2483+
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
2484+
g.GatewayClass.Valid = false
2485+
return g
2486+
}),
2487+
expConf: defaultPlusConfig,
2488+
msg: "invalid gatewayclass",
2489+
},
2490+
{
2491+
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
2492+
g.GatewayClass = nil
2493+
return g
2494+
}),
2495+
expConf: defaultPlusConfig,
2496+
msg: "missing gatewayclass",
2497+
},
2498+
{
2499+
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
2500+
g.Gateway = nil
2501+
return g
2502+
}),
2503+
expConf: defaultPlusConfig,
2504+
msg: "missing gateway",
2505+
},
24122506
}
24132507

24142508
for _, test := range tests {
@@ -2421,6 +2515,7 @@ func TestBuildConfiguration(t *testing.T) {
24212515
test.graph,
24222516
fakeResolver,
24232517
1,
2518+
true,
24242519
)
24252520

24262521
g.Expect(result.BackendGroups).To(ConsistOf(test.expConf.BackendGroups))

0 commit comments

Comments
 (0)