|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2024 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# hack script for preparing libvirt to run cluster-api-provider-openstack e2e |
| 18 | + |
| 19 | +set -x -o errexit -o nounset -o pipefail |
| 20 | + |
| 21 | +# Required environment variables: |
| 22 | +# SSH_PUBLIC_KEY_FILE |
| 23 | +# SSH_PRIVATE_KEY_FILE |
| 24 | +# LIBVIRT_NETWORK_NAME |
| 25 | + |
| 26 | +function cloud_init { |
| 27 | + LIBVIRT_NETWORK_NAME=${LIBVIRT_NETWORK_NAME:-${CLUSTER_NAME}-network} |
| 28 | + LIBVIRT_IMAGE_NAME=${LIBVIRT_IMAGE_NAME:-ubuntu-2404-lts} |
| 29 | + |
| 30 | + LIBVIRT_MEMORY=${LIBVIRT_MEMORY:-8192} |
| 31 | + LIBVIRT_MEMORY_controller=${LIBVIRT_MEMORY_controller:-$LIBVIRT_MEMORY} |
| 32 | + LIBVIRT_MEMORY_worker=${LIBVIRT_MEMORY_worker:-$LIBVIRT_MEMORY} |
| 33 | + |
| 34 | + LIBVIRT_VCPU=${LIBVIRT_VCPU:-4} |
| 35 | + LIBVIRT_VCPU_controller=${LIBVIRT_VCPU_controller:-$LIBVIRT_VCPU} |
| 36 | + LIBVIRT_VCPU_worker=${LIBVIRT_VCPU_worker:-$LIBVIRT_VCPU} |
| 37 | + |
| 38 | + LIBVIRT_MAC_controller="00:60:2f:32:81:00" |
| 39 | + LIBVIRT_MAC_worker="00:60:2f:32:81:01" |
| 40 | +} |
| 41 | + |
| 42 | +function init_infrastructure() { |
| 43 | + if ! virsh net-info "${LIBVIRT_NETWORK_NAME}" &>/dev/null; then |
| 44 | + virsh net-define <(cat <<EOF |
| 45 | +<network> |
| 46 | + <name>${LIBVIRT_NETWORK_NAME}</name> |
| 47 | + <forward mode='nat'> |
| 48 | + <nat> |
| 49 | + <port start='1024' end='65535'/> |
| 50 | + </nat> |
| 51 | + </forward> |
| 52 | + <bridge name="capobr0" stp="on" delay="0"/> |
| 53 | + <ip address="${PRIVATE_NETWORK_CIDR%/*}" netmask="255.255.255.0"> |
| 54 | + <dhcp> |
| 55 | + <range start="${PRIVATE_NETWORK_CIDR%.*}.10" end="${PRIVATE_NETWORK_CIDR%.*}.199"/> |
| 56 | + <host mac="${LIBVIRT_MAC_controller}" name='controller' ip="${CONTROLLER_IP}"/> |
| 57 | + <host mac="${LIBVIRT_MAC_worker}" name='worker' ip="${WORKER_IP}"/> |
| 58 | + </dhcp> |
| 59 | + </ip> |
| 60 | +</network> |
| 61 | +EOF |
| 62 | +) |
| 63 | + virsh net-start "${LIBVIRT_NETWORK_NAME}" |
| 64 | + virsh net-autostart "${LIBVIRT_NETWORK_NAME}" |
| 65 | + fi |
| 66 | + |
| 67 | + if [ ! -f "/tmp/${LIBVIRT_IMAGE_NAME}.qcow2" ]; then |
| 68 | + curl -o "/tmp/${LIBVIRT_IMAGE_NAME}.qcow2" https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-24.04-server-cloudimg-amd64.img |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +function create_vm { |
| 73 | + local name=$1 && shift |
| 74 | + local ip=$1 && shift |
| 75 | + local userdata=$1 && shift |
| 76 | + local public=$1 && shift |
| 77 | + |
| 78 | + local memory=LIBVIRT_MEMORY_${name} |
| 79 | + memory=${!memory} |
| 80 | + local vcpu=LIBVIRT_VCPU_${name} |
| 81 | + vcpu=${!vcpu} |
| 82 | + local servername="${CLUSTER_NAME}-${name}" |
| 83 | + local mac=LIBVIRT_MAC_${name} |
| 84 | + mac=${!mac} |
| 85 | + |
| 86 | + # Values which weren't initialised if we skipped init_infrastructure. Use names instead. |
| 87 | + networkid=${networkid:-${LIBVIRT_NETWORK_NAME}} |
| 88 | + volumeid=${volumeid:-${LIBVIRT_IMAGE_NAME}_${name}.qcow2} |
| 89 | + |
| 90 | + sudo cp "/tmp/${LIBVIRT_IMAGE_NAME}.qcow2" "/var/lib/libvirt/images/${volumeid}" |
| 91 | + sudo qemu-img resize "/var/lib/libvirt/images/${volumeid}" +200G |
| 92 | + |
| 93 | + local serverid |
| 94 | + local serverid |
| 95 | + if ! virsh dominfo "${servername}" &>/dev/null; then |
| 96 | + sudo virt-install \ |
| 97 | + --name "${servername}" \ |
| 98 | + --memory "${memory}" \ |
| 99 | + --vcpus "${vcpu}" \ |
| 100 | + --import \ |
| 101 | + --disk "/var/lib/libvirt/images/${volumeid},format=qcow2,bus=virtio" \ |
| 102 | + --network network="${networkid}",mac="${mac}" \ |
| 103 | + --os-variant=ubuntu22.04 \ |
| 104 | + --graphics none \ |
| 105 | + --cloud-init user-data="${userdata}" \ |
| 106 | + --noautoconsole |
| 107 | + fi |
| 108 | +} |
| 109 | + |
| 110 | +function get_public_ip { |
| 111 | + echo "${CONTROLLER_IP}" |
| 112 | +} |
| 113 | + |
| 114 | +function get_mtu { |
| 115 | + # Set MTU statically for libvirt |
| 116 | + echo 1500 |
| 117 | +} |
| 118 | + |
| 119 | +function get_ssh_public_key_file { |
| 120 | + echo "${SSH_PUBLIC_KEY_FILE}" |
| 121 | +} |
| 122 | + |
| 123 | +function get_ssh_private_key_file { |
| 124 | + # Allow this to be unbound. This is handled in create_devstack.sh |
| 125 | + echo "${SSH_PRIVATE_KEY_FILE:-}" |
| 126 | +} |
| 127 | + |
| 128 | +function cloud_cleanup { |
| 129 | + for serverid in $(virsh list --all --name | grep -E "${CLUSTER_NAME}-controller|${CLUSTER_NAME}-worker"); do |
| 130 | + virsh destroy "${serverid}" |
| 131 | + virsh undefine "${serverid}" --remove-all-storage |
| 132 | + done |
| 133 | + |
| 134 | + for networkid in $(virsh net-list --name | grep -E "${CLUSTER_NAME}"); do |
| 135 | + virsh net-destroy "${networkid}" |
| 136 | + virsh net-undefine "${networkid}" |
| 137 | + done |
| 138 | + |
| 139 | + true |
| 140 | +} |
0 commit comments