Commit 2ff6426b authored by Jose A. Lopes's avatar Jose A. Lopes

Refactor instance communication network add and connect

Factor out the opcodes 'OpNetworkAdd' and 'OpNetworkConnect' used in
'LUClusterSetParams' and 'LUGroupAdd' in order to reduce code
duplication and keep the configuration of the instance communication
in one place.
Signed-off-by: default avatarJose A. Lopes <jabolopes@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent f985ecbd
......@@ -56,7 +56,8 @@ from ganeti.cmdlib.common import ShareAll, RunPostHook, \
CheckOSParams, CheckHVParams, AdjustCandidatePool, CheckNodePVs, \
ComputeIPolicyInstanceViolation, AnnotateDiskParams, SupportsOob, \
CheckIpolicyVsDiskTemplates, CheckDiskAccessModeValidity, \
CheckDiskAccessModeConsistency, CreateNewClientCert
CheckDiskAccessModeConsistency, CreateNewClientCert, \
OpAddInstanceCommunicationNetwork, OpConnectInstanceCommunicationNetwork
import ganeti.masterd.instance
......@@ -1391,17 +1392,7 @@ class LUClusterSetParams(LogicalUnit):
network_exists = False
if not network_exists:
op = opcodes.OpNetworkAdd(
network_name=network_name,
gateway=None,
network=constants.INSTANCE_COMMUNICATION_NETWORK4,
gateway6=None,
network6=constants.INSTANCE_COMMUNICATION_NETWORK6,
mac_prefix=constants.INSTANCE_COMMUNICATION_MAC_PREFIX,
add_reserved_ips=None,
conflicts_check=True,
tags=[])
jobs.append(op)
jobs.append(OpAddInstanceCommunicationNetwork(network_name))
for group_uuid in cfg.GetNodeGroupList():
group = cfg.GetNodeGroup(group_uuid)
......@@ -1416,12 +1407,7 @@ class LUClusterSetParams(LogicalUnit):
network_connected = False
if not network_connected:
op = opcodes.OpNetworkConnect(
group_name=group_uuid,
network_name=network_name,
network_mode=constants.NIC_MODE_ROUTED,
network_link=constants.INSTANCE_COMMUNICATION_NETWORK_LINK,
conflicts_check=True)
op = OpConnectInstanceCommunicationNetwork(group_uuid, network_name)
jobs.append(op)
if jobs:
......
......@@ -1285,3 +1285,55 @@ def CreateNewClientCert(lu, node_uuid, filename=None):
((crypto_type, new_digest), ) = result.payload
assert crypto_type == constants.CRYPTO_TYPE_SSL_DIGEST
return new_digest
def OpAddInstanceCommunicationNetwork(network):
"""Create OpCode that adds the instance communication network.
This OpCode contains the configuration necessary for the instance
communication network.
@type network: string
@param network: name or UUID of the instance communication network
@rtype: L{ganeti.opcodes.OpCode}
@return: OpCode that creates the instance communication network
"""
return opcodes.OpNetworkAdd(
network_name=network,
gateway=None,
network=constants.INSTANCE_COMMUNICATION_NETWORK4,
gateway6=None,
network6=constants.INSTANCE_COMMUNICATION_NETWORK6,
mac_prefix=constants.INSTANCE_COMMUNICATION_MAC_PREFIX,
add_reserved_ips=None,
conflicts_check=True,
tags=[])
def OpConnectInstanceCommunicationNetwork(group_uuid, network):
"""Create OpCode that connects a group to the instance communication
network.
This OpCode contains the configuration necessary for the instance
communication network.
@type group_uuid: string
@param group_uuid: UUID of the group to connect
@type network: string
@param network: name or UUID of the network to connect to, i.e., the
instance communication network
@rtype: L{ganeti.opcodes.OpCode}
@return: OpCode that connects the group to the instance
communication network
"""
return opcodes.OpNetworkConnect(
group_name=group_uuid,
network_name=network,
network_mode=constants.NIC_MODE_ROUTED,
network_link=constants.INSTANCE_COMMUNICATION_NETWORK_LINK,
conflicts_check=True)
......@@ -28,7 +28,6 @@ from ganeti import constants
from ganeti import errors
from ganeti import locking
from ganeti import objects
from ganeti import opcodes
from ganeti import utils
from ganeti.masterd import iallocator
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU, ResultWithJobs
......@@ -38,7 +37,7 @@ from ganeti.cmdlib.common import MergeAndVerifyHvState, \
ComputeNewInstanceViolations, GetDefaultIAllocator, ShareAll, \
CheckInstancesNodeGroups, LoadNodeEvacResult, MapInstanceLvsToNodes, \
CheckIpolicyVsDiskTemplates, CheckDiskAccessModeValidity, \
CheckDiskAccessModeConsistency
CheckDiskAccessModeConsistency, OpConnectInstanceCommunicationNetwork
import ganeti.masterd.instance
......@@ -157,8 +156,6 @@ class LUGroupAdd(LogicalUnit):
L{None}
"""
jobs = []
try:
cfg.LookupNetwork(network_name)
network_exists = True
......@@ -166,16 +163,8 @@ class LUGroupAdd(LogicalUnit):
network_exists = False
if network_exists:
op = opcodes.OpNetworkConnect(
group_name=group_uuid,
network_name=network_name,
network_mode=constants.NIC_MODE_ROUTED,
network_link=constants.INSTANCE_COMMUNICATION_NETWORK_LINK,
conflicts_check=True)
jobs.append(op)
if jobs:
return ResultWithJobs([jobs])
op = OpConnectInstanceCommunicationNetwork(group_uuid, network_name)
return ResultWithJobs([[op]])
else:
return None
......
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