From b87165962822a34998f999e8a5223a8abf990eef Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 16 Nov 2009 14:51:29 +0100
Subject: [PATCH] Re-add check for duplicate instance IP
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This was originally implemented in 0ce8f948 and partially
rolled back in 9b65e0d4. Apart from re-adding the check,
this patch does some housekeeping by renaming the β€œ_helper”
function to β€œ_AddIpAddress”.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cmdlib.py |  2 +-
 lib/config.py | 37 +++++++++++++++++++++++++++++--------
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 14df5b15b..7ed082ed8 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -5688,7 +5688,7 @@ class LUCreateInstance(LogicalUnit):
                                      errors.ECODE_INVAL)
         nic_ip = ip
 
-      # TODO: check the ip for uniqueness !!
+      # TODO: check the ip address for uniqueness
       if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
         raise errors.OpPrereqError("Routed nic mode requires an ip address",
                                    errors.ECODE_INVAL)
diff --git a/lib/config.py b/lib/config.py
index c1030640a..5374c3982 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -430,22 +430,43 @@ class ConfigWriter:
                     " %s and %s" % (minor, node, instance_a, instance_b))
 
     # IP checks
-    ips = { data.cluster.master_ip: ["cluster_ip"] }
-    def _helper(ip, name):
-      if ip in ips:
-        ips[ip].append(name)
-      else:
-        ips[ip] = [name]
+    default_nicparams = data.cluster.nicparams[constants.PP_DEFAULT]
+    ips = {}
+
+    def _AddIpAddress(ip, name):
+      ips.setdefault(ip, []).append(name)
+
+    _AddIpAddress(data.cluster.master_ip, "cluster_ip")
 
     for node in data.nodes.values():
-      _helper(node.primary_ip, "node:%s/primary" % node.name)
+      _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
       if node.secondary_ip != node.primary_ip:
-        _helper(node.secondary_ip, "node:%s/secondary" % node.name)
+        _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
+
+    for instance in data.instances.values():
+      for idx, nic in enumerate(instance.nics):
+        if nic.ip is None:
+          continue
+
+        nicparams = objects.FillDict(default_nicparams, nic.nicparams)
+        nic_mode = nicparams[constants.NIC_MODE]
+        nic_link = nicparams[constants.NIC_LINK]
+
+        if nic_mode == constants.NIC_MODE_BRIDGED:
+          link = "bridge:%s" % nic_link
+        elif nic_mode == constants.NIC_MODE_ROUTED:
+          link = "route:%s" % nic_link
+        else:
+          raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
+
+        _AddIpAddress("%s/%s" % (link, nic.ip),
+                      "instance:%s/nic:%d" % (instance.name, idx))
 
     for ip, owners in ips.items():
       if len(owners) > 1:
         result.append("IP address %s is used by multiple owners: %s" %
                       (ip, ", ".join(owners)))
+
     return result
 
   @locking.ssynchronized(_config_lock, shared=1)
-- 
GitLab