Skip to content

Commit 9112706

Browse files
authored
add sync-to-aws-eks-charts.sh script (#697)
1 parent e6ab754 commit 9112706

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ KO = $(BIN_DIR)/ko
66
SETUP_ENVTEST = $(BIN_DIR)/setup-envtest
77
GINKGO = $(BIN_DIR)/ginkgo
88
GUM = $(BIN_DIR)/gum
9+
GH = $(BIN_DIR)/gh
910
HELM_BASE_OPTS ?= --set aws.region=${AWS_REGION},serviceAccount.name=${SERVICE_ACCOUNT_NAME},serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn=${SERVICE_ACCOUNT_ROLE_ARN}
1011
GINKGO_BASE_OPTS ?= --coverpkg $(shell head -n 1 $(PROJECT_DIR)/go.mod | cut -s -d ' ' -f 2)/pkg/...
1112
KODATA = \
@@ -67,6 +68,9 @@ $(KO):
6768
$(GUM):
6869
@$(PROJECT_DIR)/scripts/download-gum.sh "$(BIN_DIR)"
6970

71+
$(GH):
72+
@$(PROJECT_DIR)/scripts/download-gh.sh "$(BIN_DIR)"
73+
7074
$(SETUP_ENVTEST):
7175
GOBIN="$(BIN_DIR)" go install sigs.k8s.io/controller-runtime/tools/[email protected]
7276
PATH="$(BIN_DIR):$(PATH)" $(SCRIPTS_DIR)/download-kubebuilder-assets.sh
@@ -151,5 +155,9 @@ latest-release-tag: ## Get tag of most recent release.
151155
repo-full-name: ## Get the full name of the GitHub repository for Node Termination Handler.
152156
@echo "$(GITHUB_REPO_FULL_NAME)"
153157

158+
.PHONY: ekscharts-sync-release
159+
ekscharts-sync-release: $(GH)
160+
@PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/sync-to-aws-eks-charts.sh -n
161+
154162
.PHONY: version
155163
version: latest-release-tag ## Get the most recent release version.

scripts/download-gh.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
usage=$(cat << EOM
6+
usage: $(basename "$0") -h | DIR_PATH
7+
8+
Download the gh binary to DIR_PATH.
9+
10+
Arguments:
11+
-h Print usage message then exit.
12+
13+
EOM
14+
)
15+
16+
while getopts "h" opt; do
17+
case $opt in
18+
h ) echo "${usage}"
19+
exit 0
20+
;;
21+
\? ) echo "${usage}" 1>&2
22+
exit 1
23+
;;
24+
esac
25+
done
26+
27+
dir_path="$1"
28+
29+
if [[ -z "${dir_path}" ]]; then
30+
echo "error: missing directory path" 1>&2
31+
echo 1>&2
32+
echo "${usage}" 1>&2
33+
exit 1
34+
fi
35+
36+
if ! command -v wget >/dev/null; then
37+
echo "error: wget not installed" 1>&2
38+
exit 1
39+
fi
40+
41+
version="2.16.1"
42+
arch="$(go env GOHOSTARCH)"
43+
os="$(go env GOHOSTOS)"
44+
45+
if [[ "${os}" == "darwin" ]]; then
46+
os="macOS"
47+
fi
48+
49+
echo "Downloading github.com/cli/cli@v${version} ..."
50+
51+
mkdir -p "${dir_path}"
52+
cd "${dir_path}"
53+
wget https://github.com/cli/cli/releases/download/v${version}/gh_${version}_${os}_${arch}.tar.gz -O - | \
54+
tar xzf - gh

scripts/sync-to-aws-eks-charts.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
repo_root_path="$(cd "$(dirname "$0")"; cd ..; pwd -P)"
6+
makefile_path="${repo_root_path}/Makefile"
7+
8+
if ! command -v gh >/dev/null; then
9+
echo "error: required executable 'gh' not found" >&2
10+
exit 1
11+
fi
12+
13+
github_nth_full_repo_name="$(make -s -f "${makefile_path}" repo-full-name)"
14+
nth_version="$(make -s -f "${makefile_path}" version)"
15+
16+
helm_chart_name="aws-node-termination-handler-2"
17+
helm_chart_path="${repo_root_path}/charts/${helm_chart_name}"
18+
19+
github_eks_charts_full_repo_name="aws/eks-charts"
20+
github_username="${GITHUB_USERNAME:-}"
21+
github_token="${GITHUB_TOKEN:-}"
22+
23+
include_notes=0
24+
25+
usage=$(cat << EOM
26+
usage: $(basename $0) -h | [-n] [-r REPOSITORY] [-e REPOSITORY]
27+
28+
Open Pull Request to eks-charts repository with the latest ${helm_chart_name} Helm chart.
29+
30+
Options:
31+
-h Display this help message then exit.
32+
-n Include application release node in the created Pull Request.
33+
-r REPOSITORY Node Termination Handler GitHub repository. Defaults to "${github_nth_full_repo_name}".
34+
35+
Options for testing:
36+
-e REPOSITORY EKS Charts GitHub repository. Defaults to "${github_eks_charts_full_repo_name}".
37+
-u USERNAME GitHub username. Defaults to the value of the environment variable, GITHUB_USERNAME.
38+
-t TOKEN GitHub token. Defaults to the value of the environment variable, GITHUB_TOKEN .
39+
-v VERSION NTH version. Defaults to "${nth_version}".
40+
41+
EOM
42+
)
43+
44+
while getopts "nr:e:u:t:v:h" opt; do
45+
case "${opt}" in
46+
n ) include_notes=1
47+
;;
48+
r ) github_nth_full_repo_name="${OPTARG}"
49+
;;
50+
e ) github_eks_charts_full_repo_name="${OPTARG}"
51+
;;
52+
u ) github_username="${OPTARG}"
53+
;;
54+
t ) github_token="${OPTARG}"
55+
;;
56+
v ) nth_version="${OPTARG}"
57+
;;
58+
h ) echo "${usage}"
59+
exit 0
60+
;;
61+
\?) echo "${usage}" >&2
62+
exit 1
63+
;;
64+
esac
65+
done
66+
67+
assert_not_empty() {
68+
if [[ -z "${!1}" ]]; then
69+
echo "error: missing argument ${1}" >&2
70+
echo "${usage}" >&2
71+
exit 1
72+
fi
73+
}
74+
75+
assert_not_empty github_nth_full_repo_name
76+
assert_not_empty github_eks_charts_full_repo_name
77+
assert_not_empty github_username
78+
assert_not_empty github_token
79+
assert_not_empty nth_version
80+
81+
github_eks_charts_repo_name="$(echo "${github_eks_charts_full_repo_name}" | cut -d '/' -f2)"
82+
83+
#################################################
84+
85+
echo -e "🥑 Configure gh cli"
86+
87+
gh_client_config_dir="${HOME}/.config/gh"
88+
gh_client_config_path="${gh_client_config_dir}/config.yml"
89+
gh_client_config_backup_path="${gh_client_config_dir}/config.yml.backup"
90+
91+
restore_gh_config() {
92+
if [[ -f "${gh_client_config_backup_path}" ]]; then
93+
echo -e "🥑 Restore gh cli configuration"
94+
mv -f "${gh_client_config_backup_path}" "${gh_client_config_path}" || :
95+
fi
96+
}
97+
98+
trap restore_gh_config EXIT
99+
100+
echo "Backing up existing configuration"
101+
mkdir -p "${gh_client_config_dir}"
102+
mv -f "${gh_client_config_path}" "${gh_client_config_backup_path}"
103+
104+
echo "Writing custom configuration"
105+
cat << EOF > "${gh_client_config_path}"
106+
hosts:
107+
github.com:
108+
oauth_token: "${github_token}"
109+
user: "${github_username}"
110+
EOF
111+
112+
#################################################
113+
114+
echo -e "🥑 Clone ${github_eks_charts_full_repo_name}"
115+
116+
eks_sync_path="${repo_root_path}/build/eks-sync"
117+
rm -rf "${eks_sync_path}"
118+
mkdir -p "${eks_sync_path}"
119+
120+
cd "${eks_sync_path}"
121+
gh repo fork "${github_eks_charts_full_repo_name}" --clone --remote
122+
eks_charts_repo_path="${eks_sync_path}/${github_eks_charts_repo_name}"
123+
124+
cd "${eks_charts_repo_path}"
125+
git_default_branch="$(git rev-parse --abbrev-ref HEAD | tr -d '\n')"
126+
git merge --ff-only "upstream/${git_default_branch}"
127+
git remote set-url origin "https://${github_username}:${github_token}@github.com/${github_username}/${github_eks_charts_repo_name}.git"
128+
git push origin "${git_default_branch}"
129+
130+
#################################################
131+
132+
echo -e "🥑 Check whether chart is in sync"
133+
134+
eks_charts_nth_path="${eks_charts_repo_path}/stable/${helm_chart_name}"
135+
if diff -x ".*" -r "${helm_chart_path}/" "${eks_charts_nth_path}/" &>/dev/null ; then
136+
echo " ✅ Charts already in sync; no updates needed"
137+
exit
138+
fi
139+
140+
echo -e "🚨 Charts are NOT in sync"
141+
142+
#################################################
143+
144+
echo -e "🥑 Copy updates to chart"
145+
146+
rm -rf "${eks_charts_nth_path}"
147+
cp -R "${helm_chart_path}/" "${eks_charts_nth_path}/"
148+
149+
#################################################
150+
151+
echo -e "🥑 Commit updates"
152+
153+
helm_chart_version="$(cat "${helm_chart_path}/Chart.yaml" | grep "version:" | cut -d ' ' -f2 | tr -d '"')"
154+
pr_id="$(uuidgen | cut -d '-' -f1)"
155+
git_release_branch="${helm_chart_name}-${helm_chart_version}-${pr_id}"
156+
git checkout -b "${git_release_branch}"
157+
158+
git add --all
159+
git commit --author="ec2-bot 🤖 <[email protected]>" -m "${helm_chart_name}: ${helm_chart_version}"
160+
git push -u origin "${git_release_branch}"
161+
162+
#################################################
163+
164+
echo -e "🥑 Create pull request"
165+
166+
format_release_notes() {
167+
echo "## ${helm_chart_name} ${helm_chart_version} Automated Chart Sync! 🤖🤖"
168+
169+
if [[ ${include_notes} -ne 1 ]]; then
170+
return 0
171+
fi
172+
173+
local authorization_header="Authorization: token ${GITHUB_TOKEN}"
174+
local release_id="$(curl -s -H "${authorization_header}" \
175+
https://api.github.com/repos/${github_nth_full_repo_name}/releases | \
176+
jq --arg ver "${nth_version}" '.[] | select(.tag_name==$ver) | .id')"
177+
178+
echo
179+
echo "### 📝 Release Notes 📝"
180+
echo
181+
echo "$(curl -s -H "${authorization_header}" \
182+
https://api.github.com/repos/${github_nth_full_repo_name}/releases/${release_id} | \
183+
jq -r '.body')"
184+
echo
185+
}
186+
187+
if ! gh pr create \
188+
--title "🥳 ${helm_chart_name} ${helm_chart_version} Automated Release! 🥑" \
189+
--body "$(format_release_notes)" \
190+
--repo "${github_eks_charts_full_repo_name}" \
191+
--base "master"; then
192+
echo -e "❌ Failed to create pull request"
193+
exit 1
194+
fi
195+
196+
echo -e "✅ Pull request created"
197+
198+
echo -e "✅ EKS charts sync complete"

0 commit comments

Comments
 (0)