diff --git a/.gitignore b/.gitignore index 165945413..251381f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -bin/ +_output vendor-v[0-9].[0-9].[0-9]/ hack/*/.vagrant/ diff --git a/.travis.yml b/.travis.yml index 07020f8f6..6d69480db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ go: - 1.6 script: - test -z "$(go generate pkg/asset/templates_gen.go && git diff | tee /dev/stderr)" - - make clean check all + - make clean release diff --git a/Makefile b/Makefile index ef894be79..11eada4a2 100644 --- a/Makefile +++ b/Makefile @@ -2,24 +2,33 @@ export GO15VENDOREXPERIMENT:=1 export CGO_ENABLED:=0 export GOARCH:=amd64 +LOCAL_OS:=$(shell uname | tr A-Z a-z) GOFILES:=$(shell find . -name '*.go' | grep -v -E '(./vendor|internal/templates.go)') +TEMPLATES:=$(shell find pkg/asset/templates -type f) GOPATH_BIN:=$(shell echo ${GOPATH} | awk 'BEGIN { FS = ":" }; { print $1 }')/bin -all: bin/linux/bootkube bin/darwin/bootkube +all: _output/bin/linux/bootkube _output/bin/darwin/bootkube + +release: clean check _output/release/bootkube-linux-amd64.tar.gz _output/release/bootkube-darwin-amd64.tar.gz check: pkg/asset/internal/templates.go @find . -name vendor -prune -o -name '*.go' -exec gofmt -s -d {} + @go vet $(shell go list ./... | grep -v '/vendor/') @go test -v $(shell go list ./... | grep -v '/vendor/') -bin/%/bootkube: $(GOFILES) pkg/asset/internal/templates.go +_output/bin/%/bootkube: LDFLAGS=-X github.com/coreos/bootkube/pkg/version.Version=$(shell $(CURDIR)/build/git-version.sh) +_output/bin/%/bootkube: $(GOFILES) pkg/asset/internal/templates.go mkdir -p $(dir $@) - GOOS=$* go build -o bin/$*/bootkube github.com/coreos/bootkube/cmd/bootkube + GOOS=$* go build -ldflags "$(LDFLAGS)" -o _output/bin/$*/bootkube github.com/coreos/bootkube/cmd/bootkube + +_output/release/bootkube-%-amd64.tar.gz: _output/bin/%/bootkube + @mkdir -p $(dir $@) + tar czf $@ -C $(dir $<) bootkube -install: bin/$(shell uname | tr A-Z a-z)/bootkube +install: _output/bin/$(LOCAL_OS)/bootkube cp $< $(GOPATH_BIN) -pkg/asset/internal/templates.go: $(GOFILES) +pkg/asset/internal/templates.go: $(GOFILES) $(TEMPLATES) mkdir -p $(dir $@) go generate pkg/asset/templates_gen.go @@ -47,8 +56,7 @@ vendor-$(VENDOR_VERSION): @rm -rf $@/k8s.io/kubernetes/Godeps $@/k8s.io/kubernetes/.git clean: - rm -rf bin/ + rm -rf _output rm -rf pkg/asset/internal -.PHONY: all check clean install vendor pkg/asset/internal/templates.go - +.PHONY: all check clean install release vendor diff --git a/build/build-release.sh b/build/build-release.sh new file mode 100755 index 000000000..c0957e32a --- /dev/null +++ b/build/build-release.sh @@ -0,0 +1,27 @@ +#!/bin/bash +set -euo pipefail + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +BOOTKUBE_ROOT=${SCRIPT_DIR}/.. +BOOTKUBE_RELEASE=${BOOTKUBE_BIN:-${BOOTKUBE_ROOT}/_output/release/bootkube-linux-amd64.tar.gz} +DOCKER_REPO=${DOCKER_REPO:-quay.io/coreos/bootkube} +DOCKER_TAG=${DOCKER_TAG:-$(${BOOTKUBE_ROOT}/build/git-version.sh)} +DOCKER_PUSH=${DOCKER_PUSH:-false} + +sudo rkt run \ + --volume bk,kind=host,source=${BOOTKUBE_ROOT} \ + --mount volume=bk,target=/go/src/github.com/coreos/bootkube \ + --insecure-options=image docker://golang:1.6.2 --exec /bin/bash -- -c \ + "cd /go/src/github.com/coreos/bootkube && make clean release" + +TEMPDIR=$(mktemp -d -t bootkube.XXXX) + +printf "FROM scratch\nCOPY bootkube /bootkube" > ${TEMPDIR}/Dockerfile +tar xzvf ${BOOTKUBE_RELEASE} -C ${TEMPDIR} +docker build -t ${DOCKER_REPO}:${DOCKER_TAG} ${TEMPDIR} +rm -rf ${TEMPDIR} + +if [[ ${DOCKER_PUSH} == "true" ]]; then + docker push ${DOCKER_REPO}:${DOCKER_TAG} +fi diff --git a/build/git-version.sh b/build/git-version.sh new file mode 100755 index 000000000..672f9796f --- /dev/null +++ b/build/git-version.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -euo pipefail + +# parse the current git commit hash +COMMIT=$(git rev-parse HEAD) + +# check if the current commit has a matching tag +TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true) + +# use the matching tag as the version, if available +if [ -z "$TAG" ]; then + VERSION=$COMMIT +else + VERSION=$TAG +fi + +# check for changed files (not untracked files) +if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then + VERSION="${VERSION}-dirty" +fi + +echo $VERSION diff --git a/cmd/bootkube/main.go b/cmd/bootkube/main.go index 4abc97013..229c42489 100644 --- a/cmd/bootkube/main.go +++ b/cmd/bootkube/main.go @@ -5,6 +5,8 @@ import ( "os" "github.com/spf13/cobra" + + "github.com/coreos/bootkube/pkg/version" ) var ( @@ -13,9 +15,20 @@ var ( Short: "Bootkube!", Long: "", } + + cmdVersion = &cobra.Command{ + Use: "version", + Short: "Output version information", + Long: "", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Printf("Version: %s\n", version.Version) + return nil + }, + } ) func main() { + cmdRoot.AddCommand(cmdVersion) if err := cmdRoot.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..df29d6a01 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,3 @@ +package version + +var Version string = "none"