From 1a873cb58a2c838c4c54cbabfc6dcee27f384be4 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Tue, 5 Jul 2022 09:53:23 -0500 Subject: [PATCH] Improved logging of registered mappings (#96) --- server/k8s_test.go | 6 ++---- server/routes.go | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/server/k8s_test.go b/server/k8s_test.go index 639952d..eb2e2f7 100644 --- a/server/k8s_test.go +++ b/server/k8s_test.go @@ -78,8 +78,7 @@ func TestK8sWatcherImpl_handleAddThenUpdate(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - // reset the routes - Routes.RegisterAll(map[string]string{}) + Routes.Reset() watcher := &k8sWatcherImpl{} initialSvc := v1.Service{} @@ -150,8 +149,7 @@ func TestK8sWatcherImpl_handleAddThenDelete(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - // reset the routes - Routes.RegisterAll(map[string]string{}) + Routes.Reset() watcher := &k8sWatcherImpl{} initialSvc := v1.Service{} diff --git a/server/routes.go b/server/routes.go index a315c27..bd0554e 100644 --- a/server/routes.go +++ b/server/routes.go @@ -86,6 +86,7 @@ func routesSetDefault(writer http.ResponseWriter, request *http.Request) { } type IRoutes interface { + Reset() RegisterAll(mappings map[string]string) // FindBackendForServerAddress returns the host:port for the external server address, if registered. // Otherwise, an empty string is returned. Also returns the normalized version of the given serverAddress. @@ -98,7 +99,7 @@ type IRoutes interface { SimplifySRV(srvEnabled bool) } -var Routes IRoutes = &routesImpl{} +var Routes = NewRoutes() func NewRoutes() IRoutes { r := &routesImpl{ @@ -109,12 +110,8 @@ func NewRoutes() IRoutes { } func (r *routesImpl) RegisterAll(mappings map[string]string) { - r.Lock() - defer r.Unlock() - - r.mappings = make(map[string]mapping) for k, v := range mappings { - r.mappings[k] = mapping{backend: v, waker: func(ctx context.Context) error { return nil }} + r.CreateMapping(k, v, func(ctx context.Context) error { return nil }) } } @@ -130,6 +127,10 @@ type routesImpl struct { simplifySRV bool } +func (r *routesImpl) Reset() { + r.mappings = make(map[string]mapping) +} + func (r *routesImpl) SetDefaultRoute(backend string) { r.defaultRoute = backend @@ -146,6 +147,10 @@ func (r *routesImpl) FindBackendForServerAddress(ctx context.Context, serverAddr r.RLock() defer r.RUnlock() + logrus.WithFields(logrus.Fields{ + "serverAddress": serverAddress, + }).Debug("Finding backend for server address") + if r.simplifySRV { serverAddress = strings.TrimSuffix(serverAddress, ".") parts := strings.Split(serverAddress, ".") @@ -208,6 +213,6 @@ func (r *routesImpl) CreateMapping(serverAddress string, backend string, waker f logrus.WithFields(logrus.Fields{ "serverAddress": serverAddress, "backend": backend, - }).Info("Creating route") + }).Info("Created route mapping") r.mappings[serverAddress] = mapping{backend: backend, waker: waker} }