Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add work queue to process endpoint changes #482

Merged
merged 1 commit into from
Oct 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Controller struct {

// serviceQueue takes service key as work item. Service key with format "namespace/name".
serviceQueue workqueue.RateLimitingInterface
// endpointQueue takes endpoint key as work item. Endpoint key with format "namespace/name".
endpointQueue workqueue.RateLimitingInterface

// syncTracker tracks the latest time that service and endpoint changes are processed
syncTracker utils.TimeTracker
Expand Down Expand Up @@ -103,6 +105,7 @@ func NewController(
ingressLister: ctx.IngressInformer.GetIndexer(),
serviceLister: ctx.ServiceInformer.GetIndexer(),
serviceQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
endpointQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
syncTracker: utils.NewTimeTracker(),
}

Expand Down Expand Up @@ -147,10 +150,10 @@ func NewController(
})

ctx.EndpointInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: negController.processEndpoint,
DeleteFunc: negController.processEndpoint,
AddFunc: negController.enqueueEndpoint,
DeleteFunc: negController.enqueueEndpoint,
UpdateFunc: func(old, cur interface{}) {
negController.processEndpoint(cur)
negController.enqueueEndpoint(cur)
},
})
ctx.AddHealthCheck("neg-controller", negController.IsHealthy)
Expand All @@ -170,6 +173,7 @@ func (c *Controller) Run(stopCh <-chan struct{}) {
}()

go wait.Until(c.serviceWorker, time.Second, stopCh)
go wait.Until(c.endpointWorker, time.Second, stopCh)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expose the sync interval as a flag

go func() {
// Wait for gcPeriod to run the first GC
// This is to make sure that all services are fully processed before running GC.
Expand All @@ -195,23 +199,33 @@ func (c *Controller) IsHealthy() error {
func (c *Controller) stop() {
glog.V(2).Infof("Shutting down network endpoint group controller")
c.serviceQueue.ShutDown()
c.endpointQueue.ShutDown()
c.manager.ShutDown()
}

func (c *Controller) endpointWorker() {
for {
func() {
key, quit := c.endpointQueue.Get()
if quit {
return
}
c.processEndpoint(key.(string))
c.endpointQueue.Done(key)
}()
}
}

// processEndpoint finds the related syncers and signal it to sync
func (c *Controller) processEndpoint(obj interface{}) {
func (c *Controller) processEndpoint(key string) {
defer func() {
now := c.syncTracker.Track()
lastSyncTimestamp.WithLabelValues().Set(float64(now.UTC().UnixNano()))
}()

key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
glog.Errorf("Failed to generate endpoint key: %v", err)
return
}
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
glog.Errorf("Failed to split endpoint namespaced key %q: %v", key, err)
return
}
c.manager.Sync(namespace, name)
Expand Down Expand Up @@ -361,6 +375,15 @@ func (c *Controller) handleErr(err error, key interface{}) {
c.serviceQueue.AddRateLimited(key)
}

func (c *Controller) enqueueEndpoint(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
glog.Errorf("Failed to generate endpoint key: %v", err)
return
}
c.endpointQueue.Add(key)
}

func (c *Controller) enqueueService(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
Expand Down