From a11424c3b60d3460ff68edaa4b4768f9381dc0f7 Mon Sep 17 00:00:00 2001 From: talsabagport Date: Wed, 28 Aug 2024 13:56:35 +0300 Subject: [PATCH] Fix small bugs --- pkg/event_handler/event_handler.go | 3 ++- pkg/event_handler/polling/polling.go | 6 ++---- pkg/handlers/controllers.go | 6 ++++++ pkg/handlers/controllers_test.go | 11 +++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/event_handler/event_handler.go b/pkg/event_handler/event_handler.go index ae4b41d..0f72413 100644 --- a/pkg/event_handler/event_handler.go +++ b/pkg/event_handler/event_handler.go @@ -2,6 +2,7 @@ package event_handler import ( "fmt" + "github.com/port-labs/port-k8s-exporter/pkg/handlers" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/klog/v2" ) @@ -22,7 +23,7 @@ func Start(eventListener IListener, initControllerHandler func() (IStoppableRsyn return eventListener.Run(func() { klog.Infof("Resync request received. Recreating controllers for the new port configuration") - if controllerHandler != nil { + if controllerHandler != (*handlers.ControllersHandler)(nil) { controllerHandler.Stop() } diff --git a/pkg/event_handler/polling/polling.go b/pkg/event_handler/polling/polling.go index 8a7bdc6..770d1a5 100644 --- a/pkg/event_handler/polling/polling.go +++ b/pkg/event_handler/polling/polling.go @@ -75,10 +75,8 @@ func (h *Handler) Run(resync func()) { klog.Infof("Polling event listener iteration after %d seconds. Checking for changes...", h.pollingRate) configuration, err := integration.GetIntegration(h.portClient, h.stateKey) if err != nil { - klog.Errorf("error resyncing: %s", err.Error()) - } - - if reflect.DeepEqual(currentState, configuration) != true { + klog.Errorf("error getting integration: %s", err.Error()) + } else if reflect.DeepEqual(currentState, configuration) != true { klog.Infof("Changes detected. Resyncing...") currentState = configuration resync() diff --git a/pkg/handlers/controllers.go b/pkg/handlers/controllers.go index f88a548..db2b722 100644 --- a/pkg/handlers/controllers.go +++ b/pkg/handlers/controllers.go @@ -23,6 +23,7 @@ type ControllersHandler struct { stateKey string portClient *cli.PortClient stopCh chan struct{} + isStopped bool } func NewControllersHandler(exporterConfig *port.Config, portConfig *port.IntegrationAppConfig, k8sClient *k8s.Client, portClient *cli.PortClient) *ControllersHandler { @@ -140,6 +141,11 @@ func (c *ControllersHandler) runDeleteStaleEntities(ctx context.Context, current } func (c *ControllersHandler) Stop() { + if c.isStopped { + return + } + klog.Info("Stopping controllers") close(c.stopCh) + c.isStopped = true } diff --git a/pkg/handlers/controllers_test.go b/pkg/handlers/controllers_test.go index 9629eaf..c8db339 100644 --- a/pkg/handlers/controllers_test.go +++ b/pkg/handlers/controllers_test.go @@ -527,3 +527,14 @@ func TestControllersHandleTolerateFailure(t *testing.T) { return err != nil && strings.Contains(err.Error(), "was not found") }, time.Second*5, time.Millisecond*500) } + +func TestControllersHandler_Stop(t *testing.T) { + resources := []port.Resource{getBaseResource(deploymentKind)} + f := newFixture(t, &fixtureConfig{resources: resources, existingObjects: []runtime.Object{}}) + + f.controllersHandler.Stop() + assert.True(t, f.controllersHandler.isStopped) + f.controllersHandler.Stop() + assert.True(t, f.controllersHandler.isStopped) + assert.Panics(t, func() { close(f.controllersHandler.stopCh) }) +}