Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wccsama authored Jul 9, 2019
2 parents fca1970 + d6f20a1 commit 83c4d33
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 11 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
VERSION ?= $(shell git describe --always --dirty)
include hack/Makefile.buildinfo

GOOS ?= linux
ARCH ?= amd64
REGISTRY := hub.baidubce.com/jpaas-public
BIN := cce-cloud-controller-manager
IMAGE := $(REGISTRY)/$(BIN)
SRC_DIRS := cmd pkg # directories which hold app source (not vendored)

LDFLAGS=$(VERSION_LDFLAGS)

.PHONY: all
all: build

Expand All @@ -20,6 +23,7 @@ build: build-output
-o output/${BIN} \
-installsuffix "static" \
-ldflags "-X main.version=${VERSION}" \
-ldflags "${LDFLAGS}" \
./cmd/cce-cloud-controller-manager

.PHONY: local-build
Expand All @@ -29,6 +33,7 @@ local-build: build-output
-o output/${BIN} \
-installsuffix "static" \
-ldflags "-X main.version=${VERSION}" \
-ldflags "${LDFLAGS}" \
./cmd/cce-cloud-controller-manager

.PHONY: image-build
Expand All @@ -50,4 +55,4 @@ deploy:

.PHONY: version
version:
@echo ${VERSION}
@echo ${VERSION}
56 changes: 56 additions & 0 deletions hack/Makefile.buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# adapted from the hack scripts in kubernetes/kubernetes

GIT_COMMIT:=$(shell git rev-parse "HEAD^{commit}" 2>/dev/null)

# the raw git version from `git describe` -- our starting point
GIT_VERSION_RAW:=$(shell git describe --tags --abbrev=14 "$(GIT_COMMIT)^{commit}" 2>/dev/null)

# use the number of dashes in the raw version to figure out what kind of
# version this is, and turn it into a semver-compatible version
DASHES_IN_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/[^-]//g")

# just use the raw version by default
GIT_VERSION:=$(GIT_VERSION_RAW)

ifeq ($(DASHES_IN_VERSION), ---)
# we have a distance to a subversion (v1.1.0-subversion-1-gCommitHash)
GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$$/.\1\+\2/")
endif
ifeq ($(DASHES_IN_VERSION), --)
# we have distance to base tag (v1.1.0-1-gCommitHash)
GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-g\([0-9a-f]\{14\}\)$$/+\1/")
endif

# figure out if we have new or changed files
ifeq ($(shell git status --porcelain 2>/dev/null), "")
GIT_TREE_STATE:=clean
else
# append the -dirty manually, since `git describe --dirty` only considers
# changes to existing files
GIT_TREE_STATE:=dirty
GIT_VERSION:=$(GIT_VERSION)-dirty
endif

# construct a "shorter" version without the commit info, etc for use as container image tag, etc
VERSION?=$(shell echo "$(GIT_VERSION)" | grep -E -o '^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-(alpha|beta)\.[[:digit:]]+)?')

# construct the build date, taking into account SOURCE_DATE_EPOCH, which is
# used for the purpose of reproducible builds
ifdef SOURCE_DATE_EPOCH
BUILD_DATE:=$(shell date --date=@${SOURCE_DATE_EPOCH} -u +'%Y-%m-%dT%H:%M:%SZ')
else
BUILD_DATE:=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
endif

# set the build information version ldflags (but not other ldflags)
VERSION_LDFLAGS:=-X k8s.io/cloud-provider-baiducloud/vendor/k8s.io/kubernetes/pkg/version.gitVersion=$(GIT_VERSION) -X k8s.io/cloud-provider-baiducloud/vendor/k8s.io/kubernetes/pkg/version.gitCommit=$(GIT_COMMIT) -X k8s.io/cloud-provider-baiducloud/vendor/k8s.io/kubernetes/pkg/version.gitTreeState=$(GIT_TREE_STATE) -X k8s.io/cloud-provider-baiducloud/vendor/k8s.io/kubernetes/pkg/version.buildDate=$(BUILD_DATE)

export VERSION
export VERSION_LDFLAGS

# print out a summary of the current version info
version-info:
@echo "Version: $(GIT_VERSION) ($(VERSION))"
@echo " built from $(GIT_COMMIT) ($(GIT_TREE_STATE))"
@echo " built on $(BUILD_DATE)"
.PHONY: version-info
30 changes: 22 additions & 8 deletions pkg/cloud-provider/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,21 @@ const (

// NodeAnnotationCCMVersion is the version of CCM
NodeAnnotationCCMVersion = NodeAnnotationPrefix + "ccm-version"

// NodeAnnotationAdvertiseRoute indicates whether to advertise route to vpc route table
NodeAnnotationAdvertiseRoute = NodeAnnotationPrefix + "advertise-route"
)

// ServiceAnnotation contains annotations from service
type ServiceAnnotation struct {
/* BLB */
CceAutoAddLoadBalancerId string
CceAutoAddEip string
LoadBalancerExistId string
LoadBalancerInternalVpc string
LoadBalancerAllocateVip string
LoadBalancerSubnetId string
LoadBalancerScheduler string
LoadBalancerRsMaxNum int
CceAutoAddLoadBalancerId string
LoadBalancerExistId string
LoadBalancerInternalVpc string
LoadBalancerAllocateVip string
LoadBalancerSubnetId string
LoadBalancerScheduler string
LoadBalancerRsMaxNum int

LoadBalancerHealthCheckTimeoutInSecond int
LoadBalancerHealthCheckInterval int
Expand All @@ -121,6 +123,7 @@ type NodeAnnotation struct {
VpcRouteTableId string
VpcRouteRuleId string
CCMVersion string
AdvertiseRoute bool
}

// ExtractServiceAnnotation extract annotations from service
Expand Down Expand Up @@ -290,5 +293,16 @@ func ExtractNodeAnnotation(node *v1.Node) (*NodeAnnotation, error) {
result.CCMVersion = ccmVersion
}

advertiseRoute, ok := annotation[NodeAnnotationAdvertiseRoute]
if ok {
advertise, err := strconv.ParseBool(advertiseRoute)
if err != nil {
return nil, fmt.Errorf("NodeAnnotationAdvertiseRoute syntex error: %v", err)
}
result.AdvertiseRoute = advertise
} else {
result.AdvertiseRoute = true
}

return result, nil
}
48 changes: 47 additions & 1 deletion pkg/cloud-provider/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func (bc *Baiducloud) ListRoutes(ctx context.Context, clusterName string) (route
DestinationCIDR: r.DestinationAddress,
TargetNode: types.NodeName(insName),
}

advertiseRoute, err := bc.advertiseRoute(insName)
if err != nil {
continue
}
// use route.Blackhole to mark this route to be deleted
if !advertiseRoute {
route.Blackhole = true
}

vpcId, err := bc.getVpcID()
if err != nil {
return nil, err
Expand All @@ -104,6 +114,17 @@ func (bc *Baiducloud) CreateRoute(ctx context.Context, clusterName string, nameH
if len(vpcRoutes) < 1 {
return fmt.Errorf("VPC route length error: length is : %d", len(vpcRoutes))
}

advertiseRoute, err := bc.advertiseRoute(string(kubeRoute.TargetNode))
if err != nil {
return err
}

if !advertiseRoute {
glog.V(3).Infof("Node %s has annotation not to advertise route", string(kubeRoute.TargetNode))
return nil
}

var insID string
inss, err := bc.clientSet.Cce().ListInstances(bc.ClusterID)
if err != nil {
Expand Down Expand Up @@ -141,6 +162,11 @@ func (bc *Baiducloud) CreateRoute(ctx context.Context, clusterName string, nameH
}
}

if insID == "" {
glog.Errorf("InstanceId not found for k8s node %s, not create route", string(kubeRoute.TargetNode))
return fmt.Errorf("InstanceId not found for k8s node %s, create route failed", string(kubeRoute.TargetNode))
}

args := vpc.CreateRouteRuleArgs{
RouteTableID: vpcRoutes[0].RouteTableID,
NexthopType: "custom",
Expand Down Expand Up @@ -228,7 +254,7 @@ func (bc *Baiducloud) ensureRouteInfoToNode(nodeName, vpcId, vpcRouteTableId, vp
if err != nil {
return err
}

isChanged := false
if nodeAnnotation.VpcId != vpcId {
curNode.Annotations[NodeAnnotationVpcId] = vpcId
Expand Down Expand Up @@ -342,3 +368,23 @@ func (bc *Baiducloud) isConflict(otherRR vpc.RouteRule, cceRR vpc.RouteRule) boo

return false
}

func (bc *Baiducloud) advertiseRoute(nodename string) (bool, error) {

// check node resource in k8s has advertise route annotation, if is false, not create route
curNode, err := bc.kubeClient.CoreV1().Nodes().Get(nodename, metav1.GetOptions{})
if err != nil {
if !strings.Contains(err.Error(), "not found") {
return true, err
}
}

if curNode.Annotations == nil {
curNode.Annotations = make(map[string]string)
}
nodeAnnotation, err := ExtractNodeAnnotation(curNode)
if err != nil {
return true, err
}
return nodeAnnotation.AdvertiseRoute, nil
}

0 comments on commit 83c4d33

Please sign in to comment.