From 1e94c19ac43034f10c4f9cbf0d543f77dfe2811a Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Mon, 17 Jul 2023 11:03:49 +0200 Subject: [PATCH 1/2] e2e-setup: Add support for deploying CDI Signed-off-by: Felix Matouschek --- hack/e2e-setup.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hack/e2e-setup.sh b/hack/e2e-setup.sh index 8ba7000..9e62edc 100755 --- a/hack/e2e-setup.sh +++ b/hack/e2e-setup.sh @@ -29,6 +29,7 @@ set_default_params() { KUBECTL_VERSION=${KUBECTL_VERSION:-v1.27.3} KUBEVIRT_VERSION=${KUBEVIRT_VERSION:-v1.0.0} + KUBEVIRT_CDI_VERSION=${KUBEVIRT_CDI_VERSION:-v1.56.0} KUBEVIRT_COMMON_INSTANCETYPES_VERSION=${KUBEVIRT_COMMON_INSTANCETYPES_VERSION:-v0.3.0} KUBEVIRT_USE_EMULATION=${KUBEVIRT_USE_EMULATION:-"false"} @@ -175,6 +176,12 @@ is_nested_virt_enabled() { [ "$kvm_nested" == "1" ] || [ "$kvm_nested" == "Y" ] || [ "$kvm_nested" == "y" ] } +deploy_kubevirt_containerized_data_importer() { + echo "Deploying KubeVirt containerized-data-importer" + ${KUBECTL} apply -f "https://github.com/kubevirt/containerized-data-importer/releases/download/${KUBEVIRT_CDI_VERSION}/cdi-operator.yaml" + ${KUBECTL} apply -f "https://github.com/kubevirt/containerized-data-importer/releases/download/${KUBEVIRT_CDI_VERSION}/cdi-cr.yaml" +} + deploy_kubevirt_common_instancetypes() { echo "Deploying KubeVirt common-instancetypes" ${KUBECTL} apply -f "https://github.com/kubevirt/common-instancetypes/releases/download/${KUBEVIRT_COMMON_INSTANCETYPES_VERSION}/common-instancetypes-all-bundle-${KUBEVIRT_COMMON_INSTANCETYPES_VERSION}.yaml" @@ -255,6 +262,7 @@ set_default_options() { OPT_CREATE_CLUSTER=false OPT_CONFIGURE_SECONDARY_NETWORK=false OPT_DEPLOY_KUBEVIRT=false + OPT_DEPLOY_KUBEVIRT_CDI=false OPT_DEPLOY_KUBEVIRT_COMMON_INSTANCETYPES=false OPT_DEPLOY_CNAO=false OPT_CREATE_NAD=false @@ -270,6 +278,7 @@ parse_args() { --create-cluster) OPT_CREATE_CLUSTER=true ;; --configure-secondary-network) OPT_CONFIGURE_SECONDARY_NETWORK=true ;; --deploy-kubevirt) OPT_DEPLOY_KUBEVIRT=true ;; + --deploy-kubevirt-cdi) OPT_DEPLOY_KUBEVIRT_CDI=true ;; --deploy-kubevirt-common-instancetypes) OPT_DEPLOY_KUBEVIRT_COMMON_INSTANCETYPES=true ;; --deploy-cnao) OPT_DEPLOY_CNAO=true ;; --create-nad) OPT_CREATE_NAD=true ;; @@ -310,6 +319,7 @@ if [ "${ARGCOUNT}" -eq "0" ]; then OPT_CREATE_CLUSTER=true OPT_CONFIGURE_SECONDARY_NETWORK=true OPT_DEPLOY_KUBEVIRT=true + OPT_DEPLOY_KUBEVIRT_CDI=true OPT_DEPLOY_KUBEVIRT_COMMON_INSTANCETYPES=true OPT_DEPLOY_CNAO=true OPT_CREATE_NAD=true @@ -344,6 +354,10 @@ if [ "${OPT_DEPLOY_KUBEVIRT}" == true ]; then deploy_kubevirt fi +if [ "${OPT_DEPLOY_KUBEVIRT_CDI}" == true ]; then + deploy_kubevirt_containerized_data_importer +fi + if [ "${OPT_DEPLOY_KUBEVIRT_COMMON_INSTANCETYPES}" == true ]; then deploy_kubevirt_common_instancetypes fi From bce4f17188543046aa11015e8ffb46cb6bab5b11 Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Mon, 17 Jul 2023 11:05:02 +0200 Subject: [PATCH 2/2] kubevirt_vm: Allow to specify DataVolume templates By allowing to specify DataVolume templates VMs with persistent storage can be created using the kubevirt_vm module. Signed-off-by: Felix Matouschek --- examples/play-create-dv.yml | 41 +++++++++ examples/play-delete-dv.yml | 8 ++ plugins/modules/kubevirt_vm.py | 51 +++++++++++ tests/unit/modules/test_module_kubevirt_vm.py | 85 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 examples/play-create-dv.yml create mode 100644 examples/play-delete-dv.yml diff --git a/examples/play-create-dv.yml b/examples/play-create-dv.yml new file mode 100644 index 0000000..38f6aa4 --- /dev/null +++ b/examples/play-create-dv.yml @@ -0,0 +1,41 @@ +- hosts: localhost + tasks: + - name: Create VM + kubernetes.kubevirt.kubevirt_vm: + state: present + name: testvm-with-dv + namespace: default + labels: + app: test + instancetype: + name: u1.medium + preference: + name: fedora + data_volume_templates: + - metadata: + name: testdv + spec: + source: + registry: + url: docker://quay.io/containerdisks/fedora:latest + storage: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 5Gi + spec: + domain: + devices: {} + volumes: + - dataVolume: + name: testdv + name: datavolume + - cloudInitNoCloud: + userData: |- + #cloud-config + # The default username is: fedora + ssh_authorized_keys: + - ssh-ed25519 AAAA... + name: cloudinit + wait: yes diff --git a/examples/play-delete-dv.yml b/examples/play-delete-dv.yml new file mode 100644 index 0000000..b17cf2b --- /dev/null +++ b/examples/play-delete-dv.yml @@ -0,0 +1,8 @@ +- hosts: localhost + tasks: + - name: Delete VM + kubernetes.kubevirt.kubevirt_vm: + name: testvm-with-dv + namespace: default + state: absent + wait: yes diff --git a/plugins/modules/kubevirt_vm.py b/plugins/modules/kubevirt_vm.py index 008a798..85e2efa 100644 --- a/plugins/modules/kubevirt_vm.py +++ b/plugins/modules/kubevirt_vm.py @@ -75,6 +75,12 @@ options: - Specify the preference matcher of the VirtualMachine. - Only used when I(state=present). type: dict + data_volume_templates: + description: + - Specify the DataVolume templates of the VirtualMachine. + - 'See: http://kubevirt.io/api-reference/v1.0.0/definitions.html#_v1_datavolumetemplatespec' + type: list + elements: 'dict' spec: description: - Specify the template spec of the VirtualMachine. @@ -144,6 +150,46 @@ EXAMPLES = """ - ssh-ed25519 AAAA... name: cloudinit +- name: Create a VirtualMachine with a DataVolume template + kubernetes.kubevirt.kubevirt_vm: + state: present + name: testvm-with-dv + namespace: default + labels: + app: test + instancetype: + name: u1.medium + preference: + name: fedora + data_volume_templates: + - metadata: + name: testdv + spec: + source: + registry: + url: docker://quay.io/containerdisks/fedora:latest + storage: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 5Gi + spec: + domain: + devices: {} + volumes: + - dataVolume: + name: testdv + name: datavolume + - cloudInitNoCloud: + userData: |- + #cloud-config + # The default username is: fedora + ssh_authorized_keys: + - ssh-ed25519 AAAA... + name: cloudinit + wait: yes + - name: Delete a VirtualMachine kubernetes.kubevirt.kubevirt_vm: name: testvm @@ -243,6 +289,10 @@ spec: preference: {{ preference | to_yaml | indent(4) }} {% endif %} + {% if data_volume_templates %} + dataVolumeTemplates: + {{ data_volume_templates | to_yaml | indent(4) }} + {%- endif %} template: {% if annotations or labels %} metadata: @@ -292,6 +342,7 @@ def arg_spec() -> Dict: "running": {"type": "bool", "default": True}, "instancetype": {"type": "dict"}, "preference": {"type": "dict"}, + "data_volume_templates": {"type": "list", "elements": "dict"}, "spec": {"type": "dict"}, "wait": {"type": "bool", "default": False}, "wait_sleep": {"type": "int", "default": 5}, diff --git a/tests/unit/modules/test_module_kubevirt_vm.py b/tests/unit/modules/test_module_kubevirt_vm.py index 339e4e3..7cdc769 100644 --- a/tests/unit/modules/test_module_kubevirt_vm.py +++ b/tests/unit/modules/test_module_kubevirt_vm.py @@ -35,6 +35,30 @@ FIXTURE1 = { }, "spec": { "running": True, + "dataVolumeTemplates": [ + { + "metadata": { + "name": "testdv" + }, + "spec": { + "source": { + "registry": { + "url": "docker://quay.io/containerdisks/fedora:latest" + }, + }, + "storage": { + "accessModes": [ + "ReadWriteOnce" + ], + "resources": { + "requests": { + "storage": "5Gi" + } + } + } + } + } + ], "template": { "metadata": { "labels": { @@ -62,6 +86,19 @@ metadata: service: loadbalancer spec: running: True + dataVolumeTemplates: + - metadata: + name: testdv + spec: + source: + registry: + url: docker://quay.io/containerdisks/fedora:latest + storage: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 5Gi template: metadata: labels: @@ -81,6 +118,30 @@ FIXTURE2 = { 'service': 'loadbalancer', 'environment': 'staging' }, + 'data_volume_templates': [ + { + 'metadata': { + 'name': 'testdv' + }, + 'spec': { + 'source': { + 'registry': { + 'url': 'docker://quay.io/containerdisks/fedora:latest' + }, + }, + 'storage': { + 'accessModes': [ + 'ReadWriteOnce' + ], + 'resources': { + 'requests': { + 'storage': '5Gi' + } + } + } + } + } + ], 'spec': { 'domain': { 'devices': {} @@ -135,6 +196,30 @@ class TestCreateVM(unittest.TestCase): "service": "loadbalancer", "environment": "staging" }, + 'data_volume_templates': [ + { + 'metadata': { + 'name': 'testdv' + }, + 'spec': { + 'source': { + 'registry': { + 'url': 'docker://quay.io/containerdisks/fedora:latest' + }, + }, + 'storage': { + 'accessModes': [ + 'ReadWriteOnce' + ], + 'resources': { + 'requests': { + 'storage': '5Gi' + } + } + } + } + } + ], 'spec': { 'domain': { 'devices': {}