Skip to content

Commit 27c825c

Browse files
committed
Add build scaffolding
1 parent 33c326a commit 27c825c

8 files changed

+300
-2
lines changed

.gitignore

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
1515

16-
kubeconfig
17-
main
16+
/kubeconfig
17+
/main
18+
19+
/bin
20+
/.go
21+
/.push-*
22+
/.container-*
23+
/.dockerfile-*

Dockerfile.in

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM ARG_FROM
16+
17+
MAINTAINER Joe Beda <[email protected]>
18+
19+
ADD bin/ARG_ARCH/ARG_BIN /ARG_BIN
20+
21+
USER nobody:nobody
22+
ENTRYPOINT ["/ARG_BIN"]

Makefile

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# The binary to build (just the basename).
16+
BIN := tgik-controller
17+
18+
# This repo's root import path (under GOPATH).
19+
PKG := github.com/jbeda/tgik-controller
20+
21+
# Where to push the docker image.
22+
REGISTRY ?= jbeda
23+
24+
# Which architecture to build - see $(ALL_ARCH) for options.
25+
ARCH ?= amd64
26+
27+
# This version-strategy uses git tags to set the version string
28+
VERSION := $(shell git describe --tags --always --dirty)
29+
#
30+
# This version-strategy uses a manual value to set the version string
31+
#VERSION := 1.2.3
32+
33+
###
34+
### These variables should not need tweaking.
35+
###
36+
37+
ALL_ARCH := amd64 arm arm64 ppc64le
38+
39+
# Set default base image dynamically for each arch
40+
ifeq ($(ARCH),amd64)
41+
BASEIMAGE?=alpine
42+
endif
43+
ifeq ($(ARCH),arm)
44+
BASEIMAGE?=armel/busybox
45+
endif
46+
ifeq ($(ARCH),arm64)
47+
BASEIMAGE?=aarch64/busybox
48+
endif
49+
ifeq ($(ARCH),ppc64le)
50+
BASEIMAGE?=ppc64le/busybox
51+
endif
52+
53+
IMAGE := $(REGISTRY)/$(BIN)-$(ARCH)
54+
55+
BUILD_IMAGE ?= golang:1.8-alpine
56+
57+
DOCKER_MOUNT_MODE=delegated
58+
59+
# If you want to build all binaries, see the 'all-build' rule.
60+
# If you want to build all containers, see the 'all-container' rule.
61+
# If you want to build AND push all containers, see the 'all-push' rule.
62+
all: build
63+
64+
build-%:
65+
@$(MAKE) --no-print-directory ARCH=$* build
66+
67+
container-%:
68+
@$(MAKE) --no-print-directory ARCH=$* container
69+
70+
push-%:
71+
@$(MAKE) --no-print-directory ARCH=$* push
72+
73+
all-build: $(addprefix build-, $(ALL_ARCH))
74+
75+
all-container: $(addprefix container-, $(ALL_ARCH))
76+
77+
all-push: $(addprefix push-, $(ALL_ARCH))
78+
79+
build: bin/$(ARCH)/$(BIN)
80+
81+
bin/$(ARCH)/$(BIN): build-dirs
82+
@echo "building: $@"
83+
@docker run \
84+
-ti \
85+
-u $$(id -u):$$(id -g) \
86+
-v $$(pwd)/.go:/go:$(DOCKER_MOUNT_MODE) \
87+
-v $$(pwd):/go/src/$(PKG):$(DOCKER_MOUNT_MODE) \
88+
-v $$(pwd)/bin/$(ARCH):/go/bin:$(DOCKER_MOUNT_MODE) \
89+
-v $$(pwd)/bin/$(ARCH):/go/bin/linux_$(ARCH):$(DOCKER_MOUNT_MODE) \
90+
-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static:$(DOCKER_MOUNT_MODE) \
91+
-w /go/src/$(PKG) \
92+
$(BUILD_IMAGE) \
93+
/bin/sh -c " \
94+
ARCH=$(ARCH) \
95+
VERSION=$(VERSION) \
96+
PKG=$(PKG) \
97+
./build/build.sh \
98+
"
99+
100+
DOTFILE_IMAGE = $(subst :,_,$(subst /,_,$(IMAGE))-$(VERSION))
101+
102+
container: .container-$(DOTFILE_IMAGE) container-name
103+
.container-$(DOTFILE_IMAGE): bin/$(ARCH)/$(BIN) Dockerfile.in
104+
@sed \
105+
-e 's|ARG_BIN|$(BIN)|g' \
106+
-e 's|ARG_ARCH|$(ARCH)|g' \
107+
-e 's|ARG_FROM|$(BASEIMAGE)|g' \
108+
Dockerfile.in > .dockerfile-$(ARCH)
109+
@docker build -t $(IMAGE):$(VERSION) -f .dockerfile-$(ARCH) .
110+
@docker images -q $(IMAGE):$(VERSION) > $@
111+
112+
container-name:
113+
@echo "container: $(IMAGE):$(VERSION)"
114+
115+
push: .push-$(DOTFILE_IMAGE) push-name
116+
.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE)
117+
ifeq ($(findstring gcr.io,$(REGISTRY)),gcr.io)
118+
@gcloud docker -- push $(IMAGE):$(VERSION)
119+
else
120+
@docker push $(IMAGE):$(VERSION)
121+
endif
122+
@docker images -q $(IMAGE):$(VERSION) > $@
123+
124+
push-name:
125+
@echo "pushed: $(IMAGE):$(VERSION)"
126+
127+
version:
128+
@echo $(VERSION)
129+
130+
test: build-dirs
131+
@docker run \
132+
-ti \
133+
-u $$(id -u):$$(id -g) \
134+
-v $$(pwd)/.go:/go:$(DOCKER_MOUNT_MODE) \
135+
-v $$(pwd):/go/src/$(PKG):$(DOCKER_MOUNT_MODE) \
136+
-v $$(pwd)/bin/$(ARCH):/go/bin:$(DOCKER_MOUNT_MODE) \
137+
-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static:$(DOCKER_MOUNT_MODE) \
138+
-w /go/src/$(PKG) \
139+
$(BUILD_IMAGE) \
140+
/bin/sh -c " \
141+
./build/test.sh \
142+
"
143+
144+
build-dirs:
145+
@mkdir -p bin/$(ARCH)
146+
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(ARCH)
147+
148+
clean: container-clean bin-clean
149+
150+
container-clean:
151+
rm -rf .container-* .dockerfile-* .push-*
152+
153+
bin-clean:
154+
rm -rf .go bin

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
Example controller for TGIK talk.
44

5+
## Ideas/code stolen from...
56
This repo vendors in client-go along with all of the appropriate dependencies. The procedure followed is described by @ncdc in this [blog post](https://blog.heptio.com/straighten-out-your-kubernetes-client-go-dependencies-heptioprotip-8baeed46fe7d).
7+
8+
The docker build makefile and such was taken from https://github.com/thockin/go-build-template.

build/build.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
# Copyright 2016 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
if [ -z "${PKG}" ]; then
22+
echo "PKG must be set"
23+
exit 1
24+
fi
25+
if [ -z "${ARCH}" ]; then
26+
echo "ARCH must be set"
27+
exit 1
28+
fi
29+
if [ -z "${VERSION}" ]; then
30+
echo "VERSION must be set"
31+
exit 1
32+
fi
33+
34+
export CGO_ENABLED=0
35+
export GOARCH="${ARCH}"
36+
37+
go install \
38+
-installsuffix "static" \
39+
-ldflags "-X ${PKG}/version.VERSION=${VERSION}" \
40+
./...

build/test.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
# Copyright 2016 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
export CGO_ENABLED=0
22+
23+
TARGETS=$(go list ./... | grep -v /vendor/)
24+
25+
echo "Running tests:"
26+
go test -i -installsuffix "static" ${TARGETS}
27+
go test -installsuffix "static" ${TARGETS}
28+
echo
29+
30+
echo -n "Checking gofmt: "
31+
ERRS=$(find . -path ./vendor -prune -o -type f -name \*.go -print | xargs gofmt -l 2>&1 || true)
32+
if [ -n "${ERRS}" ]; then
33+
echo "FAIL - the following files need to be gofmt'ed:"
34+
for e in ${ERRS}; do
35+
echo " $e"
36+
done
37+
echo
38+
exit 1
39+
fi
40+
echo "PASS"
41+
echo
42+
43+
echo -n "Checking go vet: "
44+
ERRS=$(go vet ${TARGETS} 2>&1 || true)
45+
if [ -n "${ERRS}" ]; then
46+
echo "FAIL"
47+
echo "${ERRS}"
48+
echo
49+
exit 1
50+
fi
51+
echo "PASS"
52+
echo

main.go tgik-controller.go

File renamed without changes.

version/version.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
// VERSION is the app-global version string, which should be substituted with a
20+
// real value during build.
21+
var VERSION = "UNKNOWN"

0 commit comments

Comments
 (0)