Skip to content

Commit

Permalink
Terraform release (#4613)
Browse files Browse the repository at this point in the history
* Add script to build the ingress controller image using terraform

* Update terraform to 0.12.9
  • Loading branch information
aledbf authored Sep 30, 2019
1 parent 2de5a89 commit 7aca7bc
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 1 deletion.
52 changes: 52 additions & 0 deletions build/build-ingress-controller.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/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

DIR=$(cd $(dirname "${BASH_SOURCE}") && pwd -P)

AWS_FILE="${DIR}/images/nginx/aws.tfvars"
ENV_FILE="${DIR}/images/nginx/env.tfvars"

if [ ! -f "${AWS_FILE}" ]; then
echo "File $AWS_FILE does not exist. Please create this file with keys access_key an secret_key"
exit 1
fi

if [ ! -f "${ENV_FILE}" ]; then
echo "File $ENV_FILE does not exist. Please create this file with keys docker_username and docker_password"
exit 1
fi

# build local terraform image to build nginx
docker build -t build-ingress-controller-terraform $DIR/images/ingress-controller

# build nginx and publish docker images to quay.io.
# this can take up to two hours.
docker run --rm -it \
--volume $DIR/images/ingress-controller:/tf \
-w /tf \
-v ${AWS_FILE}:/root/aws.tfvars:ro \
-v ${ENV_FILE}:/root/env.tfvars:ro \
build-ingress-controller-terraform

docker rmi -f build-ingress-controller-terraform
2 changes: 2 additions & 0 deletions build/build-nginx-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ docker run --rm -it \
-v ${AWS_FILE}:/root/aws.tfvars:ro \
-v ${ENV_FILE}:/root/env.tfvars:ro \
build-nginx-terraform

docker rmi -f build-nginx-terraform
1 change: 1 addition & 0 deletions build/images/ingress-controller/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tfvars
7 changes: 7 additions & 0 deletions build/images/ingress-controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.terraform*
terraform*
*.tfstate
*.tfstate.backup
id_rsa*
aws.tfvars
env.tfvars
19 changes: 19 additions & 0 deletions build/images/ingress-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM quay.io/kubernetes-ingress-controller/debian-base-amd64:0.1

ENV TERRAFORM_VERSION 0.12.9

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" ]
93 changes: 93 additions & 0 deletions build/images/ingress-controller/build-ingress-controller.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/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

function source_tfvars() {
eval "$(
awk 'BEGIN {FS=OFS="="}
!/^(#| *$)/ && /^.+=.+$/ {
gsub(/^[ \t]+|[ \t]+$/, "", $1);
gsub(/\./, "_", $1);
gsub(/^[ \t]+|[ \t]+$/, "", $2);
if ($1 && $2) print $0
}' "$@"
)"
}

source_tfvars /tmp/env

export DEBIAN_FRONTEND=noninteractive

apt -q=3 update

apt -q=3 dist-upgrade --yes

add-apt-repository universe --yes
add-apt-repository multiverse --yes

apt -q=3 update

apt -q=3 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 -q=3 update

apt -q=3 install docker-ce --yes

echo ${docker_password} | docker login -u ${docker_username} --password-stdin quay.io

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.1)"

export GOPATH="/tmp/go"

INGRESS_DIRECTORY="${GOPATH}/src/k8s.io"

mkdir -p ${INGRESS_DIRECTORY}
cd ${INGRESS_DIRECTORY}

git clone https://github.com/kubernetes/ingress-nginx

cd ingress-nginx

make register-qemu

echo "Building NGINX image..."
make all-container

echo "Publishing NGINX images..."
make all-push

# Requires https://github.com/kubernetes/ingress-nginx/pull/4271
#echo "Creating multi-arch images..."
#make push-manifest
54 changes: 54 additions & 0 deletions build/images/ingress-controller/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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

catch() {
if [ "$1" == "0" ]; then
exit 0
fi

echo "Error $1 occurred on $2"

echo "Removing temporal resources..."
terraform destroy -auto-approve \
-var-file /root/aws.tfvars \
-var-file /root/env.tfvars \
-var valid_until="${EC2_VALID_UNTIL}"
}
trap 'catch $? $LINENO' ERR

terraform init

# destroy spot instance after two hours
EC2_VALID_UNTIL=$(date -d "+2 hours" +%Y-%m-%dT%H:%M:%SZ)

terraform plan \
-var-file /root/aws.tfvars \
-var-file /root/env.tfvars \
-var valid_until="${EC2_VALID_UNTIL}"

terraform apply -auto-approve \
-var-file /root/aws.tfvars \
-var-file /root/env.tfvars \
-var valid_until="${EC2_VALID_UNTIL}"

terraform destroy -auto-approve \
-var-file /root/aws.tfvars \
-var-file /root/env.tfvars \
-var valid_until="${EC2_VALID_UNTIL}"
Loading

0 comments on commit 7aca7bc

Please sign in to comment.