forked from openshift/release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-operators-commands.sh
executable file
·211 lines (189 loc) · 8.27 KB
/
install-operators-commands.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
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
# Set the cluster proxy configuration, if its present.
if test -s "${SHARED_DIR}/proxy-conf.sh" ; then
echo "setting the proxy"
echo "source ${SHARED_DIR}/proxy-conf.sh"
source "${SHARED_DIR}/proxy-conf.sh"
else
echo "no proxy setting."
fi
# If not provided in the JSON, will use the following defaults.
DEFAULT_OPERATOR_SOURCE="redhat-operators"
DEFAULT_OPERATOR_SOURCE_DISPLAY="Red Hat Operators"
DEFAULT_OPERATOR_CHANNEL="!default"
DEFAULT_OPERATOR_INSTALL_NAMESPACE="openshift-operators"
# Read each operator in the JSON provided to an item in a BASH array.
readarray -t OPERATOR_ARRAY < <(jq --compact-output '.[]' <<< "$OPERATORS")
# Iterate through each operator.
for operator_obj in "${OPERATOR_ARRAY[@]}"; do
# Set variables for this operator.
operator_name=$(jq --raw-output '.name' <<< "$operator_obj")
operator_source=$(jq --raw-output '.source // ""' <<< "$operator_obj")
operator_channel=$(jq --raw-output '.channel // ""' <<< "$operator_obj")
operator_install_namespace=$(jq --raw-output '.install_namespace // ""' <<< "$operator_obj")
operator_group=$(jq --raw-output '.operator_group // ""' <<< "$operator_obj")
operator_target_namespaces=$(jq --raw-output '.target_namespaces // ""' <<< "$operator_obj")
operator_config=$(jq --raw-output '.config // ""' <<< "$operator_obj")
# If name not defined, exit.
if [[ -z "${operator_name}" ]]; then
echo "ERROR: name is not defined"
exit 1
fi
# If source is not defined, use DEFAULT_OPERATOR_SOURCE.
if [[ -z "${operator_source}" ]]; then
operator_source="${DEFAULT_OPERATOR_SOURCE}"
else
# If source is any, use any available catalog
if [[ "${operator_source}" == "!any" ]]; then
# Prioritize the use of the default catalog
operator_source=$(oc get packagemanifest | grep "${operator_name}.*${DEFAULT_OPERATOR_SOURCE_DISPLAY}" || echo)
if [[ -n "${operator_source}" ]]; then
operator_source="${DEFAULT_OPERATOR_SOURCE}" ;
else
operator_source=$(oc get packagemanifest ${operator_name} -ojsonpath='{.metadata.labels.catalog}' || echo)
if [[ -z "${operator_source}" ]]; then
echo "ERROR: '${operator_name}' packagemanifest not found in any available catalog"
exit 1
fi
fi
echo "Selecting '${operator_source}' catalog to install '${operator_name}'"
fi
fi
# If install_namespace not defined, use DEFAULT_OPERATOR_INSTALL_NAMESPACE.
if [[ -z "${operator_install_namespace}" ]]; then
operator_install_namespace="${DEFAULT_OPERATOR_INSTALL_NAMESPACE}"
fi
# If channel is not defined, use DEFAULT_OPERATOR_CHANNEL.
if [[ -z "${operator_channel}" ]]; then
operator_channel="${DEFAULT_OPERATOR_CHANNEL}"
fi
echo "Getting '${operator_name}' packagemanifest from '${operator_channel}' channel using '${operator_source}' catalog"
# If the channel is "!default", find the default channel of the operator
if [[ "${operator_channel}" == "!default" ]]; then
operator_channel=$(oc get packagemanifest \
-l catalog=${operator_source} \
-ojsonpath='{.items[?(.metadata.name=="'${operator_name}'")].status.defaultChannel}' 2>/dev/null || echo)
if [[ -z "${operator_channel}" ]]; then
echo "ERROR: Default channel not found in '${operator_name}' packagemanifest."
echo "Checking if the ${operator_name} packagemanifest is available in other catalogs for debugging purpose:"
set -x
oc get packagemanifest "${operator_name}" || \
echo "There is not any available packagemanifest for '${operator_name}' operator"
exit 1
else
echo "INFO: Default channel is ${operator_channel}"
fi
fi
# If "!install" in target_namespaces, use the install namespace
if [[ "${operator_target_namespaces}" == "!install" ]]; then
operator_target_namespaces="${operator_install_namespace}"
fi
echo "Installing ${operator_name} from ${operator_source} channel ${operator_channel} into ${operator_install_namespace}${operator_target_namespaces:+, targeting $operator_target_namespaces}"
# Create the install namespace
oc apply -f - <<EOF
apiVersion: v1
kind: Namespace
metadata:
labels:
openshift.io/cluster-monitoring: "true"
name: "${operator_install_namespace}"
EOF
# Deploy new operator group if operator group is defined
if [[ -n "$operator_group" ]]; then
if [[ -z "$operator_target_namespaces" ]]; then
echo "Deploying OperatorGroup ${operator_group} in to ${operator_install_namespace}"
oc apply -f - <<EOF
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: "${operator_group}"
namespace: "${operator_install_namespace}"
EOF
else
echo "Deploying OperatorGroup ${operator_group} in to ${operator_install_namespace} with target namespaces: ${operator_target_namespaces}"
oc apply -f - <<EOF
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: "${operator_group}"
namespace: "${operator_install_namespace}"
spec:
targetNamespaces:
- $(echo \"${operator_target_namespaces}\" | sed "s|,|\"\n - \"|g")
EOF
fi
fi
echo "Creating subscription for ${operator_name} operator using ${operator_source} source"
# Subscribe to the operator
if [[ -z "$operator_config" ]]; then
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: "${operator_name}"
namespace: "${operator_install_namespace}"
spec:
channel: "${operator_channel}"
installPlanApproval: Automatic
name: "${operator_name}"
source: "${operator_source}"
sourceNamespace: openshift-marketplace
EOF
else
cat <<EOF | oc apply -f -
{
"apiVersion": "operators.coreos.com/v1alpha1",
"kind": "Subscription",
"metadata": {
"name": "${operator_name}",
"namespace": "${operator_install_namespace}"
},
"spec": {
"channel": "${operator_channel}",
"installPlanApproval": "Automatic",
"name": "${operator_name}",
"source": "${operator_source}",
"sourceNamespace": "openshift-marketplace",
"config": ${operator_config}
}
}
EOF
fi
# Need to allow some time before checking if the operator is installed.
sleep 60
RETRIES=30
CSV=
for i in $(seq "${RETRIES}"); do
if [[ -z "${CSV}" ]]; then
CSV=$(oc get subscription -n "${operator_install_namespace}" "${operator_name}" -o jsonpath='{.status.installedCSV}')
fi
if [[ -z "${CSV}" ]]; then
echo "Try ${i}/${RETRIES}: can't get the ${operator_name} yet. Checking again in 30 seconds"
sleep 30
fi
if [[ $(oc get csv -n ${operator_install_namespace} ${CSV} -o jsonpath='{.status.phase}') == "Succeeded" ]]; then
echo "${operator_name} is deployed"
break
else
echo "Try ${i}/${RETRIES}: ${operator_name} is not deployed yet. Checking again in 30 seconds"
sleep 30
fi
done
if [[ $(oc get csv -n "${operator_install_namespace}" "${CSV}" -o jsonpath='{.status.phase}') != "Succeeded" ]]; then
echo "Error: Failed to deploy ${operator_name}"
echo
echo "Assert that the '${operator_name}' packagemanifest belongs to '${operator_source}' catalog"
echo
oc get packagemanifest | grep ${operator_name} || echo
echo "CSV ${CSV} YAML"
oc get csv "${CSV}" -n "${operator_install_namespace}" -o yaml
echo
echo "CSV ${CSV} Describe"
oc describe csv "${CSV}" -n "${operator_install_namespace}"
exit 1
fi
echo "Successfully installed ${operator_name}"
done