Skip to content

Commit

Permalink
Use context with leader election
Browse files Browse the repository at this point in the history
  • Loading branch information
pleshakov committed Nov 9, 2018
1 parent 6a3a89f commit 672de59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 14 additions & 11 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controller

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -62,7 +63,8 @@ type LoadBalancerController struct {
configMapLister utils.StoreToConfigMapLister
secretLister utils.StoreToSecretLister
syncQueue *queue.TaskQueue
stop chan struct{}
ctx context.Context
cancel context.CancelFunc
configurator *nginx.Configurator
watchNginxConfigMaps bool
isNginxPlus bool
Expand Down Expand Up @@ -101,7 +103,6 @@ type NewLoadBalancerControllerInput struct {
func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalancerController {
lbc := LoadBalancerController{
client: input.KubeClient,
stop: make(chan struct{}),
configurator: input.NginxConfigurator,
defaultServerSecret: input.DefaultServerSecret,
isNginxPlus: input.IsNginxPlus,
Expand Down Expand Up @@ -243,23 +244,25 @@ func (lbc *LoadBalancerController) GetDefaultServerSecret() string {

// Run starts the loadbalancer controller
func (lbc *LoadBalancerController) Run() {
lbc.ctx, lbc.cancel = context.WithCancel(context.Background())

if lbc.leaderElector != nil {
go lbc.leaderElector.Run()
go lbc.leaderElector.Run(lbc.ctx)
}
go lbc.svcController.Run(lbc.stop)
go lbc.endpointController.Run(lbc.stop)
go lbc.secretController.Run(lbc.stop)
go lbc.svcController.Run(lbc.ctx.Done())
go lbc.endpointController.Run(lbc.ctx.Done())
go lbc.secretController.Run(lbc.ctx.Done())
if lbc.watchNginxConfigMaps {
go lbc.configMapController.Run(lbc.stop)
go lbc.configMapController.Run(lbc.ctx.Done())
}
go lbc.ingressController.Run(lbc.stop)
go lbc.syncQueue.Run(time.Second, lbc.stop)
<-lbc.stop
go lbc.ingressController.Run(lbc.ctx.Done())
go lbc.syncQueue.Run(time.Second, lbc.ctx.Done())
<-lbc.ctx.Done()
}

// Stop shutdowns the load balancer controller
func (lbc *LoadBalancerController) Stop() {
close(lbc.stop)
lbc.cancel()

lbc.syncQueue.Shutdown()
}
Expand Down
4 changes: 3 additions & 1 deletion internal/handlers/leader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handlers

import (
"context"

"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/controller"
"k8s.io/client-go/tools/leaderelection"
Expand All @@ -9,7 +11,7 @@ import (
// CreateLeaderHandler builds the handler funcs for leader handling
func CreateLeaderHandler(lbc *controller.LoadBalancerController) leaderelection.LeaderCallbacks {
return leaderelection.LeaderCallbacks{
OnStartedLeading: func(stop <-chan struct{}) {
OnStartedLeading: func(ctx context.Context) {
glog.V(3).Info("started leading, updating ingress status")
ingresses, mergeableIngresses := lbc.GetManagedIngresses()
err := lbc.UpdateManagedAndMergeableIngresses(ingresses, mergeableIngresses)
Expand Down

0 comments on commit 672de59

Please sign in to comment.