This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.sh
executable file
·301 lines (266 loc) · 8.06 KB
/
setup.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
#!/bin/bash
SCRIPT_DIR=$(readlink -f `dirname "${BASH_SOURCE[0]}"`)
function help() {
echo "usage: setup.sh [-d|-D] [-pto] FILE"
echo
echo "Required:"
echo " FILE a file of 'operatorname channel github' space-separated triplets, one per line"
echo " Check the README.md for more information."
echo
echo "Options:"
echo
echo " -p command will affect projects"
echo " -t command will affect test subdirectories"
echo " -o command will affect operators"
echo
echo " The default behavior without any of the above options is '-pto'. If any of the"
echo " three options are specified, then only those specified will be set"
echo
echo " -d delete and recreate projects and operators, mutually exclusive with -D."
echo " -D delete projects and operators, mutually exclusive with -d. Also skips git clone."
}
function delete_operator() {
local namespace
if [ "$2" == "true" ]; then
# we installed this globally in openshift-operators
namespace=openshift-operators
else
# If there's no project, there can't be an operator there
namespace=$(find_project $1)
if [ "$namespace" == "" ]; then
echo Operator $1 not found, nothing to delete
return 0
fi
fi
set +e
csv=$(oc get subscription -l peak.test.subscription=$1 -n $namespace -o=jsonpath="{.items[0].status.currentCSV}" 2>/dev/null)
if [ "$?" -eq 0 ]; then
echo Attempting to uninstall operator $1
oc delete subscription -l peak.test.subscription=$1 -n $namespace
oc delete clusterserviceversion $csv -n $namespace
if [ "$2" == "false" ]; then
oc delete operatorgroup -l peak.test.operatorgroup=$1 -n $namespace
fi
else
echo Operator $1 not found, nothing to delete
fi
set -e
}
function handleproj() {
if [ "$delete" == "true" ]; then
del_project $1
elif [ "$recreate" == "true" ]; then
clean_project $1
else
make_project $1
fi
}
function getmanifest() {
installMode=""
set +e
oc get packagemanifest $1 &> /dev/null
manifest_present="$?"
if [ "$manifest_present" -eq 0 ]; then
installMode=$(oc get packagemanifest $1 -o=jsonpath="{.status.channels[?(@.name==\"$2\")].currentCSVDesc.installModes[?(@.type==\"AllNamespaces\")].supported}")
fi
set -e
}
function installop() {
# $1 operator name
# $2 channel
# Note that in the case of an operator in a single namespace,
# the operator group and subscription will be created here.
if [ "$manifest_present" -ne 0 ]; then
echo "operator manifest not present for $1, skipping install"
return 0
fi
# subscription namespace
local subsns
# testproj is based on the operator name with a random string,
# we'll use it to name subscriptions etc too
local testproj
testproj=$(find_project $1)
# Look up the specified channel and find the catalog source
csource=$(oc get packagemanifest $1 -o=jsonpath="{.status.catalogSource}")
# Look up the specified channel and find the catalog source namespace
csourcens=$(oc get packagemanifest $1 -o=jsonpath="{.status.catalogSourceNamespace}")
set +e
if [ "$installMode" == "true" ]; then
echo "install $1 in namespace openshift-operators"
subsns=openshift-operators
# For global operators we're just using testproj for a name, so make
# one if we have not created the project
if [ "$testproj" == "" ]; then
testproj=$(random_name $1)
fi
else
# make sure project exists since we can decouple project and operator creation
if [ "$testproj" == "" ]; then
echo "project for $1 does not exist, skipping operator install"
return 0
fi
echo "install $1 in namespace $testproj"
subsns=$testproj
# in this case we need to make an operator group in the new project
cat <<- EOF | oc create -f -
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: "$testproj"
namespace: "$subsns"
labels:
peak.test.operatorgroup: $1
spec:
targetNamespaces:
- "$testproj"
EOF
fi
# create a subscription object
cat <<- EOF | oc create -f -
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: $testproj
namespace: $subsns
labels:
peak.test.subscription: $1
spec:
channel: $2
name: $1
source: $csource
sourceNamespace: $csourcens
EOF
set -e
# wait for install
for _ in $(seq 1 30); do
csv=$(oc -n "$subsns" get subscription "$testproj" -o jsonpath='{.status.installedCSV}' || true)
if [[ -n "$csv" ]]; then
if [[ "$(oc -n "$subsns" get csv "$csv" -o jsonpath='{.status.phase}')" == "Succeeded" ]]; then
echo "$1 installed"
return
fi
fi
sleep 10
done
echo "$1 install failed"
}
function addtestdir() {
if [ ! -d $SCRIPT_DIR/operator-tests/$1 ]; then
echo Cloning test repository for $1
if [ -n "$3" ]; then
echo git clone $2 --branch $3 $SCRIPT_DIR/operator-tests/$1
git clone $2 --branch $3 $SCRIPT_DIR/operator-tests/$1
else
echo git clone $2 $SCRIPT_DIR/operator-tests/$1
git clone $2 $SCRIPT_DIR/operator-tests/$1
fi
else
echo Test repository exists for $1, skipping clone
fi
}
delete=false
recreate=false
everything=true
projects=false
operators=false
tests=false
manifest_present=-1
installMode=
while getopts Ddhpto option; do
case $option in
D)
delete=true
;;
d)
recreate=true
;;
h)
help
exit 0
;;
p)
everything=false
projects=true
;;
t)
everything=false
tests=true
;;
o)
everything=false
operators=true
;;
*)
;;
esac
done
# The last argument is the name of the operator file
shift $((OPTIND-1))
if [ "$#" -lt 1 ]; then
help
exit 0
fi
if [ "$delete" == "true" -a "$recreate" == "true" ]; then
echo "Options -d and -D are mutually exclusive"
help
exit -1
fi
if [ "$everything" == "true" ]; then
tests=true
projects=true
operators=true
fi
# Track whether we have a valid oc login
source $SCRIPT_DIR/util
check_ocp
# We have to have a login for operators and projects
if [ "$operators" == "true" -o "$projects" == "true" ]; then
if [ "$OCP" -ne 0 ]; then
echo "No active openshift identity, can't set up projects or operators, exiting."
echo "To clone test subdirectories, run with the just '-t' option."
exit 0
fi
fi
# If the NAMESPACE env var is set and the namespace doesn't exist, try to create it
if [ ! -z ${NAMESPACE:-} ] && [ "$OCP" -eq 0 ]; then
set +e
oc get namespace $NAMESPACE
if [ "$?" -ne 0 ]; then
oc create namespace $NAMESPACE
fi
set -e
fi
while IFS= read -r line
do
vals=($line)
echo ++++++++++++++ Processing entry for operator "${vals[0]}" ++++++++++++++
if [ "${#vals[@]}" -lt 2 ]; then
echo "Invalid tuple '${vals[@]}' in $1, skipping"
continue
fi
if [ "$operators" == "true" ]; then
getmanifest ${vals[@]:0:2}
fi
# Uninstall the operator in the delete or recreate case
if [ "$delete" == "true" -o "$recreate" == "true" ] && [ "$manifest_present" -eq 0 -a "$operators" == "true" ]; then
delete_operator ${vals[0]} $installMode
fi
# Use vals[0] for the project name because that's the base and we may not have made it yet
if [ "$projects" == "true" ]; then
handleproj ${vals[0]} $installMode
fi
# install operator if we're (re)creating
if [ "$operators" == "true" -a "$delete" == "false" ]; then
installop ${vals[@]:0:2}
fi
# clone a specific repository for tests if one is listed
if [ "$tests" == "true" -a "$delete" == "false" ]; then
branch=""
if [ "${#vals[@]}" -gt 2 ]; then
if [ "${#vals[@]}" -gt 3 ]; then
branch=${vals[3]}
fi
addtestdir ${vals[0]} ${vals[2]} $branch
fi
fi
done < "$1"