kubevirt: Rewrite merge dict method
This commit is contained in:
parent
94e2d87b2f
commit
6bceea4f51
2 changed files with 24 additions and 26 deletions
|
@ -7,6 +7,7 @@
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from distutils.version import Version
|
from distutils.version import Version
|
||||||
|
|
||||||
|
from ansible.module_utils.common._collections_compat import Sequence
|
||||||
from ansible.module_utils.k8s.common import list_dict_str
|
from ansible.module_utils.k8s.common import list_dict_str
|
||||||
from ansible.module_utils.k8s.raw import KubernetesRawModule
|
from ansible.module_utils.k8s.raw import KubernetesRawModule
|
||||||
|
|
||||||
|
@ -127,28 +128,25 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
super(KubeVirtRawModule, self).__init__(*args, **kwargs)
|
super(KubeVirtRawModule, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def merge_dicts(x, yy):
|
def merge_dicts(base_dict, merging_dicts):
|
||||||
|
"""This function merges a base dictionary with one or more other dictionaries.
|
||||||
|
The base dictionary takes precedence when there is a key collision.
|
||||||
|
merging_dicts can be a dict or a list or tuple of dicts. In the latter case, the
|
||||||
|
dictionaries at the front of the list have higher precedence over the ones at the end.
|
||||||
"""
|
"""
|
||||||
This function merge two dictionaries, where the first dict has
|
if not merging_dicts:
|
||||||
higher priority in merging two same keys.
|
merging_dicts = ({},)
|
||||||
"""
|
|
||||||
if not yy:
|
|
||||||
yy = {}
|
|
||||||
|
|
||||||
if not isinstance(yy, list):
|
if not isinstance(merging_dicts, Sequence):
|
||||||
yy = [yy]
|
merging_dicts = (merging_dicts,)
|
||||||
|
|
||||||
for y in yy:
|
new_dict = {}
|
||||||
for k in set(x.keys()).union(y.keys()):
|
for d in reversed(merging_dicts):
|
||||||
if k in x and k in y:
|
new_dict.update(d)
|
||||||
if isinstance(x[k], dict) and isinstance(y[k], dict):
|
|
||||||
yield (k, dict(KubeVirtRawModule.merge_dicts(x[k], y[k])))
|
new_dict.update(base_dict)
|
||||||
else:
|
|
||||||
yield (k, x[k])
|
return new_dict
|
||||||
elif k in x:
|
|
||||||
yield (k, x[k])
|
|
||||||
else:
|
|
||||||
yield (k, y[k])
|
|
||||||
|
|
||||||
def get_resource(self, resource):
|
def get_resource(self, resource):
|
||||||
try:
|
try:
|
||||||
|
@ -238,7 +236,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
spec_interfaces = []
|
spec_interfaces = []
|
||||||
for i in interfaces:
|
for i in interfaces:
|
||||||
spec_interfaces.append(
|
spec_interfaces.append(
|
||||||
dict(self.merge_dicts(dict((k, v) for k, v in i.items() if k != 'network'), defaults['interfaces']))
|
self.merge_dicts(dict((k, v) for k, v in i.items() if k != 'network'), defaults['interfaces'])
|
||||||
)
|
)
|
||||||
if 'interfaces' not in template_spec['domain']['devices']:
|
if 'interfaces' not in template_spec['domain']['devices']:
|
||||||
template_spec['domain']['devices']['interfaces'] = []
|
template_spec['domain']['devices']['interfaces'] = []
|
||||||
|
@ -249,7 +247,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
for i in interfaces:
|
for i in interfaces:
|
||||||
net = i['network']
|
net = i['network']
|
||||||
net['name'] = i['name']
|
net['name'] = i['name']
|
||||||
spec_networks.append(dict(self.merge_dicts(net, defaults['networks'])))
|
spec_networks.append(self.merge_dicts(net, defaults['networks']))
|
||||||
if 'networks' not in template_spec:
|
if 'networks' not in template_spec:
|
||||||
template_spec['networks'] = []
|
template_spec['networks'] = []
|
||||||
template_spec['networks'].extend(spec_networks)
|
template_spec['networks'].extend(spec_networks)
|
||||||
|
@ -269,7 +267,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
spec_disks = []
|
spec_disks = []
|
||||||
for d in disks:
|
for d in disks:
|
||||||
spec_disks.append(
|
spec_disks.append(
|
||||||
dict(self.merge_dicts(dict((k, v) for k, v in d.items() if k != 'volume'), defaults['disks']))
|
self.merge_dicts(dict((k, v) for k, v in d.items() if k != 'volume'), defaults['disks'])
|
||||||
)
|
)
|
||||||
if 'disks' not in template_spec['domain']['devices']:
|
if 'disks' not in template_spec['domain']['devices']:
|
||||||
template_spec['domain']['devices']['disks'] = []
|
template_spec['domain']['devices']['disks'] = []
|
||||||
|
@ -280,7 +278,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
for d in disks:
|
for d in disks:
|
||||||
volume = d['volume']
|
volume = d['volume']
|
||||||
volume['name'] = d['name']
|
volume['name'] = d['name']
|
||||||
spec_volumes.append(dict(self.merge_dicts(volume, defaults['volumes'])))
|
spec_volumes.append(self.merge_dicts(volume, defaults['volumes']))
|
||||||
if 'volumes' not in template_spec:
|
if 'volumes' not in template_spec:
|
||||||
template_spec['volumes'] = []
|
template_spec['volumes'] = []
|
||||||
template_spec['volumes'].extend(spec_volumes)
|
template_spec['volumes'].extend(spec_volumes)
|
||||||
|
@ -350,7 +348,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
template_spec['domain']['cpu']['model'] = cpu_model
|
template_spec['domain']['cpu']['model'] = cpu_model
|
||||||
|
|
||||||
if labels:
|
if labels:
|
||||||
template['metadata']['labels'] = dict(self.merge_dicts(labels, template['metadata']['labels']))
|
template['metadata']['labels'] = self.merge_dicts(labels, template['metadata']['labels'])
|
||||||
|
|
||||||
if machine_type:
|
if machine_type:
|
||||||
template_spec['domain']['machine']['type'] = machine_type
|
template_spec['domain']['machine']['type'] = machine_type
|
||||||
|
@ -378,7 +376,7 @@ class KubeVirtRawModule(KubernetesRawModule):
|
||||||
# Define datavolumes:
|
# Define datavolumes:
|
||||||
self._define_datavolumes(datavolumes, definition['spec'])
|
self._define_datavolumes(datavolumes, definition['spec'])
|
||||||
|
|
||||||
return dict(self.merge_dicts(definition, self.resource_definitions[0]))
|
return self.merge_dicts(definition, self.resource_definitions[0])
|
||||||
|
|
||||||
def construct_vm_definition(self, kind, definition, template, defaults=None):
|
def construct_vm_definition(self, kind, definition, template, defaults=None):
|
||||||
definition = self._construct_vm_definition(kind, definition, template, self.params, defaults)
|
definition = self._construct_vm_definition(kind, definition, template, self.params, defaults)
|
||||||
|
|
|
@ -380,7 +380,7 @@ class KubeVirtVM(KubeVirtRawModule):
|
||||||
template['metadata']['labels']['vm.cnv.io/name'] = self.params.get('name')
|
template['metadata']['labels']['vm.cnv.io/name'] = self.params.get('name')
|
||||||
dummy, definition = self.construct_vm_definition(kind, definition, template, defaults)
|
dummy, definition = self.construct_vm_definition(kind, definition, template, defaults)
|
||||||
|
|
||||||
return dict(self.merge_dicts(definition, processedtemplate))
|
return self.merge_dicts(definition, processedtemplate)
|
||||||
|
|
||||||
def execute_module(self):
|
def execute_module(self):
|
||||||
# Parse parameters specific to this module:
|
# Parse parameters specific to this module:
|
||||||
|
|
Loading…
Reference in a new issue