diff --git a/Dockerfile.amd64 b/Dockerfile.amd64 new file mode 100644 index 0000000..95b735f --- /dev/null +++ b/Dockerfile.amd64 @@ -0,0 +1,9 @@ +FROM alpine:3.7 + +RUN mkdir -p /usr/local/bin +ADD flexvol/docker/flexvol.sh /usr/local/bin/ +ADD bin/flexvol-amd64 /usr/local/bin/flexvol + +RUN chmod +x /usr/local/bin/flexvol.sh + +ENTRYPOINT ["/usr/local/bin/flexvol.sh"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..56397f2 --- /dev/null +++ b/Makefile @@ -0,0 +1,307 @@ +# Shortcut targets +default: build + +## Build binary for current platform +all: build + +## Run the tests for the current platform/architecture +test: ut + +############################################################################### +# Both native and cross architecture builds are supported. +# The target architecture is select by setting the ARCH variable. +# When ARCH is undefined it is set to the detected host architecture. +# When ARCH differs from the host architecture a crossbuild will be performed. +ARCHES=$(patsubst flexvol/docker/Dockerfile.%,%,$(wildcard flexvol/docker/Dockerfile.*)) + +# BUILDARCH is the host architecture +# ARCH is the target architecture +# we need to keep track of them separately +BUILDARCH ?= $(shell uname -m) + +# canonicalized names for host architecture +ifeq ($(BUILDARCH),aarch64) + BUILDARCH=arm64 +endif +ifeq ($(BUILDARCH),x86_64) + BUILDARCH=amd64 +endif + +# unless otherwise set, I am building for my own architecture, i.e. not cross-compiling +ARCH ?= $(BUILDARCH) + +# canonicalized names for target architecture +ifeq ($(ARCH),aarch64) + override ARCH=arm64 +endif +ifeq ($(ARCH),x86_64) + override ARCH=amd64 +endif + + +# list of arches *not* to build when doing *-all +# until s390x works correctly +EXCLUDEARCH ?= s390x +VALIDARCHES = $(filter-out $(EXCLUDEARCH),$(ARCHES)) + + +############################################################################### +GO_BUILD_VER?=v0.17 +GO_BUILD_CONTAINER?=calico/go-build:$(GO_BUILD_VER) +PROTOC_VER?=v0.1 +PROTOC_CONTAINER?=calico/protoc:$(PROTOC_VER)-$(BUILDARCH) + +# Figure out the users UID/GID. These are needed to run docker containers +# as the current user and ensure that files built inside containers are +# owned by the current user. +LOCAL_USER_ID:=$(shell id -u) +MY_GID:=$(shell id -g) + +PACKAGE_NAME?=github.com/projectcalico/pod2daemon +SRC_FILES=$(shell find -name '*.go' |grep -v vendor) +CONTAINER_NAME?=calico/pod2daemon-flexvol + +# Pre-configured docker run command that runs as this user with the repo +# checked out to /code, uses the --rm flag to avoid leaving the container +# around afterwards. +DOCKER_RUN_RM:=docker run --rm --user $(MY_UID):$(MY_GID) -v ${CURDIR}:/code + +.PHONY: clean +## Clean enough that a new release build will be clean +clean: + find . -name '*.created-$(ARCH)' -exec rm -f {} + + + docker rmi $(CONTAINER_NAME):latest-$(ARCH) || true + docker rmi $(CONTAINER_NAME):$(VERSION)-$(ARCH) || true +ifeq ($(ARCH),amd64) + docker rmi $(CONTAINER_NAME):latest || true + docker rmi $(CONTAINER_NAME):$(VERSION) || true +endif +############################################################################### +# Building the binary +############################################################################### +.PHONY: build-all +## Build the binaries for all architectures and platforms +build-all: $(addprefix bin/flexvol-,$(VALIDARCHES)) + +.PHONY: build +## Build the binary for the current architecture and platform +build: bin/flexvol-$(ARCH) + +## Create the vendor directory +vendor: Gopkg.toml + $(DOCKER_GO_BUILD) dep ensure + +bin/flexvol-amd64: ARCH=amd64 +bin/flexvol-arm64: ARCH=arm64 +bin/flexvol-ppc64le: ARCH=ppc64le +bin/flexvol-s390x: ARCH=s390x +bin/flexvol-%: vendor $(SRC_FILES) + mkdir -p bin + -mkdir -p .go-pkg-cache + docker run --rm -ti \ + -v $(CURDIR):/go/src/$(PACKAGE_NAME):ro \ + -v $(CURDIR)/bin:/go/src/$(PACKAGE_NAME)/bin \ + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ + -v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \ + -e GOCACHE=/go-cache \ + -w /go/src/$(PACKAGE_NAME) \ + $(GO_BUILD_CONTAINER) go build -v -o bin/flexvol-$(ARCH) flexvol/flexvoldriver.go + +############################################################################### +# Building the image +############################################################################### +CONTAINER_CREATED=.pod2daemon-flexvol.created-$(ARCH) +.PHONY: image calico/pod2daemon-flexvol +image: $(CONTAINER_NAME) +image-all: $(addprefix sub-image-,$(VALIDARCHES)) +sub-image-%: + $(MAKE) image ARCH=$* + +$(CONTAINER_NAME): $(CONTAINER_CREATED) +$(CONTAINER_CREATED): Dockerfile.$(ARCH) bin/flexvol-$(ARCH) + docker build -t $(CONTAINER_NAME):latest-$(ARCH) --build-arg QEMU_IMAGE=$(CALICO_BUILD) -f Dockerfile.$(ARCH) . +ifeq ($(ARCH),amd64) + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):latest +endif + touch $@ + +# ensure we have a real imagetag +imagetag: +ifndef IMAGETAG + $(error IMAGETAG is undefined - run using make IMAGETAG=X.Y.Z) +endif + +## push one arch +push: imagetag + docker push $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) + docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) +ifeq ($(ARCH),amd64) + docker push $(CONTAINER_NAME):$(IMAGETAG) + docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG) +endif + +push-all: imagetag $(addprefix sub-push-,$(VALIDARCHES)) +sub-push-%: + $(MAKE) push ARCH=$* IMAGETAG=$(IMAGETAG) + +## tag images of one arch +tag-images: imagetag + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) + docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) +ifeq ($(ARCH),amd64) + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG) + docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG) +endif + +## tag images of all archs +tag-images-all: imagetag $(addprefix sub-tag-images-,$(VALIDARCHES)) +sub-tag-images-%: + $(MAKE) tag-images ARCH=$* IMAGETAG=$(IMAGETAG) + +############################################################################### +# Static checks +############################################################################### +## Perform static checks on the code. +.PHONY: static-checks +static-checks: vendor + docker run --rm \ + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ + -v $(CURDIR):/go/src/$(PACKAGE_NAME) \ + -w /go/src/$(PACKAGE_NAME) \ + $(GO_BUILD_CONTAINER) gometalinter --deadline=300s --disable-all --enable=goimports --vendor ./... + +.PHONY: fix +## Fix static checks +fix: + goimports -w $(SRC_FILES) + +############################################################################### +# UTs +############################################################################### +.PHONY: ut +## Run the tests in a container. Useful for CI, Mac dev +ut: $(SRC_FILES) + docker run --rm -v $(CURDIR):/go/src/$(PACKAGE_NAME):rw \ + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ + -w /go/src/$(PACKAGE_NAME) \ + $(GO_BUILD_CONTAINER) go test -v ./... + +############################################################################### +# CI +############################################################################### +.PHONY: ci +## Run what CI runs +ci: clean build-all static-checks ut + +############################################################################### +# CD +############################################################################### +.PHONY: cd +## Deploys images to registry +cd: image-all +ifndef CONFIRM + $(error CONFIRM is undefined - run using make CONFIRM=true) +endif +ifndef BRANCH_NAME + $(error BRANCH_NAME is undefined - run using make BRANCH_NAME=var or set an environment variable) +endif + $(MAKE) tag-images-all push-all IMAGETAG=${BRANCH_NAME} EXCLUDEARCH="$(EXCLUDEARCH)" + $(MAKE) tag-images-all push-all IMAGETAG=$(shell git describe --tags --dirty --always --long) EXCLUDEARCH="$(EXCLUDEARCH)" + +############################################################################### +# Release +############################################################################### +PREVIOUS_RELEASE=$(shell git describe --tags --abbrev=0) + +## Tags and builds a release from start to finish. +release: release-prereqs + $(MAKE) VERSION=$(VERSION) release-tag + $(MAKE) VERSION=$(VERSION) release-build + $(MAKE) VERSION=$(VERSION) release-verify + + @echo "" + @echo "Release build complete. Next, push the produced images." + @echo "" + @echo " make VERSION=$(VERSION) release-publish" + @echo "" + +## Produces a git tag for the release. +release-tag: release-prereqs release-notes + git tag $(VERSION) -F release-notes-$(VERSION) + @echo "" + @echo "Now you can build the release:" + @echo "" + @echo " make VERSION=$(VERSION) release-build" + @echo "" + +## Produces a clean build of release artifacts at the specified version. +release-build: release-prereqs clean + $(MAKE) image + $(MAKE) tag-images IMAGETAG=$(VERSION) + # Generate the `latest` images. + $(MAKE) tag-images IMAGETAG=latest + +## Verifies the release artifacts produces by `make release-build` are correct. +release-verify: release-prereqs + # TODO: Check the reported version is correct for each release artifact. Uncomment when binary supports version command. + # if ! docker run $(CONTAINER_NAME):$(VERSION)-$(ARCH) version | grep 'Version:\s*$(VERSION)$$'; then \ + # echo "Reported version:" `docker run --rm $(CONTAINER_NAME):$(VERSION)-$(ARCH) version` "\nExpected version: $(VERSION)"; \ + # false; \ + # else \ + # echo "Version check passed\n"; \ + # fi + +## Generates release notes based on commits in this version. +release-notes: release-prereqs + mkdir -p dist + echo "# Changelog" > release-notes-$(VERSION) + sh -c "git cherry -v $(PREVIOUS_RELEASE) | cut '-d ' -f 2- | sed 's/^/- /' >> release-notes-$(VERSION)" + +## Pushes a github release and release artifacts produced by `make release-build`. +release-publish: release-prereqs + # Push the git tag. + git push origin $(VERSION) + + # Push images. + $(MAKE) push IMAGETAG=$(VERSION) ARCH=$(ARCH) + + @echo "Finalize the GitHub release based on the pushed tag." + @echo "" + @echo " https://$(PACKAGE_NAME)/releases/tag/$(VERSION)" + @echo "" + @echo "If this is the latest stable release, then run the following to push 'latest' images." + @echo "" + @echo " make VERSION=$(VERSION) release-publish-latest" + @echo "" + +# WARNING: Only run this target if this release is the latest stable release. Do NOT +# run this target for alpha / beta / release candidate builds, or patches to earlier Calico versions. +## Pushes `latest` release images. WARNING: Only run this for latest stable releases. +release-publish-latest: release-prereqs + $(MAKE) push IMAGETAG=latest ARCH=$(ARCH) + +# release-prereqs checks that the environment is configured properly to create a release. +release-prereqs: +ifndef VERSION + $(error VERSION is undefined - run using make release VERSION=vX.Y.Z) +endif + +############################################################################### +# Developer helper scripts (not used by build or test) +############################################################################### +.PHONY: help +## Display this help text +help: # Some kind of magic from https://gist.github.com/rcmachado/af3db315e31383502660 + @awk '/^[a-zA-Z\-\_0-9\/]+:/ { \ + nb = sub( /^## /, "", helpMsg ); \ + if(nb == 0) { \ + helpMsg = $$0; \ + nb = sub( /^[^:]*:.* ## /, "", helpMsg ); \ + } \ + if (nb) \ + printf "\033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \ + } \ + { helpMsg = $$0 }' \ + width=20 \ + $(MAKEFILE_LIST) diff --git a/flexvol/creds/creds.go b/flexvol/creds/creds.go index fef7638..4299613 100644 --- a/flexvol/creds/creds.go +++ b/flexvol/creds/creds.go @@ -17,11 +17,11 @@ package creds // Credentials are the credentials published by the Flexvolume driver type Credentials struct { // UID is the unique identifier for the workload. - UID string + UID string // Workload is the name of the workload. - Workload string + Workload string // Namespace is the namespace of the workload. - Namespace string + Namespace string // ServiceAccount is the service account of the workload. ServiceAccount string } diff --git a/flexvol/docker/Dockerfile.debug b/flexvol/docker/Dockerfile.debug deleted file mode 100644 index 8ab0e10..0000000 --- a/flexvol/docker/Dockerfile.debug +++ /dev/null @@ -1,9 +0,0 @@ -FROM alpine:3.7 - -RUN mkdir -p /usr/loca/bin -ADD flexvol.sh /usr/local/bin/ -ADD flexvol /usr/local/bin/ - -RUN chmod +x /usr/local/bin/flexvol.sh - -ENTRYPOINT ["/usr/local/bin/flexvol.sh"] diff --git a/nodeagent/nodeagent.go b/nodeagent/nodeagent.go index cfbf28f..dac718f 100644 --- a/nodeagent/nodeagent.go +++ b/nodeagent/nodeagent.go @@ -67,7 +67,7 @@ func Run() { <-sigc // Shut down the binder. - bstop <- nil + bstop <- true } func main() {