Skip to content

Commit

Permalink
Make static build (#22)
Browse files Browse the repository at this point in the history
* Make build static and consistent with other plugins

Make build work in the same way as for other velero plugins.
That ways it is easier to follow velero changes, and easier
for other velero plugin developers to jump into the project
if needed.

Signed-off-by: Bartosz Rybacki <[email protected]>

* Fix func-tests

Signed-off-by: Bartosz Rybacki <[email protected]>

* Simplify func-tests

Travis no longer used

Signed-off-by: Bartosz Rybacki <[email protected]>
  • Loading branch information
brybacki authored Dec 9, 2021
1 parent b0cb4c3 commit d15dfc1
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 377 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

FROM alpine:3.13
RUN mkdir /plugins
COPY bin/kubevirt-velero-plugin /plugins/
ADD ./kubevirt-velero-plugin /plugins/
USER nobody:nogroup
ENTRYPOINT ["/bin/sh", "-c", "cp /plugins/* /target/."]
176 changes: 118 additions & 58 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
.PHONY: clean \
all \
build-all \
build-builder \
build-image \
build-dirs \
push \
Expand All @@ -23,30 +24,20 @@
modules \
vendor \
gomod-update \
stop-builder \
local-deploy-velero \
add-plugin \
remove-plugin \
local-undeploy-velero \
push-builder \
cluster-up \
cluster-down \
cluster-sync \
test-functional \
rebuild-functest \
clean-test

DOCKER?=1
ifeq (${DOCKER}, 1)
# use entrypoint.sh (default) as your entrypoint into the container
DO=./hack/build/in-docker.sh
else
DO=eval
endif

GREEN=\e[0;32m
WHITE=\e[0;37m
BIN=bin/kubevirt-velero-plugin
BIN=kubevirt-velero-plugin
SRC_FILES=main.go \
$(shell find pkg -name "*.go")

Expand All @@ -58,65 +49,108 @@ TESTS_SRC_FILES=\
DOCKER_PREFIX?=kubevirt
DOCKER_TAG?=latest
IMAGE_NAME?=kubevirt-velero-plugin

# Which architecture to build - see $(ALL_ARCH) for options.
# if the 'local' rule is being run, detect the ARCH from 'go env'
# if it wasn't specified by the caller.
local : ARCH ?= $(shell go env GOOS)-$(shell go env GOARCH)
ARCH ?= linux-amd64

platform_temp = $(subst -, ,$(ARCH))
GOOS = $(word 1, $(platform_temp))
GOARCH = $(word 2, $(platform_temp))

# registry prefix is the prefix usable from inside the local cluster
KUBEVIRTCI_REGISTRY_PREFIX=registry:5000/kubevirt
PORT=$(shell ./cluster-up/cli.sh ports registry)

all: clean build-image
BUILD_IMAGE ?= golang:1.16-stretch

build-image: build-all
@echo -e "${GREEN}Building plugin image${WHITE}"
@docker build -t ${DOCKER_PREFIX}/${IMAGE_NAME}:${DOCKER_TAG} .
all: build-image

build-all: build-builder build-dirs ${BIN}

${BIN}: ${SRC_FILES}
@echo -e "${GREEN}Building...${WHITE}"
@${DO} hack/build/build.sh
local: build-dirs
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
PKG=$(PKG) \
BIN=$(BIN) \
GIT_SHA=$(GIT_SHA) \
GIT_DIRTY="$(GIT_DIRTY)" \
OUTPUT_DIR=$$(pwd)/_output/bin/$(GOOS)/$(GOARCH) \
GO111MODULE=on \
GOFLAGS=-mod=readonly \
./hack/build.sh

push: build-image
@echo -e "${GREEN}Pushing plugin image to local registry${WHITE}"
@docker push ${DOCKER_PREFIX}/${IMAGE_NAME}:${DOCKER_TAG}
build-all: build-dirs _output/bin/$(GOOS)/$(GOARCH)/$(BIN)

cluster-push-image: build-image
@echo -e "${GREEN}Pushing plugin image to local K8s cluster${WHITE}"
DOCKER_PREFIX=${DOCKER_PREFIX} IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} PORT=${PORT} KUBEVIRTCI_REGISTRY_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} \
hack/build/cluster-push-image.sh
build-builder:
@echo "deprecated"

_output/bin/$(GOOS)/$(GOARCH)/$(BIN): build-dirs ${SRC_FILES}
@echo -e "${GREEN}Building...${WHITE}"
@echo "building: $@"
$(MAKE) shell CMD="-c '\
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
PKG=$(PKG) \
BIN=$(BIN) \
GIT_SHA=$(GIT_SHA) \
GIT_DIRTY="$(GIT_DIRTY)" \
OUTPUT_DIR=/output/$(GOOS)/$(GOARCH) \
GO111MODULE=on \
GOFLAGS=-mod=readonly \
./hack/build/build.sh'"

TTY := $(shell tty -s && echo "-t")

shell: build-dirs
@echo "running docker: $@"
@docker run \
-e GOFLAGS \
-i $(TTY) \
--rm \
-u $$(id -u):$$(id -g) \
-v "$$(pwd)/_output/bin:/output:delegated" \
-v $$(pwd)/.go/pkg:/go/pkg \
-v $$(pwd)/.go/src:/go/src \
-v $$(pwd)/.go/std:/go/std \
-v $$(pwd):/go/src/kubevirt-velero-plugin:z \
-v $$(pwd)/.go/std/$(GOOS)_$(GOARCH):/usr/local/go/pkg/$(GOOS)_$(GOARCH)_static \
-v "$$(pwd)/.go/go-build:/.cache/go-build:delegated" \
-e CGO_ENABLED=0 \
-w /go/src/kubevirt-velero-plugin \
$(BUILD_IMAGE) \
/bin/sh $(CMD)

add-plugin: local-deploy-velero
@echo -e "${GREEN}Adding the plugin to local Velero${WHITE}"
IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} DOCKER_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} hack/velero/add-plugin.sh
build-dirs:
@mkdir -p _output/bin/$(GOOS)/$(GOARCH)
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(GOOS)/$(GOARCH) .go/go-build

remove-plugin:
@echo -e "${GREEN}Removing the plugin from local Velero${WHITE}"
IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} DOCKER_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} hack/velero/remove-plugin.sh
container-name:
@echo "container: ${DOCKER_PREFIX}/${IMAGE_NAME}:${DOCKER_TAG}"

local-deploy-velero:
@echo -e "${GREEN}Deploying velero to local cluster${WHITE}"
@hack/velero/deploy-velero.sh
build-image: build-all
@echo -e "${GREEN}Building plugin image${WHITE}"
cp Dockerfile _output/bin/$(GOOS)/$(GOARCH)/Dockerfile
docker build -t ${DOCKER_PREFIX}/${IMAGE_NAME}:${DOCKER_TAG} -f _output/bin/$(GOOS)/$(GOARCH)/Dockerfile _output/bin/$(GOOS)/$(GOARCH)

local-undeploy-velero:
@echo -e "${GREEN}Removing velero from local cluster${WHITE}"
@hack/velero/undeploy-velero.sh
push: build-image
@echo -e "${GREEN}Pushing plugin image to local registry${WHITE}"
@docker push ${DOCKER_PREFIX}/${IMAGE_NAME}:${DOCKER_TAG}

gomod-update: modules vendor

build-builder: stop-builder
@hack/build/build-builder.sh

push-builder:
@hack/build/push-builder.sh

clean-dirs:
@echo -e "${GREEN}Removing output directories${WHITE}"
@${DO} "rm -rf _output bin"
rm -rf .container-* _output/.dockerfile-*
rm -rf .go _output

clean: clean-dirs stop-builder
clean: clean-dirs
@echo "cleaning"
docker rmi $(BUILD_IMAGE)

test: build-dirs
@echo -e "${GREEN}Testing${WHITE}"
@${DO} "CGO_ENABLED=0 go test -v -timeout 60s ./pkg/..."
@$(MAKE) shell CMD="-c 'CGO_ENABLED=0 go test -v -timeout 60s ./pkg/...'"

test-functional: ${TESTS_BINARY}
@echo -e "${GREEN}Running functional tests${WHITE}"
Expand All @@ -129,7 +163,19 @@ clean-test:

${TESTS_BINARY}: ${TESTS_SRC_FILES} ${TESTS_OUT_DIR}
@echo -e "${GREEN}Building functional tests${WHITE}"
@${DO} hack/build/build-functest.sh
$(MAKE) shell CMD="-c '\
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
PKG=$(PKG) \
BIN=$(BIN) \
GIT_SHA=$(GIT_SHA) \
GIT_DIRTY="$(GIT_DIRTY)" \
OUTPUT_DIR=/output/$(GOOS)/$(GOARCH) \
GO111MODULE=on \
GOFLAGS=-mod=readonly \
TESTS_OUT_DIR=$(TESTS_OUT_DIR) \
JOB_TYPE="${JOB_TYPE:-}" \
./hack/build/build-functest.sh'"

${TESTS_OUT_DIR}:
@mkdir -p ${TESTS_OUT_DIR}
Expand All @@ -141,18 +187,32 @@ vendor:
@${DO} "GO111MODULE=on go mod tidy -v"
@${DO} "GO111MODULE=on go mod vendor -v"

build-dirs:
@echo -e "${GREEN}Creating output directories${WHITE}"
@hack/build/build-dirs.sh

stop-builder:
@echo -n -e "${GREEN}Stopping builder...${WHITE}"
@hack/build/stop-builder.sh > /dev/null
@echo -e "${GREEN} done${WHITE}"

goveralls: test
${DO} "TRAVIS_JOB_ID=${TRAVIS_JOB_ID} TRAVIS_PULL_REQUEST=${TRAVIS_PULL_REQUEST} TRAVIS_BRANCH=${TRAVIS_BRANCH} ./hack/build/goveralls.sh"

# local test cluster targets
cluster-push-image: build-image
@echo -e "${GREEN}Pushing plugin image to local K8s cluster${WHITE}"
DOCKER_PREFIX=${DOCKER_PREFIX} IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} PORT=${PORT} KUBEVIRTCI_REGISTRY_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} \
hack/build/cluster-push-image.sh


add-plugin: local-deploy-velero
@echo -e "${GREEN}Adding the plugin to local Velero${WHITE}"
IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} DOCKER_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} hack/velero/add-plugin.sh

remove-plugin:
@echo -e "${GREEN}Removing the plugin from local Velero${WHITE}"
IMAGE_NAME=${IMAGE_NAME} DOCKER_TAG=${DOCKER_TAG} DOCKER_PREFIX=${KUBEVIRTCI_REGISTRY_PREFIX} hack/velero/remove-plugin.sh

local-deploy-velero:
@echo -e "${GREEN}Deploying velero to local cluster${WHITE}"
@hack/velero/deploy-velero.sh

local-undeploy-velero:
@echo -e "${GREEN}Removing velero from local cluster${WHITE}"
@hack/velero/undeploy-velero.sh

cluster-up:
@hack/cluster-up.sh

Expand Down
31 changes: 0 additions & 31 deletions hack/build/build-builder.sh

This file was deleted.

24 changes: 0 additions & 24 deletions hack/build/build-dirs.sh

This file was deleted.

19 changes: 11 additions & 8 deletions hack/build/build-functest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
#See the License for the specific language governing permissions and
#limitations under the License.

set -e
set -euo pipefail

script_dir="$(cd "$(dirname "$0")" && pwd -P)"
source "${script_dir}"/../config.sh
export PATH=$PATH:$HOME/gopath/bin

test_path="tests"
(cd $test_path; go install github.com/onsi/ginkgo/ginkgo@latest)
(cd $test_path; GOFLAGS= go get github.com/onsi/gomega)
(cd $test_path; go mod tidy; go mod vendor)
test_out_path=${test_path}/_out
mkdir -p ${test_out_path}
(cd $test_path; ginkgo build .)
mv ${test_path}/tests.test ${TESTS_OUT_DIR}

mkdir -p ${TESTS_OUT_DIR}/
# use vendor
export GO111MODULE=off
ginkgo build tests/
mv tests/tests.test ${TESTS_OUT_DIR}/
36 changes: 30 additions & 6 deletions hack/build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,36 @@
#See the License for the specific language governing permissions and
#limitations under the License.

set -e
set -o errexit
set -o nounset
set -o pipefail

script_dir="$(cd "$(dirname "$0")" && pwd -P)"
source "${script_dir}"/../config.sh
if [ -z "${BIN}" ]; then
echo "BIN must be set"
exit 1
fi
if [ -z "${GOOS}" ]; then
echo "GOOS must be set"
exit 1
fi
if [ -z "${GOARCH}" ]; then
echo "GOARCH must be set"
exit 1
fi

mkdir -p _output/bin/${GOOS}/${GOARCH}
go build -a -ldflags '-extldflags "-static"' -tags static -o _output/bin/${GOOS}/${GOARCH} .

ln -f _output/bin/${GOOS}/${GOARCH}/${BIN} ${BIN_DIR}/${BIN}
export CGO_ENABLED=0

if [[ -z "${OUTPUT_DIR:-}" ]]; then
OUTPUT_DIR=.
fi
OUTPUT=${OUTPUT_DIR}/${BIN}
if [[ "${GOOS}" = "windows" ]]; then
OUTPUT="${OUTPUT}.exe"
fi

go build \
-o ${OUTPUT} \
-installsuffix "static" \
-mod=readonly \
./
Loading

0 comments on commit d15dfc1

Please sign in to comment.