Support to create VM from template (#54222)

This commit is contained in:
Ondra Machacek 2019-04-01 12:32:47 +02:00 committed by ansibot
parent 9705977ca1
commit f6bdadaecd
2 changed files with 35 additions and 2 deletions

View file

@ -210,6 +210,7 @@ from ansible.module_utils.k8s.common import AUTH_ARG_SPEC
from ansible.module_utils.kubevirt import (
virtdict,
KubeVirtRawModule,
API_GROUP,
MAX_SUPPORTED_API_VERSION
)
@ -355,7 +356,7 @@ class KubeVirtVMTemplate(KubeVirtRawModule):
vm_definition['spec']['template']['spec']['networks'] = [self.params.get('default_network')]
# Set kubevirt API version:
vm_definition['apiVersion'] = MAX_SUPPORTED_API_VERSION
vm_definition['apiVersion'] = '%s/%s' % (API_GROUP, MAX_SUPPORTED_API_VERSION)
# Contruct k8s vm API object:
vm_template = vm_definition['spec']['template']

View file

@ -62,6 +62,14 @@ options:
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
template:
description:
- "Template to used to create a virtual machine."
type: str
parameters:
description:
- "Value of parameters to be replaced in template parameters."
type: dict
extends_documentation_fragment:
- k8s_auth_options
@ -234,6 +242,8 @@ VM_ARG_SPEC = {
'default': 'present'
},
'datavolumes': {'type': 'list'},
'template': {'type': 'str'},
'parameters': {'type': 'dict'},
}
@ -308,6 +318,7 @@ class KubeVirtVM(KubeVirtRawModule):
def execute_module(self):
# Parse parameters specific for this module:
self.client = self.get_api_client()
definition = virtdict()
ephemeral = self.params.get('ephemeral')
state = self.params.get('state')
@ -315,10 +326,31 @@ class KubeVirtVM(KubeVirtRawModule):
if not ephemeral:
definition['spec']['running'] = state == 'running'
# Execute the CURD of VM:
# Construct the API object definition:
vm_template = self.params.get('template')
processedtemplate = {}
if vm_template:
# Find the template the VM should be created from:
template_resource = self.client.resources.get(api_version='template.openshift.io/v1', kind='Template', name='templates')
proccess_template = template_resource.get(name=vm_template, namespace=self.params.get('namespace'))
# Set proper template values set by Ansible parameter 'parameters':
for k, v in self.params.get('parameters', {}).items():
for parameter in proccess_template.parameters:
if parameter.name == k:
parameter.value = v
# Proccess the template:
processedtemplates_res = self.client.resources.get(api_version='template.openshift.io/v1', kind='Template', name='processedtemplates')
processedtemplate = processedtemplates_res.create(proccess_template.to_dict()).to_dict()['objects'][0]
template = definition if ephemeral else definition['spec']['template']
kind = 'VirtualMachineInstance' if ephemeral else 'VirtualMachine'
template['labels']['vm.cnv.io/name'] = self.params.get('name')
dummy, definition = self.construct_vm_definition(kind, definition, template)
definition = dict(self.merge_dicts(processedtemplate, definition))
# Create the VM:
result = self.execute_crud(kind, definition)
changed = result['changed']