From 7ec79c206f738b1bb4597cc43212a57926e2a1d8 Mon Sep 17 00:00:00 2001 From: "xianwei.zw" Date: Tue, 24 Sep 2019 12:08:18 +0800 Subject: [PATCH] code refactoring: isEniBackend and IsENIBackendType Signed-off-by: xianwei.zw --- .../controller/service/context.go | 9 --------- .../controller/service/controller.go | 4 ++-- cloud-controller-manager/framework.go | 17 +++++++++-------- cloud-controller-manager/listeners.go | 6 +----- cloud-controller-manager/utils/const.go | 14 ++++++++++++++ cloud-controller-manager/vgroups.go | 3 ++- 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cloud-controller-manager/controller/service/context.go b/cloud-controller-manager/controller/service/context.go index 482a7f42d..d6e624d96 100644 --- a/cloud-controller-manager/controller/service/context.go +++ b/cloud-controller-manager/controller/service/context.go @@ -4,12 +4,10 @@ import ( "github.com/golang/glog" "k8s.io/api/core/v1" "k8s.io/client-go/tools/record" - "k8s.io/cloud-provider-alibaba-cloud/cloud-controller-manager/utils" "reflect" "sort" "strings" "sync" - "os" ) type Context struct{ ctx sync.Map } @@ -42,13 +40,6 @@ func ServiceModeLocal(svc *v1.Service) bool { return svc.Spec.ExternalTrafficPolicy == v1.ServiceExternalTrafficPolicyTypeLocal } -func IsENIBackendType(svc *v1.Service) bool { - if os.Getenv("SERVICE_FORCE_BACKEND_ENI") == "true" { - return true - } - return svc.Annotations[utils.BACKEND_TYPE_LABEL] == utils.BACKEND_TYPE_ENI -} - // NeedUpdate compare old and new service for possible changes func NeedUpdate(old, newm *v1.Service, record record.EventRecorder) bool { if !NeedLoadBalancer(old) && diff --git a/cloud-controller-manager/controller/service/controller.go b/cloud-controller-manager/controller/service/controller.go index b50e6790d..43e0bb874 100644 --- a/cloud-controller-manager/controller/service/controller.go +++ b/cloud-controller-manager/controller/service/controller.go @@ -487,7 +487,7 @@ func (con *Controller) update(cached, svc *v1.Service) error { var ( err error ) - if IsENIBackendType(svc) { + if utils.IsENIBackendType(svc) { // Ensure ENI type backend eni, ok := con.cloud.(EnsureENI) if !ok { @@ -656,7 +656,7 @@ func (con *Controller) NodeSyncTask(k string) error { } defer utils.Logf(service, "finish sync backend for service\n\n") - if IsENIBackendType(service) { + if utils.IsENIBackendType(service) { eni, ok := con.cloud.(EnsureENI) if !ok { return fmt.Errorf("cloud does not implement EnsureENI interface") diff --git a/cloud-controller-manager/framework.go b/cloud-controller-manager/framework.go index b29740fd9..9a17b4418 100644 --- a/cloud-controller-manager/framework.go +++ b/cloud-controller-manager/framework.go @@ -10,6 +10,7 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/controller/service" + "k8s.io/cloud-provider-alibaba-cloud/cloud-controller-manager/utils" "sort" "strconv" "strings" @@ -686,8 +687,8 @@ func (f *FrameWork) ListenerEqual(id string, p v1.ServicePort, proto string) err return err } if resp.BackendServerPort == 0 || - (!isEniBackend(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || - (isEniBackend(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { + (!utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || + (utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { return fmt.Errorf("TCPBackendServerPortNotEqual") } @@ -712,8 +713,8 @@ func (f *FrameWork) ListenerEqual(id string, p v1.ServicePort, proto string) err return err } if resp.BackendServerPort == 0 || - (!isEniBackend(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || - (isEniBackend(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { + (!utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || + (utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { return fmt.Errorf("UDPBackendServerPortNotEqual") } @@ -733,8 +734,8 @@ func (f *FrameWork) ListenerEqual(id string, p v1.ServicePort, proto string) err return err } if resp.BackendServerPort == 0 || - (!isEniBackend(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || - (isEniBackend(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { + (!utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || + (utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { return fmt.Errorf("HTTPBackendServerPortNotEqual: %v, %v,%v", resp.BackendServerPort, p.NodePort, p.Port) } @@ -761,8 +762,8 @@ func (f *FrameWork) ListenerEqual(id string, p v1.ServicePort, proto string) err return err } if resp.BackendServerPort == 0 || - (!isEniBackend(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || - (isEniBackend(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { + (!utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.NodePort)) || + (utils.IsENIBackendType(f.SVC) && resp.BackendServerPort != int(p.TargetPort.IntVal)) { return fmt.Errorf("HTTPSBackendServerPortNotEqual") } if resp.ServerCertificateId == "" || diff --git a/cloud-controller-manager/listeners.go b/cloud-controller-manager/listeners.go index 2d64d012f..c0852e69b 100644 --- a/cloud-controller-manager/listeners.go +++ b/cloud-controller-manager/listeners.go @@ -467,10 +467,6 @@ func BuildActionsForListeners(svc *v1.Service, service, console Listeners) (List return append(append(deletion, addition...), updation...), nil } -func isEniBackend(svc *v1.Service) bool { - return svc.Annotations[utils.BACKEND_TYPE_LABEL] == "eni" -} - // BuildListenersFromService Build expected listeners func BuildListenersFromService( svc *v1.Service, @@ -500,7 +496,7 @@ func BuildListenersFromService( VGroups: vgrps, LoadBalancerID: lb.LoadBalancerId, } - if isEniBackend(svc) { + if utils.IsENIBackendType(svc) { n.NodePort = port.TargetPort.IntVal } n.Name = n.NamedKey.Key() diff --git a/cloud-controller-manager/utils/const.go b/cloud-controller-manager/utils/const.go index b4897ff31..75996f13d 100644 --- a/cloud-controller-manager/utils/const.go +++ b/cloud-controller-manager/utils/const.go @@ -1,5 +1,10 @@ package utils +import ( + "k8s.io/api/core/v1" + "os" +) + const ( BACKEND_TYPE_LABEL = "service.beta.kubernetes.io/backend-type" BACKEND_TYPE_ENI = "eni" @@ -8,3 +13,12 @@ const ( // LabelNodeRoleExcludeNode specifies that the node should be exclude from CCM LabelNodeRoleExcludeNode = "service.beta.kubernetes.io/exclude-node" ) + + +func IsENIBackendType(svc *v1.Service) bool { + if os.Getenv("SERVICE_FORCE_BACKEND_ENI") == "true" { + return true + } + return svc.Annotations[BACKEND_TYPE_LABEL] == BACKEND_TYPE_ENI +} + diff --git a/cloud-controller-manager/vgroups.go b/cloud-controller-manager/vgroups.go index 33e413154..111fce096 100644 --- a/cloud-controller-manager/vgroups.go +++ b/cloud-controller-manager/vgroups.go @@ -8,6 +8,7 @@ import ( "github.com/golang/glog" "k8s.io/api/core/v1" "k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/json" + "k8s.io/cloud-provider-alibaba-cloud/cloud-controller-manager/utils" "reflect" "strconv" "strings" @@ -426,7 +427,7 @@ func BuildVirturalGroupFromService( InsClient: client.ins, VpcID: client.vpcid, } - if isEniBackend(service) { + if utils.IsENIBackendType(service) { vg.NamedKey.Port = port.TargetPort.IntVal } vgrps = append(vgrps, vg)