Skip to content

Commit cd1026d

Browse files
committed
Use the go-build-template
Faster builds and versions from tags. Also use alpine as a base image. We need alpine for other architectures...
1 parent 94ff3e8 commit cd1026d

File tree

10 files changed

+276
-85
lines changed

10 files changed

+276
-85
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
/bin
2+
/.go
3+
/.push-*
4+
/.container-*
5+
/.dockerfile-*

Dockerfile

Lines changed: 0 additions & 21 deletions
This file was deleted.

Dockerfile.in

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 Tim Hockin <thockin@google.com>
18+
19+
ADD bin/ARG_ARCH/ARG_BIN /ARG_BIN
20+
ENV GIT_SYNC_DEST /git
21+
22+
# Move the existing SSH binary, then replace it with the wrapper script
23+
RUN apk update --no-cache && apk add ca-certificates git openssh-client
24+
RUN mv /usr/bin/ssh /usr/bin/ssh-binary
25+
ADD ssh-wrapper.sh /usr/bin/ssh
26+
RUN chmod 755 /usr/bin/ssh
27+
28+
USER nobody:nobody
29+
ENTRYPOINT ["/ARG_BIN"]

Makefile

Lines changed: 131 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,150 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
.PHONY: all push push-legacy container clean
15+
# The binary to build (just the basename).
16+
BIN := git-sync
1617

17-
REGISTRY ?= gcr.io/google_containers
18-
IMAGE = $(REGISTRY)/git-sync-$(ARCH)
19-
LEGACY_AMD64_IMAGE = $(REGISTRY)/git-sync
18+
# This repo's root import path (under GOPATH).
19+
PKG := k8s.io/git-sync
2020

21-
TAG = v2.0.2
21+
# Where to push the docker image.
22+
REGISTRY ?= gcr.io/google-containers
2223

23-
# Architectures supported: amd64, arm, arm64 and ppc64le
24+
# Which architecture to build - see $(ALL_ARCH) for options.
2425
ARCH ?= amd64
2526

26-
# TODO: get a base image for non-x86 archs
27-
# arm arm64 ppc64le
28-
ALL_ARCH = amd64
29-
30-
KUBE_CROSS_IMAGE ?= gcr.io/google_containers/kube-cross
31-
KUBE_CROSS_VERSION ?= v1.6.3-2
32-
33-
GO_PKG = k8s.io/git-sync
34-
BIN = git-sync
35-
36-
# If you want to build all containers, see the 'all-container' rule.
37-
# If you want to build AND push all containers, see the 'all-push' rule.
38-
all: all-build
39-
40-
sub-container-%:
41-
$(MAKE) ARCH=$* container
42-
43-
sub-push-%:
44-
$(MAKE) ARCH=$* push
45-
46-
all-build: $(addprefix bin/$(BIN)-,$(ALL_ARCH))
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
4732

48-
all-container: $(addprefix sub-container-,$(ALL_ARCH))
33+
###
34+
### These variables should not need tweaking.
35+
###
4936

50-
all-push: $(addprefix sub-push-,$(ALL_ARCH))
37+
SRC_DIRS := cmd pkg # directories which hold app source (not vendored)
5138

52-
build: bin/$(BIN)-$(ARCH)
39+
ALL_ARCH := amd64
5340

54-
bin/$(BIN)-$(ARCH): FORCE
55-
mkdir -p bin
56-
docker run \
57-
-u $$(id -u):$$(id -g) \
58-
-v $$(pwd):/go/src/$(GO_PKG) \
59-
$(KUBE_CROSS_IMAGE):$(KUBE_CROSS_VERSION) \
60-
/bin/bash -c " \
61-
cd /go/src/$(GO_PKG) && \
62-
CGO_ENABLED=0 go build \
63-
-installsuffix cgo \
64-
-ldflags '-w' \
65-
-o $@"
41+
# TODO: get a baseimage that works for other architectures
42+
# arm arm64 ppc64le
6643

67-
container: .container-$(ARCH)
68-
.container-$(ARCH): bin/$(BIN)-$(ARCH)
69-
docker build -t $(IMAGE):$(TAG) --build-arg ARCH=$(ARCH) .
44+
# Set default base image dynamically for each arch
7045
ifeq ($(ARCH),amd64)
71-
docker tag $(IMAGE):$(TAG) $(LEGACY_AMD64_IMAGE):$(TAG)
46+
BASEIMAGE?=alpine
7247
endif
73-
touch $@
74-
75-
push: .push-$(ARCH)
76-
.push-$(ARCH): .container-$(ARCH)
77-
gcloud docker push $(IMAGE):$(TAG)
78-
touch $@
79-
80-
push-legacy: .push-legacy-$(ARCH)
81-
.push-legacy-$(ARCH): .container-$(ARCH)
82-
ifeq ($(ARCH),amd64)
83-
gcloud docker push $(LEGACY_AMD64_IMAGE):$(TAG)
48+
ifeq ($(ARCH),arm)
49+
BASEIMAGE?=armel/busybox
50+
endif
51+
ifeq ($(ARCH),arm64)
52+
BASEIMAGE?=aarch64/busybox
53+
endif
54+
ifeq ($(ARCH),ppc64le)
55+
BASEIMAGE?=ppc64le/busybox
8456
endif
85-
touch $@
8657

87-
test:
88-
@./test.sh
58+
IMAGE := $(REGISTRY)/$(BIN)-$(ARCH)
59+
LEGACY_IMAGE := $(REGISTRY)/$(BIN)
8960

90-
clean:
91-
rm -rf .container-* .push-* bin/
61+
BUILD_IMAGE ?= golang:1.7-alpine
9262

93-
FORCE:
63+
# If you want to build all binaries, see the 'all-build' rule.
64+
# If you want to build all containers, see the 'all-container' rule.
65+
# If you want to build AND push all containers, see the 'all-push' rule.
66+
all: build
67+
68+
build-%:
69+
@$(MAKE) --no-print-directory ARCH=$* build
70+
71+
container-%:
72+
@$(MAKE) --no-print-directory ARCH=$* container
73+
74+
push-%:
75+
@$(MAKE) --no-print-directory ARCH=$* push
76+
77+
all-build: $(addprefix build-, $(ALL_ARCH))
78+
79+
all-container: $(addprefix container-, $(ALL_ARCH))
80+
81+
all-push: $(addprefix push-, $(ALL_ARCH))
82+
83+
build: bin/$(ARCH)/$(BIN)
84+
85+
bin/$(ARCH)/$(BIN): build-dirs
86+
@echo "building: $@"
87+
@docker run \
88+
-ti \
89+
-u $$(id -u):$$(id -g) \
90+
-v $$(pwd)/.go:/go \
91+
-v $$(pwd):/go/src/$(PKG) \
92+
-v $$(pwd)/bin/$(ARCH):/go/bin \
93+
-v $$(pwd)/bin/$(ARCH):/go/bin/linux_$(ARCH) \
94+
-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static \
95+
-w /go/src/$(PKG) \
96+
$(BUILD_IMAGE) \
97+
/bin/sh -c " \
98+
ARCH=$(ARCH) \
99+
VERSION=$(VERSION) \
100+
PKG=$(PKG) \
101+
./build/build.sh \
102+
"
103+
104+
DOTFILE_IMAGE = $(subst /,_,$(IMAGE))-$(VERSION)
105+
106+
container: .container-$(DOTFILE_IMAGE) container-name
107+
.container-$(DOTFILE_IMAGE): bin/$(ARCH)/$(BIN) Dockerfile.in
108+
@sed \
109+
-e 's|ARG_BIN|$(BIN)|g' \
110+
-e 's|ARG_ARCH|$(ARCH)|g' \
111+
-e 's|ARG_FROM|$(BASEIMAGE)|g' \
112+
Dockerfile.in > .dockerfile-$(ARCH)
113+
@docker build -t $(IMAGE):$(VERSION) -f .dockerfile-$(ARCH) .
114+
@docker images -q $(IMAGE):$(VERSION) > $@
115+
@if [ "$(ARCH)" == "amd64" ]; then \
116+
docker tag -f $(IMAGE):$(VERSION) $(LEGACY_IMAGE):$(VERSION); \
117+
fi
118+
119+
container-name:
120+
@echo "container: $(IMAGE):$(VERSION)"
121+
122+
push: .push-$(DOTFILE_IMAGE) push-name
123+
.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE)
124+
@gcloud docker push $(IMAGE):$(VERSION)
125+
@docker images -q $(IMAGE):$(VERSION) > $@
126+
@if [ "$(ARCH)" == "amd64" ]; then \
127+
gcloud docker push $(LEGACY_IMAGE):$(TAG); \
128+
fi
129+
130+
push-name:
131+
@echo "pushed: $(IMAGE):$(VERSION)"
132+
133+
version:
134+
@echo $(VERSION)
135+
136+
test: build-dirs
137+
@docker run \
138+
-ti \
139+
-u $$(id -u):$$(id -g) \
140+
-v $$(pwd)/.go:/go \
141+
-v $$(pwd):/go/src/$(PKG) \
142+
-v $$(pwd)/bin/$(ARCH):/go/bin \
143+
-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static \
144+
-w /go/src/$(PKG) \
145+
$(BUILD_IMAGE) \
146+
/bin/sh -c " \
147+
./build/test.sh $(SRC_DIRS) \
148+
"
149+
@./test_e2e.sh
150+
151+
build-dirs:
152+
@mkdir -p bin/$(ARCH)
153+
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(ARCH)
154+
155+
clean: container-clean bin-clean
156+
157+
container-clean:
158+
rm -rf .container-* .dockerfile-* .push-*
159+
160+
bin-clean:
161+
rm -rf .go bin

build/build.sh

Lines changed: 40 additions & 0 deletions
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}/pkg/version.VERSION=${VERSION}" \
40+
./...

build/test.sh

Lines changed: 52 additions & 0 deletions
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=$(for d in "$@"; do echo ./$d/...; done)
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 "$@" -type f -name \*.go | 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 renamed to cmd/git-sync/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
// git-sync is a command that pull a git repository to a local directory.
1818

19-
package main // import "k8s.io/git-sync"
19+
package main // import "k8s.io/git-sync/cmd/git-sync"
2020

2121
import (
2222
"bytes"
File renamed without changes.

pkg/version/version.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
var VERSION = "UNKNOWN"

test.sh renamed to test_e2e.sh

File renamed without changes.

0 commit comments

Comments
 (0)