diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 000000000..d9101dd7c --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,22 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +builds: +- + env: + - CGO_ENABLED=0 + main: ./multus/ + goos: + - linux + goarch: + - 386 + - amd64 + - arm + - arm64 +archive: + wrap_in_directory: true +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-snapshot" +release: + draft: true diff --git a/.travis.yml b/.travis.yml index ca8e38348..2d4d757c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,15 @@ language: go # for the detail # sudo: requried dist: trusty + +go: + - 1.11.x + env: global: - REGISTRY_USER=${REGISTRY_USER} - REGISTRY_PASS=${REGISTRY_PASS} + - MULTUS_GOPATH=${PWD}/gopath - secure: "${REGISTRY_SECURE}" before_install: @@ -17,31 +22,34 @@ install: - go get -u golang.org/x/lint/golint before_script: - - golint ./multus/... | grep -v ALL_CAPS | xargs -r false - - go fmt ./multus/... - - go vet ./multus/... + # Make gopath... to run golint/go fmt/go vet + - |- + if [ ! -h gopath/src/github.com/intel/multus-cni ]; then + mkdir -p gopath/src/github.com/intel + ln -s ../../../.. gopath/src/github.com/intel/multus-cni || exit 255 + fi + - env GOPATH=${MULTUS_GOPATH} golint gopath/src/github.com/intel/multus-cni/multus/... | grep -v ALL_CAPS | xargs -r false + - env GOPATH=${MULTUS_GOPATH} go fmt gopath/src/github.com/intel/multus-cni/... + - go tool vet */*.go # - gocyclo -over 15 ./multus script: - ./build - sudo ./test.sh - - $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci + - |- + GOV_GOPATH=${PWD}/gopath + pushd gopath/src/github.com/intel/multus-cni + env GOPATH=${GOV_GOPATH} ${GOPATH}/bin/goveralls -coverprofile=coverage.out -service=travis-ci + popd - mkdir -p ${TRAVIS_BUILD_DIR}/dist - tar cvfz ${TRAVIS_BUILD_DIR}/dist/multus-cni_amd64.tar.gz --warning=no-file-changed --exclude="dist" --exclude="vendor" . - docker build -t nfvpe/multus . -before_deploy: - - go get -u github.com/laher/goxc - - mkdir -p $TRAVIS_BUILD_DIR/dist - - goxc -d=$TRAVIS_BUILD_DIR/dist -pv=$TRAVIS_TAG -bc=linux -tasks=clean-destination,xc,archive,rmbin - deploy: - - provider: releases - api_key: - secure: "${DEPLOY_SECURE}" - file_glob: true - file: "$TRAVIS_BUILD_DIR/dist/*/*.gz" + # Release on versioned tag (e.g. v1.0) + - provider: script skip_cleanup: true + script: curl -sL https://git.io/goreleaser | bash on: tags: true all_branches: true diff --git a/build b/build index ac31e28e2..924ae2752 100755 --- a/build +++ b/build @@ -4,6 +4,20 @@ set -e ORG_PATH="github.com/intel" REPO_PATH="${ORG_PATH}/multus-cni" +# Add version/commit/date into binary +# In case of TravisCI, need to check error code of 'git describe'. +set +e +git describe --tags --abbrev=0 > /dev/null 2>&1 +if [ "$?" != "0" ]; then + VERSION="master" +else + VERSION=$(git describe --tags --abbrev=0) +fi +set -e +DATE=$(date --iso-8601=seconds) +COMMIT=$(git rev-parse --verify HEAD) +LDFLAGS="-X main.version=${VERSION:-master} -X main.commit=${COMMIT} -X main.date=${DATE}" + if [ ! -h gopath/src/${REPO_PATH} ]; then mkdir -p gopath/src/${ORG_PATH} ln -s ../../../.. gopath/src/${REPO_PATH} || exit 255 @@ -14,4 +28,4 @@ export GOBIN=${PWD}/bin export GOPATH=${PWD}/gopath echo "Building plugins" -go install "$@" ${REPO_PATH}/multus +go install -ldflags "${LDFLAGS}" "$@" ${REPO_PATH}/multus diff --git a/multus/multus.go b/multus/multus.go index b6e029833..efe762db5 100644 --- a/multus/multus.go +++ b/multus/multus.go @@ -20,6 +20,7 @@ package main import ( "encoding/json" + "flag" "fmt" "io/ioutil" "net" @@ -32,7 +33,7 @@ import ( "github.com/containernetworking/cni/pkg/invoke" "github.com/containernetworking/cni/pkg/skel" cnitypes "github.com/containernetworking/cni/pkg/types" - "github.com/containernetworking/cni/pkg/version" + cniversion "github.com/containernetworking/cni/pkg/version" "github.com/containernetworking/plugins/pkg/ns" k8s "github.com/intel/multus-cni/k8sclient" "github.com/intel/multus-cni/logging" @@ -41,6 +42,10 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) +var version = "master@git" +var commit = "unknown commit" +var date = "unknown date" + var defaultReadinessBackoff = wait.Backoff{ Steps: 4, Duration: 250 * time.Millisecond, @@ -48,6 +53,11 @@ var defaultReadinessBackoff = wait.Backoff{ Jitter: 0.1, } +func printVersionString() string { + return fmt.Sprintf("multus-cni version:%s, commit:%s, date:%s", + version, commit, date) +} + func saveScratchNetConf(containerID, dataDir string, netconf []byte) error { logging.Debugf("saveScratchNetConf: %s, %s, %s", containerID, dataDir, string(netconf)) if err := os.MkdirAll(dataDir, 0700); err != nil { @@ -496,6 +506,20 @@ func cmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient k8s.KubeClient) err } func main() { + + // Init command line flags to clear vendored packages' one, especially in init() + flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) + + // add version flag + versionOpt := false + flag.BoolVar(&versionOpt, "version", false, "Show application version") + flag.BoolVar(&versionOpt, "v", false, "Show application version") + flag.Parse() + if versionOpt == true { + fmt.Printf("%s\n", printVersionString()) + return + } + skel.PluginMain( func(args *skel.CmdArgs) error { result, err := cmdAdd(args, nil, nil) @@ -512,5 +536,5 @@ func main() { return result.Print() }, func(args *skel.CmdArgs) error { return cmdDel(args, nil, nil) }, - version.All, "meta-plugin that delegates to other CNI plugins") + cniversion.All, "meta-plugin that delegates to other CNI plugins") } diff --git a/multus/multus_test.go b/multus/multus_test.go index 28f22e369..961b383a2 100644 --- a/multus/multus_test.go +++ b/multus/multus_test.go @@ -28,7 +28,7 @@ import ( "github.com/containernetworking/cni/pkg/skel" cnitypes "github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types/020" - "github.com/containernetworking/cni/pkg/version" + cniversion "github.com/containernetworking/cni/pkg/version" "github.com/containernetworking/plugins/pkg/ns" "github.com/containernetworking/plugins/pkg/testutils" @@ -52,7 +52,7 @@ type fakePlugin struct { } type fakeExec struct { - version.PluginDecoder + cniversion.PluginDecoder addIndex int delIndex int