Commit 73e0328b authored by Iustin Pop's avatar Iustin Pop
Browse files

Add cluster.SimpleFill*() functions



Currently, the existing cluster.Fill* functions take as argument an
instance. This means that in any case where we don't have an actual
instance object, we have to resort to calling the low-level
objects.FillDict function.

This is bad for two reasons:
- we have to know of, and we hardcode, the cluster object internals
  (e.g. that the nicparams are stored in a dict indexed by group)
- which can result in subtle bugs, if the underlying storage mechanisms
  change

This patch adds a lower-level implementation SimpleFillHV for FillHV and
SimpleFillBE for FillBE, and adds a completely new SimpleFillNIC (all
use cases until now hardcoded cluster.nicparams[constant.PP_DEFAULT]
directly); it then uses these new functions in cmdlib.py.

A side effect is that _CheckNicsBridgesExist loses the 'profile'
parameter, which was unused. If it's needed, we should add it later via
a proper profile parameter to SimpleFillNIC.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 31ca841c
...@@ -751,11 +751,11 @@ def _NICListToTuple(lu, nics): ...@@ -751,11 +751,11 @@ def _NICListToTuple(lu, nics):
""" """
hooks_nics = [] hooks_nics = []
c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT] cluster = lu.cfg.GetClusterInfo()
for nic in nics: for nic in nics:
ip = nic.ip ip = nic.ip
mac = nic.mac mac = nic.mac
filled_params = objects.FillDict(c_nicparams, nic.nicparams) filled_params = cluster.SimpleFillNIC(nic.nicparams)
mode = filled_params[constants.NIC_MODE] mode = filled_params[constants.NIC_MODE]
link = filled_params[constants.NIC_LINK] link = filled_params[constants.NIC_LINK]
hooks_nics.append((ip, mac, mode, link)) hooks_nics.append((ip, mac, mode, link))
...@@ -827,14 +827,12 @@ def _DecideSelfPromotion(lu, exceptions=None): ...@@ -827,14 +827,12 @@ def _DecideSelfPromotion(lu, exceptions=None):
return mc_now < mc_should return mc_now < mc_should
def _CheckNicsBridgesExist(lu, target_nics, target_node, def _CheckNicsBridgesExist(lu, target_nics, target_node):
profile=constants.PP_DEFAULT):
"""Check that the brigdes needed by a list of nics exist. """Check that the brigdes needed by a list of nics exist.
""" """
c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile] cluster = lu.cfg.GetClusterInfo()
paramslist = [objects.FillDict(c_nicparams, nic.nicparams) paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
for nic in target_nics]
brlist = [params[constants.NIC_LINK] for params in paramslist brlist = [params[constants.NIC_LINK] for params in paramslist
if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED] if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
if brlist: if brlist:
...@@ -2332,13 +2330,11 @@ class LUSetClusterParams(LogicalUnit): ...@@ -2332,13 +2330,11 @@ class LUSetClusterParams(LogicalUnit):
# validate params changes # validate params changes
if self.op.beparams: if self.op.beparams:
utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES) utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
self.new_beparams = objects.FillDict( self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
if self.op.nicparams: if self.op.nicparams:
utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES) utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
self.new_nicparams = objects.FillDict( self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
objects.NIC.CheckParameterSyntax(self.new_nicparams) objects.NIC.CheckParameterSyntax(self.new_nicparams)
nic_errors = [] nic_errors = []
...@@ -4816,8 +4812,7 @@ class LUQueryInstances(NoHooksLU): ...@@ -4816,8 +4812,7 @@ class LUQueryInstances(NoHooksLU):
iout = [] iout = []
i_hv = cluster.FillHV(instance, skip_globals=True) i_hv = cluster.FillHV(instance, skip_globals=True)
i_be = cluster.FillBE(instance) i_be = cluster.FillBE(instance)
i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], i_nicp = [cluster.SimpleFillNIC(nic.nicparams) for nic in instance.nics]
nic.nicparams) for nic in instance.nics]
for field in self.op.output_fields: for field in self.op.output_fields:
st_match = self._FIELDS_STATIC.Matches(field) st_match = self._FIELDS_STATIC.Matches(field)
if field in self._SIMPLE_FIELDS: if field in self._SIMPLE_FIELDS:
...@@ -6470,17 +6465,17 @@ class LUCreateInstance(LogicalUnit): ...@@ -6470,17 +6465,17 @@ class LUCreateInstance(LogicalUnit):
""" """
# hvparams # hvparams
hv_defs = cluster.GetHVDefaults(self.op.hypervisor, self.op.os_type) hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {})
for name in self.op.hvparams.keys(): for name in self.op.hvparams.keys():
if name in hv_defs and hv_defs[name] == self.op.hvparams[name]: if name in hv_defs and hv_defs[name] == self.op.hvparams[name]:
del self.op.hvparams[name] del self.op.hvparams[name]
# beparams # beparams
be_defs = cluster.beparams.get(constants.PP_DEFAULT, {}) be_defs = cluster.SimpleFillBE({})
for name in self.op.beparams.keys(): for name in self.op.beparams.keys():
if name in be_defs and be_defs[name] == self.op.beparams[name]: if name in be_defs and be_defs[name] == self.op.beparams[name]:
del self.op.beparams[name] del self.op.beparams[name]
# nic params # nic params
nic_defs = cluster.nicparams.get(constants.PP_DEFAULT, {}) nic_defs = cluster.SimpleFillNIC({})
for nic in self.op.nics: for nic in self.op.nics:
for name in constants.NICS_PARAMETERS: for name in constants.NICS_PARAMETERS:
if name in nic and name in nic_defs and nic[name] == nic_defs[name]: if name in nic and name in nic_defs and nic[name] == nic_defs[name]:
...@@ -6514,8 +6509,7 @@ class LUCreateInstance(LogicalUnit): ...@@ -6514,8 +6509,7 @@ class LUCreateInstance(LogicalUnit):
# check hypervisor parameter syntax (locally) # check hypervisor parameter syntax (locally)
utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES) utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
filled_hvp = objects.FillDict(cluster.GetHVDefaults(self.op.hypervisor, filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type,
self.op.os_type),
self.op.hvparams) self.op.hvparams)
hv_type = hypervisor.GetHypervisor(self.op.hypervisor) hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
hv_type.CheckParameterSyntax(filled_hvp) hv_type.CheckParameterSyntax(filled_hvp)
...@@ -6525,8 +6519,7 @@ class LUCreateInstance(LogicalUnit): ...@@ -6525,8 +6519,7 @@ class LUCreateInstance(LogicalUnit):
# fill and remember the beparams dict # fill and remember the beparams dict
utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES) utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT], self.be_full = cluster.SimpleFillBE(self.op.beparams)
self.op.beparams)
# now that hvp/bep are in final format, let's reset to defaults, # now that hvp/bep are in final format, let's reset to defaults,
# if told to do so # if told to do so
...@@ -6599,8 +6592,7 @@ class LUCreateInstance(LogicalUnit): ...@@ -6599,8 +6592,7 @@ class LUCreateInstance(LogicalUnit):
if link: if link:
nicparams[constants.NIC_LINK] = link nicparams[constants.NIC_LINK] = link
check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], check_params = cluster.SimpleFillNIC(nicparams)
nicparams)
objects.NIC.CheckParameterSyntax(check_params) objects.NIC.CheckParameterSyntax(check_params)
self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams)) self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
...@@ -8382,7 +8374,6 @@ class LUSetInstanceParams(LogicalUnit): ...@@ -8382,7 +8374,6 @@ class LUSetInstanceParams(LogicalUnit):
if self.op.nics: if self.op.nics:
args['nics'] = [] args['nics'] = []
nic_override = dict(self.op.nics) nic_override = dict(self.op.nics)
c_nicparams = self.cluster.nicparams[constants.PP_DEFAULT]
for idx, nic in enumerate(self.instance.nics): for idx, nic in enumerate(self.instance.nics):
if idx in nic_override: if idx in nic_override:
this_nic_override = nic_override[idx] this_nic_override = nic_override[idx]
...@@ -8399,7 +8390,7 @@ class LUSetInstanceParams(LogicalUnit): ...@@ -8399,7 +8390,7 @@ class LUSetInstanceParams(LogicalUnit):
if idx in self.nic_pnew: if idx in self.nic_pnew:
nicparams = self.nic_pnew[idx] nicparams = self.nic_pnew[idx]
else: else:
nicparams = objects.FillDict(c_nicparams, nic.nicparams) nicparams = self.cluster.SimpleFillNIC(nic.nicparams)
mode = nicparams[constants.NIC_MODE] mode = nicparams[constants.NIC_MODE]
link = nicparams[constants.NIC_LINK] link = nicparams[constants.NIC_LINK]
args['nics'].append((ip, mac, mode, link)) args['nics'].append((ip, mac, mode, link))
...@@ -9699,9 +9690,7 @@ class IAllocator(object): ...@@ -9699,9 +9690,7 @@ class IAllocator(object):
for iinfo, beinfo in i_list: for iinfo, beinfo in i_list:
nic_data = [] nic_data = []
for nic in iinfo.nics: for nic in iinfo.nics:
filled_params = objects.FillDict( filled_params = cluster_info.SimpleFillNIC(nic.nicparams)
cluster_info.nicparams[constants.PP_DEFAULT],
nic.nicparams)
nic_dict = {"mac": nic.mac, nic_dict = {"mac": nic.mac,
"ip": nic.ip, "ip": nic.ip,
"mode": filled_params[constants.NIC_MODE], "mode": filled_params[constants.NIC_MODE],
......
...@@ -961,9 +961,31 @@ class Cluster(TaggableObject): ...@@ -961,9 +961,31 @@ class Cluster(TaggableObject):
return ret_dict return ret_dict
def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
"""Fill a given hvparams dict with cluster defaults.
@type hv_name: string
@param hv_name: the hypervisor to use
@type os_name: string
@param os_name: the OS to use for overriding the hypervisor defaults
@type skip_globals: boolean
@param skip_globals: if True, the global hypervisor parameters will
not be filled
@rtype: dict
@return: a copy of the given hvparams with missing keys filled from
the cluster defaults
"""
if skip_globals:
skip_keys = constants.HVC_GLOBALS
else:
skip_keys = []
def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
return FillDict(def_dict, hvparams, skip_keys=skip_keys)
def FillHV(self, instance, skip_globals=False): def FillHV(self, instance, skip_globals=False):
"""Fill an instance's hvparams dict. """Fill an instance's hvparams dict with cluster defaults.
@type instance: L{objects.Instance} @type instance: L{objects.Instance}
@param instance: the instance parameter to fill @param instance: the instance parameter to fill
...@@ -975,17 +997,23 @@ class Cluster(TaggableObject): ...@@ -975,17 +997,23 @@ class Cluster(TaggableObject):
the cluster defaults the cluster defaults
""" """
if skip_globals: return self.SimpleFillHV(instance.hypervisor, instance.os,
skip_keys = constants.HVC_GLOBALS instance.hvparams, skip_globals)
else:
skip_keys = []
def_dict = self.GetHVDefaults(instance.hypervisor, instance.os, def SimpleFillBE(self, beparams):
skip_keys=skip_keys) """Fill a given beparams dict with cluster defaults.
return FillDict(def_dict, instance.hvparams, skip_keys=skip_keys)
@type beparam: dict
@param beparam: the dict to fill
@rtype: dict
@return: a copy of the passed in beparams with missing keys filled
from the cluster defaults
"""
return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
def FillBE(self, instance): def FillBE(self, instance):
"""Fill an instance's beparams dict. """Fill an instance's beparams dict with cluster defaults.
@type instance: L{objects.Instance} @type instance: L{objects.Instance}
@param instance: the instance parameter to fill @param instance: the instance parameter to fill
...@@ -994,8 +1022,19 @@ class Cluster(TaggableObject): ...@@ -994,8 +1022,19 @@ class Cluster(TaggableObject):
the cluster defaults the cluster defaults
""" """
return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), return self.SimpleFillBE(instance.beparams)
instance.beparams)
def SimpleFillNIC(self, nicparams):
"""Fill a given nicparams dict with cluster defaults.
@type nicparam: dict
@param nicparam: the dict to fill
@rtype: dict
@return: a copy of the passed in nicparams with missing keys filled
from the cluster defaults
"""
return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
class BlockDevStatus(ConfigObject): class BlockDevStatus(ConfigObject):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment