This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path.util.sh
executable file
·161 lines (133 loc) · 3.44 KB
/
.util.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
#!/bin/bash
if [[ "${FATS_LOADED:-x}" == "true" ]]; then
return
fi
FATS_LOADED=true
ANSI_RED="\033[31;1m"
ANSI_GREEN="\033[32;1m"
ANSI_BLUE="\033[34;1m"
ANSI_RESET="\033[0m"
ANSI_CLEAR="\033[0K"
`dirname "${BASH_SOURCE[0]}"`/install.sh kubectl
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
MINGW*) machine=MinGw;;
MSYS_NT*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
if [ "$machine" == "MinGw" ]; then
sudo() {
$@
}
fi
wait_for_service_ip() {
local name=$1
local namespace=$2
wait_kube_ready \
'service' \
"$namespace" \
"$name" \
'{$.status.loadBalancer.ingress[].ip}' \
'[0-9]'
}
wait_for_service_hostname() {
local name=$1
local namespace=$2
local pattern=$3
wait_kube_ready \
'service' \
"$namespace" \
"$name" \
'{$.status.loadBalancer.ingress[].hostname}' \
"$pattern"
}
wait_pod_selector_ready() {
local label=$1
local namespace=$2
wait_kube_ready \
'pods' \
"$namespace" \
"$label" \
'{range .items[*]}{@.metadata.name};{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
';Ready=True;'
}
wait_kservice_ready() {
local name=$1
local namespace=$2
wait_knative_ready 'services.serving.knative.dev' "$name" "$namespace"
}
wait_knative_ready() {
local type=$1
local name=$2
local namespace=$3
wait_kube_ready \
"$type" \
"$namespace" \
"$name" \
';{range @.status.conditions[*]}{@.type}={@.status};{end}' \
';Ready=True;'
}
wait_kube_selector_exists() {
local type=$1
local selector=$2
local namespace=$3
local name=$4
until kubectl get $type --namespace $namespace -l $selector \
-o yaml | grep -qE $name; \
do sleep 1; \
done
echo "$type found for $selector in $namespace"
kubectl get $type --namespace $namespace -l $selector
}
wait_kube_ready() {
local type=$1
local namespace=$2
local jsonpath=$4
local pattern=$5
if [[ $3 = *"="* ]]; then
local selector=$3
# TODO look for all resources to be ready, not just one
until kubectl get $type --namespace $namespace -l $selector \
-o jsonpath="$jsonpath" 2>&1 | grep -qE $pattern; \
do sleep 1; \
done
else
local name=$3
until kubectl get $type --namespace $namespace $name \
-o jsonpath="$jsonpath" 2>&1 | grep -qE $pattern; \
do sleep 1; \
done
fi
}
fats_echo() {
echo -e "$ANSI_BLUE[`date -u +%Y-%m-%dT%H:%M:%SZ`]$ANSI_RESET $@"
}
# derived from https://github.com/travis-ci/travis-build/blob/4f580b238530108cdd08719c326cd571d4e7b99f/lib/travis/build/bash/travis_retry.bash
# MIT licenced https://github.com/travis-ci/travis-build/blob/4f580b238530108cdd08719c326cd571d4e7b99f/LICENSE
fats_retry() {
local result=0
local count=1
while [[ "${count}" -le 3 ]]; do
[[ "${result}" -ne 0 ]] && {
echo -e "\\n${ANSI_RED}The command \"${*}\" failed. Retrying, ${count} of 3.${ANSI_RESET}\\n" >&2
}
"${@}" && { result=0 && break; } || result="${?}"
count="$((count + 1))"
sleep 1
done
[[ "${count}" -gt 3 ]] && {
echo -e "\\n${ANSI_RED}The command \"${*}\" failed 3 times.${ANSI_RESET}\\n" >&2
}
return "${result}"
}
fats_assert() {
local expected_data=${1}
local actual_data=${2}
if [ "$actual_data" != "$expected_data" ]; then
echo -e "${ANSI_RED}did not produce expected result${ANSI_RESET}:";
echo -e " expected: $expected_data"
echo -e " actual: $actual_data"
exit 1
fi
}