Skip to content

Commit 3866159

Browse files
lentzi90k8s-infra-cherrypick-robot
authored andcommitted
Add libvirt resource type for create_devstack.sh
This makes it possible to set up a local devstack on libvirt. It is probably not useful for running the full e2e suite (unless you have a very beefy PC), but it is enough for development with Tilt. Signed-off-by: Lennart Jern <lennart.jern@est.tech>
1 parent 25b9986 commit 3866159

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

hack/ci/create_devstack.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ PRIVATE_NETWORK_CIDR=${PRIVATE_NETWORK_CIDR:-"10.0.3.0/24"}
4747
CONTROLLER_IP=${CONTROLLER_IP:-"10.0.3.15"}
4848
WORKER_IP=${WORKER_IP:-"10.0.3.16"}
4949

50+
SKIP_INIT_INFRA=${SKIP_INIT_INFRA:-}
51+
SKIP_SECONDARY_AZ=${SKIP_SECONDARY_AZ:-}
52+
5053
PRIMARY_AZ=testaz1
5154
SECONDARY_AZ=testaz2
5255

@@ -273,7 +276,11 @@ function main() {
273276
# is available, and wait if it is not.
274277
#
275278
# For efficiency, tests which require multi-AZ SHOULD run as late as possible.
276-
create_worker
279+
if [[ -n "${SKIP_SECONDARY_AZ:-}" ]]; then
280+
echo "Skipping worker creation..."
281+
else
282+
create_worker
283+
fi
277284

278285
public_ip=$(get_public_ip)
279286
cat << EOF > "${REPO_ROOT_ABSOLUTE}/clouds.yaml"

hack/ci/libvirt.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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-2204-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/jammy/release/ubuntu-22.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

Comments
 (0)