Skip to content

Commit 06fa667

Browse files
chrisplounclejack
authored andcommitted
Build versioned v2plugin from versioned binaries (#1040)
* Build versioned v2plugin from versioned binaries This enables build and docker image caching on a build machine for the v2plugin. The new target demo-v2plugin-from-local does everything it can locally before spawning the VM, failing much faster on any PR issues, which is good for CI turnaround. It runs a little faster than demo-v2plugin after base image layers are cached. 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 The plugin rootfs is left as a tarball because: * 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 both use a new target 'host-pluginfs-unpack' Drive-by: * 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]> * review feedback * comments, revert a var rename for smaller diff * more dockerignore * comments, and don't remove rootfs * review testing feedback binaries-from-container and host-plugin-update * simplify usage for v2plugin-update * feedback for v2plugin docker version variable
1 parent cbc71c4 commit 06fa667

File tree

7 files changed

+150
-53
lines changed

7 files changed

+150
-53
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# we want to get the git commit SHA but not all the binary repo data
22
.git/objects/pack
33
bin/
4+
**/*.pyc
45
.vagrant
56
vagrant/
67
docs/
78
scripts/gobgp
9+
# this is the dockerfile for binary compilation, other dockerfiles
10+
# may want archive artifacts, but this one does not
811
*.tar
12+
**/*.tar.gz
913
*.bz2
1014
# export from a docker container, no need to copy into any docker containers
1115
install/v2plugin/rootfs

.gitignore

Lines changed: 2 additions & 1 deletion
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

Makefile

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
golint-src govet-src run-build compile-with-docker
77

88
DEFAULT_DOCKER_VERSION := 1.12.6
9+
V2PLUGIN_DOCKER_VERSION := 1.13.1
910
SHELL := /bin/bash
1011
EXCLUDE_DIRS := bin docs Godeps scripts vagrant vendor install
1112
PKG_DIRS := $(filter-out $(EXCLUDE_DIRS),$(subst /,,$(sort $(dir $(wildcard */)))))
@@ -14,18 +15,21 @@ HOST_GOBIN := `if [ -n "$$(go env GOBIN)" ]; then go env GOBIN; else dirname $$(
1415
HOST_GOROOT := `go env GOROOT`
1516
NAME := netplugin
1617
VERSION := $(shell scripts/getGitVersion.sh)
18+
TAR := $(shell command -v gtar || echo command -v tar || echo "Could not find tar")
1719
TAR_EXT := tar.bz2
18-
NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitVersion.sh)
20+
export NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitVersion.sh)
1921
TAR_FILENAME := $(NAME)-$(VERSION).$(TAR_EXT)
2022
TAR_LOC := .
21-
TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
23+
export TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
24+
export V2PLUGIN_TAR_FILENAME := v2plugin-$(VERSION).tar.gz
2225
GO_MIN_VERSION := 1.7
2326
GO_MAX_VERSION := 1.8
2427
GO_VERSION := $(shell go version | cut -d' ' -f3 | sed 's/go//')
2528
CI_HOST_TARGETS ?= "host-unit-test host-integ-test host-build-docker-image"
2629
SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy|TestTrigger|ACIM|Netprofile"
2730
K8S_SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy"
2831
ACI_GW_IMAGE ?= "contiv/aci-gw:04-12-2017.2.2_1n"
32+
export CONTIV_V2PLUGIN_NAME ?= contiv/v2plugin:0.1
2933

3034
all: build unit-test system-test ubuntu-tests
3135

@@ -38,7 +42,7 @@ all-CI: stop clean start
3842
&& cd /opt/gopath/src/github.com/contiv/netplugin \
3943
&& make ${CI_HOST_TARGETS}"'
4044
ifdef SKIP_SYSTEM_TEST
41-
echo "Skipping system tests"
45+
@echo "Skipping system tests"
4246
else
4347
make system-test
4448
endif
@@ -90,13 +94,14 @@ checks: go-version govet-src golint-src gofmt-src misspell-src
9094
checks-with-docker:
9195
scripts/code_checks_in_docker.sh $(PKG_DIRS)
9296

97+
# install binaries into GOPATH and update file netplugin-version
9398
compile:
9499
cd $(GOPATH)/src/github.com/contiv/netplugin && \
95100
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} TO_BUILD="${TO_BUILD}" \
96101
BUILD_VERSION=$(VERSION) scripts/build.sh
97102

98103
# fully prepares code for pushing to branch, includes building binaries
99-
run-build: deps checks clean compile
104+
run-build: deps checks clean compile archive
100105

101106
compile-with-docker:
102107
docker build \
@@ -284,55 +289,117 @@ host-restart:
284289

285290
# create the rootfs for v2plugin. this is required for docker plugin create command
286291
host-pluginfs-create:
287-
@echo dev: creating a docker v2plugin rootfs ...
288-
sh scripts/v2plugin_rootfs.sh
292+
./scripts/v2plugin_rootfs.sh
289293

290-
# if rootfs already exists, copy newly compiled contiv binaries and start plugin on local host
291-
host-plugin-update:
292-
@echo dev: updating docker v2plugin ...
294+
# remove the v2plugin from docker
295+
host-plugin-remove:
296+
@echo dev: removing docker v2plugin ...
293297
docker plugin disable ${CONTIV_V2PLUGIN_NAME}
294298
docker plugin rm -f ${CONTIV_V2PLUGIN_NAME}
295-
cp bin/netplugin bin/netmaster bin/netctl install/v2plugin/rootfs
299+
300+
# add the v2plugin to docker with the current rootfs
301+
host-plugin-create:
302+
@echo Creating docker v2plugin
296303
docker plugin create ${CONTIV_V2PLUGIN_NAME} install/v2plugin
297304
docker plugin enable ${CONTIV_V2PLUGIN_NAME}
298305

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

372+
# unarchive versioned binaries to bin, usually as a helper for other targets
373+
unarchive:
374+
@echo Updating bin/ with binaries versioned $(VERSION)
375+
tar -xf $(TAR_FILE) -C bin
376+
377+
# pulls netplugin binaries from build container
378+
binaries-from-container:
379+
docker rm netplugin-build 2>/dev/null || :
380+
c_id=$$(docker create --name netplugin-build \
381+
netplugin-build:$(NETPLUGIN_CONTAINER_TAG)) && \
382+
for f in netplugin netmaster netctl contivk8s netcontiv; do \
383+
docker cp -a $${c_id}:/go/bin/$$f bin/$$f; done && \
384+
docker rm $${c_id}
385+
319386
##########################
320387
## Packaging and Releasing
321388
##########################
322389

323-
# build tarball
324-
tar: compile-with-docker
325-
docker rm netplugin-build || :
326-
c_id=$$(docker create --name netplugin-build netplugin-build:$(NETPLUGIN_CONTAINER_TAG)) && \
327-
for f in netplugin netmaster netctl contivk8s netcontiv; do \
328-
docker cp $${c_id}:/go/bin/$$f bin/$$f; done && \
329-
docker rm $${c_id}
330-
tar -jcf $(TAR_FILE) \
390+
archive:
391+
$(TAR) --version | grep -q GNU \
392+
|| (echo Please use GNU tar as \'gtar\' or \'tar\'; exit 1)
393+
$(TAR) --owner=0 --group=0 -jcf $(TAR_FILE) \
331394
-C bin netplugin netmaster netctl contivk8s netcontiv \
332395
-C ../scripts contrib/completion/bash/netctl get-contiv-diags
333396

397+
# build versioned archive of netplugin binaries
398+
tar: compile-with-docker binaries-from-container archive
399+
334400
clean-tar:
335401
@rm -f $(TAR_LOC)/*.$(TAR_EXT)
402+
@rm -f install/v2plugin/v2plugin-*.tar.gz
336403

337404
# do not run directly, use "release" target
338405
release-built-version: tar

install/v2plugin/Dockerfile

Lines changed: 7 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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/getGitVersion.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ NIGHTLY_RELEASE=${NIGHTLY_RELEASE:-}
77

88
# calculate version
99
if command -v git &>/dev/null && git rev-parse &>/dev/null; then
10-
GIT_COMMIT=$(git describe --tags --always 2>/dev/null || echo unknown)
11-
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
12-
GIT_COMMIT="$GIT_COMMIT-unsupported"
13-
fi
14-
VERSION=$GIT_COMMIT
10+
VERSION=$(git describe --abbrev=7 --dirty=-unsupported --tags --always 2>/dev/null || echo unknown)
1511
else
1612
echo >&2 'error: unable to determine the git revision'
1713
exit 1

scripts/v2plugin_rootfs.sh

100644100755
Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
#!/bin/bash
2+
23
# Script to create the docker v2 plugin
34
# run this script from contiv/netplugin directory
45

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
6+
# requires NETPLUGIN_CONTAINER_TAG for contivrootfs image
7+
# requires CONTIV_V2PLUGIN_NAME, the Network Driver name for requests to
8+
# dockerd, should look like contiv/v2plugin:$NETPLUGIN_CONTAINER_TAG
9+
# requires TAR_FILE to point to the netplugin binaries
10+
# requires V2PLUGIN_TAR_FILENAME to point to the v2plugin archive
11+
12+
set -euxo pipefail
13+
14+
echo "Creating rootfs for v2plugin: ${CONTIV_V2PLUGIN_NAME}"
15+
16+
17+
# config.json is docker's runtime configuration for the container
18+
# delete comments and replace placeholder with ${CONTIV_V2PLUGIN_NAME}
19+
sed '/##/d;s/__CONTIV_V2PLUGIN_NAME__/${CONTIV_V2PLUGIN_NAME}/' \
20+
install/v2plugin/config.template > install/v2plugin/config.json
21+
22+
# copy over binaries
23+
cp ${TAR_FILE} install/v2plugin/
24+
25+
DOCKER_IMAGE=contivrootfs:${NETPLUGIN_CONTAINER_TAG}
26+
docker build -t ${DOCKER_IMAGE} \
27+
--build-arg TAR_FILE=$(basename "${TAR_FILE}") install/v2plugin
28+
29+
rm install/v2plugin/${TAR_FILE} # this was a copy of netplugin archive
30+
31+
# creates a ready to run container but doesn't run it
32+
id=$(docker create $DOCKER_IMAGE true)
33+
34+
# create the rootfs archive based on the created container contents
35+
docker export "${id}" > install/v2plugin/${V2PLUGIN_TAR_FILENAME}
36+
37+
# clean up created container
1538
docker rm -vf "${id}"
16-
docker rmi contivrootfs
17-
rm install/v2plugin/netplugin install/v2plugin/netmaster install/v2plugin/netctl
39+
40+
echo netplugin\'s docker plugin rootfs is archived at install/v2plugin/${V2PLUGIN_TAR_FILENAME}

0 commit comments

Comments
 (0)