Skip to content

Use build template #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/bin
/.go
/.push-*
/.container-*
/.dockerfile-*
21 changes: 0 additions & 21 deletions Dockerfile

This file was deleted.

33 changes: 33 additions & 0 deletions Dockerfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2016 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.

FROM ARG_FROM

MAINTAINER Tim Hockin <thockin@google.com>

ADD bin/ARG_ARCH/ARG_BIN /ARG_BIN
ENV GIT_SYNC_DEST /git

# Move the existing SSH binary, then replace it with the wrapper script
RUN apk update --no-cache && apk add \
ca-certificates \
coreutils \
git \
openssh-client
RUN mv /usr/bin/ssh /usr/bin/ssh-binary
ADD ssh-wrapper.sh /usr/bin/ssh
RUN chmod 755 /usr/bin/ssh

USER nobody:nobody
ENTRYPOINT ["/ARG_BIN"]
194 changes: 131 additions & 63 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,82 +12,150 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

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

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

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

# TODO: get a base image for non-x86 archs
# arm arm64 ppc64le
ALL_ARCH = amd64

KUBE_CROSS_IMAGE ?= gcr.io/google_containers/kube-cross
KUBE_CROSS_VERSION ?= v1.6.3-2

GO_PKG = k8s.io/git-sync
BIN = git-sync

# If you want to build all containers, see the 'all-container' rule.
# If you want to build AND push all containers, see the 'all-push' rule.
all: all-build

sub-container-%:
$(MAKE) ARCH=$* container

sub-push-%:
$(MAKE) ARCH=$* push

all-build: $(addprefix bin/$(BIN)-,$(ALL_ARCH))
# This version-strategy uses git tags to set the version string
VERSION := $(shell git describe --tags --always --dirty)
#
# This version-strategy uses a manual value to set the version string
#VERSION := 1.2.3

all-container: $(addprefix sub-container-,$(ALL_ARCH))
###
### These variables should not need tweaking.
###

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

build: bin/$(BIN)-$(ARCH)
ALL_ARCH := amd64

bin/$(BIN)-$(ARCH): FORCE
mkdir -p bin
docker run \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/go/src/$(GO_PKG) \
$(KUBE_CROSS_IMAGE):$(KUBE_CROSS_VERSION) \
/bin/bash -c " \
cd /go/src/$(GO_PKG) && \
CGO_ENABLED=0 go build \
-installsuffix cgo \
-ldflags '-w' \
-o $@"
# TODO: get a baseimage that works for other architectures
# arm arm64 ppc64le

container: .container-$(ARCH)
.container-$(ARCH): bin/$(BIN)-$(ARCH)
docker build -t $(IMAGE):$(TAG) --build-arg ARCH=$(ARCH) .
# Set default base image dynamically for each arch
ifeq ($(ARCH),amd64)
docker tag $(IMAGE):$(TAG) $(LEGACY_AMD64_IMAGE):$(TAG)
BASEIMAGE?=alpine:3.4
endif
touch $@

push: .push-$(ARCH)
.push-$(ARCH): .container-$(ARCH)
gcloud docker push $(IMAGE):$(TAG)
touch $@

push-legacy: .push-legacy-$(ARCH)
.push-legacy-$(ARCH): .container-$(ARCH)
ifeq ($(ARCH),amd64)
gcloud docker push $(LEGACY_AMD64_IMAGE):$(TAG)
ifeq ($(ARCH),arm)
BASEIMAGE?=armel/busybox
endif
ifeq ($(ARCH),arm64)
BASEIMAGE?=aarch64/busybox
endif
ifeq ($(ARCH),ppc64le)
BASEIMAGE?=ppc64le/busybox
endif
touch $@

test:
@./test.sh
IMAGE := $(REGISTRY)/$(BIN)-$(ARCH)
LEGACY_IMAGE := $(REGISTRY)/$(BIN)

clean:
rm -rf .container-* .push-* bin/
BUILD_IMAGE ?= golang:1.7-alpine

FORCE:
# If you want to build all binaries, see the 'all-build' rule.
# If you want to build all containers, see the 'all-container' rule.
# If you want to build AND push all containers, see the 'all-push' rule.
all: build

build-%:
@$(MAKE) --no-print-directory ARCH=$* build

container-%:
@$(MAKE) --no-print-directory ARCH=$* container

push-%:
@$(MAKE) --no-print-directory ARCH=$* push

all-build: $(addprefix build-, $(ALL_ARCH))

all-container: $(addprefix container-, $(ALL_ARCH))

all-push: $(addprefix push-, $(ALL_ARCH))

build: bin/$(ARCH)/$(BIN)

bin/$(ARCH)/$(BIN): build-dirs
@echo "building: $@"
@docker run \
-ti \
-u $$(id -u):$$(id -g) \
-v $$(pwd)/.go:/go \
-v $$(pwd):/go/src/$(PKG) \
-v $$(pwd)/bin/$(ARCH):/go/bin \
-v $$(pwd)/bin/$(ARCH):/go/bin/linux_$(ARCH) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems unnecessary.

-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh -c " \
ARCH=$(ARCH) \
VERSION=$(VERSION) \
PKG=$(PKG) \
./build/build.sh \
"

DOTFILE_IMAGE = $(subst /,_,$(IMAGE))-$(VERSION)

container: .container-$(DOTFILE_IMAGE) container-name
.container-$(DOTFILE_IMAGE): bin/$(ARCH)/$(BIN) Dockerfile.in
@sed \
-e 's|ARG_BIN|$(BIN)|g' \
-e 's|ARG_ARCH|$(ARCH)|g' \
-e 's|ARG_FROM|$(BASEIMAGE)|g' \
Dockerfile.in > .dockerfile-$(ARCH)
@docker build -t $(IMAGE):$(VERSION) -f .dockerfile-$(ARCH) .
@docker images -q $(IMAGE):$(VERSION) > $@
@if [ "$(ARCH)" == "amd64" ]; then \
docker tag $(IMAGE):$(VERSION) $(LEGACY_IMAGE):$(VERSION); \
fi

container-name:
@echo "container: $(IMAGE):$(VERSION)"

push: .push-$(DOTFILE_IMAGE) push-name
.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE)
@gcloud docker push $(IMAGE):$(VERSION)
@docker images -q $(IMAGE):$(VERSION) > $@
@if [ "$(ARCH)" == "amd64" ]; then \
gcloud docker push $(LEGACY_IMAGE):$(TAG); \
fi

push-name:
@echo "pushed: $(IMAGE):$(VERSION)"

version:
@echo $(VERSION)

test: build-dirs
@docker run \
-ti \
-u $$(id -u):$$(id -g) \
-v $$(pwd)/.go:/go \
-v $$(pwd):/go/src/$(PKG) \
-v $$(pwd)/bin/$(ARCH):/go/bin \
-v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh -c " \
./build/test.sh $(SRC_DIRS) \
"
@./test_e2e.sh

build-dirs:
@mkdir -p bin/$(ARCH)
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(ARCH)

clean: container-clean bin-clean

container-clean:
rm -rf .container-* .dockerfile-* .push-*

bin-clean:
rm -rf .go bin
40 changes: 40 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh
#
# Copyright 2016 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 "${VERSION}" ]; then
echo "VERSION must be set"
exit 1
fi

export CGO_ENABLED=0
export GOARCH="${ARCH}"

go install \
-installsuffix "static" \
-ldflags "-X ${PKG}/pkg/version.VERSION=${VERSION}" \
./...
52 changes: 52 additions & 0 deletions build/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh
#
# Copyright 2016 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

export CGO_ENABLED=0

TARGETS=$(for d in "$@"; do echo ./$d/...; done)

echo "Running tests:"
go test -i -installsuffix "static" ${TARGETS}
go test -installsuffix "static" ${TARGETS}
echo

echo -n "Checking gofmt: "
ERRS=$(find "$@" -type f -name \*.go | xargs gofmt -l 2>&1 || true)
if [ -n "${ERRS}" ]; then
echo "FAIL - the following files need to be gofmt'ed:"
for e in ${ERRS}; do
echo " $e"
done
echo
exit 1
fi
echo "PASS"
echo

echo -n "Checking go vet: "
ERRS=$(go vet ${TARGETS} 2>&1 || true)
if [ -n "${ERRS}" ]; then
echo "FAIL"
echo "${ERRS}"
echo
exit 1
fi
echo "PASS"
echo
Loading