blob: 7e089cbcc35fa5fbe979affbc2cc60973865d79b [file] [log] [blame]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01001#!/bin/bash
2set -x
Deniscaa72dd2021-06-04 17:13:05 +03003set -e
Dennis Dmitrievb12d2342020-03-12 19:36:24 +02004# allow access to the local variables from prepare-metadata.py
5set -a
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01006
Vasyl Saienko9824df42021-03-17 15:43:02 +02007# ensure we don't re-source this in the same environment
Vasyl Saienko4a2832d2024-05-16 09:00:03 +03008[[ -z "$_FUNCTIONS_SCRIPT" ]] || return 0
9declare -r -g _FUNCTIONS_SCRIPT=1
Vasyl Saienko9824df42021-03-17 15:43:02 +020010
Oleh Hryhorovca772092020-02-13 11:15:17 +020011PUBLIC_INTERFACE_NETMASK=$(echo ${PUBLIC_INTERFACE_CIDR} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020012STORAGE_BACKEND_NETWORK_NETMASK=$(echo ${STORAGE_BACKEND_NETWORK} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020013STORAGE_FRONTEND_NETWORK_NETMASK=$(echo ${STORAGE_FRONTEND_NETWORK} | cut -d'/' -f2)
Vasyl Saienko552250d2021-03-19 12:07:23 +020014IRONIC_BAREMETAL_INTERFACE_IP=${IRONIC_BAREMETAL_INTERFACE_IP:-$ironic_baremetal_interface_ip}
Vasyl Saienko4c468192020-05-19 11:51:13 +030015IRONIC_BAREMETAL_NETWORK_NETMASK=$(echo ${IRONIC_BAREMETAL_NETWORK} | cut -d'/' -f2)
Vasyl Saienkod3964202023-09-24 15:09:48 +000016IRONIC_BAREMETAL_INTERFACE=$(ip route |grep ${IRONIC_BAREMETAL_NETWORK} | awk '/ src / {print $3}')
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020017TUNNEL_INTERFACE_NETWORK_NETMASK=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP} | awk '{print $4}' |awk -F '/' '{print $2}')
18TUNNEL_INTERFACE=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK} | awk '{print $2}')
Michael Polenchuk486cadf2020-08-24 16:25:51 +040019IRONIC_BAREMETAL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_NETWORK)
20IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_TUNNEL_NETWORK)
Vasyl Saienko4a2832d2024-05-16 09:00:03 +030021STORAGE_FRONTEND_NETWORK_NETMASK=$(echo ${STORAGE_FRONTEND_NETWORK} | cut -d'/' -f2)
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010022DOCKER_DEFAULT_ADDRESS_POOL=${DOCKER_DEFAULT_ADDRESS_POOL:-10.10.1.0/16}
23# DOCKER_DEFAULT_ADDRESS_SIZE have to be less then netmask in DOCKER_DEFAULT_ADDRESS_POOL because
24# to the fact that actual netmask for docker_gwbridge is given from it
25DOCKER_DEFAULT_ADDRESS_SIZE=${DOCKER_DEFAULT_ADDRESS_SIZE:-24}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +020026DOCKER_EE_RELEASE=${DOCKER_EE_RELEASE:-stable-19.03}
Vasyl Saienkofa714102024-03-22 09:30:25 +020027DOCKER_EE_PACKAGES=${DOCKER_EE_PACKAGES:-'docker-ee'}
Vasyl Saienkoa94bf182023-04-01 10:42:17 +000028BINARY_BASE_URL=${BINARY_BASE_URL:-"http://binary.mirantis.com"}
Vasyl Saienko7c595922021-03-17 13:58:13 +020029
30### COMMON FUNCTIONS ###
31function get_interface_prefix {
32 local interface=$1
33 local interface_ip
34 local interface_mask
35
36 interface_ip=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")
37 interface_mask=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)
38 echo "${interface_ip}/${interface_mask}"
39}
40### END COMMON FUNCTIONS ###
41
Oleh Hryhorovf4677af2021-04-26 18:00:15 +030042DEFAULT_INTERFACE=${DEFAULT_INTERFACE:-$(ip route show |awk '/default/ {print $5}')}
Vasyl Saienko7c595922021-03-17 13:58:13 +020043
Vasyl Saienko390c53a2021-03-18 12:45:11 +020044if [[ -n ${CONTROL_NETWORK_CIDR} ]]; then
Vasyl Saienkod3964202023-09-24 15:09:48 +000045 CONTROL_IP_ADDRESS=$(ip route |grep ${CONTROL_NETWORK_CIDR} | head -n1 | fgrep -v ' via ' | awk '/ src / {print $9}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020046else
47 CONTROL_IP_ADDRESS=$(get_interface_prefix ${DEFAULT_INTERFACE} | awk -F '/' '{print $1}')
Vasyl Saienkoe9677062021-03-19 13:31:32 +020048 CONTROL_NETWORK_CIDR=$(ip route show dev ${DEFAULT_INTERFACE} | awk '/kernel/ {print $1}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020049fi
50
51
Oleh Hryhorovb58460e2020-02-05 15:53:48 +020052PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010053UCP_USERNAME=${UCP_USERNAME:-admin}
54UCP_PASSWORD=${UCP_PASSWORD:-administrator}
55OS_CODENAME=$(lsb_release -c -s)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +020056NODE_DEPLOYMENT_RETRIES=${NODE_DEPLOYMENT_RETRIES:-15}
Vasyl Saienko7d7acea2020-01-03 16:59:05 +020057FLOATING_NETWORK_PREFIXES=${FLOATING_NETWORK_PREFIXES:-10.11.12.0/24}
Oleh Hryhorov404e9572020-01-21 18:57:22 +020058PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Vasyl Saienko0fa6f192020-03-06 16:08:51 +020059UCP_MASTER_HOST=${UCP_MASTER_HOST:-${CONTROL_IP_ADDRESS}}
60UCP_IP_ADDRESS=${UCP_IP_ADDRESS:-$CONTROL_IP_ADDRESS}
Vasyl Saienkocfbedc72021-02-15 18:02:56 +020061# Change default VXLAN port, need for https://mirantis.jira.com/browse/PRODX-11679
62UCP_DOCKER_SWARM_DATA_PORT=${UCP_DOCKER_SWARM_DATA_PORT:-4789}
Vasyl Saienko06c7afe2022-07-01 17:53:56 +000063UCP_DOCKER_CALICO_VXLAN_PORT=${UCP_DOCKER_CALICO_VXLAN_PORT:-${UCP_DOCKER_SWARM_DATA_PORT}}
Vasyl Saienko08166342020-02-28 12:20:51 +020064NTP_SERVERS=${NTP_SERVERS:-"ldap.scc.mirantis.net ldap.bud.mirantis.net"}
Michael Polenchuk486cadf2020-08-24 16:25:51 +040065IRONIC_BAREMETAL_VXLAN_INTERFACE='vxlan10'
Mykyta Karpin6a153c52020-11-03 15:21:38 +020066HUGE_PAGES_1G_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $1}')
67HUGE_PAGES_2Mb_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $2}')
68CONFIGURE_HUGE_PAGES=false
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020069TUNNEL_INTERFACE_NETPLAN_MANAGE=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040070TUNGSTENFABRIC_ENABLED=${TUNGSTENFABRIC_ENABLED:-false}
Mykyta Karpin6a153c52020-11-03 15:21:38 +020071if [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]] || [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
72 CONFIGURE_HUGE_PAGES=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040073 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == false ]]; then
74 TUNNEL_INTERFACE_NETPLAN_MANAGE=false
75 fi
Mykyta Karpin6a153c52020-11-03 15:21:38 +020076fi
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +020077SINGLE_NODE=${SINGLE_NODE:-true}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010078
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020079#FRR Options
80FRR_BGP_AS_NUMBER=${FRR_BGP_AS_NUMBER:-64512}
81FRR_ROUTER_ID=${TUNNEL_INTERFACE_IP}
Vasyl Saienko846c6342021-02-24 17:01:36 +020082FRR_BGP_SOURCE_ADDRESS=${TUNNEL_INTERFACE_IP}
83FRR_BGP_NEIGHBORS=${FRR_BGP_NEIGHBORS:-}
Vasyl Saienko5f953782021-02-25 10:30:32 +020084FRR_EVPN_TUNNELS_RANGE=${FRR_EVPN_TUNNELS_RANGE:-'20 50'}
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +020085FRR_EVPN_VXLAN_DST_PORT=${FRR_EVPN_VXLAN_DST_PORT:-4790}
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020086
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020087LVM_LOOP_DEVICE_SIZE=${LVM_LOOP_DEVICE_SIZE:-0}
Vasyl Saienkocd302062021-04-21 22:25:18 +030088CINDER_LVM_LOOP_DEVICE_SIZE=${CINDER_LVM_LOOP_DEVICE_SIZE:-0}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020089
Vasyl Saienko8d34ad02021-07-26 11:55:23 +030090_KUBECTL_CMD="kubectl --kubeconfig /etc/kubernetes/admin.conf "
91
Vasyl Saienkoa51d1ca2020-01-02 15:59:16 +020092function retry {
93 local retries=$1
94 shift
95 local msg="$1"
96 shift
97
98 local count=0
99 until "$@"; do
100 exit=$?
101 wait=$((2 ** $count))
102 count=$(($count + 1))
103 if [ $count -lt $retries ]; then
104 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
105 sleep $wait
106 else
107 echo "Retry $count/$retries exited $exit, no more retries left."
108 echo "$msg"
109 return $exit
110 fi
111 done
112 return 0
113}
114
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000115function install_pkgs {
116 local pkgs="$@"
117 function _retry_install {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300118 apt update && DEBIAN_FRONTEND=noninteractive apt install -o DPkg::Options::=--force-confdef -y $pkgs
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000119 }
120 retry 10 "Labeling node failed" _retry_install
121}
122
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200123function setup_bind_mounts {
124 mkdir -p /var/lib/persistant-data
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000125 for component in nova glance cinder docker; do
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000126 if [[ -d /var/lib/${component} ]]; then
127 continue
128 fi
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200129 mkdir -p /var/lib/data/${component} /var/lib/${component}
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000130 echo "/var/lib/data/${component} /var/lib/${component} none bind 0 0" >> /etc/fstab
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200131 done
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000132
133 if [[ "${KUBERNETES_INSTALLER}" == "k0s" ]]; then
134 mkdir -p /var/lib/k0s/kubelet
135 mkdir -p /var/lib/kubelet
136 echo "/var/lib/k0s/kubelet /var/lib/kubelet none bind 0 0" >> /etc/fstab
137 fi
138
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200139 mount -a
140}
141
Vasyl Saienko392dc712020-04-07 15:56:10 +0300142function configure_atop {
143 sed -i 's/INTERVAL=600/INTERVAL=60/' /usr/share/atop/atop.daily
144 systemctl restart atop
145}
146
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200147function install_required_packages {
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300148 # Disable unattended upgrades
149 sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
Oleh Hryhorovbe7bdf72021-04-05 18:21:22 +0300150 sed -i 's/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/g' /etc/apt/apt.conf.d/*
Mykyta Karpind6cde5e2022-07-05 12:06:06 +0300151 local pkg_list="apt-transport-https ca-certificates curl software-properties-common jq unzip atop iptables-persistent socat ntp rng-tools open-iscsi nfs-common"
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300152
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200153 if [[ "${CONFIGURE_HUGE_PAGES}" == true ]]; then
154 pkg_list="$pkg_list dpdk-igb-uio-dkms"
Anton Samoylovec019d52020-11-19 18:55:47 +0400155 # linux-image-extra-$(uname -r) provides uio_pci_generic module, used by TF DPDK as default driver
156 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
157 pkg_list="$pkg_list linux-modules-extra-$(uname -r)"
158 fi
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200159 fi
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000160 install_pkgs $pkg_list
161 # Since version 4 yq uses another syntax
Oleksandr Kononenko231933b2024-02-05 16:46:35 +0200162 if [[ ! -f /usr/bin/yq ]]; then
163 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/yq/yq-v3.3.2 -o /usr/bin/yq
164 chmod +x /usr/bin/yq
165 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200166}
167
Oleksii Grudev4399e232021-02-24 19:08:25 +0200168function enable_iscsi {
169 systemctl enable iscsid
170 systemctl restart iscsid
171}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200172
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200173function configure_lvm {
Vasyl Saienkocd302062021-04-21 22:25:18 +0300174 function _setup_lvm {
175 local device_num=$1
176 local device_size=$2
177 local vg_name=$3
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300178 if vgdisplay | grep ${vg_name}; then
179 echo "Volume group ${vg_name} is already initialize"
Deniscaa72dd2021-06-04 17:13:05 +0300180 else
181 #configure lvm only on compute nodes
182 if [[ ${NODE_METADATA} == *"openstack-compute-node"* ]]; then
183 truncate --size ${device_size}G /srv/disk${device_num}
184 cat <<EOF > /etc/systemd/system/setup-loopback-loop${device_num}.service
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200185[Unit]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300186Description=Setup loop${device_num} device
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200187DefaultDependencies=no
188After=systemd-udev-settle.service
189Before=lvm2-activation-early.service
190Wants=systemd-udev-settle.service
191
192[Service]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300193ExecStart=-/sbin/losetup /dev/loop${device_num} /srv/disk${device_num}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200194RemainAfterExit=true
195Type=oneshot
196
197[Install]
198WantedBy=local-fs.target
199EOF
Deniscaa72dd2021-06-04 17:13:05 +0300200 systemctl enable setup-loopback-loop${device_num}
201 systemctl start setup-loopback-loop${device_num}
202 #adding short sleep to give time for service to start
203 sleep 3
204 pvcreate /dev/loop${device_num}
205 vgcreate ${vg_name} /dev/loop${device_num}
206 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300207 fi
208 }
209
210 if [[ "${LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300211 _setup_lvm 10 ${LVM_LOOP_DEVICE_SIZE} nova-vol
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200212 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300213 if [[ "${CINDER_LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300214 _setup_lvm 11 ${CINDER_LVM_LOOP_DEVICE_SIZE} cinder-vol
Vasyl Saienkocd302062021-04-21 22:25:18 +0300215 fi
216
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200217}
218
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200219function install_docker {
220 function install_retry {
Vasyl Saienkofa714102024-03-22 09:30:25 +0200221 curl --retry 6 --retry-delay 5 -fsSL "${DOCKER_EE_URL}/gpg" | sudo apt-key add -
222 add-apt-repository "deb [arch=amd64] ${DOCKER_EE_URL}/ ${OS_CODENAME} ${DOCKER_EE_RELEASE}"
223 install_pkgs ${DOCKER_EE_PACKAGES}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300224 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
225 NERDCTL_VERSION=1.7.6
226 curl --retry 6 --retry-delay 5 -fsSL https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz --output nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz
227 tar zxvf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin
228 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200229 }
230 retry 10 "Failed to install docker" install_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100231}
232
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300233function prepare_docker_config {
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100234 mkdir -p /etc/docker
235 cat <<EOF > /etc/docker/daemon.json
236{
Vasyl Saienkoa37de442024-05-02 17:15:47 +0300237 "default-cgroupns-mode": "host",
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100238 "default-address-pools": [
239 { "base": "${DOCKER_DEFAULT_ADDRESS_POOL}", "size": ${DOCKER_DEFAULT_ADDRESS_SIZE} }
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300240 ],
241 "log-driver": "json-file",
242 "log-opts": {
243 "max-size": "1g",
244 "max-file": "3"
245 }
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100246}
247EOF
248
Vasyl Saienko92133c02024-05-16 10:18:51 +0300249 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
250 mkdir -p /etc/containerd/
251cat <<EOF> /etc/containerd/config.toml
252disabled_plugins = []
253
254#root = "/var/lib/containerd"
255#state = "/run/containerd"
256#subreaper = true
257#oom_score = 0
258
259#[grpc]
260# address = "/run/containerd/containerd.sock"
261# uid = 0
262# gid = 0
263
264#[debug]
265# address = "/run/containerd/debug.sock"
266# uid = 0
267# gid = 0
268# level = "info"
269EOF
270 fi
271
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100272}
273
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300274function pull_image {
275 local image="$1"
276 function pull_image_retry {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300277 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
278 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io pull $image
279 fi
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300280 docker pull ${image}
281 }
282 retry 10 "Can't pool docker image $image" pull_image_retry
283}
284
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300285function cache_images {
286
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300287 # Cache UCP image with retries
288 pull_image "${DOCKER_UCP_IMAGE}"
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300289
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300290 local image_prefix=${DOCKER_UCP_IMAGE%/*}
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200291 for image in $(docker container run --rm --name ucp -v /var/run/docker.sock:/var/run/docker.sock ${DOCKER_UCP_IMAGE} images --list); do
292 pull_image "${image_prefix}/${image##*/}"
Vasyl Saienko92133c02024-05-16 10:18:51 +0300293 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
294 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io tag ${image_prefix}/${image##*/} mirantis/${image##*/}
295 fi
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200296 docker tag ${image_prefix}/${image##*/} mirantis/${image##*/}
297 done
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300298}
299
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100300function install_ucp {
Deniscaa72dd2021-06-04 17:13:05 +0300301 if docker ps --all | grep ucp-controller; then
302 echo "Docker UCP container is running"
303 else
304 local tmpd
305 tmpd=$(mktemp -d)
306 function docker_run_retry {
307 docker container run --rm --name ucp \
308 -v /var/run/docker.sock:/var/run/docker.sock \
309 ${DOCKER_UCP_IMAGE} install \
310 --host-address $UCP_IP_ADDRESS \
311 --admin-username $UCP_USERNAME \
312 --admin-password $UCP_PASSWORD \
313 --existing-config
314 }
315 retry 10 "Can't bring up docker UCP container" docker_run_retry
316 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100317}
318
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300319function download_k0s {
320 curl -sSLf https://get.k0s.sh | sudo sh
321}
322
323function install_k0s {
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000324 mkdir -p /etc/k0s
325 k0s config create > /etc/k0s/k0s.yaml
326 k0s install controller -c /etc/k0s/k0s.yaml
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300327 k0s start
328
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000329 sleep 60
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300330
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200331 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300332 k0s kubeconfig admin > /etc/kubernetes/admin.conf
333 cp /etc/kubernetes/admin.conf /root/.kube/config
334
335 mkdir -p /etc/k0s
336 k0s kubeconfig admin > /etc/k0s/admin.conf
337 k0s token create --role=worker > /etc/k0s/worker_token.yaml
338 k0s token create --role=controller > /etc/k0s/controller_token.yaml
339
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000340 install_pkgs nginx
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300341 rm -f /etc/nginx/sites-enabled/default
342 ln -s /etc/k0s/ /var/www/k0s
343 cat << EOF > /etc/nginx/sites-enabled/k0s
344server {
345 listen *:80;
346 root /var/www;
347
348 location /k0s {
349 autoindex on;
350 }
351}
352EOF
353 systemctl restart nginx
354}
355
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200356function get_authtoken_retry {
357# Download the bundle https://docs.docker.com/ee/ucp/user-access/cli/
358# Create an environment variable with the user security token
359 AUTHTOKEN=$(curl --retry 6 --retry-delay 5 -sk -d '{"username":"'$UCP_USERNAME'","password":"'$UCP_PASSWORD'"}' https://${UCP_MASTER_HOST}/auth/login | jq -r .auth_token)
360 if [ -z ${AUTHTOKEN} ]; then
361 return -1
362 fi
363}
364
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100365function download_bundles {
366 local tmpd
367 tmpd=$(mktemp -d)
Oleh Hryhorova0754872020-01-24 16:45:05 +0200368
369 function download_bundles_retry {
370 # Download the client certificate bundle
Vasyl Saienkof3dba8c2020-02-14 10:07:23 +0200371 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" https://${UCP_MASTER_HOST}/api/clientbundle -o ${tmpd}/bundle.zip
Oleh Hryhorova0754872020-01-24 16:45:05 +0200372 }
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200373
Vasyl Saienko9b688992020-02-13 13:50:53 +0200374 retry 10 "Can't get AUTHTOKEN from master." get_authtoken_retry
375 retry 10 "Can't download bundle file from master." download_bundles_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100376
377 pushd $tmpd
378 # Unzip the bundle.
379 unzip bundle.zip
380
381 # Run the utility script.
382 eval "$(<env.sh)"
Vasyl Saienkof9f668c2020-01-04 11:36:35 +0200383 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko5b379422020-01-04 12:24:08 +0200384 cp kube.yml /etc/kubernetes/admin.conf
385 cp kube.yml /root/.kube/config
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100386 popd
387}
388
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200389function wait_for_node {
390 function retry_wait {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300391 ${_KUBECTL_CMD} get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200392 }
393 retry $NODE_DEPLOYMENT_RETRIES "The node didn't come up." retry_wait
394}
395
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300396function download_k8s_metadata {
397 mkdir -p /etc/k0s/
398 mkdir -p /etc/kubernetes
399 for f in worker_token.yaml controller_token.yaml admin.conf; do
400 curl --retry 6 --retry-delay 5 -L http://${UCP_MASTER_HOST}/k0s/${f} -o /etc/k0s/${f}
401 done
402 cp /etc/k0s/admin.conf /etc/kubernetes/admin.conf
403 mkdir -p /root/.kube/
404 cp /etc/k0s/admin.conf /root/.kube/config
405}
406
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100407function join_node {
Deniscaa72dd2021-06-04 17:13:05 +0300408 if kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname); then
409 echo "This node is already join"
410 else
411 local type=${1}
412 function retry_join_node {
413 env -i $(docker swarm join-token $type |grep 'docker swarm join' | xargs)
Mykyta Karpine2d2fda2023-11-23 10:41:51 +0200414 sleep 20 && systemctl restart docker && sleep 20
Deniscaa72dd2021-06-04 17:13:05 +0300415 }
416 retry 10 "Failed to join node to swarm" retry_join_node
417 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100418}
419
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300420function join_k0s_node {
421 k0s install worker --token-file /etc/k0s/worker_token.yaml
422 k0s start
423}
424
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100425function create_ucp_config {
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200426 if [[ "${SINGLE_NODE}" == true ]]; then
427 max_pods="kubelet_max_pods = 220"
428 fi
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400429 if [[ "${SECURE_OVERLAY_ENABLED,,}" == true ]]; then
430 secure_overlay="secure_overlay = true
431 calico_vxlan = false"
432 fi
Vasyl Saienko92133c02024-05-16 10:18:51 +0300433 kubelet_custom_flags=""
434 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
435 kubelet_custom_flags='custom_kubelet_flags=["--container-runtime-endpoint=unix:///run/containerd/containerd.sock"]'
436 fi
Deniscaa72dd2021-06-04 17:13:05 +0300437 if docker config ls | grep com.docker.ucp.config ; then
438 echo "Config com.docker.ucp.config already exists"
439 else
440 echo "
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200441[scheduling_configuration]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100442 enable_admin_ucp_scheduling = true
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200443 default_node_orchestrator = \"kubernetes\"
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200444[cluster_config]
445 dns = [\"172.18.208.44\"]
Vasyl Saienko06c7afe2022-07-01 17:53:56 +0000446 calico_vxlan_port = \"$UCP_DOCKER_CALICO_VXLAN_PORT\"
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200447 ${max_pods}
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400448 ${secure_overlay}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300449 ${kubelet_custom_flags}
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200450" | docker config create com.docker.ucp.config -
Deniscaa72dd2021-06-04 17:13:05 +0300451 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100452}
453
454function swarm_init {
Deniscaa72dd2021-06-04 17:13:05 +0300455 if docker node ls | grep $HOSTNAME; then
456 echo "This node is already part of a swarm"
457 else
458 docker swarm init --advertise-addr ${UCP_IP_ADDRESS} --data-path-addr ${UCP_IP_ADDRESS} --listen-addr ${UCP_IP_ADDRESS} --data-path-port ${UCP_DOCKER_SWARM_DATA_PORT}
Vasyl Saienko80b4c592023-10-13 14:24:00 +0000459 sleep 5 && systemctl restart docker && sleep 5
Deniscaa72dd2021-06-04 17:13:05 +0300460 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100461}
462
463function rm_ucp_config {
464 docker config rm com.docker.ucp.config
465}
466
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200467function install_kubectl {
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400468 local kubectl=/usr/local/bin/kubectl
Vasyl Saienkoa94bf182023-04-01 10:42:17 +0000469 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/kubectl/kubectl-${KUBECTL_VERSION}-linux -o ${kubectl}
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400470 chmod +x ${kubectl}
Ilya Menkov2d22bd02020-02-14 15:01:29 +0400471cat << EOF >> ~/.bashrc
472source /usr/share/bash-completion/bash_completion
473source <(kubectl completion bash)
474EOF
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200475}
476
Vasyl Saienko08166342020-02-28 12:20:51 +0200477function configure_ntp {
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200478 echo "" > /etc/ntp.conf
Vasyl Saienko08166342020-02-28 12:20:51 +0200479
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400480 for server in $NTP_SERVERS; do
481cat << EOF >> /etc/ntp.conf
482server ${server} iburst
483EOF
484 done
485
486 cat << EOF >> /etc/ntp.conf
487# Set general access to this service
488restrict -4 default kod nomodify notrap nopeer noquery
489restrict -6 default kod nomodify notrap nopeer noquery
490
491# Allow access from localhost
492restrict 127.0.0.1
493restrict ::1
494
495# Do not listen on any interface address by default
496interface ignore wildcard
497interface listen lo
498interface listen ${DEFAULT_INTERFACE}
499
500# Location of drift file
501driftfile /var/lib/ntp/ntp.drift
502EOF
503
ibumarskov35370042022-04-19 14:27:02 +0400504 systemctl disable systemd-timesyncd
505 systemctl enable ntp
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400506 systemctl restart ntp
Vasyl Saienko08166342020-02-28 12:20:51 +0200507}
508
Vasyl Saienko5af97c32022-01-25 09:45:45 +0200509function wait_for_external_network {
510 function _check_access {
511 curl --connect-timeout 10 ${DOCKER_UCP_IMAGE%%/*} || (sleep 1; /bin/false)
512 }
513 retry 180 "Failed to wait for external networks reachable." _check_access
514}
515
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200516function prepare_network {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200517 if [ -z "${CONTROL_IP_ADDRESS}" ]; then
Vasyl Saienko7c595922021-03-17 13:58:13 +0200518 wait_condition_send "FAILURE" "CONTROL_IP_ADDRESS is not found."
Dennis Dmitrievea894212020-03-12 21:36:43 +0200519 exit 1
520 fi
521
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200522 systemctl restart systemd-resolved
Vasyl Saienkoc14c43d2020-01-08 13:06:18 +0200523 # Make sure local hostname is present in /etc/hosts
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300524 sed -i "s/127.0.0.1 localhost/127.0.0.1 localhost\n${CONTROL_IP_ADDRESS} $(hostname -s).cluster.local $(hostname -s)/" /etc/hosts
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200525}
526
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200527function workaround_default_forward_policy {
Vasyl Saienkod67e35e2023-09-27 14:44:01 +0000528 local should_run=0
529 for net in $FLOATING_NETWORK_PREFIXES; do
530 if ! iptables -nvL DOCKER-USER |grep $net; then
531 should_run=1
532 break
533 fi
534 done
535 if [[ $should_run == 0 ]]; then
536 echo "Iptables is already configured"
Deniscaa72dd2021-06-04 17:13:05 +0300537 else
538 cat << EOF > /etc/iptables/rules.v4
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200539*filter
540:DOCKER-USER - [0:0]
541EOF
Deniscaa72dd2021-06-04 17:13:05 +0300542 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200543cat << EOF >> /etc/iptables/rules.v4
544-A DOCKER-USER -d ${net} -j ACCEPT
545-A DOCKER-USER -s ${net} -j ACCEPT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200546EOF
Deniscaa72dd2021-06-04 17:13:05 +0300547 done
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200548
549cat << EOF >> /etc/iptables/rules.v4
550-A DOCKER-USER -j RETURN
Vasyl Saienko1785c352020-03-11 11:39:40 +0200551COMMIT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200552EOF
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300553
554#Allow access to Internet from VMs for virtualized environment.
555cat << EOF >> /etc/iptables/rules.v4
556*nat
557:POSTROUTING ACCEPT - [0:0]
558EOF
Deniscaa72dd2021-06-04 17:13:05 +0300559 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300560cat << EOF >> /etc/iptables/rules.v4
561-A POSTROUTING -s ${net} -o ${DEFAULT_INTERFACE} -j MASQUERADE
562EOF
Deniscaa72dd2021-06-04 17:13:05 +0300563 done
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300564
565cat << EOF >> /etc/iptables/rules.v4
566COMMIT
567EOF
Deniscaa72dd2021-06-04 17:13:05 +0300568 sudo netfilter-persistent reload
569 fi
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200570}
571
Dennis Dmitrievea894212020-03-12 21:36:43 +0200572function disable_rp_filter {
573 # Run this func before "network_config" to create new interfaces with the default rp_filter value
574 cat << EOF > /etc/sysctl.d/99-disable-rp-filter.conf
575net.ipv4.conf.all.rp_filter=0
576net.ipv4.conf.default.rp_filter=0
577EOF
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300578 for iface in $(find /proc/sys/net/ipv4/conf/ -name "enp*" -o -name "ens*" -o -name "eth*"); do
579 echo 0 > $iface/rp_filter
Oleh Hryhorov207137d2021-04-13 12:03:39 +0300580 done
581
Dennis Dmitrievea894212020-03-12 21:36:43 +0200582 sysctl -p /etc/sysctl.d/99-disable-rp-filter.conf
583}
584
Vasyl Saienko83f309e2020-05-06 11:14:28 +0300585function configure_contrack {
586 cat << EOF > /etc/sysctl.d/100-contrackd.conf
587net.netfilter.nf_conntrack_log_invalid=255
588net.netfilter.nf_conntrack_tcp_be_liberal=1
589EOF
590
591 sysctl -p /etc/sysctl.d/100-contrackd.conf
592}
593
Vasyl Saienkob377de02020-05-25 14:51:31 +0300594# NOTE(vsaienko): disable calling iptables when sending packets via bridge.
595function disable_iptables_for_bridges {
596 cat << EOF > /etc/sysctl.d/101-bridge-nf-call-iptables.conf
597net.bridge.bridge-nf-call-ip6tables=0
598net.bridge.bridge-nf-call-iptables=0
599net.bridge.bridge-nf-call-arptables=0
600EOF
601
602 sysctl -p /etc/sysctl.d/101-bridge-nf-call-iptables.conf
603}
604
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200605function network_config {
Vasyl Saienko9b688992020-02-13 13:50:53 +0200606 PUBLIC_NODE_IP_ADDRESS=${PUBLIC_INTERFACE_IP:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")}
607 PUBLIC_NODE_IP_NETMASK=${PUBLIC_INTERFACE_NETMASK:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)}
608
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200609 local public_interface=${1:-${PUBLIC_INTERFACE}}
610 local cloud_netplan_cfg="/etc/netplan/50-cloud-init.yaml"
611 local match_ip_line
Vasyl Saienko9b688992020-02-13 13:50:53 +0200612
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000613 install_pkgs bridge-utils
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200614
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200615cat << EOF > /etc/systemd/network/10-veth-phy-br.netdev
616[NetDev]
617Name=veth-phy
618Kind=veth
619[Peer]
620Name=veth-br
621EOF
Vasyl Saienko9b688992020-02-13 13:50:53 +0200622
Vasyl Saienko66522982020-05-25 09:34:43 +0300623if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
624
Vasyl Saienko4c468192020-05-19 11:51:13 +0300625cat << EOF > /etc/systemd/network/11-veth-bm.netdev
626[NetDev]
627Name=veth-bm
628Kind=veth
629[Peer]
630Name=veth-bm-br
631EOF
632
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300633cat << EOF > /etc/systemd/network/12-veth-vbmc.netdev
634[NetDev]
635Name=veth-vbmc
636Kind=veth
637[Peer]
638Name=veth-vbmc-br
639EOF
Vasyl Saienko66522982020-05-25 09:34:43 +0300640fi
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300641
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200642 sed -i 's/.*ethernets:.*/&\n veth-phy: {}/' ${cloud_netplan_cfg}
643 sed -i 's/.*ethernets:.*/&\n veth-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko66522982020-05-25 09:34:43 +0300644if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
Vasyl Saienko4c468192020-05-19 11:51:13 +0300645 sed -i 's/.*ethernets:.*/&\n veth-bm: {}/' ${cloud_netplan_cfg}
646 sed -i 's/.*ethernets:.*/&\n veth-bm-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300647 sed -i 's/.*ethernets:.*/&\n veth-vbmc: {}/' ${cloud_netplan_cfg}
648 sed -i 's/.*ethernets:.*/&\n veth-vbmc-br: {}/' ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200649
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400650 if [[ "${IRONIC_MT_ENABLED,,}" == true ]] ; then
651 sed -i "s/.*ethernets:.*/&\n ${IRONIC_BAREMETAL_VXLAN_INTERFACE}: {}/" ${cloud_netplan_cfg}
652 sed -i 's/.*ethernets:.*/&\n veth-pxe: {}/' ${cloud_netplan_cfg}
653 sed -i 's/.*ethernets:.*/&\n veth-pxe-br: {}/' ${cloud_netplan_cfg}
654cat << EOF > /etc/systemd/network/13-veth-pxe.netdev
655[NetDev]
656Name=veth-pxe
657Kind=veth
658[Peer]
659Name=veth-pxe-br
660EOF
661 sed -i "s/- ${IRONIC_BAREMETAL_NETWORK_PREFIX}\([0-9]*\)/- ${IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX}\1/" ${cloud_netplan_cfg}
662cat << EOF >> ${cloud_netplan_cfg}
663 vlans:
664 pxe.1000:
665 id: 1000
666 link: veth-pxe
667 addresses:
668 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
669EOF
670 else
671 ironic_baremetal_address_match_ip_line=$(grep -nm1 "${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
672 sed -i "$((${ironic_baremetal_address_match_ip_line}-1)),$((${ironic_baremetal_address_match_ip_line}))d" ${cloud_netplan_cfg}
673 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300674fi
Vasyl Saienko4c468192020-05-19 11:51:13 +0300675
Vasyl Saienko66522982020-05-25 09:34:43 +0300676 public_address_match_ip_line=$(grep -nm1 "${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
Denis20740c22021-06-17 18:09:46 +0300677 if [ -n "${public_address_match_ip_line}" ] ; then
678 sed -i "$((${public_address_match_ip_line}-1)),$((${public_address_match_ip_line}))d" ${cloud_netplan_cfg}
679 fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200680
681cat << EOF >> ${cloud_netplan_cfg}
682 bridges:
683 br-public:
684 dhcp4: false
685 interfaces:
686 - ${PUBLIC_INTERFACE}
687 - veth-br
688 addresses:
689 - ${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko66522982020-05-25 09:34:43 +0300690EOF
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000691# Assign more ips for neutron dynamic routing PRODX-31417
692if [[ ${NODE_METADATA} == *"tempest"* ]]; then
693 for i in {71..76}; do
694cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienkoc7639002023-04-11 07:40:29 +0000695 - ${PUBLIC_NODE_IP_ADDRESS%.*}.${i}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000696EOF
697 done
698fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300699
Vasyl Saienkof79d0942020-11-17 14:33:55 +0200700# Remove Tunnel interface from netplan
701if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
702 sed -i "/ ${TUNNEL_INTERFACE}/,/ set-name: ${TUNNEL_INTERFACE}/d" ${cloud_netplan_cfg}
703fi
704
Vasyl Saienko66522982020-05-25 09:34:43 +0300705if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
706cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300707 br-baremetal:
708 dhcp4: false
709 interfaces:
Vasyl Saienko4c468192020-05-19 11:51:13 +0300710 - veth-bm-br
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300711 - veth-vbmc-br
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400712EOF
713 if [[ "${IRONIC_MT_ENABLED,,}" != true ]] ; then
714cat << EOF >> ${cloud_netplan_cfg}
715 - ${IRONIC_BAREMETAL_INTERFACE}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300716 addresses:
717 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200718EOF
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400719 else
720cat << EOF >> ${cloud_netplan_cfg}
721 - ${IRONIC_BAREMETAL_VXLAN_INTERFACE}
722 - veth-pxe-br
723EOF
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200724
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400725cat << EOF > /etc/systemd/system/ironic-vxlan-tunnel.service
726[Unit]
727Description=Ironic VXLAN tunnel
728After=network.target
729
730[Service]
731Type=oneshot
732RemainAfterExit=true
733ExecStart=/sbin/ip link add ${IRONIC_BAREMETAL_VXLAN_INTERFACE} type vxlan id 10 group 239.1.1.10 dstport 0 dev ${IRONIC_BAREMETAL_INTERFACE}
734ExecStart=/sbin/ip link set ${IRONIC_BAREMETAL_VXLAN_INTERFACE} up
735
736[Install]
737WantedBy=multi-user.target
738EOF
739
740 systemctl enable ironic-vxlan-tunnel.service
741 systemctl start ironic-vxlan-tunnel.service
742 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300743fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200744 netplan --debug apply
Vasyl Saienko9b688992020-02-13 13:50:53 +0200745
746 # NOTE(vsaienko): give some time to apply changes
747 sleep 15
Vasyl Saienko16c81c02020-11-17 17:10:55 +0200748
749# Remove Tunnel interface from netplan
750if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
751 ip addr flush ${TUNNEL_INTERFACE}
752 ip link set ${TUNNEL_INTERFACE} up
753fi
754
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200755}
756
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200757$functions_override
758
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200759function set_node_labels {
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200760 function set_node_labels_retry {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300761 ${_KUBECTL_CMD} patch node $(hostname) -p "{\"metadata\": $(echo $NODE_METADATA | jq -c ".")}"
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200762 }
Vasyl Saienko08024582020-02-20 11:45:34 +0200763 retry 10 "Labeling node failed" set_node_labels_retry
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200764}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100765
Vasyl Saienko1eb06d62020-11-16 10:16:22 +0200766HW_METADATA='{}'
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200767function prepare_metadata_files {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200768 /usr/sbin/prepare-metadata.py --metadata-file /usr/share/metadata/lab-metadata.yaml
769}
770
771function collect_ceph_metadata {
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200772 local ceph_osd_node
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300773 ceph_osd_node=$(${_KUBECTL_CMD} get nodes -l role=ceph-osd-node -o jsonpath={.items[?\(@.metadata.name==\"$(hostname)\"\)].metadata.name})
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200774
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200775 if [[ -f /usr/share/metadata/ceph.yaml && ${ceph_osd_node} ]]; then
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200776 HW_METADATA="{\"ceph\": {\"$(hostname)\": \"$(base64 -w 0 /usr/share/metadata/ceph.yaml)\"}}"
Oleh Hryhorov39fbfe52020-03-12 15:26:53 +0200777 ceph_store_drive=$(cat /usr/share/metadata/ceph.yaml | egrep '\- name\: vd?' | awk '{print $3}')
778 if [[ -b /dev/${ceph_store_drive} ]]; then
779 sgdisk --zap-all /dev/${ceph_store_drive}
780 fi
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200781 fi
782}
783
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200784function collect_interfaces_metadata {
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200785
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200786 local if_metadata_file="/usr/share/metadata/interfaces.yaml"
Denisd2635bf2021-06-15 13:35:36 +0300787 if [ -z "${TUNNEL_INTERFACE}" ] ; then
788 echo "Tunnel interface is empty"
789 else
790 pci_id=$(ethtool -i ${TUNNEL_INTERFACE} |grep bus-info | awk '{print $2}')
791 mac=$(cat $(find /sys/bus/pci/devices/${pci_id}/ -name net)/${TUNNEL_INTERFACE}/address)
792 ip=${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK}
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200793
Denisd2635bf2021-06-15 13:35:36 +0300794 cat << EOF > ${if_metadata_file}
Vasyl Saienko592730c2020-11-17 11:36:46 +0200795${TUNNEL_INTERFACE}:
Vasyl Saienko1152e982020-11-16 16:23:32 +0200796 pci_id: "${pci_id}"
797 ip_addr: "${ip}"
798 mac: "${mac}"
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200799EOF
800
Denisd2635bf2021-06-15 13:35:36 +0300801 fi
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200802 HW_METADATA=$(echo ${HW_METADATA} | jq -cr ". += {\"interfaces\": {\"$(hostname)\": \"$(base64 -w 0 ${if_metadata_file})\"}}")
803
804}
805
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300806function nested_virt_config {
807
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300808if grep --color vmx /proc/cpuinfo; then
809 cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300810options kvm_intel nested=1
811options kvm_intel enable_apicv=1
812options kvm_intel ept=1
813EOF
814
815 modprobe -r kvm_intel || true
816 modprobe kvm_intel nested=1
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300817else
818 echo "Nested virtualization is not supported"
819fi
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300820
821}
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200822
Mykyta Karpin6a153c52020-11-03 15:21:38 +0200823function configure_huge_pages {
824 local nr_2Mb
825 local nr_1G
826 local apply_grub=false
827
828 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=2M.*") ]] && [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
829 nr_2Mb=$(( "${HUGE_PAGES_2Mb_SIZE}" / 2 ))
830 echo "Configure GRUB kernel cmd to allocate ${nr_2Mb} 2Mb huge pages"
831 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=2M hugepages=$nr_2Mb\"" >> /etc/default/grub
832 apply_grub=true
833 fi
834
835 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=1G.*") ]] && [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]]; then
836 echo "Configure GRUB kernel cmd to allocate ${HUGE_PAGES_1G_SIZE} 1G huge pages"
837 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=1G hugepages=${HUGE_PAGES_1G_SIZE}\"" >> /etc/default/grub
838 apply_grub=true
839 fi
840
841 if [[ "${apply_grub}" == true ]]; then
842 update-grub
843 cloud-init clean -r
844 else
845 echo "GRUB already configured with huge pages"
846 fi
847}
848
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200849function disable_master_taint {
850
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200851 function disable_master_taint_retry {
852 # Enable "Allow all authenticated users, including service accounts to schedule on all nodes, including UCP managers."
853 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" -k -X PUT https://${UCP_MASTER_HOST}/collectionGrants/authenticated/swarm/scheduler
854 }
855
856 retry 10 "Can't enable permission to schedule all pods on UCP master." disable_master_taint_retry
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200857
858}
859
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200860function disable_ipv6 {
861 cat << EOF > /etc/sysctl.d/11-ipv6-disable.conf
862net.ipv6.conf.all.disable_ipv6 = 1
863net.ipv6.conf.default.disable_ipv6 = 1
864net.ipv6.conf.lo.disable_ipv6 = 1
865EOF
866 sysctl -p /etc/sysctl.d/11-ipv6-disable.conf
867}
868
Vasyl Saienko846c6342021-02-24 17:01:36 +0200869function setup_evpn {
870
871cat << EOF > /etc/netplan/60-evpn.yaml
872network:
873 version: 2
874 ethernets:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200875EOF
876for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
877cat << EOF >> /etc/netplan/60-evpn.yaml
878 vxlan$vni: {}
879EOF
880done
881cat << EOF >> /etc/netplan/60-evpn.yaml
Vasyl Saienko846c6342021-02-24 17:01:36 +0200882 bridges:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200883EOF
884for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
885cat << EOF >> /etc/netplan/60-evpn.yaml
886 br$vni:
Vasyl Saienko846c6342021-02-24 17:01:36 +0200887 dhcp4: false
Vasyl Saienkof18b4782021-03-03 14:15:40 +0200888 link-local: []
Vasyl Saienko846c6342021-02-24 17:01:36 +0200889 parameters:
890 stp: false
Vasyl Saienko5f953782021-02-25 10:30:32 +0200891 interfaces:
892 - vxlan$vni
Vasyl Saienko846c6342021-02-24 17:01:36 +0200893 addresses:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200894 - 192.168.$vni.254/24
Vasyl Saienko846c6342021-02-24 17:01:36 +0200895EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200896done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200897
Vasyl Saienko5f953782021-02-25 10:30:32 +0200898for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
899cat << EOF > /etc/systemd/system/evpn-vxlan-tunnel-${vni}.service
Vasyl Saienko846c6342021-02-24 17:01:36 +0200900[Unit]
Vasyl Saienko5f953782021-02-25 10:30:32 +0200901Description=EVPN tunnel ${vni}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200902After=network.target
903
904[Service]
905Type=oneshot
906RemainAfterExit=true
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200907ExecStart=/sbin/ip link add vxlan${vni} type vxlan id ${vni} local ${TUNNEL_INTERFACE_IP} nolearning dstport ${FRR_EVPN_VXLAN_DST_PORT}
Vasyl Saienko5f953782021-02-25 10:30:32 +0200908ExecStart=/sbin/ip link set vxlan${vni} up
Vasyl Saienko846c6342021-02-24 17:01:36 +0200909
910[Install]
911WantedBy=multi-user.target
912EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200913 systemctl enable evpn-vxlan-tunnel-${vni}.service
914 systemctl start evpn-vxlan-tunnel-${vni}.service
915done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200916
917 netplan --debug apply
918 # NOTE(vsaienko): give some time to apply changes
919 sleep 15
Vasyl Saienko846c6342021-02-24 17:01:36 +0200920}
921
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200922function install_frr {
923 function _install_frr_packages {
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200924 FRRVER="frr-stable"
Oleh Hryhorov6243c9f2023-09-21 09:53:09 +0000925 curl -s https://mirror.mirantis.com/.snapshots/frr-stable-latest/keys.asc | sudo apt-key add -
Oleh Hryhorov2ae1db82023-09-21 07:39:50 +0000926 add-apt-repository "deb [arch=amd64] https://mirror.mirantis.com/.snapshots/frr-stable-latest/ ${OS_CODENAME} ${FRRVER}"
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000927 install_pkgs frr frr-pythontools
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200928 }
929 retry 3 "Can't install frr packages" _install_frr_packages
930
931 sed -i 's/^bgpd=no/bgpd=yes/g' /etc/frr/daemons
932
933 cat << EOF > /etc/frr/frr.conf
934log syslog informational
935no ipv6 forwarding
936service integrated-vtysh-config
937!
938router bgp ${FRR_BGP_AS_NUMBER}
939 bgp router-id ${FRR_ROUTER_ID}
940 no bgp default ipv4-unicast
941 bgp cluster-id ${FRR_ROUTER_ID}
942 coalesce-time 1000
Vasyl Saienko846c6342021-02-24 17:01:36 +0200943 neighbor rr peer-group
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200944 neighbor rr capability extended-nexthop
945 neighbor rr remote-as ${FRR_BGP_AS_NUMBER}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200946 neighbor rr update-source ${FRR_BGP_SOURCE_ADDRESS}
947EOF
948
949for neighbor_ip in $(echo $FRR_BGP_NEIGHBORS | tr "," "\n"); do
950 cat << EOF >> /etc/frr/frr.conf
951 neighbor $neighbor_ip peer-group rr
952EOF
953
954done
955 cat << EOF >> /etc/frr/frr.conf
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200956 !
957 address-family ipv4 unicast
Vasyl Saienko846c6342021-02-24 17:01:36 +0200958 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200959 exit-address-family
960 !
961 address-family ipv4 vpn
Vasyl Saienko846c6342021-02-24 17:01:36 +0200962 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200963 exit-address-family
964 !
965 address-family l2vpn evpn
Vasyl Saienko846c6342021-02-24 17:01:36 +0200966 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200967 advertise-all-vni
968 advertise-default-gw
969 exit-address-family
970!
971line vty
972!
973EOF
974
975 systemctl restart frr
Vasyl Saienko846c6342021-02-24 17:01:36 +0200976
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200977 disable_ipv6
Vasyl Saienko846c6342021-02-24 17:01:36 +0200978 setup_evpn
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200979}
980
Mykyta Karpinbac78b42021-12-07 11:25:03 +0200981function cron_disable_calico_offloading {
Denis Biletskiy49b91ea2022-06-09 11:41:39 +0200982 # The workaround is to disable offloading on vxlan interface
983 if [ -f /etc/cron.d/disable_calico_offloading ]; then
984 echo "Cronjob for disable callico offloading already exists"
985 else
986 script_path="/usr/sbin/calico_disable_offloading.sh"
987 cat << 'EOF' >> $script_path
988#!/usr/bin/env bash
989if /sbin/ethtool --show-offload vxlan.calico | grep -E '^.x-checksumming:' | grep -q ': on'; then
990 /sbin/ethtool --offload vxlan.calico rx off tx off > /dev/null
991fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +0200992EOF
Denis Biletskiy49b91ea2022-06-09 11:41:39 +0200993 chmod +x $script_path
994 cat << EOF >> /etc/cron.d/disable_calico_offloading
995* * * * * root $script_path 2>&1 | /usr/bin/logger -t calico_disable_offloading
996EOF
997 fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +0200998}
999
Vasyl Saienko114333f2022-05-24 09:34:51 +00001000function increase_iscsi_timeout {
1001 cat <<EOF > /etc/udev/rules.d/50-iscsi-timeout.rules
1002ACTION=="add", SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14",
1003RUN+="/bin/sh -c 'echo 120 > /sys/block/%k/device/timeout'"
1004EOF
1005 udevadm control --reload-rules && udevadm trigger
1006}