Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datavolumes support to kubevirt_vm module #52998

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/ansible/module_utils/kubevirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,54 @@ def get_resource(self, resource):

return existing

def _define_datavolumes(self, datavolumes, spec):
"""
Takes datavoulmes parameter of Ansible and create kubevirt API datavolumesTemplateSpec
structure from it
"""
if not datavolumes:
return

spec['dataVolumeTemplates'] = []
for dv in datavolumes:
# Add datavolume to datavolumetemplates spec:
dvt = virtdict()
dvt['metadata']['name'] = dv.get('name')
dvt['spec']['pvc'] = {
'accessModes': dv.get('pvc').get('accessModes'),
'resources': {
'requests': {
'storage': dv.get('pvc').get('storage'),
}
}
}
dvt['spec']['source'] = dv.get('source')
spec['dataVolumeTemplates'].append(dvt)

# Add datavolume to disks spec:
if not spec['template']['spec']['domain']['devices']['disks']:
spec['template']['spec']['domain']['devices']['disks'] = []

spec['template']['spec']['domain']['devices']['disks'].append(
{
'name': dv.get('name'),
'disk': dv.get('disk', {'bus': 'virtio'}),
}
)

# Add datavolume to volumes spec:
if not spec['template']['spec']['volumes']:
spec['template']['spec']['volumes'] = []

spec['template']['spec']['volumes'].append(
{
'dataVolume': {
'name': dv.get('name')
},
'name': dv.get('name'),
}
)

def _define_cloud_init(self, cloud_init_nocloud, template_spec):
"""
Takes the user's cloud_init_nocloud parameter and fill it in kubevirt
Expand Down Expand Up @@ -237,6 +285,7 @@ def _construct_vm_definition(self, kind, definition, template, params):
memory = params.get('memory')
cpu_cores = params.get('cpu_cores')
labels = params.get('labels')
datavolumes = params.get('datavolumes')
interfaces = params.get('interfaces')
cloud_init_nocloud = params.get('cloud_init_nocloud')
machine_type = params.get('machine_type')
Expand Down Expand Up @@ -264,6 +313,9 @@ def _construct_vm_definition(self, kind, definition, template, params):
# Define interfaces:
self._define_interfaces(interfaces, template_spec)

# Define datavolumes:
self._define_datavolumes(datavolumes, definition['spec'])

# Perform create/absent action:
definition = dict(self.merge_dicts(self.resource_definitions[0], definition))
resource = self.find_supported_resource(kind)
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/modules/cloud/kubevirt/kubevirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
- Works only with C(state) I(present) and I(absent).
type: bool
default: false
datavolumes:
description:
- "DataVolumes are a way to automate importing virtual machine disks onto pvcs during the virtual machine's
launch flow. Without using a DataVolume, users have to prepare a pvc with a disk image before assigning
it to a VM or VMI manifest. With a DataVolume, both the pvc creation and import is automated on behalf of the user."
type: list

extends_documentation_fragment:
- k8s_auth_options
Expand Down Expand Up @@ -218,6 +224,7 @@
],
'default': 'present'
},
'datavolumes': {'type': 'list'},
}


Expand Down