From c77d6f7d59b83490b26886fe0ac805df3ae83109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Tue, 5 Jan 2016 10:52:12 +0200 Subject: [PATCH] Make the etcd image cross-platform --- cluster/images/etcd/.gitignore | 4 --- cluster/images/etcd/Dockerfile | 6 ++-- cluster/images/etcd/Makefile | 57 +++++++++++++++++++++++-------- cluster/images/etcd/build-etcd.sh | 57 +++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 22 deletions(-) delete mode 100644 cluster/images/etcd/.gitignore create mode 100755 cluster/images/etcd/build-etcd.sh diff --git a/cluster/images/etcd/.gitignore b/cluster/images/etcd/.gitignore deleted file mode 100644 index 707ba939718..00000000000 --- a/cluster/images/etcd/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -etcd -etcdctl -etcd-*-linux-amd64.tar.gz -etcd-*-linux-amd64/ diff --git a/cluster/images/etcd/Dockerfile b/cluster/images/etcd/Dockerfile index 8a295099b11..28b4092abd7 100644 --- a/cluster/images/etcd/Dockerfile +++ b/cluster/images/etcd/Dockerfile @@ -1,5 +1,5 @@ -FROM busybox +FROM BASEIMAGE MAINTAINER Dawn Chen -ADD ./etcd /usr/local/bin/etcd -ADD ./etcdctl /usr/local/bin/etcdctl +COPY ./etcd /usr/local/bin/etcd +COPY ./etcdctl /usr/local/bin/etcdctl diff --git a/cluster/images/etcd/Makefile b/cluster/images/etcd/Makefile index 945bdc3c62f..35d78944363 100644 --- a/cluster/images/etcd/Makefile +++ b/cluster/images/etcd/Makefile @@ -1,21 +1,48 @@ -.PHONY: clean build push +# Build the etcd image +# +# Usage: +# [TAG=2.2.1] [REGISTRY=gcr.io/google_containers] [ARCH=amd64] [BASEIMAGE=busybox] make (build|push) -IMAGE = etcd -TAG = 2.2.1 -ETCD_VERSION = 2.2.1 -OUTPUT_DIR = $(IMAGE)-v$(ETCD_VERSION)-linux-amd64 +TAG?=2.2.1 +ARCH?=amd64 +REGISTRY?=gcr.io/google_containers +TEMP_DIR:=$(shell mktemp -d) -clean: - rm -rf $(OUTPUT_DIR) $(IMAGE)-v$(ETCD_VERSION)-linux-amd64.tar.gz etcd etcdctl +# Default base images for different architectures +# '/' in images has to be written as '\\/' +ifeq ($(ARCH),amd64) + BASEIMAGE?=busybox +endif +ifeq ($(ARCH),arm) + BASEIMAGE?=hypriot\\/armhf-busybox +endif -build: clean - curl -L -O https://github.com/coreos/etcd/releases/download/v$(ETCD_VERSION)/$(IMAGE)-v$(ETCD_VERSION)-linux-amd64.tar.gz - tar xzvf $(IMAGE)-v$(ETCD_VERSION)-linux-amd64.tar.gz - cp $(OUTPUT_DIR)/etcd . - cp $(OUTPUT_DIR)/etcdctl . - docker build -t gcr.io/google_containers/$(IMAGE):$(TAG) . +build: + # Download etcd source in a golang container and cross-compile it statically + # or download official binaries if the ARCH is amd64 + ./build-etcd.sh ${TAG} ${ARCH} ${TEMP_DIR} + + # Copy the content in this dir to the temp dir + cp ./* ${TEMP_DIR} + + # Replace BASEIMAGE with the real base image + cd ${TEMP_DIR} && sed -i "s/BASEIMAGE/${BASEIMAGE}/g" Dockerfile + + # And build the image + docker build -t ${REGISTRY}/etcd-${ARCH}:${TAG} ${TEMP_DIR} + +ifeq ($(ARCH),amd64) + # Backward compatability. TODO: deprecate this image tag + docker tag -f ${REGISTRY}/etcd-${ARCH}:${TAG} ${REGISTRY}/etcd:${TAG} +endif push: build - gcloud docker push gcr.io/google_containers/$(IMAGE):$(TAG) + gcloud docker push ${REGISTRY}/etcd-${ARCH}:${TAG} -all: push + # Backward compatability. TODO: deprecate this image tag +ifeq ($(ARCH),amd64) + gcloud docker push ${REGISTRY}/etcd:${TAG} +endif + +all: build +.PHONY: build push \ No newline at end of file diff --git a/cluster/images/etcd/build-etcd.sh b/cluster/images/etcd/build-etcd.sh new file mode 100755 index 00000000000..0573d33d4e6 --- /dev/null +++ b/cluster/images/etcd/build-etcd.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Copyright 2015 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. + +# Build etcd from source in a docker container + +# Require three arguments +if [[ $# != 3 ]]; then + echo "Usage: ./build-etcd.sh TAG ARCH TARGET_DIR" + exit +fi + +# Example tag should be 2.2.1, not v2.2.1 +TAG=$1 +ARCH=$2 +TARGET_DIR=$3 + +GOLANG_VERSION=${GOLANG_VERSION:-1.5.2} +GOARM=6 + +# Create the ${TARGET_DIR} directory, if it doesn't exist +mkdir -p ${TARGET_DIR} + +# Do not compile if we should make an image for amd64, use the "official" etcd binaries instead +if [[ ${ARCH} == "amd64" ]]; then + + # Just download the binaries to ${TARGET_DIR} + curl -sSL https://github.com/coreos/etcd/releases/download/v${TAG}/etcd-v${TAG}-linux-amd64.tar.gz | tar -xz -C ${TARGET_DIR} --strip-components=1 +else + # Download etcd in a golang container and cross-compile it statically + CID=$(docker run -d golang:${GOLANG_VERSION} /bin/sh -c \ + "mkdir /etcd \ + && curl -sSL https://github.com/coreos/etcd/archive/v${TAG}.tar.gz | tar -C /etcd -xz --strip-components=1 \ + && cd /etcd \ + && GOARM=${GOARM} GOARCH=${ARCH} ./build") + + # Wait until etcd has compiled + docker wait ${CID} + + # Copy out the bin folder to ${TARGET_DIR} + docker cp ${CID}:/etcd/bin ${TARGET_DIR} + + # Move the contents in bin to the target directory + mv ${TARGET_DIR}/bin/* ${TARGET_DIR} +fi \ No newline at end of file