Skip to content

Commit

Permalink
Refactor plugin (#17)
Browse files Browse the repository at this point in the history
update deps
update build script
add version.go
fix parameter type changes in hostnic and utils.
  • Loading branch information
martinyunify authored Aug 17, 2017
1 parent 678d531 commit 5095464
Show file tree
Hide file tree
Showing 1,606 changed files with 242,712 additions and 43,970 deletions.
67 changes: 2 additions & 65 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,69 +1,6 @@
bin
*.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
*.exe
*.dll
*.so
*.dylib
*.test
*.out

.glide/
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
Session.vim
.netrwhist
*~
tags
.idea/
*.iws
glide.lock
*.iml
/out/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.metadata
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
.project
.externalToolBuilders/
*.launch
*.pydevproject
.cproject
.classpath
.factorypath
.buildpath
.target
.tern-project
.texlipse
.springBeans
.recommenders/
.cache-main
.scala_dependencies
.worksheet
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
sudo: false
language: go
go:
- 1.7
- 1.8
before_install:
install:
- go build -o bin/hostnic ./hostnic/
- make
script:
- echo "test skip"
before_deploy:
Expand Down
68 changes: 68 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# tab space is 4
# GitHub viewer defaults to 8, change with ?ts=4 in URL

# Vars describing project
NAME = hostnic-cni
GIT_REPOSITORY = github.com/yunify/hostnic-cni

# Generate vars to be included from external script
# Allows using bash to generate complex vars, such as project versions
GENERATE_VERSION_INFO_SCRIPT = ./generate_version.sh
GENERATE_VERSION_INFO_OUTPUT = version_info

# Define newline needed for subsitution to allow evaluating multiline script output
define newline


endef

# Call the version_info script with keyvalue option and evaluate the output
# Will import the keyvalue pairs and make available as Makefile variables
# Use dummy variable to only have execute once
$(eval $(subst #,$(newline),$(shell $(GENERATE_VERSION_INFO_SCRIPT) keyvalue | tr '\n' '#')))

# Call the verson_info script with json option and store result into output file and variable
# Will only execute once due to ':='
#GENERATE_VERSION_INFO := $(shell $(GENERATE_VERSION_INFO_SCRIPT) json | tee $(GENERATE_VERSION_INFO_OUTPUT))

# Set defaults for needed vars in case version_info script did not set
# Revision set to number of commits ahead
VERSION ?= 0.0
COMMITS ?= 0
REVISION ?= $(COMMITS)
BUILD_LABEL ?= unknown_build
BUILD_DATE ?= $(shell date -u +%Y%m%d.%H%M%S)
GIT_SHA1 ?= unknown_sha1

# Vars for export ; generate list of ENV vars based on matching export prefix
# Use strip to get rid of excessive spaces due to the foreach / filter / if logic
EXPORT_VAR_PREFIX = EXPORT_VAR_
EXPORT_VARS = $(strip $(foreach v,$(filter $(EXPORT_VAR_PREFIX)%,$(.VARIABLES)),$(if $(filter environment%,$(origin $(v))),$(v))))

# Vars for go phase
# All vars which being with prefix will be included in ldflags
# Defaulting to full static build
GO_VARIABLE_PREFIX = GO_VAR_
GO_VAR_BUILD_LABEL := $(BUILD_LABEL)
GO_VAR_VERSION := $(VERSION)
GO_VAR_GIT_SHA1 := $(GIT_SHA1)
GO_VAR_BUILD_LABEL := $(BUILD_LABEL)
GO_LDFLAGS = $(foreach v,$(filter $(GO_VARIABLE_PREFIX)%, $(.VARIABLES)),-X github.com/yunify/hostnic-cni/pkg.$(patsubst $(GO_VARIABLE_PREFIX)%,%,$(v))=$(value $(value v)))
GO_BUILD_FLAGS = -a -tags netgo -installsuffix nocgo -ldflags "$(GO_LDFLAGS)"

# Define targets

# default just build binary
default : go-build

# target for debugging / printing variables
print-% :
@echo '$*=$($*)'

# perform go build on project
go-build :
go build -o bin/hostnic $(GO_BUILD_FLAGS) $(GIT_REPOSITORY)/cmd/hostnic/
go build -o bin/nicclearner $(GO_BUILD_FLAGS) $(GIT_REPOSITORY)/cmd/niccleaner/


.PHONY : default all go-build
10 changes: 6 additions & 4 deletions hostnic/hostnic.go → cmd/hostnic/hostnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (

"net"

"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/ipam"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
Expand All @@ -37,7 +38,7 @@ import (
"github.com/yunify/hostnic-cni/pkg"
"github.com/yunify/hostnic-cni/provider"
_ "github.com/yunify/hostnic-cni/provider/qingcloud"
"github.com/yunify/qingcloud-sdk-go/logger"
logger "github.com/sirupsen/logrus"
)

const processLockFile = pkg.DefaultDataDir + "/lock"
Expand Down Expand Up @@ -133,7 +134,8 @@ func cmdAdd(args *skel.CmdArgs) error {
}
gateWay := net.ParseIP(nic.VxNet.GateWay)
netIF := &current.Interface{Name: args.IfName, Mac: nic.HardwareAddr, Sandbox: args.ContainerID}
ipConfig := &current.IPConfig{Address: net.IPNet{IP: net.ParseIP(nic.Address), Mask: ipNet.Mask}, Interface: 0, Version: "4", Gateway: gateWay}
numOfiface :=0
ipConfig := &current.IPConfig{Address: net.IPNet{IP: net.ParseIP(nic.Address), Mask: ipNet.Mask}, Interface: &numOfiface, Version: "4", Gateway: gateWay}
//TODO support ipv6
route := &types.Route{Dst: net.IPNet{IP: net.IPv4zero, Mask: net.IPMask(net.IPv4zero)}, GW: gateWay}
var routeTable = []*types.Route{route}
Expand All @@ -149,7 +151,7 @@ func cmdAdd(args *skel.CmdArgs) error {
if err := netlink.LinkSetName(nsIface, args.IfName); err != nil {
return fmt.Errorf("set link %s to name %s err: %v", nsIface.Attrs().HardwareAddr.String(), srcName, args.IfName)
}
return pkg.ConfigureIface(args.IfName, result)
return ipam.ConfigureIface(args.IfName, result)
})
if err != nil {
deleteNic(nic.ID, nicProvider)
Expand Down
File renamed without changes.
140 changes: 140 additions & 0 deletions generate_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash
# Define the project string to be used
PROJECT=hostnic-cni

# Use `git describe` to get parts for version information and state
GIT_DESCRIBE=$(git describe --long --dirty --tags)
if [[ $? != 0 ]]; then
echo "No tags!"
exit
fi

# Parse the output of `git describe`
# VERSION: The latest `git tag`
# COMMITS: The number of commits ahead workspace is of `git tag`
# SHA1: Unique SHA1 reference of current commit state
# DIRTY: Whether this workspace contains uncommitted / local changes
IFS='-' read -a myarray <<< "$GIT_DESCRIBE"

VERSION=${myarray[0]}
COMMITS=${myarray[1]}
SHA1=${myarray[2]}

if [ -z ${myarray[3]} ]; then
DIRTY=false
else
DIRTY=true
fi

if [[ $(git ls-files --directory --exclude-standard --others -t) ]]; then
DIRTY=true
fi

# Which branch was the git workspace tracking
BRANCH=$(git rev-parse --abbrev-ref HEAD)

# Generate a well known label based on branch
# Unknown branches will go by their SHA1
case $BRANCH in
master)
LABEL="ga"
;;
develop)
LABEL="dev"
;;
release/*)
LABEL="rc"
;;
*)
LABEL=$SHA1
esac

# Generate a label to represent this build
# Combination of various information about the git workspace
# Can be used for a filename of the package
BUILD_LABEL="${PROJECT}-${VERSION}-${COMMITS}-${LABEL}"

if [ "$DIRTY" = true ]; then
BUILD_LABEL=${BUILD_LABEL}-dirty
fi

# Generate a build date if was not passed into the env or present in parent env
BUILD_DATE=${BUILD_DATE:=$(date -u +%Y%m%d.%H%M%S)}

# Set the variables to be outputted
# All vars that begin with the prefix will be outputted
# Allows callers of the script to inject new vars through ENV
# e.g. Jenkins can set EXPORT_JENKINS_SERVER=build-server
VAR_PREFIX=EXPORT_VAR_

EXPORT_VAR_GIT_DESCRIBE="$GIT_DESCRIBE"
EXPORT_VAR_BRANCH="$BRANCH"
EXPORT_VAR_LABEL="$LABEL"
EXPORT_VAR_VERSION="$VERSION"
EXPORT_VAR_COMMITS="$COMMITS"
EXPORT_VAR_GIT_SHA1="$SHA1"
EXPORT_VAR_DIRTY="$DIRTY"
EXPORT_VAR_BUILD_LABEL="$BUILD_LABEL"
EXPORT_VAR_BUILD_DATE="$BUILD_DATE"


# Get a list of all ENV vars and internal defined vars that begin with prefix
VAR_LIST=$(eval "set -o posix ; set | grep '^$VAR_PREFIX'")

# Generate a JSON document for version_info containing the vars
# Using HERE documents to allow pretty printing
JSON=$(cat <<EOF
{
"version_info": {
EOF
)

# Generate key value pair for each exported var
# Make the keys lower case
while IFS='=' read -r name value ; do
JSON+="
"
JSON+=" "
# Strip prefix and make lower case
JSON+=\"$(echo ${name#$VAR_PREFIX} | tr '[:upper:]' '[:lower:]')\"
# Value of variable, same as $value also
JSON+=": \"${!name}\","
done <<< "$VAR_LIST"

# Eliminate trailing comma
# Only trim the comma from last matching line, JSON var contains newlines
JSON=$(echo "$JSON" | sed '$ s/,$//')

# End the JSON document
JSON+=$(cat <<-EOF
}
}
EOF
)

# Based on parameter, output key value pairs or JSON document
case "$1" in
keyvalue)

# For each exported var, output key value pair
# Allows sourcing the output to be used in Makefile
while IFS='=' read -r name value ; do
# Strip prefix
echo "${name#$VAR_PREFIX}=${!name}"
done <<< "$VAR_LIST"

# Also output the JSON document in single line format for completeness
echo VERSION_INFO_JSON=\'$JSON\'
;;

json)
# Output the JSON variable, wrap in quotes to preserve newlines
echo "$JSON"
;;

*)
echo "Usage: $0 [keyvalue|json]"
exit 1
;;
esac
56 changes: 56 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5095464

Please sign in to comment.