forked from aws/amazon-vpc-cni-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-to-eks-charts.sh
executable file
·197 lines (156 loc) · 5.3 KB
/
sync-to-eks-charts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
set -euo pipefail
set +x
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
BUILD_DIR="${SCRIPTPATH}/../build"
REPO="aws/amazon-vpc-cni-k8s"
HELM_CHART_NAME=${HELM_CHART_NAME:-'aws-vpc-cni'}
HELM_CHART_BASE_BIR="${SCRIPTPATH}/../charts"
CHARTS_REPO="aws/eks-charts"
CHARTS_REPO_NAME=$(echo ${CHARTS_REPO} | cut -d'/' -f2)
PR_ID=$(uuidgen | cut -d '-' -f1)
SYNC_DIR="${BUILD_DIR}/eks-charts-sync"
FORK_DIR="${SYNC_DIR}/${CHARTS_REPO_NAME}"
BINARY_BASE=""
INCLUDE_NOTES=0
MANUAL_VERIFY=1
GH_CLI_VERSION="0.10.1"
GH_CLI_CONFIG_PATH="${HOME}/.config/gh/config.yml"
KERNEL=$(uname -s | tr '[:upper:]' '[:lower:]')
OS="${KERNEL}"
if [[ "${KERNEL}" == "darwin" ]]; then
OS="macOS"
fi
VERSION=$(echo $(make -s -f "${SCRIPTPATH}/../Makefile" version) | cut -d'-' -f1)
USAGE=$(cat << EOM
Usage: sync-to-eks-charts -r <repo>
Syncs Helm chart to aws/eks-charts
Example: sync-to-eks-charts -r "${REPO}"
Required:
-b Binary basename (i.e. -b "${HELM_CHART_NAME}")
Optional:
-r Github repo to sync to in the form of "org/name" (i.e. -r "${REPO}")
-n Include application release notes in the sync PR
-y Without asking for manual confirmation before creating PR to upstream
EOM
)
# Process our input arguments
while getopts b:r:ny opt; do
case "${opt}" in
r ) # Github repo
REPO="$OPTARG"
;;
b ) # binary basename
HELM_CHART_NAME="$OPTARG"
BINARY_BASE="$OPTARG"
;;
n ) # Include release notes
INCLUDE_NOTES=1
;;
y ) # Manual verify
MANUAL_VERIFY=0
;;
\? )
echo "$USAGE" 1>&2
exit
;;
esac
done
if [[ -n "${BINARY_BASE}" ]]; then
HELM_CHART_DIR="${HELM_CHART_BASE_BIR}/${BINARY_BASE}"
HELM_CHART_NAME=${BINARY_BASE}
fi
if [[ "$HELM_CHART_NAME" =~ ^(aws-vpc-cni|cni-metrics-helper)$ ]]; then
echo "starting to sync chart $HELM_CHART_NAME"
else
echo "invalid chart name, quit the script"
exit 0
fi
if [[ -z "${REPO}" ]]; then
echo "Repo (-r) must be specified if no \"make repo-full-name\" target exists"
fi
echo $REPO
if [[ -z $(command -v gh) ]] || [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then
mkdir -p "${BUILD_DIR}"/gh
curl -Lo "${BUILD_DIR}"/gh/gh.tar.gz "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/gh_${GH_CLI_VERSION}_${OS}_amd64.tar.gz"
tar -C "${BUILD_DIR}"/gh -xvf "${BUILD_DIR}/gh/gh.tar.gz"
export PATH="${BUILD_DIR}/gh/gh_${GH_CLI_VERSION}_${OS}_amd64/bin:$PATH"
if [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then
echo "❌ Failed install of github cli"
exit 4
fi
fi
function restore_gh_config() {
mv -f "${GH_CLI_CONFIG_PATH}.bkup" "${GH_CLI_CONFIG_PATH}" || :
}
if [[ -n $(env | grep GITHUB_TOKEN) ]] && [[ -n "${GITHUB_TOKEN}" ]]; then
trap restore_gh_config EXIT INT TERM ERR
mkdir -p "${HOME}/.config/gh"
cp -f "${GH_CLI_CONFIG_PATH}" "${GH_CLI_CONFIG_PATH}.bkup" || :
cat << EOF > "${GH_CLI_CONFIG_PATH}"
hosts:
github.com:
oauth_token: ${GITHUB_TOKEN}
user: ${GITHUB_USERNAME}
EOF
fi
function fail() {
echo "❌ EKS charts sync failed"
exit 5
}
trap fail ERR TERM INT
rm -rf "${SYNC_DIR}"
mkdir -p "${SYNC_DIR}"
cd "${SYNC_DIR}"
gh repo fork $CHARTS_REPO --clone --remote
cd "${FORK_DIR}"
git remote set-url origin https://"${GITHUB_USERNAME}":"${GITHUB_TOKEN}"@github.com/"${GITHUB_USERNAME}"/"${CHARTS_REPO_NAME}".git
DEFAULT_BRANCH=$(git rev-parse --abbrev-ref HEAD | tr -d '\n')
if diff -x ".*" -r "$HELM_CHART_DIR/" "${FORK_DIR}/stable/${HELM_CHART_NAME}/" &> /dev/null ; then
echo " ✅ Charts already in sync; no updates needed"
exit
else
echo "📊 Charts are NOT in sync proceeding with PR"
fi
git config user.name "eks-bot"
git config user.email "[email protected]"
# Sync the fork
git pull upstream "${DEFAULT_BRANCH}"
git push -u origin "${DEFAULT_BRANCH}"
FORK_RELEASE_BRANCH="${BINARY_BASE}-${VERSION}-${PR_ID}"
git checkout -b "${FORK_RELEASE_BRANCH}" upstream/"${DEFAULT_BRANCH}"
rm -rf "${FORK_DIR}"/stable/${HELM_CHART_NAME}/
cp -r "$HELM_CHART_DIR/" "${FORK_DIR}/stable/${HELM_CHART_NAME}/"
git add --all
git commit -m "${BINARY_BASE}: ${VERSION}"
PR_BODY=$(cat << EOM
## ${BINARY_BASE} ${VERSION} Automated Chart Sync! 🤖🤖
EOM
)
if [[ "${INCLUDE_NOTES}" -eq 1 ]]; then
RELEASE_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/"${REPO}"/releases | \
jq --arg VERSION "$VERSION" '.[] | select(.tag_name==$VERSION) | .id')
RELEASE_NOTES=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
https://api.github.com/repos/"${REPO}"/releases/"${RELEASE_ID}" | \
jq -r '.body')
PR_BODY=$(cat << EOM
## ${BINARY_BASE} ${VERSION} Automated Chart Sync! 🤖🤖
### Release Notes 📝:
${RELEASE_NOTES}
EOM
)
fi
git push -u origin "${FORK_RELEASE_BRANCH}"
while [[ "$MANUAL_VERIFY" -eq 1 ]]; do
read -p "Please check your github and make sure ok to create a PR. (Y/N)" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "You can cancel the PR in your account and restart."; exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo "Auto creating the PR to upstream!"
gh pr create --title "🥳 ${BINARY_BASE} ${VERSION} Automated Release! 🥑" \
--body "${PR_BODY}" --repo ${CHARTS_REPO}
echo "✅ EKS charts sync complete"