Skip to content

Commit

Permalink
Merge pull request #224 from gujingit/feature/skip-add-svc
Browse files Browse the repository at this point in the history
do not sync non-LoadBalancer type svc
  • Loading branch information
k8s-ci-robot committed Dec 2, 2020
2 parents 64a4f9e + 594a326 commit 9830b58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
32 changes: 32 additions & 0 deletions cloud-controller-manager/controller/service/context.go
Expand Up @@ -3,6 +3,7 @@ package service
import (
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/cloud-provider-alibaba-cloud/cloud-controller-manager/utils"
"k8s.io/klog"
"reflect"
"sort"
Expand Down Expand Up @@ -36,6 +37,21 @@ func (c *Context) Range(f func(key string, value *v1.Service) bool) {
}
func (c *Context) Remove(name string) { c.ctx.Delete(name) }

func NeedAdd(newService *v1.Service) bool {
if NeedLoadBalancer(newService) {
return true
}

// was LoadBalancer
_, ok := newService.Labels[utils.LabelServiceHash]
if ok {
klog.Infof("service %s/%s has hash label, which may was LoadBalancer",
newService.Namespace, newService.Name)
return true
}
return false
}

// NeedUpdate compare old and new service for possible changes
func NeedUpdate(old, newm *v1.Service, record record.EventRecorder) bool {
if !NeedLoadBalancer(old) &&
Expand Down Expand Up @@ -83,6 +99,22 @@ func NeedUpdate(old, newm *v1.Service, record record.EventRecorder) bool {
return false
}

//NeedDelete
func NeedDelete(service *v1.Service) bool {
if NeedLoadBalancer(service) {
return true
}

// was LoadBalancer
_, ok := service.Labels[utils.LabelServiceHash]
if ok {
klog.Infof("service %s/%s has hash label, which may was LoadBalancer",
service.Namespace, service.Name)
return true
}
return false
}

func NeedLoadBalancer(service *v1.Service) bool {
return service.Spec.Type == v1.ServiceTypeLoadBalancer
}
Expand Down
26 changes: 11 additions & 15 deletions cloud-controller-manager/controller/service/controller.go
Expand Up @@ -206,7 +206,7 @@ func (con *Controller) HandlerForNodesChange(
utils.Logf(svc, "node change: class not empty, skip process ")
return true
}
utils.Logf(svc, "node change: enque service")
utils.Logf(svc, "node change: enqueue service")
Enqueue(que, key(svc))
return true
},
Expand Down Expand Up @@ -241,7 +241,7 @@ func (con *Controller) HandlerForEndpointChange(
syncEndpoints := func(epd interface{}) {
ep, ok := epd.(*v1.Endpoints)
if !ok || ep == nil {
klog.Info("endpoints change: endpoint object is nil, skip")
klog.Info("endpoint change: endpoint object is nil, skip")
return
}
svc := ctx.Get(fmt.Sprintf("%s/%s", ep.Namespace, ep.Name))
Expand Down Expand Up @@ -314,32 +314,28 @@ func (con *Controller) HandlerForServiceChange(
cache.ResourceEventHandlerFuncs{
AddFunc: func(add interface{}) {
svc, ok := add.(*v1.Service)
if !ok {
utils.Logf(svc, "add: not type service %s, skip", reflect.TypeOf(add))
return
if ok && NeedAdd(svc) {
utils.Logf(svc, "controller: service addition event")
syncService(svc)
}
utils.Logf(svc, "service addition event received %s", reflect.TypeOf(svc))
syncService(svc)
},
UpdateFunc: func(old, cur interface{}) {
oldd, ok1 := old.(*v1.Service)
curr, ok2 := cur.(*v1.Service)
if ok1 && ok2 &&
NeedUpdate(oldd, curr, record) {
utils.Logf(curr, "service update event")
utils.Logf(curr, "controller: service update event")
syncService(curr)
}
},
DeleteFunc: func(cur interface{}) {
svc, ok := cur.(*v1.Service)
if !ok {
utils.Logf(svc, "delete: not type service %s, skip", reflect.TypeOf(cur))
return
if ok && NeedDelete(svc) {
utils.Logf(svc, "controller: service deletion received, %s", utils.PrettyJson(svc))
// recorder service in local context
context.Set(key(svc), svc)
syncService(svc)
}
utils.Logf(svc, "controller: service deletion received, %s", utils.PrettyJson(svc))
// recorder service in local context
context.Set(key(svc), svc)
syncService(svc)
},
},
SERVICE_SYNC_PERIOD,
Expand Down

0 comments on commit 9830b58

Please sign in to comment.