Skip to content

Commit

Permalink
Use docker to build go binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Jul 7, 2018
1 parent 5827c98 commit 479a519
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ site

# temporal github pages
gh-pages

test/binaries
27 changes: 3 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@ sudo: required
services:
- docker

language: go

addons:
apt:
packages:
- luarocks
language: generic

notifications:
email:
on_failure: always
on_success: never

go:
- 1.10.3

go_import_path: k8s.io/ingress-nginx

# New secure variables can be added using travis encrypt -r kubernetes/ingress-nginx --add K=V
env:
global:
Expand All @@ -38,18 +28,8 @@ jobs:
include:
- stage: Static Check
script:
- sudo luarocks install luacheck
- make luacheck
- |
go get -d golang.org/x/lint/golint
cd $GOPATH/src/golang.org/x/tools
git checkout release-branch.go1.10
go install golang.org/x/lint/golint
cd -
- make verify-all
- make static-check
- stage: Lua Unit Test
before_script:
- rootfs/etc/nginx/lua/test/up.sh
script:
- make lua-test
- stage: Coverage
Expand All @@ -60,9 +40,8 @@ jobs:
- make cover
- stage: e2e
before_script:
- go get github.com/onsi/ginkgo/ginkgo
- test/e2e/up.sh
- make dev-env
- SKIP_MINIKUBE_START=true make dev-env
script:
- make e2e-test
# split builds to avoid job timeouts
Expand Down
112 changes: 52 additions & 60 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,40 @@
.PHONY: all
all: all-container

BUILDTAGS=

# Use the 0.0 tag for testing, it shouldn't clobber any release builds
TAG?=0.16.2
REGISTRY?=quay.io/kubernetes-ingress-controller
GOOS?=linux
DOCKER?=docker
SED_I?=sed -i
TAG ?= 0.16.2
REGISTRY ?= quay.io/kubernetes-ingress-controller
DOCKER ?= docker
SED_I ?= sed -i
GOHOSTOS ?= $(shell go env GOHOSTOS)

# e2e settings
# Allow limiting the scope of the e2e tests. By default run everything
FOCUS?=.*
FOCUS ?= .*
# number of parallel test
E2E_NODES?=3

E2E_NODES ?= 3

ifeq ($(GOHOSTOS),darwin)
SED_I=sed -i ''
endif

REPO_INFO=$(shell git config --get remote.origin.url)

ifndef COMMIT
COMMIT := git-$(shell git rev-parse --short HEAD)
ifndef GIT_COMMIT
GIT_COMMIT := git-$(shell git rev-parse --short HEAD)
endif

PKG=k8s.io/ingress-nginx
PKG = k8s.io/ingress-nginx

ARCH ?= $(shell go env GOARCH)
GOARCH = ${ARCH}
DUMB_ARCH = ${ARCH}

ALL_ARCH = amd64 arm arm64 ppc64le s390x

QEMUVERSION=v2.12.0
QEMUVERSION = v2.12.0

BUSTED_ARGS=-v --pattern=_test
BUSTED_ARGS =-v --pattern=_test

IMGNAME = nginx-ingress-controller
IMAGE = $(REGISTRY)/$(IMGNAME)
Expand All @@ -67,19 +63,27 @@ ifeq ($(ARCH),arm)
DUMB_ARCH=armhf
endif
ifeq ($(ARCH),arm64)
QEMUARCH=aarch64
QEMUARCH=aarch64
endif
ifeq ($(ARCH),ppc64le)
QEMUARCH=ppc64le
GOARCH=ppc64le
DUMB_ARCH=ppc64el
endif
ifeq ($(ARCH),s390x)
QEMUARCH=s390x
QEMUARCH=s390x
endif

TEMP_DIR := $(shell mktemp -d)

DEF_VARS:=ARCH=$(ARCH) \
TAG=$(TAG) \
PKG=$(PKG) \
GOARCH=$(GOARCH) \
GIT_COMMIT=$(GIT_COMMIT) \
REPO_INFO=$(REPO_INFO) \
PWD=$(PWD)

DOCKERFILE := $(TEMP_DIR)/rootfs/Dockerfile

.PHONY: image-info
Expand Down Expand Up @@ -119,7 +123,7 @@ else
$(SED_I) "s/CROSS_BUILD_//g" $(DOCKERFILE)
endif

$(DOCKER) build -t $(MULTI_ARCH_IMG):$(TAG) $(TEMP_DIR)/rootfs
$(DOCKER) build --no-cache --pull -t $(MULTI_ARCH_IMG):$(TAG) $(TEMP_DIR)/rootfs

ifeq ($(ARCH), amd64)
# This is for maintaining backward compatibility
Expand Down Expand Up @@ -147,72 +151,60 @@ clean:

.PHONY: build
build: clean
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -a -installsuffix cgo \
-ldflags "-s -w -X ${PKG}/version.RELEASE=${TAG} -X ${PKG}/version.COMMIT=${COMMIT} -X ${PKG}/version.REPO=${REPO_INFO}" \
-o ${TEMP_DIR}/rootfs/nginx-ingress-controller ${PKG}/cmd/nginx
@$(DEF_VARS) \
build/go-in-docker.sh build/build.sh

mkdir -p $(TEMP_DIR)/rootfs
cp bin/$(ARCH)/nginx-ingress-controller $(TEMP_DIR)/rootfs/nginx-ingress-controller

.PHONY: verify-all
verify-all:
@./hack/verify-all.sh
.PHONY: static-check
static-check:
@$(DEF_VARS) \
build/go-in-docker.sh build/static-check.sh

.PHONY: test
test:
@go test -v -race -tags "$(BUILDTAGS) cgo" $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e')
@$(DEF_VARS) \
DOCKER_OPTS="--net=host" \
build/go-in-docker.sh build/test.sh

.PHONY: lua-test
lua-test:
@busted $(BUSTED_ARGS) ./rootfs/etc/nginx/lua/test;
@$(DEF_VARS) \
BUSTED_ARGS="$(BUSTED_ARGS)" \
build/go-in-docker.sh build/test-lua.sh

.PHONY: e2e-test
e2e-test:
@ginkgo version || go get -u github.com/onsi/ginkgo/ginkgo
@ginkgo build ./test/e2e
@KUBECONFIG=${HOME}/.kube/config ginkgo \
-randomizeSuites \
-randomizeAllSpecs \
-flakeAttempts=2 \
--focus=$(FOCUS) \
-p \
-trace \
-nodes=$(E2E_NODES) \
./test/e2e/e2e.test
@$(DEF_VARS) \
FOCUS=$(FOCUS) \
E2E_NODES=$(E2E_NODES) \
DOCKER_OPTS="--net=host" \
build/go-in-docker.sh build/e2e-tests.sh

.PHONY: cover
cover:
@rm -rf coverage.txt
@for d in `go list ./... | grep -v vendor | grep -v '/test/e2e' | grep -v 'images/grpc-fortune-teller'`; do \
t=$$(date +%s); \
go test -coverprofile=cover.out -covermode=atomic $$d || exit 1; \
echo "Coverage test $$d took $$(($$(date +%s)-t)) seconds"; \
if [ -f cover.out ]; then \
cat cover.out >> coverage.txt; \
rm cover.out; \
fi; \
done
@echo "Uploading coverage results..."
@$(DEF_VARS) \
DOCKER_OPTS="--net=host" \
build/go-in-docker.sh build/cover.sh

echo "Uploading coverage results..."
@curl -s https://codecov.io/bash | bash

.PHONY: vet
vet:
@go vet $(shell go list ${PKG}/... | grep -v vendor)

.PHONY: luacheck
luacheck:
luacheck -q ./rootfs/etc/nginx/lua/

.PHONY: release
release: all-container all-push
echo "done"

.PHONY: docker-build
docker-build: all-container

.PHONY: docker-push
docker-push: all-push

.PHONY: check_dead_links
check_dead_links:
docker run -t -v $$PWD:/tmp aledbf/awesome_bot:0.1 --allow-dupe --allow-redirect $(shell find $$PWD -mindepth 1 -name "*.md" -printf '%P\n' | grep -v vendor | grep -v Changelog.md)
docker run -t \
-v $$PWD:/tmp aledbf/awesome_bot:0.1 \
--allow-dupe \
--allow-redirect $(shell find $$PWD -mindepth 1 -name "*.md" -printf '%P\n' | grep -v vendor | grep -v Changelog.md)

.PHONY: dep-ensure
dep-ensure:
Expand All @@ -223,7 +215,7 @@ dep-ensure:

.PHONY: dev-env
dev-env:
@./hack/build-dev-env.sh
@build/dev-env.sh

.PHONY: live-docs
live-docs:
Expand Down
33 changes: 33 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2018 The Kubernetes Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.10.3-stretch

RUN apt update \
&& apt install -y --no-install-recommends \
luarocks \
&& apt-get clean -y \
&& rm -rf \
/var/cache/debconf/* \
/var/lib/apt/lists/* \
/var/log/* \
/tmp/* \
/var/tmp/*

RUN luarocks install luacheck \
&& luarocks install busted 2.0.rc12 \
&& luarocks install lua-cjson 2.1.0-1

RUN go get github.com/onsi/ginkgo/ginkgo \
&& go get golang.org/x/lint/golint
53 changes: 53 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

if [ -z "${PKG}" ]; then
echo "PKG must be set"
exit 1
fi
if [ -z "${ARCH}" ]; then
echo "ARCH must be set"
exit 1
fi
if [ -z "${GIT_COMMIT}" ]; then
echo "GIT_COMMIT must be set"
exit 1
fi
if [ -z "${REPO_INFO}" ]; then
echo "REPO_INFO must be set"
exit 1
fi
if [ -z "${TAG}" ]; then
echo "TAG must be set"
exit 1
fi
if [ -z "${TAG}" ]; then
echo "TAG must be set"
exit 1
fi

export CGO_ENABLED=0

go build -a -installsuffix cgo \
-ldflags "-s -w \
-X ${PKG}/version.RELEASE=${TAG} \
-X ${PKG}/version.COMMIT=${GIT_COMMIT} \
-X ${PKG}/version.REPO=${REPO_INFO}" \
-o bin/${ARCH}/nginx-ingress-controller ${PKG}/cmd/nginx
35 changes: 35 additions & 0 deletions build/cover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

if [ -z "${PKG}" ]; then
echo "PKG must be set"
exit 1
fi

rm -rf coverage.txt
for d in `go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e' | grep -v images`; do
t=$(date +%s);
go test -coverprofile=cover.out -covermode=atomic $d || exit 1;
echo "Coverage test $d took $(($(date +%s)-$t)) seconds";
if [ -f cover.out ]; then
cat cover.out >> coverage.txt;
rm cover.out;
fi;
done
Loading

0 comments on commit 479a519

Please sign in to comment.