service controller: add OnAdd and OnDelete node event handlers

Signed-off-by: Andrew Sy Kim <kiman@vmware.com>
This commit is contained in:
Andrew Sy Kim
2019-08-08 15:49:08 -04:00
committed by andrewsykim
parent 8fb66ae965
commit 8b490dea3a

View File

@@ -19,11 +19,10 @@ package service
import (
"context"
"fmt"
"reflect"
"sync"
"time"
"reflect"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
@@ -101,6 +100,7 @@ type serviceCache struct {
type Controller struct {
cloud cloudprovider.Interface
knownHosts []*v1.Node
knownHostsLock sync.Mutex
servicesToUpdate []*v1.Service
kubeClient clientset.Interface
clusterName string
@@ -175,6 +175,21 @@ func New(
s.serviceLister = serviceInformer.Lister()
s.serviceListerSynced = serviceInformer.Informer().HasSynced
nodeInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: func(cur interface{}) {
s.nodeSyncLoop()
},
UpdateFunc: func(old, cur interface{}) {
s.nodeSyncLoop()
},
DeleteFunc: func(old interface{}) {
s.nodeSyncLoop()
},
},
time.Duration(0),
)
if err := s.init(); err != nil {
return nil, err
}
@@ -646,6 +661,8 @@ func getNodeConditionPredicate() NodeConditionPredicate {
// nodeSyncLoop handles updating the hosts pointed to by all load
// balancers whenever the set of nodes in the cluster changes.
func (s *Controller) nodeSyncLoop() {
s.knownHostsLock.Lock()
defer s.knownHostsLock.Unlock()
newHosts, err := listWithPredicate(s.nodeLister, getNodeConditionPredicate())
if err != nil {
runtime.HandleError(fmt.Errorf("Failed to retrieve current set of nodes from node lister: %v", err))