Skip to content

Refactor graph and configuration into separate packages #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/events/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
)

Expand All @@ -30,9 +32,9 @@ type EventHandlerConfig struct {
// Processor is the state ChangeProcessor.
Processor state.ChangeProcessor
// SecretStore is the state SecretStore.
SecretStore state.SecretStore
SecretStore secrets.SecretStore
// SecretMemoryManager is the state SecretMemoryManager.
SecretMemoryManager state.SecretDiskMemoryManager
SecretMemoryManager secrets.SecretDiskMemoryManager
// Generator is the nginx config Generator.
Generator config.Generator
// NginxFileMgr is the file Manager for nginx.
Expand Down Expand Up @@ -88,7 +90,7 @@ func (h *EventHandlerImpl) HandleEventBatch(ctx context.Context, batch EventBatc
h.cfg.StatusUpdater.Update(ctx, statuses)
}

func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf state.Configuration) error {
func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error {
// Write all secrets (nuke and pave).
// This will remove all secrets in the secrets directory before writing the requested secrets.
// FIXME(kate-osborn): We may want to rethink this approach in the future and write and remove secrets individually.
Expand Down
16 changes: 9 additions & 7 deletions internal/events/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/file/filefakes"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/runtime/runtimefakes"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets/secretsfakes"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/statefakes"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes"
)
Expand All @@ -40,15 +42,15 @@ var _ = Describe("EventHandler", func() {
var (
handler *events.EventHandlerImpl
fakeProcessor *statefakes.FakeChangeProcessor
fakeSecretStore *statefakes.FakeSecretStore
fakeSecretMemoryManager *statefakes.FakeSecretDiskMemoryManager
fakeSecretStore *secretsfakes.FakeSecretStore
fakeSecretMemoryManager *secretsfakes.FakeSecretDiskMemoryManager
fakeGenerator *configfakes.FakeGenerator
fakeNginxFileMgr *filefakes.FakeManager
fakeNginxRuntimeMgr *runtimefakes.FakeManager
fakeStatusUpdater *statusfakes.FakeUpdater
)

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

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

BeforeEach(func() {
fakeProcessor = &statefakes.FakeChangeProcessor{}
fakeSecretMemoryManager = &statefakes.FakeSecretDiskMemoryManager{}
fakeSecretStore = &statefakes.FakeSecretStore{}
fakeSecretMemoryManager = &secretsfakes.FakeSecretDiskMemoryManager{}
fakeSecretStore = &secretsfakes.FakeSecretStore{}
fakeGenerator = &configfakes.FakeGenerator{}
fakeNginxFileMgr = &filefakes.FakeManager{}
fakeNginxRuntimeMgr = &runtimefakes.FakeManager{}
Expand All @@ -91,7 +93,7 @@ var _ = Describe("EventHandler", func() {
DescribeTable(
"A batch with one event",
func(e interface{}) {
fakeConf := state.Configuration{}
fakeConf := dataplane.Configuration{}
fakeStatuses := state.Statuses{}
changed := true
fakeProcessor.ProcessReturns(changed, fakeConf, fakeStatuses)
Expand Down Expand Up @@ -256,7 +258,7 @@ var _ = Describe("EventHandler", func() {
batch = append(batch, upserts...)
batch = append(batch, deletes...)

fakeConf := state.Configuration{}
fakeConf := dataplane.Configuration{}
changed := true
fakeStatuses := state.Statuses{}
fakeProcessor.ProcessReturns(changed, fakeConf, fakeStatuses)
Expand Down
5 changes: 3 additions & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/relationship"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/resolver"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/secrets"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
)

Expand Down Expand Up @@ -118,8 +119,8 @@ func Start(cfg config.Config) error {
}
}

secretStore := state.NewSecretStore()
secretMemoryMgr := state.NewSecretDiskMemoryManager(secretsFolder, secretStore)
secretStore := secrets.NewSecretStore()
secretMemoryMgr := secrets.NewSecretDiskMemoryManager(secretsFolder, secretStore)

processor := state.NewChangeProcessorImpl(state.ChangeProcessorConfig{
GatewayCtlrName: cfg.GatewayCtlrName,
Expand Down
14 changes: 7 additions & 7 deletions internal/nginx/config/configfakes/fake_generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/nginx/config/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state/dataplane"
)

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

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

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

func (g GeneratorImpl) Generate(conf state.Configuration) []byte {
func (g GeneratorImpl) Generate(conf dataplane.Configuration) []byte {
var generated []byte
for _, execute := range getExecuteFuncs() {
generated = append(generated, execute(conf)...)
Expand Down
19 changes: 10 additions & 9 deletions internal/nginx/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,49 @@ import (
"k8s.io/apimachinery/pkg/types"

"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
"github.com/nginxinc/nginx-kubernetes-gateway/internal/state"
"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 := state.BackendGroup{
bg := graph.BackendGroup{
Source: types.NamespacedName{Namespace: "test", Name: "hr"},
RuleIdx: 0,
Backends: []state.BackendRef{
Backends: []graph.BackendRef{
{Name: "test", Valid: true, Weight: 1},
{Name: "test2", Valid: true, Weight: 1},
},
}

conf := state.Configuration{
HTTPServers: []state.VirtualServer{
conf := dataplane.Configuration{
HTTPServers: []dataplane.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
},
},
SSLServers: []state.VirtualServer{
SSLServers: []dataplane.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
SSL: &state.SSL{
SSL: &dataplane.SSL{
CertificatePath: "/etc/nginx/secrets/default",
},
},
},
Upstreams: []state.Upstream{
Upstreams: []dataplane.Upstream{
{
Name: "up",
Endpoints: nil,
},
},
BackendGroups: []state.BackendGroup{bg},
BackendGroups: []graph.BackendGroup{bg},
}
generator := config.NewGeneratorImpl()
cfg := string(generator.Generate(conf))
Expand Down
12 changes: 6 additions & 6 deletions internal/nginx/config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
"sigs.k8s.io/gateway-api/apis/v1beta1"

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

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

const rootPath = "/"

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

return execute(serversTemplate, servers)
}

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

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

func createSSLServer(virtualServer state.VirtualServer) http.Server {
func createSSLServer(virtualServer dataplane.VirtualServer) http.Server {
if virtualServer.IsDefault {
return createDefaultSSLServer()
}
Expand All @@ -51,7 +51,7 @@ func createSSLServer(virtualServer state.VirtualServer) http.Server {
}
}

func createServer(virtualServer state.VirtualServer) http.Server {
func createServer(virtualServer dataplane.VirtualServer) http.Server {
if virtualServer.IsDefault {
return createDefaultHTTPServer()
}
Expand All @@ -62,7 +62,7 @@ func createServer(virtualServer state.VirtualServer) http.Server {
}
}

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

if lenPathRules == 0 {
Expand Down
Loading