Skip to content

Commit

Permalink
fix: wait for the default cluster ready continuously (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers committed May 14, 2021
1 parent fb11efc commit 9d0e0b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/apisix/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ func (c *cluster) HasSynced(ctx context.Context) error {
log.Errorf("failed to wait cluster to ready: %s", ctx.Err())
return ctx.Err()
case <-c.cacheSynced:
if c.cacheSyncErr != nil {
// See https://github.com/apache/apisix-ingress-controller/issues/448
// for more details.
return c.cacheSyncErr
}
log.Warnf("cluster %s now is ready, cost time %s", c.name, time.Since(now).String())
return nil
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type Controller struct {
// Secret object.
secretSSLMap *sync.Map

// leaderContextCancelFunc will be called when apisix-ingress-controller
// decides to give up its leader role.
leaderContextCancelFunc context.CancelFunc

// common informers and listers
epInformer cache.SharedIndexInformer
epLister listerscorev1.EndpointsLister
Expand Down Expand Up @@ -287,7 +291,10 @@ func (c *Controller) Run(stop chan struct{}) error {
c.metricsCollector.ResetLeader(false)
},
},
ReleaseOnCancel: true,
// Set it to false as current leaderelection implementation will report
// "Failed to release lock: resource name may not be empty" error when
// ReleaseOnCancel is true and the Run context is cancelled.
ReleaseOnCancel: false,
Name: "ingress-apisix",
}

Expand All @@ -298,7 +305,9 @@ func (c *Controller) Run(stop chan struct{}) error {
}

election:
elector.Run(rootCtx)
curCtx, cancel := context.WithCancel(rootCtx)
c.leaderContextCancelFunc = cancel
elector.Run(curCtx)
select {
case <-rootCtx.Done():
return nil
Expand All @@ -312,6 +321,7 @@ func (c *Controller) run(ctx context.Context) {
zap.String("namespace", c.namespace),
zap.String("pod", c.name),
)
defer c.leaderContextCancelFunc()
c.metricsCollector.ResetLeader(true)

err := c.apisix.AddCluster(&apisix.ClusterOptions{
Expand Down

0 comments on commit 9d0e0b8

Please sign in to comment.