Commit 29859cb7 authored by Iustin Pop's avatar Iustin Pop
Browse files

Fix generalized relocate mode of IAllocator

The patch which generalized the IAllocator was half-true: it actually
put the selection of the node inside the IAllocator, so callers were not
able to specify replace primary node.

This patch does:
  - split the arguments to the constructor in three sets: mode and name
    are always passed, and then we differentiate between allocation
    parameters and relocation ones
  - add a new relocate_from option to the IAllocator constructor which
    is a list of nodes we want to move the instance off
  - rename the 'nodes' argument in the request object to 'relocate_from'
    since this is clearer and is not confused with the result field also
    called 'nodes'

Reviewed-by: ultrotter
parent 96bb2f71
......@@ -3143,6 +3143,7 @@ class LUCreateInstance(LogicalUnit):
nics = [{"mac": self.op.mac, "ip": getattr(self.op, "ip", None),
"bridge": self.op.bridge}]
ial = IAllocator(self.cfg, self.sstore,
mode=constants.IALLOCATOR_MODE_ALLOC,
name=self.op.instance_name,
disk_template=self.op.disk_template,
tags=[],
......@@ -3151,7 +3152,7 @@ class LUCreateInstance(LogicalUnit):
mem_size=self.op.mem_size,
disks=disks,
nics=nics,
mode=constants.IALLOCATOR_MODE_ALLOC)
)
ial.Run(self.op.iallocator)
......@@ -4762,31 +4763,42 @@ class IAllocator(object):
easy usage
"""
_KEYS = [
"mode", "name",
_ALLO_KEYS = [
"mem_size", "disks", "disk_template",
"os", "tags", "nics", "vcpus",
]
_RELO_KEYS = [
"relocate_from",
]
def __init__(self, cfg, sstore, **kwargs):
def __init__(self, cfg, sstore, mode, name, **kwargs):
self.cfg = cfg
self.sstore = sstore
# init buffer variables
self.in_text = self.out_text = self.in_data = self.out_data = None
# init all input fields so that pylint is happy
self.mode = self.name = None
self.mode = mode
self.name = name
self.mem_size = self.disks = self.disk_template = None
self.os = self.tags = self.nics = self.vcpus = None
self.relocate_from = None
# computed fields
self.required_nodes = None
# init result fields
self.success = self.info = self.nodes = None
if self.mode == constants.IALLOCATOR_MODE_ALLOC:
keyset = self._ALLO_KEYS
elif self.mode == constants.IALLOCATOR_MODE_RELOC:
keyset = self._RELO_KEYS
else:
raise errors.ProgrammerError("Unknown mode '%s' passed to the"
" IAllocator" % self.mode)
for key in kwargs:
if key not in self._KEYS:
if key not in keyset:
raise errors.ProgrammerError("Invalid input parameter '%s' to"
" IAllocator" % key)
setattr(self, key, kwargs[key])
for key in self._KEYS:
for key in keyset:
if key not in kwargs:
raise errors.ProgrammerError("Missing input parameter '%s' to"
" IAllocator" % key)
......@@ -4930,7 +4942,7 @@ class IAllocator(object):
"name": self.name,
"disk_space_total": disk_space,
"required_nodes": self.required_nodes,
"nodes": list(instance.secondary_nodes),
"relocate_from": self.relocate_from,
}
self.in_data["request"] = request
......@@ -5053,6 +5065,7 @@ class LUTestAllocator(NoHooksLU):
raise errors.OpPrereqError("Instance '%s' not found for relocation" %
self.op.name)
self.op.name = fname
self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes
else:
raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
self.op.mode)
......@@ -5068,17 +5081,24 @@ class LUTestAllocator(NoHooksLU):
"""Run the allocator test.
"""
ial = IAllocator(self.cfg, self.sstore,
mode=self.op.mode,
name=self.op.name,
mem_size=self.op.mem_size,
disks=self.op.disks,
disk_template=self.op.disk_template,
os=self.op.os,
tags=self.op.tags,
nics=self.op.nics,
vcpus=self.op.vcpus,
)
if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
ial = IAllocator(self.cfg, self.sstore,
mode=self.op.mode,
name=self.op.name,
mem_size=self.op.mem_size,
disks=self.op.disks,
disk_template=self.op.disk_template,
os=self.op.os,
tags=self.op.tags,
nics=self.op.nics,
vcpus=self.op.vcpus,
)
else:
ial = IAllocator(self.cfg, self.sstore,
mode=self.op.mode,
name=self.op.name,
relocate_from=list(self.relocate_from),
)
if self.op.direction == constants.IALLOCATOR_DIR_IN:
result = ial.in_text
......
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