diff --git a/internal/ingress/controller/nginx.go b/internal/ingress/controller/nginx.go index 935f8dcdd..a5480cbc4 100644 --- a/internal/ingress/controller/nginx.go +++ b/internal/ingress/controller/nginx.go @@ -18,6 +18,7 @@ package controller import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -256,7 +257,7 @@ func (n *NGINXController) Start() { n.store.Run(n.stopCh) if n.syncStatus != nil { - go n.syncStatus.Run() + go n.syncStatus.Run(context.Background()) } cmd := nginxExecCommand() diff --git a/internal/ingress/status/status.go b/internal/ingress/status/status.go index acf802b34..937a688b7 100644 --- a/internal/ingress/status/status.go +++ b/internal/ingress/status/status.go @@ -17,6 +17,7 @@ limitations under the License. package status import ( + "context" "fmt" "net" "os" @@ -50,7 +51,7 @@ const ( // Sync ... type Sync interface { - Run() + Run(ctx context.Context) Shutdown() } @@ -98,8 +99,8 @@ type statusSync struct { } // Run starts the loop to keep the status in sync -func (s statusSync) Run() { - s.elector.Run() +func (s statusSync) Run(ctx context.Context) { + s.elector.Run(ctx) } // Shutdown stop the sync. In case the instance is the leader it will remove the current IP @@ -179,19 +180,22 @@ func NewStatusSyncer(config Config) Sync { electionID = fmt.Sprintf("%v-%v", config.ElectionID, config.IngressClass) } + var stopCh chan struct{} callbacks := leaderelection.LeaderCallbacks{ - OnStartedLeading: func(stop <-chan struct{}) { + OnStartedLeading: func(ctx context.Context) { glog.V(2).Infof("I am the new status update leader") - go st.syncQueue.Run(time.Second, stop) + stopCh = make(chan struct{}) + go st.syncQueue.Run(time.Second, stopCh) // when this instance is the leader we need to enqueue // an item to trigger the update of the Ingress status. wait.PollUntil(updateInterval, func() (bool, error) { st.syncQueue.EnqueueTask(task.GetDummyObject("sync status")) return false, nil - }, stop) + }, stopCh) }, OnStoppedLeading: func() { glog.V(2).Infof("I am not status update leader anymore") + close(stopCh) }, OnNewLeader: func(identity string) { glog.Infof("new leader elected: %v", identity) diff --git a/internal/ingress/status/status_test.go b/internal/ingress/status/status_test.go index c4f223ec5..7234193ff 100644 --- a/internal/ingress/status/status_test.go +++ b/internal/ingress/status/status_test.go @@ -17,6 +17,7 @@ limitations under the License. package status import ( + "context" "os" "testing" "time" @@ -297,7 +298,7 @@ func TestStatusActions(t *testing.T) { fk := fkSync.(statusSync) // start it and wait for the election and syn actions - go fk.Run() + go fk.Run(context.Background()) // wait for the election time.Sleep(100 * time.Millisecond) // execute sync