forked from openshift-metal3/dev-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocp_install_env.sh
411 lines (361 loc) · 10.8 KB
/
ocp_install_env.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
eval "$(go env)"
function get_arch() {
ARCH=$(uname -m)
if [[ $ARCH == "aarch64" ]]; then
ARCH="arm64"
elif [[ $ARCH == "x86_64" ]]; then
if [[ "$1" == "install_config" ]]; then
ARCH="amd64"
fi
fi
echo $ARCH
}
function extract_command() {
local release_image
local cmd
local outdir
local extract_dir
cmd="$1"
release_image="$2"
outdir="$3"
extract_dir=$(mktemp --tmpdir -d "installer--XXXXXXXXXX")
_tmpfiles="$_tmpfiles $extract_dir"
oc adm release extract --registry-config "${PULL_SECRET_FILE}" --command=$cmd --to "${extract_dir}" ${release_image}
mv "${extract_dir}/${cmd}" "${outdir}"
}
# Let's always grab the `oc` from the release we're using.
function extract_oc() {
extract_dir=$(mktemp --tmpdir -d "installer--XXXXXXXXXX")
_tmpfiles="$_tmpfiles $extract_dir"
extract_command oc "$1" "${extract_dir}"
sudo mv "${extract_dir}/oc" /usr/local/bin
}
function save_release_info() {
local release_image
local outdir
release_image="$1"
outdir="$2"
oc adm release info --registry-config "$PULL_SECRET_FILE" "$release_image" -o json > ${outdir}/release_info.json
}
# Gives e.g 4.7.0-0.nightly-2020-10-27-051128
function openshift_release_version() {
jq -r ".metadata.version" ${OCP_DIR}/release_info.json
}
# Gives us e.g 4.7 because although OPENSHIFT_VERSION is set by users,
# but is not set in CI
function openshift_version() {
jq -r ".metadata.version" ${OCP_DIR}/release_info.json | grep -oP "\d\.\d+"
}
function extract_rhcos_json() {
local release_image
local outdir
release_image="$1"
outdir="$2"
baremetal_image=$(image_for baremetal-installer)
baremetal_container=$(podman create --authfile "$PULL_SECRET_FILE" "$baremetal_image")
# This is OK to fail as rhcos.json isn't available in every release,
# we'll download it from github if it's not available
podman cp "$baremetal_container":/var/cache/rhcos.json "$outdir" || true
podman rm -f "$baremetal_container"
}
function baremetal_network_configuration() {
if [[ "$(openshift_version $OCP_DIR)" == "4.3" ]]; then
return
fi
if [[ "$PROVISIONING_NETWORK_PROFILE" == "Disabled" ]]; then
cat <<EOF
provisioningNetwork: "${PROVISIONING_NETWORK_PROFILE}"
EOF
if printf '%s\n4.6\n' "$(openshift_version)" | sort -V -C; then
cat <<EOF
provisioningHostIP: "${CLUSTER_PROVISIONING_IP}"
bootstrapProvisioningIP: "${BOOTSTRAP_PROVISIONING_IP}"
EOF
fi
else
cat <<EOF
provisioningBridge: ${PROVISIONING_NETWORK_NAME}
provisioningNetworkCIDR: $PROVISIONING_NETWORK
provisioningNetworkInterface: $CLUSTER_PRO_IF
EOF
fi
if [ -n "${ENABLE_BOOTSTRAP_STATIC_IP}" ]; then
if [[ "${IP_STACK}" = "v6" || "${IP_STACK}" = "v6v4" ]]; then
BOOTSTRAP_IP=$(nth_ip $EXTERNAL_SUBNET_V6 $((idx + 9)))
else
BOOTSTRAP_IP=$(nth_ip $EXTERNAL_SUBNET_V4 $((idx + 9)))
fi
cat <<EOF
bootstrapExternalStaticIP: "${BOOTSTRAP_IP}"
bootstrapExternalStaticGateway: "${PROVISIONING_HOST_EXTERNAL_IP}"
EOF
if ! printf '%s\n4.13\n' "$(openshift_version)" | sort -V -C; then
cat <<EOF
bootstrapExternalStaticDNS: "${PROVISIONING_HOST_EXTERNAL_IP}"
EOF
fi
fi
}
function dnsvip() {
# dnsVIP was removed from 4.5
if printf '%s\n4.4\n' "$(openshift_version)" | sort -V -C; then
cat <<EOF
dnsVIP: ${DNS_VIP}
EOF
fi
}
function external_mac() {
if [ -n "$EXTERNAL_BOOTSTRAP_MAC" ] ; then
cat <<EOF
externalMACAddress: $EXTERNAL_BOOTSTRAP_MAC
EOF
fi
}
function renderVIPs() {
# Arguments:
# First argument: field name
# Second argument: value for the field
#
# Description:
# This function helps to write in a YAML format multiple resources. (i.e: apiVIPs and ingressVIPs)
# Example: apiVIPs, "192.168.11.5, fd2e:6f44:5dd8:c956::15"
#
# Returns:
# YAML formatted resource
FIELD_VALUES="${2}";
echo " ${1}"
for data in ${FIELD_VALUES//${VIPS_SEPARATOR}/ }; do
echo " - ${data}"
done
}
function setVIPs() {
# Arguments:
# The type of VIP: apivips OR ingressvips
#
# Description:
# apiVIP and ingressVIP both has been DEPRECATED in 4.12 in favor of apiVIPs and ingressVIPs.
# This functions helps to write the new apiVIPs/ingressVIPs format or set the old fields.
#
# Returns:
# The YAML formatted APIVIP or INGRESSVIP (supports new and old format)
case "${1}" in
"apivips")
if printf '4.12\n%s\n' "$(openshift_version)" | sort -V -C; then
# OCP version is equals or newer as 4.12 and supports the new VIPs fields
renderVIPs "apiVIPs:" "${API_VIPS}"
else
# OCP version is older as 4.12 and does not support the new VIPs fields
echo " apiVIP: ${API_VIPS%${VIPS_SEPARATOR}*}"
fi
;;
"ingressvips")
if printf '4.12\n%s\n' "$(openshift_version)" | sort -V -C; then
# OCP version is equals or newer as 4.12 and supports the new VIPs fields
renderVIPs "ingressVIPs:" "${INGRESS_VIPS}"
else
# OCP version is older as 4.12 and does not support the new VIPs fields
echo " ingressVIP: ${INGRESS_VIPS%${VIPS_SEPARATOR}*}"
fi
;;
esac
}
function libvirturi() {
if [[ "$REMOTE_LIBVIRT" -ne 0 ]]; then
cat <<EOF
libvirtURI: qemu+ssh://${PROVISIONING_HOST_USER}@$(wrap_if_ipv6 ${PROVISIONING_HOST_IP})/system
EOF
fi
}
function additional_trust_bundle() {
if [[ ! -z "$ADDITIONAL_TRUST_BUNDLE" ]]; then
if [[ -z "${MIRROR_IMAGES}" && -z "${ENABLE_LOCAL_REGISTRY}" ]]; then
echo "additionalTrustBundle: |"
fi
awk '{ print " ", $0 }' "${ADDITIONAL_TRUST_BUNDLE}"
fi
}
function cluster_network() {
if [[ "${IP_STACK}" == "v4" ]]; then
cat <<EOF
machineNetwork:
- cidr: ${EXTERNAL_SUBNET_V4}
clusterNetwork:
- cidr: ${CLUSTER_SUBNET_V4}
hostPrefix: ${CLUSTER_HOST_PREFIX_V4}
serviceNetwork:
- ${SERVICE_SUBNET_V4}
EOF
elif [[ "${IP_STACK}" == "v6" ]]; then
cat <<EOF
machineNetwork:
- cidr: ${EXTERNAL_SUBNET_V6}
clusterNetwork:
- cidr: ${CLUSTER_SUBNET_V6}
hostPrefix: ${CLUSTER_HOST_PREFIX_V6}
serviceNetwork:
- ${SERVICE_SUBNET_V6}
EOF
elif [[ "${IP_STACK}" == "v4v6" ]]; then
cat <<EOF
machineNetwork:
- cidr: ${EXTERNAL_SUBNET_V4}
- cidr: ${EXTERNAL_SUBNET_V6}
clusterNetwork:
- cidr: ${CLUSTER_SUBNET_V4}
hostPrefix: ${CLUSTER_HOST_PREFIX_V4}
- cidr: ${CLUSTER_SUBNET_V6}
hostPrefix: ${CLUSTER_HOST_PREFIX_V6}
serviceNetwork:
- ${SERVICE_SUBNET_V4}
- ${SERVICE_SUBNET_V6}
EOF
elif [[ "${IP_STACK}" == "v6v4" ]]; then
cat <<EOF
machineNetwork:
- cidr: ${EXTERNAL_SUBNET_V6}
- cidr: ${EXTERNAL_SUBNET_V4}
clusterNetwork:
- cidr: ${CLUSTER_SUBNET_V6}
hostPrefix: ${CLUSTER_HOST_PREFIX_V6}
- cidr: ${CLUSTER_SUBNET_V4}
hostPrefix: ${CLUSTER_HOST_PREFIX_V4}
serviceNetwork:
- ${SERVICE_SUBNET_V6}
- ${SERVICE_SUBNET_V4}
EOF
else
echo "Unexpected IP_STACK value: '${IP_STACK}'"
exit 1
fi
}
function cluster_os_image() {
if is_lower_version $(openshift_version) 4.10; then
cat <<EOF
clusterOSImage: http://$(wrap_if_ipv6 $MIRROR_IP)/images/${MACHINE_OS_IMAGE_NAME}?sha256=${MACHINE_OS_IMAGE_SHA256}
EOF
fi
}
function generate_ocp_install_config() {
local outdir
outdir="$1"
# when using local mirror set pull secret to just this mirror to
# ensure we don't accidentally pull from upstream
if [ ! -z "${MIRROR_IMAGES}" ]; then
install_config_pull_secret="${REGISTRY_CREDS}"
else
install_config_pull_secret="${PULL_SECRET_FILE}"
fi
mkdir -p "${outdir}"
# IPv6 network config validation
if [[ -n "${EXTERNAL_SUBNET_V6}" ]]; then
if [[ "${NETWORK_TYPE}" != "OVNKubernetes" ]]; then
echo "NETWORK_TYPE must be OVNKubernetes when using IPv6"
exit 1
fi
fi
cat > "${outdir}/install-config.yaml" << EOF
apiVersion: v1
baseDomain: ${BASE_DOMAIN}
networking:
networkType: ${NETWORK_TYPE}
$(cluster_network)
metadata:
name: ${CLUSTER_NAME}
compute:
- name: worker
replicas: $NUM_WORKERS
architecture: $(get_arch install_config)
controlPlane:
name: master
replicas: ${NUM_MASTERS}
architecture: $(get_arch install_config)
platform:
baremetal: {}
platform:
baremetal:
$(libvirturi)
$(baremetal_network_configuration)
externalBridge: ${BAREMETAL_NETWORK_NAME}
$(external_mac)
bootstrapOSImage: http://$(wrap_if_ipv6 $MIRROR_IP)/images/${MACHINE_OS_BOOTSTRAP_IMAGE_NAME}?sha256=${MACHINE_OS_BOOTSTRAP_IMAGE_UNCOMPRESSED_SHA256}
$(cluster_os_image)
$(setVIPs apivips)
$(setVIPs ingressvips)
$(dnsvip)
hosts:
EOF
if [ -z "${HOSTS_SWAP_DEFINITION:-}" ]; then
cat >> "${outdir}/install-config.yaml" << EOF
$(node_map_to_install_config_hosts $NUM_MASTERS 0 master)
$(node_map_to_install_config_hosts $NUM_WORKERS $NUM_MASTERS worker)
EOF
else
cat >> "${outdir}/install-config.yaml" << EOF
$(node_map_to_install_config_hosts $NUM_WORKERS $NUM_MASTERS worker)
$(node_map_to_install_config_hosts $NUM_MASTERS 0 master)
EOF
fi
cat >> "${outdir}/install-config.yaml" << EOF
$(image_mirror_config)
$(additional_trust_bundle)
pullSecret: |
$(jq -c . $install_config_pull_secret)
sshKey: |
${SSH_PUB_KEY}
fips: ${FIPS_MODE:-false}
EOF
if [[ ! -z "$INSTALLER_PROXY" ]]; then
cat >> "${outdir}/install-config.yaml" << EOF
proxy:
httpProxy: ${HTTP_PROXY}
httpsProxy: ${HTTPS_PROXY}
noProxy: ${NO_PROXY}
EOF
fi
cp "${outdir}/install-config.yaml" "${outdir}/install-config.yaml.save"
}
function generate_ocp_host_manifest() {
local outdir
outdir="$1"
host_input="$2"
host_output="$3"
namespace="$4"
mkdir -p "${outdir}"
rm -f "${outdir}/extra_hosts.yaml"
mkdir -p "${outdir}/extras"
rm -f "${outdir}/extras/*"
worker_index=0
jq --raw-output '.[] | .name + " " + .ports[0].address + " " + .driver_info.username + " " + .driver_info.password + " " + .driver_info.address' $host_input \
| while read name mac username password address ; do
encoded_username=$(echo -n "$username" | base64)
encoded_password=$(echo -n "$password" | base64)
secret="---
apiVersion: v1
kind: Secret
metadata:
name: ${name}-bmc-secret
namespace: $namespace
type: Opaque
data:
username: $encoded_username
password: $encoded_password
"
bmh="---
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: $name
namespace: $namespace
spec:
online: ${EXTRA_WORKERS_ONLINE_STATUS}
bootMACAddress: $mac
bmc:
address: $address
credentialsName: ${name}-bmc-secret"
echo "${secret}${bmh}" >> "${outdir}/${host_output}"
# Extra files will be used later to generate a secret used by e2e tests
echo "${secret}" >> "${outdir}/extras/extraworker-${worker_index}-secret"
echo "${bmh}" >> "${outdir}/extras/extraworker-${worker_index}-bmh"
((worker_index+=1))
done
}