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 <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2023-07-17 10:55:17 +02:00
parent 47b20a0ef7
commit 6ba62f53e8
4 changed files with 87 additions and 94 deletions

View File

@@ -0,0 +1,8 @@
- hosts: localhost
tasks:
- name: Create VM
kubernetes.kubevirt.kubevirt_vm:
state: present
name: testvm
namespace: default
running: no

View File

@@ -11,26 +11,29 @@
name: u1.medium name: u1.medium
preference: preference:
name: fedora name: fedora
interfaces: spec:
- name: default domain:
masquerade: {} devices:
- name: bridge-network interfaces:
bridge: {} - name: default
networks: masquerade: {}
- name: default - name: bridge-network
pod: {} bridge: {}
- name: bridge-network networks:
multus: - name: default
networkName: kindexgw pod: {}
volumes: - name: bridge-network
- containerDisk: multus:
image: quay.io/containerdisks/fedora:latest networkName: kindexgw
name: containerdisk volumes:
- cloudInitNoCloud: - containerDisk:
userData: |- image: quay.io/containerdisks/fedora:latest
#cloud-config name: containerdisk
# The default username is: fedora - cloudInitNoCloud:
ssh_authorized_keys: userData: |-
- ssh-ed25519 AAAA... #cloud-config
name: cloudinit # The default username is: fedora
ssh_authorized_keys:
- ssh-ed25519 AAAA...
name: cloudinit
wait: yes wait: yes

View File

@@ -65,12 +65,6 @@ options:
- Specify whether the VirtualMachine should be running. - Specify whether the VirtualMachine should be running.
type: bool type: bool
default: yes 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: instancetype:
description: description:
- Specify the instancetype matcher of the VirtualMachine. - Specify the instancetype matcher of the VirtualMachine.
@@ -81,24 +75,11 @@ options:
- Specify the preference matcher of the VirtualMachine. - Specify the preference matcher of the VirtualMachine.
- Only used when I(state=present). - Only used when I(state=present).
type: dict type: dict
interfaces: spec:
description: description:
- Specify the interfaces of the VirtualMachine. - Specify the template spec of the VirtualMachine.
- 'See: https://kubevirt.io/api-reference/main/definitions.html#_v1_interface' - 'See: http://kubevirt.io/api-reference/v1.0.0/definitions.html#_v1_virtualmachineinstancespec'
type: list type: dict
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'
wait: wait:
description: description:
- Whether to wait for the VirtualMachine to end up in the ready state. - Whether to wait for the VirtualMachine to end up in the ready state.
@@ -137,28 +118,31 @@ EXAMPLES = """
name: u1.medium name: u1.medium
preference: preference:
name: fedora name: fedora
interfaces: spec:
- name: default domain:
masquerade: {} devices:
- name: bridge-network interfaces:
bridge: {} - name: default
networks: masquerade: {}
- name: default - name: bridge-network
pod: {} bridge: {}
- name: bridge-network networks:
multus: - name: default
networkName: kindexgw pod: {}
volumes: - name: bridge-network
- containerDisk: multus:
image: quay.io/containerdisks/fedora:latest networkName: kindexgw
name: containerdisk volumes:
- cloudInitNoCloud: - containerDisk:
userData: |- image: quay.io/containerdisks/fedora:latest
#cloud-config name: containerdisk
# The default username is: fedora - cloudInitNoCloud:
ssh_authorized_keys: userData: |-
- ssh-ed25519 AAAA... #cloud-config
name: cloudinit # The default username is: fedora
ssh_authorized_keys:
- ssh-ed25519 AAAA...
name: cloudinit
- name: Delete a VirtualMachine - name: Delete a VirtualMachine
kubernetes.kubevirt.kubevirt_vm: kubernetes.kubevirt.kubevirt_vm:
@@ -250,6 +234,7 @@ metadata:
{{ labels | to_yaml | indent(4) }} {{ labels | to_yaml | indent(4) }}
{%- endif %} {%- endif %}
spec: spec:
running: {{ running }}
{% if instancetype %} {% if instancetype %}
instancetype: instancetype:
{{ instancetype | to_yaml | indent(4) }} {{ instancetype | to_yaml | indent(4) }}
@@ -258,7 +243,6 @@ spec:
preference: preference:
{{ preference | to_yaml | indent(4) }} {{ preference | to_yaml | indent(4) }}
{% endif %} {% endif %}
running: {{ running }}
template: template:
{% if annotations or labels %} {% if annotations or labels %}
metadata: metadata:
@@ -272,23 +256,12 @@ spec:
{%- endif %} {%- endif %}
{% endif %} {% endif %}
spec: spec:
{% if spec %}
{{ spec | to_yaml | indent (6) }}
{%- else %}
domain: domain:
{% if interfaces %}
devices:
interfaces:
{{ interfaces | to_yaml | indent(10) }}
{%- else %}
devices: {} devices: {}
{% endif %} {% endif %}
{% if networks %}
networks:
{{ networks | to_yaml | indent(6) }}
{%- endif %}
{% if volumes %}
volumes:
{{ volumes | to_yaml | indent(6) }}
{%- endif %}
terminationGracePeriodSeconds: {{ termination_grace_period }}
""" """
@@ -317,12 +290,9 @@ def arg_spec() -> Dict:
"annotations": {"type": "dict"}, "annotations": {"type": "dict"},
"labels": {"type": "dict"}, "labels": {"type": "dict"},
"running": {"type": "bool", "default": True}, "running": {"type": "bool", "default": True},
"termination_grace_period": {"type": "int", "default": 180},
"instancetype": {"type": "dict"}, "instancetype": {"type": "dict"},
"preference": {"type": "dict"}, "preference": {"type": "dict"},
"interfaces": {"type": "list", "elements": "dict"}, "spec": {"type": "dict"},
"networks": {"type": "list", "elements": "dict"},
"volumes": {"type": "list", "elements": "dict"},
"wait": {"type": "bool", "default": False}, "wait": {"type": "bool", "default": False},
"wait_sleep": {"type": "int", "default": 5}, "wait_sleep": {"type": "int", "default": 5},
"wait_timeout": {"type": "int", "default": 120}, "wait_timeout": {"type": "int", "default": 120},
@@ -352,7 +322,6 @@ def main() -> None:
required_one_of=[ required_one_of=[
("name", "generate_name"), ("name", "generate_name"),
], ],
required_together=[("interfaces", "networks")],
supports_check_mode=True, supports_check_mode=True,
) )

View File

@@ -70,7 +70,8 @@ spec:
spec: spec:
domain: domain:
devices: {} devices: {}
terminationGracePeriodSeconds: 180''' terminationGracePeriodSeconds: 180
'''
FIXTURE2 = { FIXTURE2 = {
'name': 'testvm', 'name': 'testvm',
@@ -80,11 +81,17 @@ FIXTURE2 = {
'service': 'loadbalancer', 'service': 'loadbalancer',
'environment': 'staging' 'environment': 'staging'
}, },
'api_version': 'kubevirt.io/v1', 'running': True, 'termination_grace_period': 180, 'wait': False, 'wait_sleep': 5, 'wait_timeout': 120, 'force': False, 'spec': {
'generate_name': None, 'annotations': None, 'instancetype': None, 'preference': None, 'interfaces': None, 'networks': None, 'volumes': None, 'domain': {
'kubeconfig': None, 'context': None, 'host': None, 'api_key': None, 'username': None, 'password': None, 'validate_certs': None, 'ca_cert': None, 'devices': {}
'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, '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, 'resource_definition': METADATA,
'wait_condition': { 'wait_condition': {
'type': 'Ready', 'type': 'Ready',
@@ -127,6 +134,12 @@ class TestCreateVM(unittest.TestCase):
"labels": { "labels": {
"service": "loadbalancer", "service": "loadbalancer",
"environment": "staging" "environment": "staging"
},
'spec': {
'domain': {
'devices': {}
},
'terminationGracePeriodSeconds': 180
} }
} }
) )