Skip to content

Commit

Permalink
Fix small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
talsabagport committed Aug 28, 2024
1 parent 5ac76d1 commit a11424c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/event_handler/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/event_handler/polling/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions pkg/handlers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions pkg/handlers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}

0 comments on commit a11424c

Please sign in to comment.