Improved logging of registered mappings (#96)

This commit is contained in:
Geoff Bourne 2022-07-05 09:53:23 -05:00 committed by GitHub
parent 9163ae66d4
commit 1a873cb58a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 11 deletions

View File

@ -78,8 +78,7 @@ func TestK8sWatcherImpl_handleAddThenUpdate(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
// reset the routes Routes.Reset()
Routes.RegisterAll(map[string]string{})
watcher := &k8sWatcherImpl{} watcher := &k8sWatcherImpl{}
initialSvc := v1.Service{} initialSvc := v1.Service{}
@ -150,8 +149,7 @@ func TestK8sWatcherImpl_handleAddThenDelete(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
// reset the routes Routes.Reset()
Routes.RegisterAll(map[string]string{})
watcher := &k8sWatcherImpl{} watcher := &k8sWatcherImpl{}
initialSvc := v1.Service{} initialSvc := v1.Service{}

View File

@ -86,6 +86,7 @@ func routesSetDefault(writer http.ResponseWriter, request *http.Request) {
} }
type IRoutes interface { type IRoutes interface {
Reset()
RegisterAll(mappings map[string]string) RegisterAll(mappings map[string]string)
// FindBackendForServerAddress returns the host:port for the external server address, if registered. // 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. // 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) SimplifySRV(srvEnabled bool)
} }
var Routes IRoutes = &routesImpl{} var Routes = NewRoutes()
func NewRoutes() IRoutes { func NewRoutes() IRoutes {
r := &routesImpl{ r := &routesImpl{
@ -109,12 +110,8 @@ func NewRoutes() IRoutes {
} }
func (r *routesImpl) RegisterAll(mappings map[string]string) { func (r *routesImpl) RegisterAll(mappings map[string]string) {
r.Lock()
defer r.Unlock()
r.mappings = make(map[string]mapping)
for k, v := range mappings { 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 simplifySRV bool
} }
func (r *routesImpl) Reset() {
r.mappings = make(map[string]mapping)
}
func (r *routesImpl) SetDefaultRoute(backend string) { func (r *routesImpl) SetDefaultRoute(backend string) {
r.defaultRoute = backend r.defaultRoute = backend
@ -146,6 +147,10 @@ func (r *routesImpl) FindBackendForServerAddress(ctx context.Context, serverAddr
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
logrus.WithFields(logrus.Fields{
"serverAddress": serverAddress,
}).Debug("Finding backend for server address")
if r.simplifySRV { if r.simplifySRV {
serverAddress = strings.TrimSuffix(serverAddress, ".") serverAddress = strings.TrimSuffix(serverAddress, ".")
parts := strings.Split(serverAddress, ".") parts := strings.Split(serverAddress, ".")
@ -208,6 +213,6 @@ func (r *routesImpl) CreateMapping(serverAddress string, backend string, waker f
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"serverAddress": serverAddress, "serverAddress": serverAddress,
"backend": backend, "backend": backend,
}).Info("Creating route") }).Info("Created route mapping")
r.mappings[serverAddress] = mapping{backend: backend, waker: waker} r.mappings[serverAddress] = mapping{backend: backend, waker: waker}
} }