From 6e3afb229eb641d8a0ef921761bfb3c8e763dccf Mon Sep 17 00:00:00 2001 From: Saylor Berman Date: Fri, 20 Dec 2024 08:59:26 -0700 Subject: [PATCH] Add logger; change function name --- internal/mode/static/handler.go | 2 +- internal/mode/static/handler_test.go | 6 +-- internal/mode/static/manager.go | 5 +- internal/mode/static/nginx/agent/agent.go | 17 ++++--- .../agent/agentfakes/fake_nginx_updater.go | 48 +++++++++---------- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/internal/mode/static/handler.go b/internal/mode/static/handler.go index 05d6540cd..a07688481 100644 --- a/internal/mode/static/handler.go +++ b/internal/mode/static/handler.go @@ -300,7 +300,7 @@ func (h *eventHandlerImpl) parseAndCaptureEvent(ctx context.Context, logger logr func (h *eventHandlerImpl) updateNginxConf(conf dataplane.Configuration) error { files := h.cfg.generator.Generate(conf) - h.cfg.nginxUpdater.UpdateNginxConfig(len(files)) + h.cfg.nginxUpdater.UpdateConfig(len(files)) // If using NGINX Plus, update upstream servers using the API. if h.cfg.plus { diff --git a/internal/mode/static/handler_test.go b/internal/mode/static/handler_test.go index 976b6a3fd..ce185565c 100644 --- a/internal/mode/static/handler_test.go +++ b/internal/mode/static/handler_test.go @@ -64,8 +64,8 @@ var _ = Describe("eventHandler", func() { Expect(fakeGenerator.GenerateCallCount()).Should(Equal(1)) Expect(fakeGenerator.GenerateArgsForCall(0)).Should(Equal(expectedConf)) - Expect(fakeNginxUpdater.UpdateNginxConfigCallCount()).Should(Equal(1)) - lenFiles := fakeNginxUpdater.UpdateNginxConfigArgsForCall(0) + Expect(fakeNginxUpdater.UpdateConfigCallCount()).Should(Equal(1)) + lenFiles := fakeNginxUpdater.UpdateConfigArgsForCall(0) Expect(expectedFiles).To(HaveLen(lenFiles)) Expect(fakeStatusUpdater.UpdateGroupCallCount()).Should(Equal(2)) @@ -433,7 +433,7 @@ var _ = Describe("eventHandler", func() { Expect(helpers.Diff(handler.GetLatestConfiguration(), &dcfg)).To(BeEmpty()) Expect(fakeGenerator.GenerateCallCount()).To(Equal(1)) - Expect(fakeNginxUpdater.UpdateNginxConfigCallCount()).To(Equal(1)) + Expect(fakeNginxUpdater.UpdateConfigCallCount()).To(Equal(1)) Expect(fakeNginxUpdater.UpdateUpstreamServersCallCount()).To(Equal(0)) }) }) diff --git a/internal/mode/static/manager.go b/internal/mode/static/manager.go index 261eded3e..fc2ced95f 100644 --- a/internal/mode/static/manager.go +++ b/internal/mode/static/manager.go @@ -177,7 +177,10 @@ func StartManager(cfg config.Config) error { }) eventHandler := newEventHandlerImpl(eventHandlerConfig{ - nginxUpdater: &agent.NginxUpdaterImpl{}, + nginxUpdater: &agent.NginxUpdaterImpl{ + Logger: cfg.Logger.WithName("nginxUpdater"), + Plus: cfg.Plus, + }, metricsCollector: handlerCollector, statusUpdater: groupStatusUpdater, processor: processor, diff --git a/internal/mode/static/nginx/agent/agent.go b/internal/mode/static/nginx/agent/agent.go index 4ce9cf3e5..5105e3e3a 100644 --- a/internal/mode/static/nginx/agent/agent.go +++ b/internal/mode/static/nginx/agent/agent.go @@ -1,6 +1,8 @@ package agent -import "fmt" +import ( + "github.com/go-logr/logr" +) //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate @@ -8,18 +10,19 @@ import "fmt" // NginxUpdater is an interface for updating NGINX using the NGINX agent. type NginxUpdater interface { - UpdateNginxConfig(int) + UpdateConfig(int) UpdateUpstreamServers() } // NginxUpdaterImpl implements the NginxUpdater interface. type NginxUpdaterImpl struct { - Plus bool + Logger logr.Logger + Plus bool } -// UpdateNginxConfig sends the nginx configuration to the agent. -func (n *NginxUpdaterImpl) UpdateNginxConfig(files int) { - fmt.Println("Sending nginx configuration to agent.", "numFiles", files) +// UpdateConfig sends the nginx configuration to the agent. +func (n *NginxUpdaterImpl) UpdateConfig(files int) { + n.Logger.Info("Sending nginx configuration to agent.", "numFiles", files) } // UpdateUpstreamServers sends an APIRequest to the agent to update upstream servers using the NGINX Plus API. @@ -29,5 +32,5 @@ func (n *NginxUpdaterImpl) UpdateUpstreamServers() { return } - fmt.Println("Updating upstream servers using NGINX Plus API.") + n.Logger.Info("Updating upstream servers using NGINX Plus API.") } diff --git a/internal/mode/static/nginx/agent/agentfakes/fake_nginx_updater.go b/internal/mode/static/nginx/agent/agentfakes/fake_nginx_updater.go index 8f7bb9da9..013381415 100644 --- a/internal/mode/static/nginx/agent/agentfakes/fake_nginx_updater.go +++ b/internal/mode/static/nginx/agent/agentfakes/fake_nginx_updater.go @@ -8,9 +8,9 @@ import ( ) type FakeNginxUpdater struct { - UpdateNginxConfigStub func(int) - updateNginxConfigMutex sync.RWMutex - updateNginxConfigArgsForCall []struct { + UpdateConfigStub func(int) + updateConfigMutex sync.RWMutex + updateConfigArgsForCall []struct { arg1 int } UpdateUpstreamServersStub func() @@ -21,35 +21,35 @@ type FakeNginxUpdater struct { invocationsMutex sync.RWMutex } -func (fake *FakeNginxUpdater) UpdateNginxConfig(arg1 int) { - fake.updateNginxConfigMutex.Lock() - fake.updateNginxConfigArgsForCall = append(fake.updateNginxConfigArgsForCall, struct { +func (fake *FakeNginxUpdater) UpdateConfig(arg1 int) { + fake.updateConfigMutex.Lock() + fake.updateConfigArgsForCall = append(fake.updateConfigArgsForCall, struct { arg1 int }{arg1}) - stub := fake.UpdateNginxConfigStub - fake.recordInvocation("UpdateNginxConfig", []interface{}{arg1}) - fake.updateNginxConfigMutex.Unlock() + stub := fake.UpdateConfigStub + fake.recordInvocation("UpdateConfig", []interface{}{arg1}) + fake.updateConfigMutex.Unlock() if stub != nil { - fake.UpdateNginxConfigStub(arg1) + fake.UpdateConfigStub(arg1) } } -func (fake *FakeNginxUpdater) UpdateNginxConfigCallCount() int { - fake.updateNginxConfigMutex.RLock() - defer fake.updateNginxConfigMutex.RUnlock() - return len(fake.updateNginxConfigArgsForCall) +func (fake *FakeNginxUpdater) UpdateConfigCallCount() int { + fake.updateConfigMutex.RLock() + defer fake.updateConfigMutex.RUnlock() + return len(fake.updateConfigArgsForCall) } -func (fake *FakeNginxUpdater) UpdateNginxConfigCalls(stub func(int)) { - fake.updateNginxConfigMutex.Lock() - defer fake.updateNginxConfigMutex.Unlock() - fake.UpdateNginxConfigStub = stub +func (fake *FakeNginxUpdater) UpdateConfigCalls(stub func(int)) { + fake.updateConfigMutex.Lock() + defer fake.updateConfigMutex.Unlock() + fake.UpdateConfigStub = stub } -func (fake *FakeNginxUpdater) UpdateNginxConfigArgsForCall(i int) int { - fake.updateNginxConfigMutex.RLock() - defer fake.updateNginxConfigMutex.RUnlock() - argsForCall := fake.updateNginxConfigArgsForCall[i] +func (fake *FakeNginxUpdater) UpdateConfigArgsForCall(i int) int { + fake.updateConfigMutex.RLock() + defer fake.updateConfigMutex.RUnlock() + argsForCall := fake.updateConfigArgsForCall[i] return argsForCall.arg1 } @@ -80,8 +80,8 @@ func (fake *FakeNginxUpdater) UpdateUpstreamServersCalls(stub func()) { func (fake *FakeNginxUpdater) Invocations() map[string][][]interface{} { fake.invocationsMutex.RLock() defer fake.invocationsMutex.RUnlock() - fake.updateNginxConfigMutex.RLock() - defer fake.updateNginxConfigMutex.RUnlock() + fake.updateConfigMutex.RLock() + defer fake.updateConfigMutex.RUnlock() fake.updateUpstreamServersMutex.RLock() defer fake.updateUpstreamServersMutex.RUnlock() copiedInvocations := map[string][][]interface{}{}