Skip to content

Commit ecd69e3

Browse files
committed
Build versioned v2plugin from versioned binaries
This enables build and caching locally or in a build machine the v2plugin before creating test environment VMs and failing much faster on any PR issues, which is good for CI turnaround. v2plugin_rootfs (called by host-pluginfs-create) builds a docker container runtime with a versioned archive of netplugin binaries, then extracts the entire filesystem from the container, which is now left as a versioned tarball. The versioned archive of netplugin binaries used for v2plugin can now be created in two ways: * run-build (dependency for demo-v2plugin) will compile then version an archive with built assets (runs only on the VM) * tar target (locally or in VM) host-pluginfs-create can be run in the VM or locally Leaving the plugin rootfs as a tarball has a few advantages reasons: * docker in a VM had internal issues when creating the plugin when the rootfs was unarchived on the host and exposed though a virtualbox mount, at least on OS X * the rootfs is versioned This adjusted host-plugin-release and demo-v2plugin to both use a new target 'host-pluginfs-unpack' Drive-by: * Fix Dockerfile by adding objdb which is needed by vendored packages * Fix config.template for the CONTIV_V2PLUGIN_NAME replacement * gtar support for OS X * use cp -a for maintaining owner Signed-off-by: Chris Plock <[email protected]>
1 parent 56fb48a commit ecd69e3

File tree

7 files changed

+148
-56
lines changed

7 files changed

+148
-56
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ cscope*
4343
.out.vagrant
4444
.etc_hosts
4545

46-
# docker v2 plugin temp files
46+
# docker v2 plugin files
4747
install/v2plugin/rootfs
4848
install/v2plugin/config.json
49+
install/v2plugin/v2plugin-*.tar.gz
4950

5051
# netcontain temporary build files
5152
scripts/netContain/bin

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ CMD ["--help"]
3737
# by far, most of the compilation time is building vendor packages
3838
# build the vendor dependencies as a separate docker caching layer
3939
COPY ./vendor/ /go/src/github.com/contiv/netplugin/vendor/
40+
COPY ./objdb/ /go/src/github.com/contiv/netplugin/objdb/
4041

4142
RUN GOGC=1500 go install -ldflags "-s -w" $(go list ./vendor/...)
4243

Makefile

+104-41
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
.PHONY: all all-CI build clean default unit-test release tar checks go-version gofmt-src \
77
golint-src govet-src run-build compile-with-docker
88

9-
DEFAULT_DOCKER_VERSION := 1.12.6
109
SHELL := /bin/bash
1110
EXCLUDE_DIRS := bin docs Godeps scripts vagrant vendor install
1211
PKG_DIRS := $(filter-out $(EXCLUDE_DIRS),$(subst /,,$(sort $(dir $(wildcard */)))))
@@ -18,19 +17,24 @@ NAME := netplugin
1817
# we evaluate and set the value of version once in a file and use it in 'tar'
1918
# and 'release' targets.
2019
VERSION_FILE := $(NAME)-version
20+
# This is treated as a string literal and is evaluated by bash in targets
21+
# It allows for compile target to set the version file for other targets
2122
VERSION := `cat $(VERSION_FILE)`
23+
TAR := $(shell command -v gtar || echo command -v tar || echo "Could not find tar")
2224
TAR_EXT := tar.bz2
23-
NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitCommit.sh)
25+
export NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitCommit.sh)
2426
TAR_FILENAME := $(NAME)-$(VERSION).$(TAR_EXT)
2527
TAR_LOC := .
26-
TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
28+
export NETPLUGIN_TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
29+
export V2PLUGIN_TAR_FILENAME := v2plugin-$(VERSION).tar.gz
2730
GO_MIN_VERSION := 1.7
2831
GO_MAX_VERSION := 1.8
2932
GO_VERSION := $(shell go version | cut -d' ' -f3 | sed 's/go//')
3033
CI_HOST_TARGETS ?= "host-unit-test host-integ-test host-build-docker-image"
3134
SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy|TestTrigger|ACIM|Netprofile"
3235
K8S_SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy"
3336
ACI_GW_IMAGE ?= "contiv/aci-gw:04-12-2017.2.2_1n"
37+
export CONTIV_V2PLUGIN_NAME ?= contiv/v2plugin:0.1
3438

3539
all: build unit-test system-test ubuntu-tests
3640

@@ -43,7 +47,7 @@ all-CI: stop clean start
4347
&& cd /opt/gopath/src/github.com/contiv/netplugin \
4448
&& make ${CI_HOST_TARGETS}"'
4549
ifdef SKIP_SYSTEM_TEST
46-
echo "Skipping system tests"
50+
@echo "Skipping system tests"
4751
else
4852
make system-test
4953
endif
@@ -95,14 +99,15 @@ checks: go-version govet-src golint-src gofmt-src misspell-src
9599
checks-with-docker:
96100
scripts/code_checks_in_docker.sh $(PKG_DIRS)
97101

102+
# install binaries into GOPATH and update file netplugin-version
98103
compile:
99104
cd $(GOPATH)/src/github.com/contiv/netplugin && \
100105
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} BUILD_VERSION=${BUILD_VERSION} \
101-
TO_BUILD="${TO_BUILD}" VERSION_FILE=${VERSION_FILE} \
106+
TO_BUILD="${TO_BUILD}" VERSION_FILE=$(VERSION_FILE) \
102107
scripts/build.sh
103108

104109
# fully prepares code for pushing to branch, includes building binaries
105-
run-build: deps checks clean compile
110+
run-build: deps checks clean compile archive
106111

107112
compile-with-docker:
108113
docker build \
@@ -129,7 +134,7 @@ update:
129134
# setting CONTIV_NODES=<number> while calling 'make demo' can be used to bring
130135
# up a cluster of <number> nodes. By default <number> = 1
131136
start:
132-
CONTIV_DOCKER_VERSION="$${CONTIV_DOCKER_VERSION:-$(DEFAULT_DOCKER_VERSION)}" CONTIV_NODE_OS=${CONTIV_NODE_OS} vagrant up
137+
CONTIV_NODE_OS=${CONTIV_NODE_OS} vagrant up
133138

134139
# ===================================================================
135140
#kubernetes demo targets
@@ -213,7 +218,7 @@ ssh:
213218
@vagrant ssh netplugin-node1 -c 'bash -lc "cd /opt/gopath/src/github.com/contiv/netplugin/ && bash"' || echo 'Please run "make demo"'
214219

215220
ssh-build: start
216-
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make run-build install-shell-completion"'
221+
$(call make-on-node1, run-build install-shell-completion)
217222

218223
unit-test: stop clean
219224
./scripts/unittests -vagrant
@@ -290,63 +295,121 @@ host-restart:
290295

291296
# create the rootfs for v2plugin. this is required for docker plugin create command
292297
host-pluginfs-create:
293-
@echo dev: creating a docker v2plugin rootfs ...
294-
sh scripts/v2plugin_rootfs.sh
298+
./scripts/v2plugin_rootfs.sh
295299

296-
# if rootfs already exists, copy newly compiled contiv binaries and start plugin on local host
297-
host-plugin-update:
298-
@echo dev: updating docker v2plugin ...
300+
# remove the v2plugin from docker
301+
host-plugin-remove:
302+
@echo dev: removing docker v2plugin ...
299303
docker plugin disable ${CONTIV_V2PLUGIN_NAME}
300304
docker plugin rm -f ${CONTIV_V2PLUGIN_NAME}
301-
cp bin/netplugin bin/netmaster bin/netctl install/v2plugin/rootfs
305+
306+
# add the v2plugin to docker with the current rootfs
307+
host-plugin-create:
308+
@echo Creating docker v2plugin
302309
docker plugin create ${CONTIV_V2PLUGIN_NAME} install/v2plugin
303310
docker plugin enable ${CONTIV_V2PLUGIN_NAME}
304311

305-
# cleanup all containers, plugins and start the v2plugin on all hosts
306-
host-plugin-restart:
312+
# re-deploy the v2plugin in docker with latest versioned binaries
313+
host-plugin-update: host-plugin-remove unarchive host-plugin-create
314+
rm -rf install/v2plugin/rootfs
315+
316+
# cleanup all containers, recreate and start the v2plugin on all hosts
317+
# uses the latest compile binaries
318+
host-plugin-restart: unarchive
307319
@echo dev: restarting services...
308-
cp bin/netplugin bin/netmaster bin/netctl install/v2plugin/rootfs
309-
cd $(GOPATH)/src/github.com/contiv/netplugin/scripts/python && PYTHONIOENCODING=utf-8 ./startPlugin.py -nodes ${CLUSTER_NODE_IPS} -plugintype "v2plugin"
310-
311-
# complete workflow to create rootfs, create/enable plugin and start swarm-mode
312-
demo-v2plugin:
313-
CONTIV_V2PLUGIN_NAME="$${CONTIV_V2PLUGIN_NAME:-contiv/v2plugin:0.1}" CONTIV_DOCKER_VERSION="$${CONTIV_DOCKER_VERSION:-1.13.1}" CONTIV_DOCKER_SWARM="$${CONTIV_DOCKER_SWARM:-swarm_mode}" make ssh-build
314-
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make host-pluginfs-create host-plugin-restart host-swarm-restart"'
315-
316-
# release a v2 plugin
317-
host-plugin-release:
318-
@echo dev: creating a docker v2plugin ...
319-
sh scripts/v2plugin_rootfs.sh
320-
docker plugin create ${CONTIV_V2PLUGIN_NAME} install/v2plugin
320+
cd $(GOPATH)/src/github.com/contiv/netplugin/scripts/python \
321+
&& PYTHONIOENCODING=utf-8 ./startPlugin.py -nodes ${CLUSTER_NODE_IPS} \
322+
-plugintype "v2plugin"
323+
324+
# unpack v2plugin archive created by host-pluginfs-create
325+
host-pluginfs-unpack:
326+
# clear out old plugin completely
327+
sudo rm -rf install/v2plugin/rootfs
328+
mkdir -p install/v2plugin/rootfs
329+
sudo tar -xf install/v2plugin/${V2PLUGIN_TAR_FILENAME} \
330+
-C install/v2plugin/rootfs/ \
331+
--exclude=usr/share/terminfo --exclude=dev/null \
332+
--exclude=etc/terminfo/v/vt220
333+
334+
# Runs make targets on the first netplugin vagrant node
335+
# this is used as a macro like $(call make-on-node1, compile checks)
336+
make-on-node1 = vagrant ssh netplugin-node1 -c '\
337+
bash -lc "source /etc/profile.d/envvar.sh \
338+
&& cd /opt/gopath/src/github.com/contiv/netplugin && make $(1)"'
339+
340+
# Calls macro make-on-node1 but can be used as a dependecy by setting
341+
# the variable "node1-make-targets"
342+
make-on-node1-dep:
343+
$(call make-on-node1, $(node1-make-targets))
344+
345+
# assumes the v2plugin archive is available, installs the v2plugin and resets
346+
# everything on the vm to clean state
347+
v2plugin-install:
348+
@echo Installing v2plugin
349+
$(call make-on-node1, install-shell-completion host-pluginfs-unpack \
350+
host-plugin-restart host-swarm-restart)
351+
352+
# Just like demo-v2plugin except builds are done locally and cached
353+
demo-v2plugin-from-local: export CONTIV_DOCKER_VERSION ?= 1.13.1
354+
demo-v2plugin-from-local: export CONTIV_DOCKER_SWARM := swarm_mode
355+
demo-v2plugin-from-local: tar host-pluginfs-create start v2plugin-install
356+
357+
# demo v2plugin on VMs: creates plugin assets, starts docker swarm
358+
# then creates and enables v2plugin
359+
demo-v2plugin: export CONTIV_DOCKER_VERSION ?= 1.13.1
360+
demo-v2plugin: export CONTIV_DOCKER_SWARM := swarm_mode
361+
demo-v2plugin: node1-make-targets := host-pluginfs-create
362+
demo-v2plugin: ssh-build make-on-node1-dep v2plugin-install
363+
364+
# release a v2 plugin from the VM
365+
host-plugin-release: tar host-pluginfs-create host-pluginfs-unpack host-plugin-create
321366
@echo dev: pushing ${CONTIV_V2PLUGIN_NAME} to docker hub
322367
@echo dev: need docker login with user in contiv org
323368
docker plugin push ${CONTIV_V2PLUGIN_NAME}
324369

370+
# unarchive versioned binaries to bin, usually as a helper for other targets
371+
# uses the version stored in file 'netplugin-version' which is produced anytime
372+
# binaries are compiled (and that version can be set with env var
373+
# 'BUILD_VERSION'
374+
unarchive:
375+
@# $(NETPLUGIN_TAR_FILE) depends on local file netplugin-version (exists in image),
376+
@# but it is evaluated after we have extracted that file to local disk
377+
@echo Updating bin/ with binaries versioned $(VERSION)
378+
tar -xf $(NETPLUGIN_TAR_FILE) -C bin
379+
380+
# pulls netplugin binaries from build container
381+
binaries-from-container:
382+
docker rm netplugin-build 2>/dev/null || :
383+
c_id=$$(docker create --name netplugin-build \
384+
netplugin-build:$(NETPLUGIN_CONTAINER_TAG)) && \
385+
docker cp \
386+
$${c_id}:/go/src/github.com/contiv/netplugin/netplugin-version ./ && \
387+
for f in netplugin netmaster netctl contivk8s netcontiv; do \
388+
docker cp -a $${c_id}:/go/bin/$$f bin/$$f; done && \
389+
docker rm $${c_id}
390+
325391
##########################
326392
## Packaging and Releasing
327393
##########################
328394

329-
# build tarball
330-
tar: compile-with-docker
331-
@# $(TAR_FILE) depends on local file netplugin-version (exists in image),
332-
@# but it is evaluated after we have extracted that file to local disk
333-
docker rm netplugin-build || :
334-
c_id=$$(docker create --name netplugin-build netplugin-build:$(NETPLUGIN_CONTAINER_TAG)) && \
335-
docker cp $${c_id}:/go/src/github.com/contiv/netplugin/netplugin-version ./ && \
336-
for f in netplugin netmaster netctl contivk8s netcontiv; do \
337-
docker cp $${c_id}:/go/bin/$$f bin/$$f; done && \
338-
docker rm $${c_id}
339-
tar -jcf $(TAR_FILE) \
395+
archive:
396+
$(TAR) --version | grep -q GNU \
397+
|| (echo Please use GNU tar as \'gtar\' or \'tar\'; exit 1)
398+
$(TAR) --owner=0 --group=0 -jcf $(NETPLUGIN_TAR_FILE) \
340399
-C bin netplugin netmaster netctl contivk8s netcontiv \
341400
-C ../scripts contrib/completion/bash/netctl get-contiv-diags
342401

402+
# build versioned archive of netplugin binaries
403+
tar: compile-with-docker binaries-from-container archive
404+
343405
clean-tar:
344406
@rm -f $(TAR_LOC)/*.$(TAR_EXT)
345407
@rm -f ${VERSION_FILE}
408+
@rm -f install/v2plugin/v2plugin-*.tar.gz
346409

347410
# GITHUB_USER and GITHUB_TOKEN are needed be set to run github-release
348411
release: tar
349-
TAR_FILENAME=$(TAR_FILENAME) TAR_FILE=$(TAR_FILE) \
412+
TAR_FILENAME=$(TAR_FILENAME) TAR_FILE=$(NETPLUGIN_TAR_FILE) \
350413
OLD_VERSION=${OLD_VERSION} BUILD_VERSION=${BUILD_VERSION} \
351414
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} scripts/release.sh
352415
@make clean-tar

install/v2plugin/Dockerfile

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ RUN mkdir -p /run/docker/plugins /etc/openvswitch /var/run/contiv/log \
1010
&& wget https://github.com/andyshinn/alpine-pkg-glibc/releases/download/2.23-r1/glibc-2.23-r1.apk \
1111
&& apk --no-cache add glibc-2.23-r1.apk
1212

13-
COPY netplugin netmaster netctl startcontiv.sh /
13+
# copy in binaries and scripts
14+
ARG TAR_FILE
15+
ADD ${TAR_FILE} /
16+
COPY startcontiv.sh /
1417

18+
# this container is never run, it is exported for docker to run as a plugin,
19+
# the startcontiv.sh script and the netplugin binaries are copied into the
20+
# plugin's rootfs after export, this avoids copying into and out of container
1521
ENTRYPOINT ["/startcontiv.sh"]

install/v2plugin/config.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"value"
103103
],
104104
## Do not change the default value, this will be replaced with $CONTIV_V2PLUGIN_NAME
105-
"Value": "PluginName"
105+
"Value": "__CONTIV_V2PLUGIN_NAME__"
106106
}
107107
],
108108
"mounts": [

scripts/jenkins_cleanup.sh

100755100644
File mode changed.

scripts/v2plugin_rootfs.sh

100644100755
+33-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
#!/bin/bash
2+
23
# Script to create the docker v2 plugin
34
# run this script from contiv/netplugin directory
5+
# requires NETPLUGIN_CONTAINER_TAG for contivrootfs image
6+
# requires CONTIV_NETPLUGIN_NAME, the Network Driver name for requests to
7+
# dockerd, should look like contiv/v2plugin:$NETPLUGIN_CONTAINER_TAG
8+
# requires NETPLUGIN_TAR_FILE to point to the netplugin binaries
9+
10+
set -euxo pipefail
11+
12+
echo "Creating rootfs for v2plugin: ${CONTIV_V2PLUGIN_NAME}"
13+
14+
15+
# config.json is docker's runtime configuration for the container
16+
# delete comments and replace placeholder with ${CONTIV_V2PLUGIN_NAME}
17+
sed '/##/d;s/__CONTIV_V2PLUGIN_NAME__/${CONTIV_V2PLUGIN_NAME}/' \
18+
install/v2plugin/config.template > install/v2plugin/config.json
19+
20+
# copy over binaries
21+
cp ${NETPLUGIN_TAR_FILE} install/v2plugin/
422

5-
echo "Creating rootfs for v2plugin ", ${CONTIV_V2PLUGIN_NAME}
6-
rm -rf install/v2plugin/rootfs
7-
rm install/v2plugin/config.json
8-
cat install/v2plugin/config.template | grep -v "##" > install/v2plugin/config.json
9-
sed -i "s%PluginName%${CONTIV_V2PLUGIN_NAME}%" install/v2plugin/config.json
10-
cp bin/netplugin bin/netmaster bin/netctl install/v2plugin
11-
docker build -t contivrootfs install/v2plugin
12-
id=$(docker create contivrootfs true)
13-
mkdir -p install/v2plugin/rootfs
14-
sudo docker export "${id}" | sudo tar -x -C install/v2plugin/rootfs
23+
DOCKER_IMAGE=contivrootfs:${NETPLUGIN_CONTAINER_TAG}
24+
docker build -t ${DOCKER_IMAGE} \
25+
--build-arg TAR_FILE=$(basename "${NETPLUGIN_TAR_FILE}") install/v2plugin
26+
27+
rm install/v2plugin/${NETPLUGIN_TAR_FILE}
28+
29+
# creates a ready to run container but doesn't run it
30+
id=$(docker create $DOCKER_IMAGE true)
31+
32+
# create the rootfs archive based on the created container contents
33+
sudo docker export "${id}" > install/v2plugin/${V2PLUGIN_TAR_FILENAME}
34+
35+
# clean up created container
1536
docker rm -vf "${id}"
16-
docker rmi contivrootfs
17-
rm install/v2plugin/netplugin install/v2plugin/netmaster install/v2plugin/netctl
37+
38+
echo netplugin\'s docker plugin rootfs is archived at install/v2plugin/${V2PLUGIN_TAR_FILENAME}

0 commit comments

Comments
 (0)