Skip to content

Commit 9f5f71e

Browse files
committed
Move config version to dataplane.Configuration type
1 parent faee5aa commit 9f5f71e

File tree

8 files changed

+31
-33
lines changed

8 files changed

+31
-33
lines changed

internal/mode/static/handler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, batch events.Ev
100100
h.cfg.statusUpdater.Update(ctx, buildStatuses(graph, nginxReloadRes))
101101
}
102102

103-
func (h *eventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Configuration) error {
104-
files, configVersion := h.cfg.generator.Generate(conf)
103+
func (h *eventHandlerImpl) updateNginx(ctx context.Context, conf *dataplane.Configuration) error {
104+
files := h.cfg.generator.Generate(conf)
105105

106106
if err := h.cfg.nginxFileMgr.ReplaceFiles(files); err != nil {
107107
return fmt.Errorf("failed to replace NGINX configuration files: %w", err)
108108
}
109109

110-
if err := h.cfg.nginxRuntimeMgr.Reload(ctx, configVersion); err != nil {
110+
if err := h.cfg.nginxRuntimeMgr.Reload(ctx, conf.Version); err != nil {
111111
return fmt.Errorf("failed to reload NGINX: %w", err)
112112
}
113113

internal/mode/static/handler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var _ = Describe("eventHandler", func() {
4545
Expect(fakeProcessor.ProcessCallCount()).Should(Equal(1))
4646

4747
Expect(fakeGenerator.GenerateCallCount()).Should(Equal(1))
48-
Expect(fakeGenerator.GenerateArgsForCall(0)).Should(Equal(expectedConf))
48+
Expect(fakeGenerator.GenerateArgsForCall(0)).Should(Equal(&expectedConf))
4949

5050
Expect(fakeNginxFileMgr.ReplaceFilesCallCount()).Should(Equal(1))
5151
files := fakeNginxFileMgr.ReplaceFilesArgsForCall(0)
@@ -100,7 +100,7 @@ var _ = Describe("eventHandler", func() {
100100
BeforeEach(func() {
101101
fakeProcessor.ProcessReturns(true /* changed */, &graph.Graph{})
102102

103-
fakeGenerator.GenerateReturns(fakeCfgFiles, 1)
103+
fakeGenerator.GenerateReturns(fakeCfgFiles)
104104
})
105105

106106
When("a batch has one event", func() {

internal/mode/static/nginx/config/configfakes/fake_generator.go

+12-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var ConfigFolders = []string{httpFolder, secretsFolder}
3232
// This interface is used for testing purposes only.
3333
type Generator interface {
3434
// Generate generates NGINX configuration files from internal representation.
35-
Generate(configuration dataplane.Configuration) ([]file.File, int)
35+
Generate(configuration *dataplane.Configuration) []file.File
3636
}
3737

3838
// GeneratorImpl is an implementation of Generator.
@@ -61,19 +61,20 @@ type executeFunc func(configuration dataplane.Configuration) []byte
6161
// It is the responsibility of the caller to validate the configuration before calling this function.
6262
// In case of invalid configuration, NGINX will fail to reload or could be configured with malicious configuration.
6363
// To validate, use the validators from the validation package.
64-
func (g *GeneratorImpl) Generate(conf dataplane.Configuration) ([]file.File, int) {
64+
func (g *GeneratorImpl) Generate(conf *dataplane.Configuration) []file.File {
6565
g.configVersion++
66+
conf.Version = g.configVersion
6667
files := make([]file.File, 0, len(conf.SSLKeyPairs)+1 /* http config */)
6768

6869
for id, pair := range conf.SSLKeyPairs {
6970
files = append(files, generatePEM(id, pair.Cert, pair.Key))
7071
}
7172

72-
files = append(files, generateHTTPConfig(conf))
73+
files = append(files, generateHTTPConfig(*conf))
7374

7475
files = append(files, generateConfigVersion(g.configVersion))
7576

76-
return files, g.configVersion
77+
return files
7778
}
7879

7980
func generatePEM(id dataplane.SSLKeyPairID, cert []byte, key []byte) file.File {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestGenerate(t *testing.T) {
6464

6565
generator := config.NewGeneratorImpl()
6666

67-
files, version := generator.Generate(conf)
67+
files := generator.Generate(&conf)
6868

6969
g.Expect(files).To(HaveLen(3))
7070

@@ -87,5 +87,5 @@ func TestGenerate(t *testing.T) {
8787
g.Expect(files[2].Type).To(Equal(file.TypeRegular))
8888
g.Expect(files[2].Path).To(Equal("/etc/nginx/conf.d/config-version.conf"))
8989
configVersion := string(files[2].Content)
90-
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", version)))
90+
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", conf.Version)))
9191
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func TestExecuteServers(t *testing.T) {
7979
func TestExecuteForDefaultServers(t *testing.T) {
8080
testcases := []struct {
8181
msg string
82-
conf dataplane.Configuration
8382
httpPorts []int
8483
sslPorts []int
84+
conf dataplane.Configuration
8585
}{
8686
{
8787
conf: dataplane.Configuration{},

internal/mode/static/state/dataplane/configuration.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
const wildcardHostname = "~^"
1717

1818
// BuildConfiguration builds the Configuration from the Graph.
19-
func BuildConfiguration(ctx context.Context, g *graph.Graph, resolver resolver.ServiceResolver) Configuration {
19+
func BuildConfiguration(ctx context.Context, g *graph.Graph, resolver resolver.ServiceResolver) *Configuration {
2020
if g.GatewayClass == nil || !g.GatewayClass.Valid {
21-
return Configuration{}
21+
return &Configuration{}
2222
}
2323

2424
if g.Gateway == nil {
25-
return Configuration{}
25+
return &Configuration{}
2626
}
2727

2828
upstreams := buildUpstreams(ctx, g.Gateway.Listeners, resolver)
@@ -38,7 +38,7 @@ func BuildConfiguration(ctx context.Context, g *graph.Graph, resolver resolver.S
3838
SSLKeyPairs: keyPairs,
3939
}
4040

41-
return config
41+
return &config
4242
}
4343

4444
// buildSSLKeyPairs builds the SSLKeyPairs from the Secrets. It will only include Secrets that are referenced by

internal/mode/static/state/dataplane/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Configuration struct {
3131
Upstreams []Upstream
3232
// BackendGroups holds all unique BackendGroups.
3333
BackendGroups []BackendGroup
34+
// Version represents the version of the generated configuration.
35+
Version int
3436
}
3537

3638
// SSLKeyPairID is a unique identifier for a SSLKeyPair.

0 commit comments

Comments
 (0)