Skip to content

Commit

Permalink
Move BackendGroup to dataplane package
Browse files Browse the repository at this point in the history
  • Loading branch information
kate-osborn committed May 9, 2023
1 parent 56e43d6 commit 512de1a
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 790 deletions.
11 changes: 5 additions & 6 deletions internal/nginx/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (

"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
)

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

Expand Down Expand Up @@ -49,7 +48,7 @@ func TestGenerate(t *testing.T) {
Endpoints: nil,
},
},
BackendGroups: []graph.BackendGroup{bg},
BackendGroups: []dataplane.BackendGroup{bg},
}
generator := config.NewGeneratorImpl()
cfg := string(generator.Generate(conf))
Expand Down
53 changes: 26 additions & 27 deletions internal/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
)

func TestExecuteServers(t *testing.T) {
Expand Down Expand Up @@ -318,54 +317,54 @@ func TestCreateServers(t *testing.T) {

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

fooGroup := graph.BackendGroup{
fooGroup := dataplane.BackendGroup{
Source: hrNsName,
RuleIdx: 0,
Backends: []graph.BackendRef{
Backends: []dataplane.Backend{
{
Name: "test_foo_80",
Valid: true,
Weight: 1,
UpstreamName: "test_foo_80",
Valid: true,
Weight: 1,
},
},
}

// barGroup has two backends, which should generate a proxy pass with a variable.
barGroup := graph.BackendGroup{
barGroup := dataplane.BackendGroup{
Source: hrNsName,
RuleIdx: 1,
Backends: []graph.BackendRef{
Backends: []dataplane.Backend{
{
Name: "test_bar_80",
Valid: true,
Weight: 50,
UpstreamName: "test_bar_80",
Valid: true,
Weight: 50,
},
{
Name: "test_bar2_80",
Valid: true,
Weight: 50,
UpstreamName: "test_bar2_80",
Valid: true,
Weight: 50,
},
},
}

// baz group has an invalid backend, which should generate a proxy pass to the invalid ref backend.
bazGroup := graph.BackendGroup{
bazGroup := dataplane.BackendGroup{
Source: hrNsName,
RuleIdx: 2,
Backends: []graph.BackendRef{
Backends: []dataplane.Backend{
{
Name: "test_baz_80",
Valid: false,
Weight: 1,
UpstreamName: "test_baz_80",
Valid: false,
Weight: 1,
},
},
}

filterGroup1 := graph.BackendGroup{Source: hrNsName, RuleIdx: 3}
filterGroup1 := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 3}

filterGroup2 := graph.BackendGroup{Source: hrNsName, RuleIdx: 4}
filterGroup2 := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 4}

invalidFilterGroup := graph.BackendGroup{Source: hrNsName, RuleIdx: 5}
invalidFilterGroup := dataplane.BackendGroup{Source: hrNsName, RuleIdx: 5}

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

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

fooGroup := graph.BackendGroup{
fooGroup := dataplane.BackendGroup{
Source: hrNsName,
RuleIdx: 0,
Backends: []graph.BackendRef{
Backends: []dataplane.Backend{
{
Name: "test_foo_80",
Valid: true,
Weight: 1,
UpstreamName: "test_foo_80",
Valid: true,
Weight: 1,
},
},
}
Expand Down
21 changes: 10 additions & 11 deletions internal/nginx/config/split_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
)

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

func createSplitClients(backendGroups []graph.BackendGroup) []http.SplitClient {
func createSplitClients(backendGroups []dataplane.BackendGroup) []http.SplitClient {
numSplits := 0
for _, group := range backendGroups {
if backendGroupNeedsSplit(group) {
Expand All @@ -40,7 +39,7 @@ func createSplitClients(backendGroups []graph.BackendGroup) []http.SplitClient {
}

splitClients = append(splitClients, http.SplitClient{
VariableName: convertStringToSafeVariableName(group.GroupName()),
VariableName: convertStringToSafeVariableName(group.Name()),
Distributions: distributions,
})

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

func createSplitClientDistributions(group graph.BackendGroup) []http.SplitClientDistribution {
func createSplitClientDistributions(group dataplane.BackendGroup) []http.SplitClientDistribution {
if !backendGroupNeedsSplit(group) {
return nil
}
Expand Down Expand Up @@ -101,9 +100,9 @@ func createSplitClientDistributions(group graph.BackendGroup) []http.SplitClient
return distributions
}

func getSplitClientValue(b graph.BackendRef) string {
func getSplitClientValue(b dataplane.Backend) string {
if b.Valid {
return b.Name
return b.UpstreamName
}
return invalidBackendRef
}
Expand All @@ -118,25 +117,25 @@ func percentOf(weight, totalWeight int32) float64 {
return math.Floor(p*100) / 100
}

func backendGroupNeedsSplit(group graph.BackendGroup) bool {
func backendGroupNeedsSplit(group dataplane.BackendGroup) bool {
return len(group.Backends) > 1
}

// backendGroupName returns the name of the backend group.
// If the group needs to be split, the name returned is the group name.
// If the group doesn't need to be split, the name returned is the name of the backend if it is valid.
// If the name cannot be determined, it returns the name of the invalid backend upstream.
func backendGroupName(group graph.BackendGroup) string {
func backendGroupName(group dataplane.BackendGroup) string {
switch len(group.Backends) {
case 0:
return invalidBackendRef
case 1:
b := group.Backends[0]
if b.Weight <= 0 || !b.Valid {
if b.Weight == 0 || !b.Valid {
return invalidBackendRef
}
return b.Name
return b.UpstreamName
default:
return group.GroupName()
return group.Name()
}
}
Loading

0 comments on commit 512de1a

Please sign in to comment.