Skip to content

Commit 512de1a

Browse files
author
Kate Osborn
committed
Move BackendGroup to dataplane package
1 parent 56e43d6 commit 512de1a

15 files changed

+610
-790
lines changed

internal/nginx/config/generator_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ import (
88

99
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
1010
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
11-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
1211
)
1312

1413
// Note: this test only verifies that Generate() returns a byte array with upstream, server, and split_client blocks.
1514
// It does not test the correctness of those blocks. That functionality is covered by other tests in this package.
1615
func TestGenerate(t *testing.T) {
17-
bg := graph.BackendGroup{
16+
bg := dataplane.BackendGroup{
1817
Source: types.NamespacedName{Namespace: "test", Name: "hr"},
1918
RuleIdx: 0,
20-
Backends: []graph.BackendRef{
21-
{Name: "test", Valid: true, Weight: 1},
22-
{Name: "test2", Valid: true, Weight: 1},
19+
Backends: []dataplane.Backend{
20+
{UpstreamName: "test", Valid: true, Weight: 1},
21+
{UpstreamName: "test2", Valid: true, Weight: 1},
2322
},
2423
}
2524

@@ -49,7 +48,7 @@ func TestGenerate(t *testing.T) {
4948
Endpoints: nil,
5049
},
5150
},
52-
BackendGroups: []graph.BackendGroup{bg},
51+
BackendGroups: []dataplane.BackendGroup{bg},
5352
}
5453
generator := config.NewGeneratorImpl()
5554
cfg := string(generator.Generate(conf))

internal/nginx/config/servers_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
1616
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http"
1717
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
18-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
1918
)
2019

2120
func TestExecuteServers(t *testing.T) {
@@ -318,54 +317,54 @@ func TestCreateServers(t *testing.T) {
318317

319318
hrNsName := types.NamespacedName{Namespace: hr.Namespace, Name: hr.Name}
320319

321-
fooGroup := graph.BackendGroup{
320+
fooGroup := dataplane.BackendGroup{
322321
Source: hrNsName,
323322
RuleIdx: 0,
324-
Backends: []graph.BackendRef{
323+
Backends: []dataplane.Backend{
325324
{
326-
Name: "test_foo_80",
327-
Valid: true,
328-
Weight: 1,
325+
UpstreamName: "test_foo_80",
326+
Valid: true,
327+
Weight: 1,
329328
},
330329
},
331330
}
332331

333332
// barGroup has two backends, which should generate a proxy pass with a variable.
334-
barGroup := graph.BackendGroup{
333+
barGroup := dataplane.BackendGroup{
335334
Source: hrNsName,
336335
RuleIdx: 1,
337-
Backends: []graph.BackendRef{
336+
Backends: []dataplane.Backend{
338337
{
339-
Name: "test_bar_80",
340-
Valid: true,
341-
Weight: 50,
338+
UpstreamName: "test_bar_80",
339+
Valid: true,
340+
Weight: 50,
342341
},
343342
{
344-
Name: "test_bar2_80",
345-
Valid: true,
346-
Weight: 50,
343+
UpstreamName: "test_bar2_80",
344+
Valid: true,
345+
Weight: 50,
347346
},
348347
},
349348
}
350349

351350
// baz group has an invalid backend, which should generate a proxy pass to the invalid ref backend.
352-
bazGroup := graph.BackendGroup{
351+
bazGroup := dataplane.BackendGroup{
353352
Source: hrNsName,
354353
RuleIdx: 2,
355-
Backends: []graph.BackendRef{
354+
Backends: []dataplane.Backend{
356355
{
357-
Name: "test_baz_80",
358-
Valid: false,
359-
Weight: 1,
356+
UpstreamName: "test_baz_80",
357+
Valid: false,
358+
Weight: 1,
360359
},
361360
},
362361
}
363362

364-
filterGroup1 := graph.BackendGroup{Source: hrNsName, RuleIdx: 3}
363+
filterGroup1 := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 3}
365364

366-
filterGroup2 := graph.BackendGroup{Source: hrNsName, RuleIdx: 4}
365+
filterGroup2 := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 4}
367366

368-
invalidFilterGroup := graph.BackendGroup{Source: hrNsName, RuleIdx: 5}
367+
invalidFilterGroup := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 5}
369368

370369
cafePathRules := []dataplane.PathRule{
371370
{
@@ -694,14 +693,14 @@ func TestCreateLocationsRootPath(t *testing.T) {
694693

695694
hrNsName := types.NamespacedName{Namespace: "test", Name: "route1"}
696695

697-
fooGroup := graph.BackendGroup{
696+
fooGroup := dataplane.BackendGroup{
698697
Source: hrNsName,
699698
RuleIdx: 0,
700-
Backends: []graph.BackendRef{
699+
Backends: []dataplane.Backend{
701700
{
702-
Name: "test_foo_80",
703-
Valid: true,
704-
Weight: 1,
701+
UpstreamName: "test_foo_80",
702+
Valid: true,
703+
Weight: 1,
705704
},
706705
},
707706
}

internal/nginx/config/split_clients.go

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

88
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http"
99
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
10-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
1110
)
1211

1312
var splitClientsTemplate = gotemplate.Must(gotemplate.New("split_clients").Parse(splitClientsTemplateText))
@@ -18,7 +17,7 @@ func executeSplitClients(conf dataplane.Configuration) []byte {
1817
return execute(splitClientsTemplate, splitClients)
1918
}
2019

21-
func createSplitClients(backendGroups []graph.BackendGroup) []http.SplitClient {
20+
func createSplitClients(backendGroups []dataplane.BackendGroup) []http.SplitClient {
2221
numSplits := 0
2322
for _, group := range backendGroups {
2423
if backendGroupNeedsSplit(group) {
@@ -40,7 +39,7 @@ func createSplitClients(backendGroups []graph.BackendGroup) []http.SplitClient {
4039
}
4140

4241
splitClients = append(splitClients, http.SplitClient{
43-
VariableName: convertStringToSafeVariableName(group.GroupName()),
42+
VariableName: convertStringToSafeVariableName(group.Name()),
4443
Distributions: distributions,
4544
})
4645

@@ -49,7 +48,7 @@ func createSplitClients(backendGroups []graph.BackendGroup) []http.SplitClient {
4948
return splitClients
5049
}
5150

52-
func createSplitClientDistributions(group graph.BackendGroup) []http.SplitClientDistribution {
51+
func createSplitClientDistributions(group dataplane.BackendGroup) []http.SplitClientDistribution {
5352
if !backendGroupNeedsSplit(group) {
5453
return nil
5554
}
@@ -101,9 +100,9 @@ func createSplitClientDistributions(group graph.BackendGroup) []http.SplitClient
101100
return distributions
102101
}
103102

104-
func getSplitClientValue(b graph.BackendRef) string {
103+
func getSplitClientValue(b dataplane.Backend) string {
105104
if b.Valid {
106-
return b.Name
105+
return b.UpstreamName
107106
}
108107
return invalidBackendRef
109108
}
@@ -118,25 +117,25 @@ func percentOf(weight, totalWeight int32) float64 {
118117
return math.Floor(p*100) / 100
119118
}
120119

121-
func backendGroupNeedsSplit(group graph.BackendGroup) bool {
120+
func backendGroupNeedsSplit(group dataplane.BackendGroup) bool {
122121
return len(group.Backends) > 1
123122
}
124123

125124
// backendGroupName returns the name of the backend group.
126125
// If the group needs to be split, the name returned is the group name.
127126
// If the group doesn't need to be split, the name returned is the name of the backend if it is valid.
128127
// If the name cannot be determined, it returns the name of the invalid backend upstream.
129-
func backendGroupName(group graph.BackendGroup) string {
128+
func backendGroupName(group dataplane.BackendGroup) string {
130129
switch len(group.Backends) {
131130
case 0:
132131
return invalidBackendRef
133132
case 1:
134133
b := group.Backends[0]
135-
if b.Weight <= 0 || !b.Valid {
134+
if b.Weight == 0 || !b.Valid {
136135
return invalidBackendRef
137136
}
138-
return b.Name
137+
return b.UpstreamName
139138
default:
140-
return group.GroupName()
139+
return group.Name()
141140
}
142141
}

0 commit comments

Comments
 (0)