Skip to content

Commit 68d1f6e

Browse files
authored
Refactor graph and configuration into separate packages
- Move graph-related types and functions to state/graph package. Those are further split into Gateway resource-related file like httproute.go - Move configuration-related types and functions to state/dataplane package - Add package docs for both packages.
1 parent f2b0bda commit 68d1f6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2419
-2332
lines changed

internal/events/handler.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file"
1414
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime"
1515
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
16+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
17+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets"
1618
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
1719
)
1820

@@ -30,9 +32,9 @@ type EventHandlerConfig struct {
3032
// Processor is the state ChangeProcessor.
3133
Processor state.ChangeProcessor
3234
// SecretStore is the state SecretStore.
33-
SecretStore state.SecretStore
35+
SecretStore secrets.SecretStore
3436
// SecretMemoryManager is the state SecretMemoryManager.
35-
SecretMemoryManager state.SecretDiskMemoryManager
37+
SecretMemoryManager secrets.SecretDiskMemoryManager
3638
// Generator is the nginx config Generator.
3739
Generator config.Generator
3840
// NginxFileMgr is the file Manager for nginx.
@@ -88,7 +90,7 @@ func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatc
8890
h.cfg.StatusUpdater.Update(ctx, statuses)
8991
}
9092

91-
func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf state.Configuration) error {
93+
func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error {
9294
// Write all secrets (nuke and pave).
9395
// This will remove all secrets in the secrets directory before writing the requested secrets.
9496
// FIXME(kate-osborn): We may want to rethink this approach in the future and write and remove secrets individually.

internal/events/handler_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file/filefakes"
2121
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime/runtimefakes"
2222
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
23+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
24+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets/secretsfakes"
2325
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/statefakes"
2426
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes"
2527
)
@@ -40,15 +42,15 @@ var _ = Describe("EventHandler", func() {
4042
var (
4143
handler *events.EventHandlerImpl
4244
fakeProcessor *statefakes.FakeChangeProcessor
43-
fakeSecretStore *statefakes.FakeSecretStore
44-
fakeSecretMemoryManager *statefakes.FakeSecretDiskMemoryManager
45+
fakeSecretStore *secretsfakes.FakeSecretStore
46+
fakeSecretMemoryManager *secretsfakes.FakeSecretDiskMemoryManager
4547
fakeGenerator *configfakes.FakeGenerator
4648
fakeNginxFileMgr *filefakes.FakeManager
4749
fakeNginxRuntimeMgr *runtimefakes.FakeManager
4850
fakeStatusUpdater *statusfakes.FakeUpdater
4951
)
5052

51-
expectReconfig := func(expectedConf state.Configuration, expectedCfg []byte, expectedStatuses state.Statuses) {
53+
expectReconfig := func(expectedConf dataplane.Configuration, expectedCfg []byte, expectedStatuses state.Statuses) {
5254
Expect(fakeProcessor.ProcessCallCount()).Should(Equal(1))
5355

5456
Expect(fakeGenerator.GenerateCallCount()).Should(Equal(1))
@@ -68,8 +70,8 @@ var _ = Describe("EventHandler", func() {
6870

6971
BeforeEach(func() {
7072
fakeProcessor = &statefakes.FakeChangeProcessor{}
71-
fakeSecretMemoryManager = &statefakes.FakeSecretDiskMemoryManager{}
72-
fakeSecretStore = &statefakes.FakeSecretStore{}
73+
fakeSecretMemoryManager = &secretsfakes.FakeSecretDiskMemoryManager{}
74+
fakeSecretStore = &secretsfakes.FakeSecretStore{}
7375
fakeGenerator = &configfakes.FakeGenerator{}
7476
fakeNginxFileMgr = &filefakes.FakeManager{}
7577
fakeNginxRuntimeMgr = &runtimefakes.FakeManager{}
@@ -91,7 +93,7 @@ var _ = Describe("EventHandler", func() {
9193
DescribeTable(
9294
"A batch with one event",
9395
func(e interface{}) {
94-
fakeConf := state.Configuration{}
96+
fakeConf := dataplane.Configuration{}
9597
fakeStatuses := state.Statuses{}
9698
changed := true
9799
fakeProcessor.ProcessReturns(changed, fakeConf, fakeStatuses)
@@ -256,7 +258,7 @@ var _ = Describe("EventHandler", func() {
256258
batch = append(batch, upserts...)
257259
batch = append(batch, deletes...)
258260

259-
fakeConf := state.Configuration{}
261+
fakeConf := dataplane.Configuration{}
260262
changed := true
261263
fakeStatuses := state.Statuses{}
262264
fakeProcessor.ProcessReturns(changed, fakeConf, fakeStatuses)

internal/manager/manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
2828
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship"
2929
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver"
30+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets"
3031
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
3132
)
3233

@@ -118,8 +119,8 @@ func Start(cfg config.Config) error {
118119
}
119120
}
120121

121-
secretStore := state.NewSecretStore()
122-
secretMemoryMgr := state.NewSecretDiskMemoryManager(secretsFolder, secretStore)
122+
secretStore := secrets.NewSecretStore()
123+
secretMemoryMgr := secrets.NewSecretDiskMemoryManager(secretsFolder, secretStore)
123124

124125
processor := state.NewChangeProcessorImpl(state.ChangeProcessorConfig{
125126
GatewayCtlrName: cfg.GatewayCtlrName,

internal/nginx/config/configfakes/fake_generator.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/nginx/config/generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package config
22

33
import (
4-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
4+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
55
)
66

77
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Generator
@@ -10,7 +10,7 @@ import (
1010
// This interface is used for testing purposes only.
1111
type Generator interface {
1212
// Generate generates NGINX configuration from internal representation.
13-
Generate(configuration state.Configuration) []byte
13+
Generate(configuration dataplane.Configuration) []byte
1414
}
1515

1616
// GeneratorImpl is an implementation of Generator.
@@ -22,9 +22,9 @@ func NewGeneratorImpl() GeneratorImpl {
2222
}
2323

2424
// executeFunc is a function that generates NGINX configuration from internal representation.
25-
type executeFunc func(configuration state.Configuration) []byte
25+
type executeFunc func(configuration dataplane.Configuration) []byte
2626

27-
func (g GeneratorImpl) Generate(conf state.Configuration) []byte {
27+
func (g GeneratorImpl) Generate(conf dataplane.Configuration) []byte {
2828
var generated []byte
2929
for _, execute := range getExecuteFuncs() {
3030
generated = append(generated, execute(conf)...)

internal/nginx/config/generator_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,49 @@ import (
77
"k8s.io/apimachinery/pkg/types"
88

99
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
10-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
10+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
11+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/graph"
1112
)
1213

1314
// Note: this test only verifies that Generate() returns a byte array with upstream, server, and split_client blocks.
1415
// It does not test the correctness of those blocks. That functionality is covered by other tests in this package.
1516
func TestGenerate(t *testing.T) {
16-
bg := state.BackendGroup{
17+
bg := graph.BackendGroup{
1718
Source: types.NamespacedName{Namespace: "test", Name: "hr"},
1819
RuleIdx: 0,
19-
Backends: []state.BackendRef{
20+
Backends: []graph.BackendRef{
2021
{Name: "test", Valid: true, Weight: 1},
2122
{Name: "test2", Valid: true, Weight: 1},
2223
},
2324
}
2425

25-
conf := state.Configuration{
26-
HTTPServers: []state.VirtualServer{
26+
conf := dataplane.Configuration{
27+
HTTPServers: []dataplane.VirtualServer{
2728
{
2829
IsDefault: true,
2930
},
3031
{
3132
Hostname: "example.com",
3233
},
3334
},
34-
SSLServers: []state.VirtualServer{
35+
SSLServers: []dataplane.VirtualServer{
3536
{
3637
IsDefault: true,
3738
},
3839
{
3940
Hostname: "example.com",
40-
SSL: &state.SSL{
41+
SSL: &dataplane.SSL{
4142
CertificatePath: "/etc/nginx/secrets/default",
4243
},
4344
},
4445
},
45-
Upstreams: []state.Upstream{
46+
Upstreams: []dataplane.Upstream{
4647
{
4748
Name: "up",
4849
Endpoints: nil,
4950
},
5051
},
51-
BackendGroups: []state.BackendGroup{bg},
52+
BackendGroups: []graph.BackendGroup{bg},
5253
}
5354
generator := config.NewGeneratorImpl()
5455
cfg := string(generator.Generate(conf))

internal/nginx/config/servers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import (
99
"sigs.k8s.io/gateway-api/apis/v1beta1"
1010

1111
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http"
12-
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
12+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
1313
)
1414

1515
var serversTemplate = gotemplate.Must(gotemplate.New("servers").Parse(serversTemplateText))
1616

1717
const rootPath = "/"
1818

19-
func executeServers(conf state.Configuration) []byte {
19+
func executeServers(conf dataplane.Configuration) []byte {
2020
servers := createServers(conf.HTTPServers, conf.SSLServers)
2121

2222
return execute(serversTemplate, servers)
2323
}
2424

25-
func createServers(httpServers, sslServers []state.VirtualServer) []http.Server {
25+
func createServers(httpServers, sslServers []dataplane.VirtualServer) []http.Server {
2626
servers := make([]http.Server, 0, len(httpServers)+len(sslServers))
2727

2828
for _, s := range httpServers {
@@ -36,7 +36,7 @@ func createServers(httpServers, sslServers []state.VirtualServer) []http.Server
3636
return servers
3737
}
3838

39-
func createSSLServer(virtualServer state.VirtualServer) http.Server {
39+
func createSSLServer(virtualServer dataplane.VirtualServer) http.Server {
4040
if virtualServer.IsDefault {
4141
return createDefaultSSLServer()
4242
}
@@ -51,7 +51,7 @@ func createSSLServer(virtualServer state.VirtualServer) http.Server {
5151
}
5252
}
5353

54-
func createServer(virtualServer state.VirtualServer) http.Server {
54+
func createServer(virtualServer dataplane.VirtualServer) http.Server {
5555
if virtualServer.IsDefault {
5656
return createDefaultHTTPServer()
5757
}
@@ -62,7 +62,7 @@ func createServer(virtualServer state.VirtualServer) http.Server {
6262
}
6363
}
6464

65-
func createLocations(pathRules []state.PathRule, listenerPort int) []http.Location {
65+
func createLocations(pathRules []dataplane.PathRule, listenerPort int) []http.Location {
6666
lenPathRules := len(pathRules)
6767

6868
if lenPathRules == 0 {

0 commit comments

Comments
 (0)