Skip to content

Commit bc62324

Browse files
committed
Go code checks can be run locally in container
Before the only option was to spin up a VM to run code checks, which is expensive in terms of time. Add Dockerfile-check to provide a container build for a runtime environment that supports go tool vet, gofmt, and golint Add script 'code_checks_in_docker.sh' intended to be run inside the Dockerfile-check container that runs `make checks` which in turn runs a variety of go code checks against packages passed in. Checks are reordered so more severe issues (e.g. functional) are resolved before less severe ones (e.g. formatting). Some checks are upated to be simpler and faster because the command accept multiple directories and recurse automatically. Drive-by: * removed some trailing whitespace * added more exclusions in dockerignore * variables for go check commands not needed, they are only refernced once and require a visual lookup to find what the command actually is Signed-off-by: Chris Plock <[email protected]>
1 parent 129a23f commit bc62324

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# we want to get the git commit SHA but not all the binary repo data
22
.git/objects/pack
33
bin/
4+
.vagrant
45
vagrant/
56
docs/
67
scripts/gobgp
78
*.tar
89
*.bz2
910
# export from a docker container, no need to copy into any docker containers
1011
install/v2plugin/rootfs
12+
vagrant/k8s/export/netctl

Dockerfile-check

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM golang:1.7.6
2+
3+
ENV GOPATH=/go
4+
5+
WORKDIR /go/src/github.com/contiv/netplugin/
6+
7+
RUN go get github.com/golang/lint/golint \
8+
github.com/client9/misspell/cmd/misspell
9+
10+
CMD ["make", "checks"]

Makefile

+15-12
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
2323
GO_MIN_VERSION := 1.7
2424
GO_MAX_VERSION := 1.8
2525
GO_VERSION := $(shell go version | cut -d' ' -f3 | sed 's/go//')
26-
GOLINT_CMD := golint -set_exit_status
27-
GOFMT_CMD := gofmt -s -l
28-
GOVET_CMD := go tool vet
2926
CI_HOST_TARGETS ?= "host-unit-test host-integ-test host-build-docker-image"
3027
SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy|TestTrigger|ACIM|Netprofile"
3128
K8S_SYSTEM_TESTS_TO_RUN ?= "00SSH|Basic|Network|Policy"
@@ -63,19 +60,20 @@ godep-restore:
6360

6461
gofmt-src:
6562
$(info +++ gofmt $(PKG_DIRS))
66-
@for dir in $(PKG_DIRS); do $(GOFMT_CMD) $${dir} | grep "go"; [[ $$? -ne 0 ]] || exit 1; done
63+
@[[ -z "$$(gofmt -s -d $(PKG_DIRS) | tee /dev/stderr)" ]] || exit 1
6764

65+
# go lint does not automatically recurse
6866
golint-src:
6967
$(info +++ golint $(PKG_DIRS))
70-
@for dir in $(PKG_DIRS); do $(GOLINT_CMD) $${dir}/... || exit 1;done
68+
@for dir in $(PKG_DIRS); do golint -set_exit_status $${dir}/...; done
7169

7270
govet-src:
7371
$(info +++ govet $(PKG_DIRS))
74-
@for dir in $(PKG_DIRS); do $(GOVET_CMD) $${dir} || exit 1;done
72+
@go tool vet $(PKG_DIRS)
7573

7674
misspell-src:
7775
$(info +++ check spelling $(PKG_DIRS))
78-
misspell -locale US -error $(PKG_DIRS)
76+
@misspell -locale US -error $(PKG_DIRS)
7977

8078
go-version:
8179
$(info +++ check go version)
@@ -86,7 +84,12 @@ ifneq ($(GO_VERSION), $(firstword $(sort $(GO_VERSION) $(GO_MAX_VERSION))))
8684
$(error go version check failed, expected <= $(GO_MAX_VERSION), found $(GO_VERSION))
8785
endif
8886

89-
checks: go-version gofmt-src golint-src govet-src misspell-src
87+
checks: go-version govet-src golint-src gofmt-src misspell-src
88+
89+
# When multi-stage builds are available in VM, source can be copied into
90+
# container FROM the netplugin-build container to simplify this target
91+
checks-with-docker:
92+
scripts/code_checks_in_docker.sh $(PKG_DIRS)
9093

9194
compile:
9295
cd $(GOPATH)/src/github.com/contiv/netplugin && \
@@ -263,7 +266,7 @@ host-integ-test: host-cleanup start-aci-gw
263266

264267
start-aci-gw:
265268
@echo dev: starting aci gw...
266-
docker pull $(ACI_GW_IMAGE)
269+
docker pull $(ACI_GW_IMAGE)
267270
docker run --net=host -itd -e "APIC_URL=SANITY" -e "APIC_USERNAME=IGNORE" -e "APIC_PASSWORD=IGNORE" --name=contiv-aci-gw $(ACI_GW_IMAGE)
268271

269272
host-build-docker-image:
@@ -307,11 +310,11 @@ demo-v2plugin:
307310
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"'
308311

309312
# release a v2 plugin
310-
host-plugin-release:
313+
host-plugin-release:
311314
@echo dev: creating a docker v2plugin ...
312-
sh scripts/v2plugin_rootfs.sh
315+
sh scripts/v2plugin_rootfs.sh
313316
docker plugin create ${CONTIV_V2PLUGIN_NAME} install/v2plugin
314-
@echo dev: pushing ${CONTIV_V2PLUGIN_NAME} to docker hub
317+
@echo dev: pushing ${CONTIV_V2PLUGIN_NAME} to docker hub
315318
@echo dev: need docker login with user in contiv org
316319
docker plugin push ${CONTIV_V2PLUGIN_NAME}
317320

scripts/code_checks_in_docker.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
PKG_DIRS=$*
6+
7+
docker build -f Dockerfile-check -t contiv/netplugin-checks .
8+
9+
# create container, stdout is the created container id
10+
container_id=$(docker create --name netplugin-checks \
11+
contiv/netplugin-checks)
12+
13+
# when there is an exit, remove the container
14+
function remove_container {
15+
docker rm ${container_id}
16+
}
17+
trap remove_container EXIT
18+
19+
NETPLUGIN_DIR=/go/src/github.com/contiv/netplugin
20+
# copy Makefile and go packages to be checked
21+
docker cp Makefile ${container_id}:${NETPLUGIN_DIR}/
22+
for pkg in ${PKG_DIRS}; do
23+
docker cp $pkg ${container_id}:${NETPLUGIN_DIR}/
24+
done
25+
26+
# run the checks
27+
docker start --attach ${container_id}

0 commit comments

Comments
 (0)