Skip to content

Commit

Permalink
code refactoring: isEniBackend and IsENIBackendType
Browse files Browse the repository at this point in the history
Signed-off-by: xianwei.zw <xianwei.zw@alibaba-inc.com>
  • Loading branch information
jovizhangwei committed Sep 24, 2019
1 parent 39181a5 commit 7ec79c2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
9 changes: 0 additions & 9 deletions cloud-controller-manager/controller/service/context.go
Expand Up @@ -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 }
Expand Down Expand Up @@ -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) &&
Expand Down
4 changes: 2 additions & 2 deletions cloud-controller-manager/controller/service/controller.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 9 additions & 8 deletions cloud-controller-manager/framework.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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)
}

Expand All @@ -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 == "" ||
Expand Down
6 changes: 1 addition & 5 deletions cloud-controller-manager/listeners.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions 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"
Expand All @@ -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
}

3 changes: 2 additions & 1 deletion cloud-controller-manager/vgroups.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7ec79c2

Please sign in to comment.