From 0bd05861acd2214ed1afde4c2b4e8bbafcefac4a Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Sun, 8 Sep 2019 18:20:08 -0300 Subject: [PATCH] Add terraform scripts to build nginx image --- build/build-nginx-image.sh | 58 ++++++++++ build/images/nginx/.gitignore | 6 + build/images/nginx/Dockerfile | 19 ++++ build/images/nginx/build-nginx.sh | 94 ++++++++++++++++ build/images/nginx/entrypoint.sh | 35 ++++++ build/images/nginx/main.tf | 176 ++++++++++++++++++++++++++++++ build/images/nginx/variables.tf | 43 ++++++++ build/images/nginx/versions.tf | 11 ++ images/nginx/rootfs/build.sh | 9 +- 9 files changed, 448 insertions(+), 3 deletions(-) create mode 100755 build/build-nginx-image.sh create mode 100644 build/images/nginx/.gitignore create mode 100644 build/images/nginx/Dockerfile create mode 100644 build/images/nginx/build-nginx.sh create mode 100755 build/images/nginx/entrypoint.sh create mode 100644 build/images/nginx/main.tf create mode 100644 build/images/nginx/variables.tf create mode 100644 build/images/nginx/versions.tf diff --git a/build/build-nginx-image.sh b/build/build-nginx-image.sh new file mode 100755 index 0000000000..80ab7bd5c1 --- /dev/null +++ b/build/build-nginx-image.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Copyright 2019 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. + +if [ -n "$DEBUG" ]; then + set -x +fi + +set -o errexit +set -o nounset +set -o pipefail + +declare -a mandatory +mandatory=( + AWS_ACCESS_KEY + AWS_SECRET_KEY +) + +missing=false +for var in "${mandatory[@]}"; do + if [[ -z "${!var:-}" ]]; then + echo "Environment variable $var must be set" + missing=true + fi +done + +if [ "$missing" = true ]; then + exit 1 +fi + +DIR=$(cd $(dirname "${BASH_SOURCE}") && pwd -P) + +# build local terraform image to build nginx +docker build -t build-nginx-terraform $DIR/images/nginx + +# build nginx and publish docker images to quay.io. +# this can take up to two hours. +docker run --rm -it \ + --volume $DIR/images/nginx:/tf \ + -w /tf \ + --env AWS_ACCESS_KEY=${AWS_ACCESS_KEY} \ + --env AWS_SECRET_KEY=${AWS_SECRET_KEY} \ + --env AWS_SECRET_KEY=${AWS_SECRET_KEY} \ + --env QUAY_USERNAME=${QUAY_USERNAME} \ + --env QUAY_PASSWORD="${QUAY_PASSWORD}" \ + build-nginx-terraform diff --git a/build/images/nginx/.gitignore b/build/images/nginx/.gitignore new file mode 100644 index 0000000000..bbbd96ef97 --- /dev/null +++ b/build/images/nginx/.gitignore @@ -0,0 +1,6 @@ +.terraform +.terraform* +terraform* +*.tfstate +*.tfstate.backup +id_rsa* diff --git a/build/images/nginx/Dockerfile b/build/images/nginx/Dockerfile new file mode 100644 index 0000000000..a075db0d35 --- /dev/null +++ b/build/images/nginx/Dockerfile @@ -0,0 +1,19 @@ +FROM quay.io/kubernetes-ingress-controller/debian-base-amd64:0.1 + +ENV TERRAFORM_VERSION 0.12.6 + +RUN clean-install \ + bash \ + curl \ + ca-certificates \ + unzip \ + git \ + openssh-client + +RUN curl -sSL -o /terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" \ + && unzip /terraform.zip -d /usr/bin \ + && rm -rf /terraform.zip + +COPY entrypoint.sh / + +CMD [ "/entrypoint.sh" ] diff --git a/build/images/nginx/build-nginx.sh b/build/images/nginx/build-nginx.sh new file mode 100644 index 0000000000..b90b27fee4 --- /dev/null +++ b/build/images/nginx/build-nginx.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +# Copyright 2019 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 DEBIAN_FRONTEND=noninteractive +export AR_FLAGS=cr + +apt update + +apt dist-upgrade --yes + +add-apt-repository universe --yes +add-apt-repository multiverse --yes + +apt update + +apt install \ + apt-transport-https \ + ca-certificates \ + curl \ + make \ + htop \ + software-properties-common --yes + +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + +add-apt-repository \ + "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) \ + stable" --yes + +apt update + +apt install docker-ce --yes + +curl -sL -o /usr/local/bin/gimme https://raw.githubusercontent.com/travis-ci/gimme/master/gimme +chmod +x /usr/local/bin/gimme + +eval "$(gimme 1.13)" +gimme 1.13 + +git clone https://github.com/kubernetes/ingress-nginx + +cd ingress-nginx/images/nginx + +make register-qemu + +PARALLELISM=${PARALLELISM:-3} + +export TAG=$(git rev-parse HEAD) + +# Borrowed from https://github.com/kubernetes-sigs/kind/blob/master/hack/release/build/cross.sh#L27 +echo "Building in parallel for:" +# What we do here: +# - use xargs to build in parallel (-P) while collecting a combined exit code +# - use cat to supply the individual args to xargs (one line each) +# - use env -S to split the line into environment variables and execute +# - ... the build +# shellcheck disable=SC2016 +if xargs -0 -n1 -P "${PARALLELISM}" bash -c 'eval $0; TAG=${TAG} make sub-container-${ARCH} > build-${ARCH}.log'; then + echo "Docker build finished without issues" 1>&2 +else + echo "Docker build failed!" 1>&2 + cat build-amd64.log + cat build-arm.log + cat build-arm64.log + exit 1 +fi < <(cat <