Blame SOURCES/bz1846733-gcp-vpc-move-vip-1-support-multiple-alias-ips.patch

45a1af
--- a/heartbeat/gcp-vpc-move-vip.in	2020-08-17 10:33:22.132531259 +0200
45a1af
+++ b/heartbeat/gcp-vpc-move-vip.in	2020-08-17 10:34:54.050633259 +0200
45a1af
@@ -22,7 +22,8 @@
45a1af
 import sys
45a1af
 import time
45a1af
 
45a1af
-OCF_FUNCTIONS_DIR="%s/lib/heartbeat" % os.environ.get("OCF_ROOT")
45a1af
+OCF_FUNCTIONS_DIR = os.environ.get("OCF_FUNCTIONS_DIR", "%s/lib/heartbeat"
45a1af
+                                   % os.environ.get("OCF_ROOT"))
45a1af
 sys.path.append(OCF_FUNCTIONS_DIR)
45a1af
 
45a1af
 from ocf import *
45a1af
@@ -43,6 +44,10 @@
45a1af
   import urllib2 as urlrequest
45a1af
 
45a1af
 
45a1af
+# Constants for alias add/remove modes
45a1af
+ADD = 0
45a1af
+REMOVE = 1
45a1af
+
45a1af
 CONN = None
45a1af
 THIS_VM = None
45a1af
 ALIAS = None
45a1af
@@ -53,27 +58,27 @@
45a1af
 
45a1af
 <resource-agent name="gcp-vpc-move-vip">
45a1af
   <version>1.0</version>
45a1af
-  <longdesc lang="en">Floating IP Address on Google Cloud Platform - Using Alias IP address functionality to attach a secondary IP address to a running instance</longdesc>
45a1af
-  <shortdesc lang="en">Floating IP Address on Google Cloud Platform</shortdesc>
45a1af
+  <longdesc lang="en">Floating IP Address or Range on Google Cloud Platform - Using Alias IP address functionality to attach a secondary IP range to a running instance</longdesc>
45a1af
+  <shortdesc lang="en">Floating IP Address or Range on Google Cloud Platform</shortdesc>
45a1af
   <parameters>
45a1af
     <parameter name="alias_ip" unique="1" required="1">
45a1af
-      <longdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</longdesc>
45a1af
-      <shortdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</shortdesc>
45a1af
+      <longdesc lang="en">IP range to be added including CIDR netmask (e.g., 192.168.0.1/32)</longdesc>
45a1af
+      <shortdesc lang="en">IP range to be added including CIDR netmask (e.g., 192.168.0.1/32)</shortdesc>
45a1af
       <content type="string" default="" />
45a1af
     </parameter>
45a1af
-    <parameter name="alias_range_name" unique="1" required="0">
45a1af
+    <parameter name="alias_range_name" unique="0" required="0">
45a1af
       <longdesc lang="en">Subnet name for the Alias IP</longdesc>
45a1af
       <shortdesc lang="en">Subnet name for the Alias IP</shortdesc>
45a1af
       <content type="string" default="" />
45a1af
     </parameter>
45a1af
-    <parameter name="hostlist" unique="1" required="0">
45a1af
-      <longdesc lang="en">List of hosts in the cluster</longdesc>
45a1af
+    <parameter name="hostlist" unique="0" required="0">
45a1af
+      <longdesc lang="en">List of hosts in the cluster, separated by spaces</longdesc>
45a1af
       <shortdesc lang="en">Host list</shortdesc>
45a1af
       <content type="string" default="" />
45a1af
     </parameter>
45a1af
     <parameter name="stackdriver_logging" unique="0" required="0">
45a1af
-      <longdesc lang="en">If enabled (set to true), IP failover logs will be posted to stackdriver logging. Using stackdriver logging requires additional libraries (google-cloud-logging).</longdesc>
45a1af
-      <shortdesc lang="en">Stackdriver-logging support. Requires additional libraries (google-cloud-logging).</shortdesc>
45a1af
+      <longdesc lang="en">If enabled (set to true), IP failover logs will be posted to stackdriver logging</longdesc>
45a1af
+      <shortdesc lang="en">Stackdriver-logging support</shortdesc>
45a1af
       <content type="boolean" default="" />
45a1af
     </parameter>
45a1af
   </parameters>
45a1af
@@ -107,7 +112,8 @@
45a1af
   url = '%s?%s' % (metadata_url, params)
45a1af
   request = urlrequest.Request(url, headers=METADATA_HEADERS)
45a1af
   request_opener = urlrequest.build_opener(urlrequest.ProxyHandler({}))
45a1af
-  return request_opener.open(request, timeout=timeout * 1.1).read().decode("utf-8")
45a1af
+  return request_opener.open(
45a1af
+      request, timeout=timeout * 1.1).read().decode("utf-8")
45a1af
 
45a1af
 
45a1af
 def get_instance(project, zone, instance):
45a1af
@@ -134,17 +140,21 @@
45a1af
     time.sleep(1)
45a1af
 
45a1af
 
45a1af
-def set_alias(project, zone, instance, alias, alias_range_name=None):
45a1af
-  fingerprint = get_network_ifaces(project, zone, instance)[0]['fingerprint']
45a1af
+def set_aliases(project, zone, instance, aliases, fingerprint):
45a1af
+  """Sets the alias IP ranges for an instance.
45a1af
+
45a1af
+  Args:
45a1af
+    project: string, the project in which the instance resides.
45a1af
+    zone: string, the zone in which the instance resides.
45a1af
+    instance: string, the name of the instance.
45a1af
+    aliases: list, the list of dictionaries containing alias IP ranges
45a1af
+      to be added to or removed from the instance.
45a1af
+    fingerprint: string, the fingerprint of the network interface.
45a1af
+  """
45a1af
   body = {
45a1af
-      'aliasIpRanges': [],
45a1af
-      'fingerprint': fingerprint
45a1af
+    'aliasIpRanges': aliases,
45a1af
+    'fingerprint': fingerprint
45a1af
   }
45a1af
-  if alias:
45a1af
-    obj = {'ipCidrRange': alias}
45a1af
-    if alias_range_name:
45a1af
-      obj['subnetworkRangeName'] = alias_range_name
45a1af
-    body['aliasIpRanges'].append(obj)
45a1af
 
45a1af
   request = CONN.instances().updateNetworkInterface(
45a1af
       instance=instance, networkInterface='nic0', project=project, zone=zone,
45a1af
@@ -153,21 +163,75 @@
45a1af
   wait_for_operation(project, zone, operation)
45a1af
 
45a1af
 
45a1af
-def get_alias(project, zone, instance):
45a1af
-  iface = get_network_ifaces(project, zone, instance)
45a1af
+def add_rm_alias(mode, project, zone, instance, alias, alias_range_name=None):
45a1af
+  """Adds or removes an alias IP range for a GCE instance.
45a1af
+
45a1af
+  Args:
45a1af
+    mode: int, a constant (ADD (0) or REMOVE (1)) indicating the
45a1af
+      operation type.
45a1af
+    project: string, the project in which the instance resides.
45a1af
+    zone: string, the zone in which the instance resides.
45a1af
+    instance: string, the name of the instance.
45a1af
+    alias: string, the alias IP range to be added to or removed from
45a1af
+      the instance.
45a1af
+    alias_range_name: string, the subnet name for the alias IP range.
45a1af
+
45a1af
+  Returns:
45a1af
+    True if the existing list of alias IP ranges was modified, or False
45a1af
+    otherwise.
45a1af
+  """
45a1af
+  ifaces = get_network_ifaces(project, zone, instance)
45a1af
+  fingerprint = ifaces[0]['fingerprint']
45a1af
+
45a1af
+  try:
45a1af
+    old_aliases = ifaces[0]['aliasIpRanges']
45a1af
+  except KeyError:
45a1af
+    old_aliases = []
45a1af
+
45a1af
+  new_aliases = [a for a in old_aliases if a['ipCidrRange'] != alias]
45a1af
+
45a1af
+  if alias:
45a1af
+    if mode == ADD:
45a1af
+      obj = {'ipCidrRange': alias}
45a1af
+      if alias_range_name:
45a1af
+        obj['subnetworkRangeName'] = alias_range_name
45a1af
+      new_aliases.append(obj)
45a1af
+    elif mode == REMOVE:
45a1af
+      pass    # already removed during new_aliases build
45a1af
+    else:
45a1af
+      raise ValueError('Invalid value for mode: {}'.format(mode))
45a1af
+
45a1af
+  if (sorted(new_aliases) != sorted(old_aliases)):
45a1af
+    set_aliases(project, zone, instance, new_aliases, fingerprint)
45a1af
+    return True
45a1af
+  else:
45a1af
+    return False
45a1af
+
45a1af
+
45a1af
+def add_alias(project, zone, instance, alias, alias_range_name=None):
45a1af
+  return add_rm_alias(ADD, project, zone, instance, alias, alias_range_name)
45a1af
+
45a1af
+
45a1af
+def remove_alias(project, zone, instance, alias):
45a1af
+  return add_rm_alias(REMOVE, project, zone, instance, alias)
45a1af
+
45a1af
+
45a1af
+def get_aliases(project, zone, instance):
45a1af
+  ifaces = get_network_ifaces(project, zone, instance)
45a1af
   try:
45a1af
-    return iface[0]['aliasIpRanges'][0]['ipCidrRange']
45a1af
+    aliases = ifaces[0]['aliasIpRanges']
45a1af
+    return [a['ipCidrRange'] for a in aliases]
45a1af
   except KeyError:
45a1af
-    return ''
45a1af
+    return []
45a1af
 
45a1af
 
45a1af
-def get_localhost_alias():
45a1af
+def get_localhost_aliases():
45a1af
   net_iface = get_metadata('instance/network-interfaces', {'recursive': True})
45a1af
   net_iface = json.loads(net_iface)
45a1af
   try:
45a1af
-    return net_iface[0]['ipAliases'][0]
45a1af
+    return net_iface[0]['ipAliases']
45a1af
   except (KeyError, IndexError):
45a1af
-    return ''
45a1af
+    return []
45a1af
 
45a1af
 
45a1af
 def get_zone(project, instance):
45a1af
@@ -201,21 +265,17 @@
45a1af
 
45a1af
 
45a1af
 def gcp_alias_start(alias):
45a1af
-  my_alias = get_localhost_alias()
45a1af
+  my_aliases = get_localhost_aliases()
45a1af
   my_zone = get_metadata('instance/zone').split('/')[-1]
45a1af
   project = get_metadata('project/project-id')
45a1af
 
45a1af
-  # If I already have the IP, exit. If it has an alias IP that isn't the VIP,
45a1af
-  # then remove it
45a1af
-  if my_alias == alias:
45a1af
+  if alias in my_aliases:
45a1af
+    # TODO: Do we need to check alias_range_name?
45a1af
     logger.info(
45a1af
         '%s already has %s attached. No action required' % (THIS_VM, alias))
45a1af
     sys.exit(OCF_SUCCESS)
45a1af
-  elif my_alias:
45a1af
-    logger.info('Removing %s from %s' % (my_alias, THIS_VM))
45a1af
-    set_alias(project, my_zone, THIS_VM, '')
45a1af
 
45a1af
-  # Loops through all hosts & remove the alias IP from the host that has it
45a1af
+  # If the alias is currently attached to another host, detach it.
45a1af
   hostlist = os.environ.get('OCF_RESKEY_hostlist', '')
45a1af
   if hostlist:
45a1af
     hostlist = hostlist.replace(THIS_VM, '').split()
45a1af
@@ -223,47 +283,53 @@
45a1af
     hostlist = get_instances_list(project, THIS_VM)
45a1af
   for host in hostlist:
45a1af
     host_zone = get_zone(project, host)
45a1af
-    host_alias = get_alias(project, host_zone, host)
45a1af
-    if alias == host_alias:
45a1af
+    host_aliases = get_aliases(project, host_zone, host)
45a1af
+    if alias in host_aliases:
45a1af
       logger.info(
45a1af
-          '%s is attached to %s - Removing all alias IP addresses from %s' %
45a1af
-          (alias, host, host))
45a1af
-      set_alias(project, host_zone, host, '')
45a1af
+          '%s is attached to %s - Removing %s from %s' %
45a1af
+          (alias, host, alias, host))
45a1af
+      remove_alias(project, host_zone, host, alias)
45a1af
       break
45a1af
 
45a1af
-  # add alias IP to localhost
45a1af
-  set_alias(
45a1af
+  # Add alias IP range to localhost
45a1af
+  add_alias(
45a1af
       project, my_zone, THIS_VM, alias,
45a1af
       os.environ.get('OCF_RESKEY_alias_range_name'))
45a1af
 
45a1af
-  # Check the IP has been added
45a1af
-  my_alias = get_localhost_alias()
45a1af
-  if alias == my_alias:
45a1af
+  # Verify that the IP range has been added
45a1af
+  my_aliases = get_localhost_aliases()
45a1af
+  if alias in my_aliases:
45a1af
     logger.info('Finished adding %s to %s' % (alias, THIS_VM))
45a1af
-  elif my_alias:
45a1af
-    logger.error(
45a1af
-        'Failed to add IP. %s has an IP attached but it isn\'t %s' %
45a1af
-        (THIS_VM, alias))
45a1af
-    sys.exit(OCF_ERR_GENERIC)
45a1af
   else:
45a1af
-    logger.error('Failed to add IP address %s to %s' % (alias, THIS_VM))
45a1af
+    if my_aliases:
45a1af
+      logger.error(
45a1af
+          'Failed to add alias IP range %s. %s has alias IP ranges attached but'
45a1af
+          + ' they don\'t include %s' % (alias, THIS_VM, alias))
45a1af
+    else:
45a1af
+      logger.error(
45a1af
+          'Failed to add IP range %s. %s has no alias IP ranges attached'
45a1af
+           % (alias, THIS_VM))
45a1af
     sys.exit(OCF_ERR_GENERIC)
45a1af
 
45a1af
 
45a1af
 def gcp_alias_stop(alias):
45a1af
-  my_alias = get_localhost_alias()
45a1af
+  my_aliases = get_localhost_aliases()
45a1af
   my_zone = get_metadata('instance/zone').split('/')[-1]
45a1af
   project = get_metadata('project/project-id')
45a1af
 
45a1af
-  if my_alias == alias:
45a1af
-    logger.info('Removing %s from %s' % (my_alias, THIS_VM))
45a1af
-    set_alias(project, my_zone, THIS_VM, '')
45a1af
+  if alias in my_aliases:
45a1af
+    logger.info('Removing %s from %s' % (alias, THIS_VM))
45a1af
+    remove_alias(project, my_zone, THIS_VM, alias)
45a1af
+  else:
45a1af
+    logger.info(
45a1af
+        '%s is not attached to %s. No action required'
45a1af
+        % (alias, THIS_VM))
45a1af
 
45a1af
 
45a1af
 def gcp_alias_status(alias):
45a1af
-  my_alias = get_localhost_alias()
45a1af
-  if alias == my_alias:
45a1af
-    logger.info('%s has the correct IP address attached' % THIS_VM)
45a1af
+  my_aliases = get_localhost_aliases()
45a1af
+  if alias in my_aliases:
45a1af
+    logger.info('%s has the correct IP range attached' % THIS_VM)
45a1af
   else:
45a1af
     sys.exit(OCF_NOT_RUNNING)
45a1af
 
45a1af
@@ -275,7 +341,8 @@
45a1af
 
45a1af
   # Populate global vars
45a1af
   try:
45a1af
-    CONN = googleapiclient.discovery.build('compute', 'v1')
45a1af
+    CONN = googleapiclient.discovery.build('compute', 'v1',
45a1af
+                                           cache_discovery=False)
45a1af
   except Exception as e:
45a1af
     logger.error('Couldn\'t connect with google api: ' + str(e))
45a1af
     sys.exit(OCF_ERR_CONFIGURED)
45a1af
@@ -283,7 +350,8 @@
45a1af
   try:
45a1af
     THIS_VM = get_metadata('instance/name')
45a1af
   except Exception as e:
45a1af
-    logger.error('Couldn\'t get instance name, is this running inside GCE?: ' + str(e))
45a1af
+    logger.error('Couldn\'t get instance name, is this running inside GCE?: '
45a1af
+                 + str(e))
45a1af
     sys.exit(OCF_ERR_CONFIGURED)
45a1af
 
45a1af
   ALIAS = os.environ.get('OCF_RESKEY_alias_ip')
45a1af
@@ -309,7 +377,8 @@
45a1af
         formatter = logging.Formatter('gcp:alias "%(message)s"')
45a1af
         handler.setFormatter(formatter)
45a1af
         log.addHandler(handler)
45a1af
-        logger = logging.LoggerAdapter(log, {'OCF_RESOURCE_INSTANCE': OCF_RESOURCE_INSTANCE})
45a1af
+        logger = logging.LoggerAdapter(log, {'OCF_RESOURCE_INSTANCE':
45a1af
+                                             OCF_RESOURCE_INSTANCE})
45a1af
       except ImportError:
45a1af
         logger.error('Couldn\'t import google.cloud.logging, '
45a1af
             'disabling Stackdriver-logging support')