Skip to content

Commit fadd9e5

Browse files
author
Kate Osborn
committed
Remove content from Include
1 parent 4b7e914 commit fadd9e5

File tree

3 files changed

+138
-78
lines changed

3 files changed

+138
-78
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,4 @@ type ProxySSLVerify struct {
110110
// Include holds all the files to include using the include directive.
111111
type Include struct {
112112
Filename string
113-
Content []byte
114113
}

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,43 @@ func executeServers(conf dataplane.Configuration) []executeResult {
7777
data: httpMatchConf,
7878
}
7979

80-
includeFileResults := createIncludeFileResults(servers)
80+
additionFileResults := createAdditionFileResults(conf)
8181

82-
allResults := make([]executeResult, 0, len(includeFileResults)+2)
83-
allResults = append(allResults, includeFileResults...)
82+
allResults := make([]executeResult, 0, len(additionFileResults)+2)
83+
allResults = append(allResults, additionFileResults...)
8484
allResults = append(allResults, serverResult, httpMatchResult)
8585

8686
return allResults
8787
}
8888

89-
func createIncludeFileResults(servers []http.Server) []executeResult {
90-
uniqueIncludes := make(map[string][]byte)
89+
func createAdditionFileResults(conf dataplane.Configuration) []executeResult {
90+
uniqueAdditions := make(map[string][]byte)
9191

92-
for _, s := range servers {
93-
for _, inc := range s.Includes {
94-
uniqueIncludes[inc.Filename] = inc.Content
92+
findUniqueAdditionsForServer := func(server dataplane.VirtualServer) {
93+
for _, add := range server.Additions {
94+
uniqueAdditions[createAdditionFileName(add)] = add.Bytes
9595
}
9696

97-
for _, l := range s.Locations {
98-
for _, inc := range l.Includes {
99-
uniqueIncludes[inc.Filename] = inc.Content
97+
for _, pr := range server.PathRules {
98+
for _, mr := range pr.MatchRules {
99+
for _, add := range mr.Additions {
100+
uniqueAdditions[createAdditionFileName(add)] = add.Bytes
101+
}
100102
}
101103
}
102104
}
103105

104-
results := make([]executeResult, 0, len(uniqueIncludes))
106+
for _, s := range conf.HTTPServers {
107+
findUniqueAdditionsForServer(s)
108+
}
109+
110+
for _, s := range conf.SSLServers {
111+
findUniqueAdditionsForServer(s)
112+
}
113+
114+
results := make([]executeResult, 0, len(uniqueAdditions))
105115

106-
for filename, contents := range uniqueIncludes {
116+
for filename, contents := range uniqueAdditions {
107117
results = append(results, executeResult{
108118
dest: filename,
109119
data: contents,
@@ -113,17 +123,24 @@ func createIncludeFileResults(servers []http.Server) []executeResult {
113123
return results
114124
}
115125

126+
func createAdditionFileName(addition *dataplane.Addition) string {
127+
if addition == nil {
128+
return ""
129+
}
130+
131+
return fmt.Sprintf("%s/%s.conf", includesFolder, addition.Identifier)
132+
}
133+
116134
func createIncludes(additions []*dataplane.Addition) []http.Include {
117135
if len(additions) == 0 {
118136
return nil
119137
}
120138

121139
includes := make([]http.Include, 0, len(additions))
122140

123-
for _, c := range additions {
141+
for _, addition := range additions {
124142
includes = append(includes, http.Include{
125-
Filename: fmt.Sprintf("%s/%s.conf", includesFolder, c.Identifier),
126-
Content: c.Bytes,
143+
Filename: createAdditionFileName(addition),
127144
})
128145
}
129146

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

Lines changed: 105 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,6 @@ func TestCreateServers(t *testing.T) {
978978
ProxySetHeaders: httpBaseHeaders,
979979
Includes: []http.Include{
980980
{
981-
Content: []byte("match-addition"),
982981
Filename: includesFolder + "/match-addition.conf",
983982
},
984983
},
@@ -1001,11 +1000,9 @@ func TestCreateServers(t *testing.T) {
10011000
Includes: []http.Include{
10021001
{
10031002
Filename: includesFolder + "/server-addition-1.conf",
1004-
Content: []byte("server-addition-1"),
10051003
},
10061004
{
10071005
Filename: includesFolder + "/server-addition-2.conf",
1008-
Content: []byte("server-addition-2"),
10091006
},
10101007
},
10111008
},
@@ -1025,11 +1022,9 @@ func TestCreateServers(t *testing.T) {
10251022
Includes: []http.Include{
10261023
{
10271024
Filename: includesFolder + "/server-addition-1.conf",
1028-
Content: []byte("server-addition-1"),
10291025
},
10301026
{
10311027
Filename: includesFolder + "/server-addition-3.conf",
1032-
Content: []byte("server-addition-3"),
10331028
},
10341029
},
10351030
},
@@ -2355,15 +2350,12 @@ func TestCreateIncludes(t *testing.T) {
23552350
includes: []http.Include{
23562351
{
23572352
Filename: includesFolder + "/one.conf",
2358-
Content: []byte("one"),
23592353
},
23602354
{
23612355
Filename: includesFolder + "/two.conf",
2362-
Content: []byte("two"),
23632356
},
23642357
{
23652358
Filename: includesFolder + "/three.conf",
2366-
Content: []byte("three"),
23672359
},
23682360
},
23692361
},
@@ -2379,91 +2371,115 @@ func TestCreateIncludes(t *testing.T) {
23792371
}
23802372
}
23812373

2382-
func TestCreateIncludeFileResults(t *testing.T) {
2383-
servers := []http.Server{
2384-
{
2385-
Locations: []http.Location{
2386-
{
2387-
Includes: []http.Include{
2388-
{
2389-
Filename: "include-1.conf",
2390-
Content: []byte("include-1"),
2391-
},
2392-
{
2393-
Filename: "include-2.conf",
2394-
Content: []byte("include-2"),
2395-
},
2396-
{
2397-
Filename: "include-3.conf",
2398-
Content: []byte("include-3"),
2374+
func TestCreateAdditionFileResults(t *testing.T) {
2375+
conf := dataplane.Configuration{
2376+
HTTPServers: []dataplane.VirtualServer{
2377+
{
2378+
Additions: []*dataplane.Addition{
2379+
{
2380+
Identifier: "include-1",
2381+
Bytes: []byte("include-1"),
2382+
},
2383+
{
2384+
Identifier: "include-2",
2385+
Bytes: []byte("include-2"),
2386+
},
2387+
},
2388+
PathRules: []dataplane.PathRule{
2389+
{
2390+
MatchRules: []dataplane.MatchRule{
2391+
{
2392+
Additions: []*dataplane.Addition{
2393+
{
2394+
Identifier: "include-3",
2395+
Bytes: []byte("include-3"),
2396+
},
2397+
{
2398+
Identifier: "include-4",
2399+
Bytes: []byte("include-4"),
2400+
},
2401+
},
2402+
},
23992403
},
24002404
},
24012405
},
24022406
},
2403-
Includes: []http.Include{
2404-
{
2405-
Filename: "include-1.conf",
2406-
Content: []byte("include-1"), // duplicate
2407-
},
2408-
{
2409-
Filename: "include-4.conf",
2410-
Content: []byte("include-4"),
2407+
{
2408+
Additions: []*dataplane.Addition{
2409+
{
2410+
Identifier: "include-1", // dupe
2411+
Bytes: []byte("include-1"),
2412+
},
2413+
{
2414+
Identifier: "include-2", // dupe
2415+
Bytes: []byte("include-2"),
2416+
},
24112417
},
24122418
},
24132419
},
2414-
{
2415-
Locations: []http.Location{
2416-
{
2417-
Includes: []http.Include{
2418-
{
2419-
Filename: "include-2.conf",
2420-
Content: []byte("include-2"), // duplicate
2421-
},
2422-
{
2423-
Filename: "include-5.conf",
2424-
Content: []byte("include-5"),
2425-
},
2420+
SSLServers: []dataplane.VirtualServer{
2421+
{
2422+
Additions: []*dataplane.Addition{
2423+
{
2424+
Identifier: "include-1", // dupe
2425+
Bytes: []byte("include-1"),
2426+
},
2427+
{
2428+
Identifier: "include-2", // dupe
2429+
Bytes: []byte("include-2"),
24262430
},
24272431
},
2428-
},
2429-
Includes: []http.Include{
2430-
{
2431-
Filename: "include-4.conf",
2432-
Content: []byte("include-4"), // duplicate
2433-
},
2434-
{
2435-
Filename: "include-6.conf",
2436-
Content: []byte("include-6"),
2432+
PathRules: []dataplane.PathRule{
2433+
{
2434+
MatchRules: []dataplane.MatchRule{
2435+
{
2436+
Additions: []*dataplane.Addition{
2437+
{
2438+
Identifier: "include-3",
2439+
Bytes: []byte("include-3"), // dupe
2440+
},
2441+
{
2442+
Identifier: "include-5",
2443+
Bytes: []byte("include-5"), // dupe
2444+
},
2445+
{
2446+
Identifier: "include-6",
2447+
Bytes: []byte("include-6"),
2448+
},
2449+
},
2450+
},
2451+
},
2452+
},
24372453
},
24382454
},
24392455
},
24402456
}
24412457

2442-
results := createIncludeFileResults(servers)
2458+
results := createAdditionFileResults(conf)
24432459

24442460
expResults := []executeResult{
24452461
{
2446-
dest: "include-1.conf",
2462+
dest: includesFolder + "/" + "include-1.conf",
24472463
data: []byte("include-1"),
24482464
},
24492465
{
2450-
dest: "include-2.conf",
2466+
dest: includesFolder + "/" + "include-2.conf",
24512467
data: []byte("include-2"),
24522468
},
24532469
{
2454-
dest: "include-3.conf",
2470+
dest: includesFolder + "/" + "include-3.conf",
24552471
data: []byte("include-3"),
24562472
},
24572473
{
2458-
dest: "include-4.conf",
2474+
dest: includesFolder + "/" + "include-4.conf",
24592475
data: []byte("include-4"),
24602476
},
24612477
{
2462-
dest: "include-5.conf",
2478+
dest: includesFolder + "/" + "include-5.conf",
24632479
data: []byte("include-5"),
24642480
},
24652481
{
2466-
dest: "include-6.conf",
2482+
dest: includesFolder + "/" + "include-6.conf",
24672483
data: []byte("include-6"),
24682484
},
24692485
}
@@ -2472,3 +2488,31 @@ func TestCreateIncludeFileResults(t *testing.T) {
24722488

24732489
g.Expect(results).To(ConsistOf(expResults))
24742490
}
2491+
2492+
func TestAdditionFilename(t *testing.T) {
2493+
tests := []struct {
2494+
name string
2495+
addition *dataplane.Addition
2496+
expName string
2497+
}{
2498+
{
2499+
name: "nil addition",
2500+
addition: nil,
2501+
expName: "",
2502+
},
2503+
{
2504+
name: "normal addition",
2505+
addition: &dataplane.Addition{Identifier: "my-addition"},
2506+
expName: includesFolder + "/" + "my-addition.conf",
2507+
},
2508+
}
2509+
2510+
for _, test := range tests {
2511+
t.Run(test.name, func(t *testing.T) {
2512+
g := NewWithT(t)
2513+
2514+
name := createAdditionFileName(test.addition)
2515+
g.Expect(name).To(Equal(test.expName))
2516+
})
2517+
}
2518+
}

0 commit comments

Comments
 (0)