-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
helper-functions.sh
214 lines (170 loc) · 4.61 KB
/
helper-functions.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
#!/bin/bash
#
# Copyright 2023 kubeflow.org
#
# 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.
retry() {
local max=$1; shift
local interval=$1; shift
until "$@"; do
echo "trying.."
max=$((max-1))
if [[ "$max" -eq 0 ]]; then
return 1
fi
sleep "$interval"
done
}
wait_for_namespace () {
if [[ $# -ne 3 ]]
then
echo "Usage: wait_for_namespace namespace max_retries sleep_time"
return 1
fi
local namespace=$1
local max_retries=$2
local sleep_time=$3
local i=0
while [[ $i -lt $max_retries ]]
do
if kubectl get ns | grep -qow "$namespace"
then
return 0
fi
echo "$namespace not found. Checking again in ${sleep_time}s."
sleep "$sleep_time"
i=$((i+1))
done
return 1
}
wait_for_pods () {
C_DIR="${BASH_SOURCE%/*}"
pip install -r "${C_DIR}"/../../../sdk/python/requirements.txt
python "${C_DIR}"/kfp-readiness/wait_for_pods.py
}
deploy_with_retries () {
if [[ $# -ne 4 ]]
then
echo "Usage: deploy_with_retries (-f FILENAME | -k DIRECTORY) manifest max_retries sleep_time"
return 1
fi
local flag="$1"
local manifest="$2"
local max_retries="$3"
local sleep_time="$4"
local i=0
while [[ $i -lt $max_retries ]]
do
local exit_code=0
kubectl apply "$flag" "$manifest" || exit_code=$?
if [[ $exit_code -eq 0 ]]
then
return 0
fi
echo "Deploy unsuccessful with error code $exit_code. Trying again in ${sleep_time}s."
sleep "$sleep_time"
i=$((i+1))
done
return 1
}
wait_for_pod () {
local namespace=$1
local pod_name=$2
local max_tries=$3
local sleep_time=$4
until pod_is_running "$namespace" "$pod_name"; do
max_tries=$((max_tries-1))
if [[ "$max_tries" -eq 0 ]]; then
return 1
fi
echo "Checking again in $sleep_time"
sleep "$sleep_time"
done
return 0
}
pod_is_running () {
local namespace=$1
local pod_name=$2
local pod_status
# May have unexpected results if pod_name has multiple matches
pod_status=$(kubectl get pod -n "$namespace" | grep "$pod_name*" | head -1 | awk '{print $3}')
if [ "$pod_status" = "Running" ]; then
return 0
fi
return 1
}
wait_for_pipeline_run () {
local run_name=$1
local max_tries=$2
local sleep_time=$3
until pipeline_run_is_success "$run_name"; do
max_tries=$((max_tries-1))
if [[ "$max_tries" -eq 0 ]]; then
return 1
fi
echo "Checking pipeline run again in $sleep_time"
sleep "$sleep_time"
done
return 0
}
wait_for_pipeline_run_rev () {
local run_name=$1
local max_tries=$2
local sleep_time=$3
until [ "$(pipeline_run_is_success_rev "$run_name")" = "0" ]; do
max_tries=$((max_tries-1))
if [[ "$max_tries" -eq 0 ]]; then
echo "1"
return
fi
sleep "$sleep_time"
done
echo "0"
return
}
pipeline_run_is_success () {
local run_name=$1
local run_status
# May have unexpected results if run_status has multiple matches
run_status=$(kubectl get pipelineruns "$run_name" | tail -1 | awk '{print $2}')
if [ "$run_status" = "True" ]; then
return 0
elif [ "$run_status" = "False" ]; then
echo "Run Failed"
exit 1
fi
return 1
}
pipeline_run_is_success_rev () {
local run_name=$1
local run_status
# May have unexpected results if run_status has multiple matches
run_status=$(kubectl get pipelineruns "$run_name" | tail -1 | awk '{print $2}')
if [ "$run_status" = "True" ]; then
echo "0"
return
elif [ "$run_status" = "False" ]; then
echo "1"
return
fi
echo "1"
return
}
collect_artifacts() {
local kubeflow_ns=$1
local log_dir=$(mktemp -d)
pods_kubeflow=$(kubectl get pods -n $kubeflow_ns --no-headers -o custom-columns=NAME:.metadata.name)
for pod in $pods_kubeflow; do
kubectl logs -n $kubeflow_ns $pod > $log_dir/$pod.log
done
}