From 6ba62f53e835255a1672d3e258dac30b0e231765 Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Mon, 17 Jul 2023 10:55:17 +0200 Subject: [PATCH] kubevirt_vm: Allow to specify template spec By allowing to specify the template spec unnecessary abstraction is avoided. The following module args are replaced by the 'spec' arg: - termination_grace_period - interfaces - networks - volumes Signed-off-by: Felix Matouschek --- examples/play-create-min.yml | 8 ++ examples/play-create.yml | 47 ++++---- plugins/modules/kubevirt_vm.py | 101 ++++++------------ tests/unit/modules/test_module_kubevirt_vm.py | 25 +++-- 4 files changed, 87 insertions(+), 94 deletions(-) create mode 100644 examples/play-create-min.yml diff --git a/examples/play-create-min.yml b/examples/play-create-min.yml new file mode 100644 index 0000000..4d41341 --- /dev/null +++ b/examples/play-create-min.yml @@ -0,0 +1,8 @@ +- hosts: localhost + tasks: + - name: Create VM + kubernetes.kubevirt.kubevirt_vm: + state: present + name: testvm + namespace: default + running: no diff --git a/examples/play-create.yml b/examples/play-create.yml index 5fe4b9b..7e654f2 100644 --- a/examples/play-create.yml +++ b/examples/play-create.yml @@ -11,26 +11,29 @@ name: u1.medium preference: name: fedora - interfaces: - - name: default - masquerade: {} - - name: bridge-network - bridge: {} - networks: - - name: default - pod: {} - - name: bridge-network - multus: - networkName: kindexgw - volumes: - - containerDisk: - image: quay.io/containerdisks/fedora:latest - name: containerdisk - - cloudInitNoCloud: - userData: |- - #cloud-config - # The default username is: fedora - ssh_authorized_keys: - - ssh-ed25519 AAAA... - name: cloudinit + spec: + domain: + devices: + interfaces: + - name: default + masquerade: {} + - name: bridge-network + bridge: {} + networks: + - name: default + pod: {} + - name: bridge-network + multus: + networkName: kindexgw + volumes: + - containerDisk: + image: quay.io/containerdisks/fedora:latest + name: containerdisk + - cloudInitNoCloud: + userData: |- + #cloud-config + # The default username is: fedora + ssh_authorized_keys: + - ssh-ed25519 AAAA... + name: cloudinit wait: yes diff --git a/plugins/modules/kubevirt_vm.py b/plugins/modules/kubevirt_vm.py index a0513b6..008a798 100644 --- a/plugins/modules/kubevirt_vm.py +++ b/plugins/modules/kubevirt_vm.py @@ -65,12 +65,6 @@ options: - Specify whether the VirtualMachine should be running. type: bool default: yes - termination_grace_period: - description: - - Specify the termination grace period of the VirtualMachine to provide - time for shutting down the guest. - type: int - default: 180 instancetype: description: - Specify the instancetype matcher of the VirtualMachine. @@ -81,24 +75,11 @@ options: - Specify the preference matcher of the VirtualMachine. - Only used when I(state=present). type: dict - interfaces: + spec: description: - - Specify the interfaces of the VirtualMachine. - - 'See: https://kubevirt.io/api-reference/main/definitions.html#_v1_interface' - type: list - elements: 'dict' - networks: - description: - - Specify the networks of the VirtualMachine. - - 'See: https://kubevirt.io/api-reference/main/definitions.html#_v1_network' - type: list - elements: 'dict' - volumes: - description: - - Specify the volumes of the VirtualMachine. - - 'See: https://kubevirt.io/api-reference/main/definitions.html#_v1_volume' - type: list - elements: 'dict' + - Specify the template spec of the VirtualMachine. + - 'See: http://kubevirt.io/api-reference/v1.0.0/definitions.html#_v1_virtualmachineinstancespec' + type: dict wait: description: - Whether to wait for the VirtualMachine to end up in the ready state. @@ -137,28 +118,31 @@ EXAMPLES = """ name: u1.medium preference: name: fedora - interfaces: - - name: default - masquerade: {} - - name: bridge-network - bridge: {} - networks: - - name: default - pod: {} - - name: bridge-network - multus: - networkName: kindexgw - volumes: - - containerDisk: - image: quay.io/containerdisks/fedora:latest - name: containerdisk - - cloudInitNoCloud: - userData: |- - #cloud-config - # The default username is: fedora - ssh_authorized_keys: - - ssh-ed25519 AAAA... - name: cloudinit + spec: + domain: + devices: + interfaces: + - name: default + masquerade: {} + - name: bridge-network + bridge: {} + networks: + - name: default + pod: {} + - name: bridge-network + multus: + networkName: kindexgw + volumes: + - containerDisk: + image: quay.io/containerdisks/fedora:latest + name: containerdisk + - cloudInitNoCloud: + userData: |- + #cloud-config + # The default username is: fedora + ssh_authorized_keys: + - ssh-ed25519 AAAA... + name: cloudinit - name: Delete a VirtualMachine kubernetes.kubevirt.kubevirt_vm: @@ -250,6 +234,7 @@ metadata: {{ labels | to_yaml | indent(4) }} {%- endif %} spec: + running: {{ running }} {% if instancetype %} instancetype: {{ instancetype | to_yaml | indent(4) }} @@ -258,7 +243,6 @@ spec: preference: {{ preference | to_yaml | indent(4) }} {% endif %} - running: {{ running }} template: {% if annotations or labels %} metadata: @@ -272,23 +256,12 @@ spec: {%- endif %} {% endif %} spec: + {% if spec %} + {{ spec | to_yaml | indent (6) }} + {%- else %} domain: - {% if interfaces %} - devices: - interfaces: - {{ interfaces | to_yaml | indent(10) }} - {%- else %} devices: {} - {% endif %} - {% if networks %} - networks: - {{ networks | to_yaml | indent(6) }} - {%- endif %} - {% if volumes %} - volumes: - {{ volumes | to_yaml | indent(6) }} - {%- endif %} - terminationGracePeriodSeconds: {{ termination_grace_period }} + {% endif %} """ @@ -317,12 +290,9 @@ def arg_spec() -> Dict: "annotations": {"type": "dict"}, "labels": {"type": "dict"}, "running": {"type": "bool", "default": True}, - "termination_grace_period": {"type": "int", "default": 180}, "instancetype": {"type": "dict"}, "preference": {"type": "dict"}, - "interfaces": {"type": "list", "elements": "dict"}, - "networks": {"type": "list", "elements": "dict"}, - "volumes": {"type": "list", "elements": "dict"}, + "spec": {"type": "dict"}, "wait": {"type": "bool", "default": False}, "wait_sleep": {"type": "int", "default": 5}, "wait_timeout": {"type": "int", "default": 120}, @@ -352,7 +322,6 @@ def main() -> None: required_one_of=[ ("name", "generate_name"), ], - required_together=[("interfaces", "networks")], supports_check_mode=True, ) diff --git a/tests/unit/modules/test_module_kubevirt_vm.py b/tests/unit/modules/test_module_kubevirt_vm.py index 53d8c02..339e4e3 100644 --- a/tests/unit/modules/test_module_kubevirt_vm.py +++ b/tests/unit/modules/test_module_kubevirt_vm.py @@ -70,7 +70,8 @@ spec: spec: domain: devices: {} - terminationGracePeriodSeconds: 180''' + terminationGracePeriodSeconds: 180 +''' FIXTURE2 = { 'name': 'testvm', @@ -80,11 +81,17 @@ FIXTURE2 = { 'service': 'loadbalancer', 'environment': 'staging' }, - 'api_version': 'kubevirt.io/v1', 'running': True, 'termination_grace_period': 180, 'wait': False, 'wait_sleep': 5, 'wait_timeout': 120, 'force': False, - 'generate_name': None, 'annotations': None, 'instancetype': None, 'preference': None, 'interfaces': None, 'networks': None, 'volumes': None, - 'kubeconfig': None, 'context': None, 'host': None, 'api_key': None, 'username': None, 'password': None, 'validate_certs': None, 'ca_cert': None, - 'client_cert': None, 'client_key': None, 'proxy': None, 'no_proxy': None, 'proxy_headers': None, 'persist_config': None, 'impersonate_user': None, - 'impersonate_groups': None, 'delete_options': None, + 'spec': { + 'domain': { + 'devices': {} + }, + 'terminationGracePeriodSeconds': 180 + }, + 'api_version': 'kubevirt.io/v1', 'running': True, 'wait': False, 'wait_sleep': 5, 'wait_timeout': 120, 'force': False, + 'generate_name': None, 'annotations': None, 'instancetype': None, 'preference': None, + 'kubeconfig': None, 'context': None, 'host': None, 'api_key': None, 'username': None, 'password': None, 'validate_certs': None, + 'ca_cert': None, 'client_cert': None, 'client_key': None, 'proxy': None, 'no_proxy': None, 'proxy_headers': None, + 'persist_config': None, 'impersonate_user': None, 'impersonate_groups': None, 'delete_options': None, 'resource_definition': METADATA, 'wait_condition': { 'type': 'Ready', @@ -127,6 +134,12 @@ class TestCreateVM(unittest.TestCase): "labels": { "service": "loadbalancer", "environment": "staging" + }, + 'spec': { + 'domain': { + 'devices': {} + }, + 'terminationGracePeriodSeconds': 180 } } )