Skip to content

Commit d7b261d

Browse files
committed
Move version state to event handler
1 parent ac09dcd commit d7b261d

File tree

8 files changed

+37
-30
lines changed

8 files changed

+37
-30
lines changed

internal/mode/static/handler.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type eventHandlerConfig struct {
4545
logger logr.Logger
4646
// controlConfigNSName is the NamespacedName of the NginxGateway config for this controller.
4747
controlConfigNSName types.NamespacedName
48+
// version is the current version number of the nginx config.
49+
version int
4850
}
4951

5052
// eventHandlerImpl implements EventHandler.
@@ -90,7 +92,11 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, batch events.Ev
9092
}
9193

9294
var nginxReloadRes nginxReloadResult
93-
if err := h.updateNginx(ctx, dataplane.BuildConfiguration(ctx, graph, h.cfg.serviceResolver)); err != nil {
95+
h.cfg.version++
96+
if err := h.updateNginx(
97+
ctx,
98+
dataplane.BuildConfiguration(ctx, graph, h.cfg.serviceResolver, h.cfg.version),
99+
); err != nil {
94100
h.cfg.logger.Error(err, "Failed to update NGINX configuration")
95101
nginxReloadRes.error = err
96102
} else {
@@ -100,7 +106,7 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, batch events.Ev
100106
h.cfg.statusUpdater.Update(ctx, buildStatuses(graph, nginxReloadRes))
101107
}
102108

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

106112
if err := h.cfg.nginxFileMgr.ReplaceFiles(files); err != nil {

internal/mode/static/handler_test.go

+3-3
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)
@@ -111,7 +111,7 @@ var _ = Describe("eventHandler", func() {
111111
handler.HandleEventBatch(context.Background(), batch)
112112

113113
checkUpsertEventExpectations(e)
114-
expectReconfig(dataplane.Configuration{}, fakeCfgFiles)
114+
expectReconfig(dataplane.Configuration{Version: 1}, fakeCfgFiles)
115115
})
116116

117117
It("should process Delete", func() {
@@ -124,7 +124,7 @@ var _ = Describe("eventHandler", func() {
124124
handler.HandleEventBatch(context.Background(), batch)
125125

126126
checkDeleteEventExpectations(e)
127-
expectReconfig(dataplane.Configuration{}, fakeCfgFiles)
127+
expectReconfig(dataplane.Configuration{Version: 1}, fakeCfgFiles)
128128
})
129129
})
130130

internal/mode/static/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func StartManager(cfg config.Config) error {
212212
eventHandler := newEventHandlerImpl(eventHandlerConfig{
213213
processor: processor,
214214
serviceResolver: resolver.NewServiceResolverImpl(mgr.GetClient()),
215-
generator: &configGenerator,
215+
generator: configGenerator,
216216
logger: cfg.Logger.WithName("eventHandler"),
217217
logLevelSetter: logLevelSetter,
218218
nginxFileMgr: nginxFileMgr,

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

+6-6
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

+6-12
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
35+
Generate(configuration dataplane.Configuration) []file.File
3636
}
3737

3838
// GeneratorImpl is an implementation of Generator.
@@ -43,15 +43,11 @@ type Generator interface {
4343
//
4444
// It also expects that the main NGINX configuration file nginx.conf is located in configFolder and nginx.conf
4545
// includes (https://nginx.org/en/docs/ngx_core_module.html#include) the files from httpFolder.
46-
type GeneratorImpl struct {
47-
configVersion int
48-
}
46+
type GeneratorImpl struct{}
4947

5048
// NewGeneratorImpl creates a new GeneratorImpl.
5149
func NewGeneratorImpl() GeneratorImpl {
52-
return GeneratorImpl{
53-
configVersion: 0,
54-
}
50+
return GeneratorImpl{}
5551
}
5652

5753
// executeFunc is a function that generates NGINX configuration from internal representation.
@@ -61,18 +57,16 @@ type executeFunc func(configuration dataplane.Configuration) []byte
6157
// It is the responsibility of the caller to validate the configuration before calling this function.
6258
// In case of invalid configuration, NGINX will fail to reload or could be configured with malicious configuration.
6359
// To validate, use the validators from the validation package.
64-
func (g *GeneratorImpl) Generate(conf *dataplane.Configuration) []file.File {
65-
g.configVersion++
66-
conf.Version = g.configVersion
60+
func (g GeneratorImpl) Generate(conf dataplane.Configuration) []file.File {
6761
files := make([]file.File, 0, len(conf.SSLKeyPairs)+1 /* http config */)
6862

6963
for id, pair := range conf.SSLKeyPairs {
7064
files = append(files, generatePEM(id, pair.Cert, pair.Key))
7165
}
7266

73-
files = append(files, generateHTTPConfig(*conf))
67+
files = append(files, generateHTTPConfig(conf))
7468

75-
files = append(files, generateConfigVersion(g.configVersion))
69+
files = append(files, generateConfigVersion(conf.Version))
7670

7771
return files
7872
}

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

+1-1
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 := generator.Generate(&conf)
67+
files := generator.Generate(conf)
6868

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

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ 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(
20+
ctx context.Context,
21+
g *graph.Graph,
22+
resolver resolver.ServiceResolver,
23+
configVersion int,
24+
) Configuration {
2025
if g.GatewayClass == nil || !g.GatewayClass.Valid {
21-
return &Configuration{}
26+
return Configuration{Version: configVersion}
2227
}
2328

2429
if g.Gateway == nil {
25-
return &Configuration{}
30+
return Configuration{Version: configVersion}
2631
}
2732

2833
upstreams := buildUpstreams(ctx, g.Gateway.Listeners, resolver)
@@ -36,9 +41,10 @@ func BuildConfiguration(ctx context.Context, g *graph.Graph, resolver resolver.S
3641
Upstreams: upstreams,
3742
BackendGroups: backendGroups,
3843
SSLKeyPairs: keyPairs,
44+
Version: configVersion,
3945
}
4046

41-
return &config
47+
return config
4248
}
4349

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1488,13 +1488,14 @@ func TestBuildConfiguration(t *testing.T) {
14881488
t.Run(test.msg, func(t *testing.T) {
14891489
g := NewGomegaWithT(t)
14901490

1491-
result := BuildConfiguration(context.TODO(), test.graph, fakeResolver)
1491+
result := BuildConfiguration(context.TODO(), test.graph, fakeResolver, 1)
14921492

14931493
g.Expect(result.BackendGroups).To(ConsistOf(test.expConf.BackendGroups))
14941494
g.Expect(result.Upstreams).To(ConsistOf(test.expConf.Upstreams))
14951495
g.Expect(result.HTTPServers).To(ConsistOf(test.expConf.HTTPServers))
14961496
g.Expect(result.SSLServers).To(ConsistOf(test.expConf.SSLServers))
14971497
g.Expect(result.SSLKeyPairs).To(Equal(test.expConf.SSLKeyPairs))
1498+
g.Expect(result.Version).To(Equal(1))
14981499
})
14991500
}
15001501
}

0 commit comments

Comments
 (0)