From 890f7b0f0282f5ec0512c259c8042bdb42362a1f Mon Sep 17 00:00:00 2001 From: Jerad C Date: Wed, 5 Oct 2022 14:21:20 -0500 Subject: [PATCH 1/3] add sync-readme-to-ecr-public.sh script --- Makefile | 21 +++++-- scripts/docker-login-ecr.sh | 55 ++++++++++++++++- scripts/sync-readme-to-ecr-public.sh | 91 ++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 scripts/sync-readme-to-ecr-public.sh diff --git a/Makefile b/Makefile index 93a20549..e9a08644 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,10 @@ KODATA = \ cmd/webhook/kodata/HEAD \ cmd/webhook/kodata/refs CODECOVERAGE_OUT = $(PROJECT_DIR)/coverprofile.out -GITHUB_REPO_FULL_NAME = "aws/aws-node-termination-handler" -ECR_PUBLIC_REGISTRY ?= "public.ecr.aws/aws-ec2" -ECR_PUBLIC_REPOSITORY_ROOT = "aws-node-termination-handler-2" +GITHUB_REPO_FULL_NAME = aws/aws-node-termination-handler +ECR_PUBLIC_REGION = us-east-1 +ECR_PUBLIC_REGISTRY ?= public.ecr.aws/aws-ec2 +ECR_PUBLIC_REPOSITORY_ROOT = aws-node-termination-handler-2 # Image URL to use all building/pushing image targets IMG ?= controller:latest @@ -129,10 +130,14 @@ apply: $(KO) $(KODATA) ## Deploy the controller into the current kubernetes clus delete: ## Delete controller from current kubernetes cluster. helm uninstall dev --namespace ${CLUSTER_NAMESPACE} +.PHONY: ecr-login +ecr-login: ## Login to default AWS ECR repository. + @$(PROJECT_DIR)/scripts/docker-login-ecr.sh + ##@ Release .PHONY: build-and-push-images -build-and-push-images: $(KO) $(KODATA) ## Build controller and webhook images and push to ECR public repository. +build-and-push-images: $(KO) $(KODATA) ecr-public-login ## Build controller and webhook images and push to ECR public repository. @PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/build-and-push-images.sh -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)" .PHONY: create-release-prep-pr @@ -143,6 +148,10 @@ create-release-prep-pr: $(GUM) ## Update version numbers in documents and open a create-release-prep-pr-draft: $(GUM) ## Update version numbers in documents and open a draft PR. @PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/prepare-for-release.sh -d +.PHONY: ecr-public-login +ecr-public-login: ## Login to the AWS ECR public repository. + @$(PROJECT_DIR)/scripts/docker-login-ecr.sh -g "$(ECR_PUBLIC_REGION)" -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)" + .PHONY: upload-resources-to-github upload-resources-to-github: ## Upload contents of resources/ as part of the most recent published release. @$(PROJECT_DIR)/scripts/upload-resources-to-github.sh @@ -159,5 +168,9 @@ repo-full-name: ## Get the full name of the GitHub repository for Node Terminati ekscharts-sync-release: $(GH) @PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/sync-to-aws-eks-charts.sh -n +.PHONY: sync-readme-to-ecr-public +sync-readme-to-ecr-public: ecr-public-login ## Upload the README.md to ECR public controller and webhook repositories. + @$(PROJECT_DIR)/scripts/sync-readme-to-ecr-public.sh -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)" + .PHONY: version version: latest-release-tag ## Get the most recent release version. diff --git a/scripts/docker-login-ecr.sh b/scripts/docker-login-ecr.sh index 63093be1..e952eaaf 100755 --- a/scripts/docker-login-ecr.sh +++ b/scripts/docker-login-ecr.sh @@ -1,8 +1,57 @@ #!/usr/bin/env bash -aws ecr get-login-password \ - --region "${AWS_REGION:?AWS_REGION is undefined or empty}" | \ +set -euo pipefail + +aws_region="${AWS_REGION:-}" +ecr_repository="${KO_DOCKER_REPO:-}" + +usage=$(cat << EOM +usage: $(basename $0) -h | [-g REGION] [-p] [-r REPOSITORY] + + Login to an AWS ECR repository. + + Options: + -h Display this help message then exit. + -g REGION AWS Region. Defaults to "${aws_region}". + -r REPOSITORY ECR Registry. Defaults to "${ecr_repository}". + +EOM +) + +while getopts "g:r:h" opt; do + case "${opt}" in + g ) aws_region="${OPTARG}" + ;; + r ) ecr_repository="${OPTARG}" + ;; + h ) echo "${usage}" + exit 0 + ;; + \?) echo "${usage}" >&2 + exit 1 + ;; + esac +done + +function assert_not_empty { + if [[ -z "${!1}" ]]; then + echo "error: missing argument ${1}" >&2 + echo "${usage}" >&2 + exit 1 + fi +} + +assert_not_empty aws_region +assert_not_empty ecr_repository + +ecr_cmd="ecr" +if echo "${ecr_repository}" | grep '^public\.ecr\.aws' >/dev/null; then + ecr_cmd="ecr-public" +fi + +aws ${ecr_cmd} get-login-password \ + --region "${aws_region}" | \ docker login \ --username AWS \ --password-stdin \ - "${KO_DOCKER_REPO:?KO_DOCKER_REPO is undefined or empty}" + "${ecr_repository}" diff --git a/scripts/sync-readme-to-ecr-public.sh b/scripts/sync-readme-to-ecr-public.sh new file mode 100644 index 00000000..18582177 --- /dev/null +++ b/scripts/sync-readme-to-ecr-public.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash + +set -euo pipefail + +repo_root_path="$(cd "$(dirname "$0")"; cd ..; pwd -P)" + +if ! command -v jq; then + echo "command not found: jq" >&2 + exit 1 +fi + +region="${AWS_REGION:-us-east-1}" +repo_root="${KO_DOCKER_REPO}" +version="$(make -s -f "${makefile_path}" version)" + +usage=$(cat << EOM +usage: $(basename $0) -h | [-g REGION] [-r REPOSITORY] [-v VERSION] + + Upload the README.md to the AWS ECR controller and webhook repositories. + + Options: + -h Display this help message then exit. + -g REGION AWS Region of ECR image repository. Defaults to "${region}". + -r REPOSITORY Image repository to push the built images. Defaults to "${repo_root}". + -v VERSION Node Termination Handler version. Defaults to "${version}". + +EOM +) + +while getopts "r:h" opt; do + case "${opt}" in + g ) region="${OPTARG}" + ;; + r ) repo_root="${OPTARG}" + ;; + v ) version="${OPTARG}" + ;; + h ) echo "${usage}" + exit 0 + ;; + \?) echo "${usage}" >&2 + exit 1 + ;; + esac +done + +assert_not_empty() { + if [[ -z "${!1}" ]]; then + echo "error: missing argument ${1}" >&2 + echo "${usage}" >&2 + exit 1 + fi +} + +assert_not_empty region +assert_not_empty repo_root +assert_not_empty version + +################################################# + +if ! (git --no-pager diff --name-only HEAD^ HEAD | grep 'README.md' >/dev/null); then + echo -e "⚠️ README.md did not change in the last commit. Not taking any action." + exit 0 +fi + +################################################# + +content=$(jq -n --arg msg "$(cat README.md)" '{"usageText": $msg}' | jq '.usageText' | sed 's/\\n/\ +/g') +if [[ ${#content} -gt 10240 ]]; then + truncation_msg="... + +**truncated due to char limits**... +A complete version of the README can be found [here](https://github.com/aws/aws-node-termination-handler/blob/${version}/README.md).\"" + content="${content:0:$((10240-${#truncation_msg}))}" + content+="${truncation_msg}" +fi + +for repo in "controller" "webhook"; do + if ! aws ecr-public put-repository-catalog-data \ + --region "${region}" \ + --repository-name="${repo_root}/${repo}" \ + --catalog-data aboutText="${content}",usageText="See About section" >/dev/null 2>&1; then + echo -e "❌ Failed to upload README.md to ${repo_root}/${repo}" >&2 + exit 1 + fi + + echo -e "✅ Uploaded README.md to 'About' section of ${repo_root}/${repo}" +done + +echo -e "✅ Finished sync'ing README.md to ECR Public" From d5ccba47e45526fc8ba60b1732c734aae0f4d9ca Mon Sep 17 00:00:00 2001 From: Jerad C Date: Thu, 6 Oct 2022 09:13:48 -0500 Subject: [PATCH 2/3] update README diff range --- Makefile | 6 +++++- scripts/sync-readme-to-ecr-public.sh | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e9a08644..55de66a0 100644 --- a/Makefile +++ b/Makefile @@ -158,7 +158,11 @@ upload-resources-to-github: ## Upload contents of resources/ as part of the most .PHONY: latest-release-tag latest-release-tag: ## Get tag of most recent release. - @git describe --tags --abbrev=0 v2 + @git describe --tags --abbrev=0 `git rev-parse --abbrev-ref HEAD` + +.PHONY: previous-release-tag +previous-release-tag: ## Get tag of second most recent release. + @git describe --tags --abbrev=0 `git rev-parse --abbrev-ref HEAD`^ .PHONY: repo-full-name repo-full-name: ## Get the full name of the GitHub repository for Node Termination Handler. diff --git a/scripts/sync-readme-to-ecr-public.sh b/scripts/sync-readme-to-ecr-public.sh index 18582177..6cd35214 100644 --- a/scripts/sync-readme-to-ecr-public.sh +++ b/scripts/sync-readme-to-ecr-public.sh @@ -3,6 +3,7 @@ set -euo pipefail repo_root_path="$(cd "$(dirname "$0")"; cd ..; pwd -P)" +makefile_path="${repo_root_path}/Makefile" if ! command -v jq; then echo "command not found: jq" >&2 @@ -58,7 +59,10 @@ assert_not_empty version ################################################# -if ! (git --no-pager diff --name-only HEAD^ HEAD | grep 'README.md' >/dev/null); then +latest_release_tag="$(make -s -f "${makefile_path}" latest-release-tag)" +previous_release_tag="$(make -s -f "${makefile_path}" previous-release-tag)" + +if ! (git --no-pager diff --name-only "${previous_release_tag}" "${latest_release_tag}" | grep 'README.md' >/dev/null); then echo -e "⚠️ README.md did not change in the last commit. Not taking any action." exit 0 fi From e3f15a11cfab073c4866b35cc6f57b14725cd055 Mon Sep 17 00:00:00 2001 From: Jerad C Date: Thu, 6 Oct 2022 09:14:35 -0500 Subject: [PATCH 3/3] update dev setup guide --- DEVELOPMENT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index d008773b..45978933 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -131,7 +131,7 @@ export SERVICE_ACCOUNT_ROLE_ARN=$(eksctl get iamserviceaccount \ ```sh export KO_DOCKER_REPO=$(./scripts/get-cfn-stack-output.sh "${DEV_INFRASTRUCTURE_STACK_NAME}" RepositoryBaseURI) -./scripts/docker-login-ecr.sh +make ecr-login ``` ## 6. Build and deploy controller to EKS cluster