-
Notifications
You must be signed in to change notification settings - Fork 65
/
upgrade-test.sh
executable file
·318 lines (257 loc) · 8.55 KB
/
upgrade-test.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env bash
# Copyright 2021 VMware
#
# 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
# shellcheck disable=SC2155
readonly DIR="$(cd "$(dirname "$0")" && pwd)"
readonly HOST_ADDR=${HOST_ADDR:-$("$DIR"/ip.py)}
readonly REGISTRY_PORT=${REGISTRY_PORT:-5000}
readonly REGISTRY=${REGISTRY:-"${HOST_ADDR}:${REGISTRY_PORT}"}
# shellcheck disable=SC2034 # This _should_ be marked as an extern but I clearly don't understand how it operates in github actions
readonly DOCKER_CONFIG=${DOCKER_CONFIG:-"/tmp/cartographer-docker"}
readonly RELEASE_VERSION="v0.0.0-dev"
readonly TEST_NAME="gitwriter-sc"
readonly GIT_SERVER="git-server.default.svc.cluster.local:80"
readonly SOURCE_REPO="hello-world"
readonly SOURCE_BRANCH="main"
readonly CONFIG_REPO="gitops-test"
readonly CONFIG_BRANCH="main"
readonly CONFIG_COMMIT_MESSAGE="Update config"
main() {
"$DIR/setup.sh" cluster example-dependencies
install_latest_released_cartographer
port=$(available_port)
setup_git_server "$port"
source_dir="$(mktemp -d)"
setup_source_repo "$source_dir" "$port"
setup_source_to_gitops
config_dir="$(mktemp -d)"
test_source_to_gitops "$config_dir" "$port"
setup_gitops_to_app
test_gitops_to_app
log "deploying cartographer from main"
install_cartographer_from_current_commit
verify_new_carto_deployed
update_source "$source_dir" "$port"
wait_for_new_config_commit "$config_dir"
wait_for_knative_deployment_update
}
install_latest_released_cartographer() {
log "installing latest released cartographer"
ytt --ignore-unknown-comments \
-f "$DIR/overlays/remove-resource-requests-from-deployments.yaml" \
-f https://github.com/vmware-tanzu/cartographer/releases/latest/download/cartographer.yaml |
kapp deploy --yes -a cartographer -f-
}
install_cartographer_from_current_commit() {
log "building cartographer release and installing it"
env \
REGISTRY="$REGISTRY" \
RELEASE_VERSION="$RELEASE_VERSION" \
DOCKER_CONFIG="$DOCKER_CONFIG" \
"$DIR/release.sh"
ytt --ignore-unknown-comments \
--data-value registry="$REGISTRY" \
-f "$DIR/overlays/remove-resource-requests-from-deployments.yaml" \
-f release/cartographer.yaml |
kapp deploy --yes -a cartographer -f-
}
setup_git_server() {
local port=$1
log "setting up git server"
kubectl apply -f "$DIR/git-server.yaml"
until [[ $(kubectl get deployment git-server -o json | jq '.status.readyReplicas') == 1 ]]; do
log "waiting for git-server deployment"
sleep 10
done
kubectl port-forward service/git-server $port:80 &
# shellcheck disable=SC2064
trap "kill $! || true" EXIT
sleep 5
}
setup_source_repo() {
local source_dir=$1
local port=$2
log "setting up source repo"
pushd "$source_dir"
git clone "http://localhost:$port/$SOURCE_REPO.git"
pushd "$SOURCE_REPO"
git pull https://github.com/kontinue/hello-world.git
if [[ $(git branch --show-current) != "$SOURCE_BRANCH" ]]; then
git checkout -b $SOURCE_BRANCH
fi
git push origin $SOURCE_BRANCH
popd
popd
}
setup_source_to_gitops() {
log "setting up source to gitops"
kapp deploy --yes -a "setup-example-$TEST_NAME" \
-f <(ytt --ignore-unknown-comments \
-f "$DIR/../examples/shared" \
-f "$DIR/../examples/$TEST_NAME/values.yaml" \
--data-value registry.server="$REGISTRY" \
--data-value registry.username=admin \
--data-value registry.password=admin \
--data-value image_prefix="$REGISTRY/example-$TEST_NAME-")
kapp deploy --yes -a "example-$TEST_NAME" \
-f <(ytt --ignore-unknown-comments \
-f "$DIR/../examples/$TEST_NAME" \
--data-value registry.server="$REGISTRY" \
--data-value registry.username=admin \
--data-value registry.password=admin \
--data-value workload_name="$TEST_NAME" \
--data-value image_prefix="$REGISTRY/example-$TEST_NAME-" \
--data-value source_repo.url="http://$GIT_SERVER/$SOURCE_REPO.git" \
--data-value source_repo.branch="$SOURCE_BRANCH" \
--data-value git_repository="http://$GIT_SERVER/$CONFIG_REPO.git" \
--data-value git_branch="$CONFIG_BRANCH" \
--data-value git_user_name="gitops-user" \
--data-value git_user_email="gitops-user@example.com" \
--data-value git_commit_message="$CONFIG_COMMIT_MESSAGE")
}
test_source_to_gitops() {
local config_dir=$1
local port=$2
log "testing source to gitops"
local success=false
pushd "$config_dir"
git clone "http://localhost:$port/$CONFIG_REPO.git"
pushd "$CONFIG_REPO"
for sleep_duration in {20..1}; do
git fetch --all --prune
git checkout "$CONFIG_BRANCH" >/dev/null 2>/dev/null || {
echo "- waiting $sleep_duration seconds"
sleep "$sleep_duration"
continue
}
git pull >/dev/null 2>/dev/null
MOST_RECENT_GIT_MESSAGE="$(git log -1 --pretty=%B)"
if [[ "$CONFIG_COMMIT_MESSAGE" = "$MOST_RECENT_GIT_MESSAGE" ]]; then
log 'gitops worked! sweet'
success=true
break
fi
echo "- waiting $sleep_duration seconds"
sleep "$sleep_duration"
done
popd
popd
if [[ "$success" = true ]]; then
return 0
else
log 'FAILED :('
exit 1
fi
}
setup_gitops_to_app() {
log "setting up gitops-to-app"
ytt --ignore-unknown-comments \
-f "$DIR/../examples/basic-delivery" \
--data-value git_writer.repository="http://$GIT_SERVER/$CONFIG_REPO.git" \
--data-value git_writer.branch="$CONFIG_BRANCH" |
kapp deploy --yes -a example-delivery -f-
}
test_gitops_to_app() {
log "testing gitops-to-app"
for _ in {1..5}; do
for sleep_duration in {15..1}; do
local deployed_pods
deployed_pods=$(kubectl get pods \
-l "serving.knative.dev/configuration=$TEST_NAME" \
-o name)
if [[ "$deployed_pods" == *"$TEST_NAME"* ]]; then
log "testing '$TEST_NAME' SUCCEEDED! sweet"
return 0
fi
echo "- waiting $sleep_duration seconds"
sleep "$sleep_duration"
done
kubectl tree deliverable gitops
done
log "testing gitops-to-app FAILED :("
exit 1
}
verify_new_carto_deployed() {
until [[ $(kubectl -n cartographer-system get deployment cartographer-controller -o json | jq '.metadata.labels."app.kubernetes.io/version"') == \"$RELEASE_VERSION\" &&
$(kubectl -n cartographer-system get deployment cartographer-controller -o json | jq '.status.availableReplicas') != 0 ]]; do
log "waiting for new cartographer deployment"
sleep 10
done
}
update_source(){
local source_dir=$1
local port=$2
log "updating source repo"
pushd "$source_dir/$SOURCE_REPO"
echo "meaningless change" >> README.md
git config user.email "gitops-user@example.com"
git config user.name "Gitops User"
git add .
git commit -m "Meaningless change"
git push origin $SOURCE_BRANCH
popd
}
wait_for_new_config_commit() {
local config_dir=$1
log "testing source to gitops part 2"
log "waiting for new config commit"
local success=false
pushd "$config_dir/$CONFIG_REPO"
for sleep_duration in {20..1}; do
git pull
if [[ $(git log --pretty=oneline | wc -l | tr -d ' ') == 2 ]]; then
log "found new config commit! source to gitops part 2 worked!"
success=true
break
fi
echo "- waiting $sleep_duration seconds"
sleep "$sleep_duration"
done
popd
if [[ "$success" = true ]]; then
return 0
else
log 'FAILED :('
exit 1
fi
}
wait_for_knative_deployment_update() {
log "testing gitops to app part 2"
for sleep_duration in {15..1}; do
if [[ $(kubectl get deployment -l "serving.knative.dev/configuration=gitwriter-sc" -o name | wc -l | tr -d ' ') == 2 ]]; then
log "gitops to app part 2 SUCCEEDED! sweet"
return 0
fi
echo "- waiting $sleep_duration seconds"
sleep "$sleep_duration"
done
log "testing gitops-to-app part 2 FAILED :("
exit 1
}
available_port() {
python - <<-EOF
import socket
s = socket.socket()
s.bind(('', 0))
print(s.getsockname()[1])
s.close()
EOF
}
log() {
printf '\n\t\033[1m%s\033[0m\n\n' "$1" 1>&2
}
main "$@"