Enable add_ips_to_server() and _needs_floating_ip() for pristine server resources

Previously, both functions [1] and [2] would have to be called with
server objects which have been enhanced by add_server_interfaces()
[3]. The latter adds 'public_v4' and 'private_v4' attributes to the
server objects which the default server resource [4] does not have.

Now, we check server's 'addresses' attributes for fixed and floating
ip addresses [5] in case the 'public_v4' and 'private_v4' attributes
are not available. The checks for fixed vs floating ip addresses are
equal to how find_nova_addresses() [6] distinguishes between both
ip address types.

[1] 3f81d0001d/openstack/cloud/_floating_ip.py (L979)
[2] 3f81d0001d/openstack/cloud/_floating_ip.py (L997)
[3] 3f81d0001d/openstack/cloud/meta.py (L439)
[4] https://opendev.org/openstack/openstacksdk/src/branch/master/openstack/compute/v2/server.py
[5] 3f81d0001d/openstack/compute/v2/server.py (L81)
[6] 3f81d0001d/openstack/cloud/meta.py (L69)

Change-Id: I064ed922be440d911fbc492dab8465e8f75236df
This commit is contained in:
Jakob Meng 2022-08-03 11:42:15 +02:00
parent 3f81d0001d
commit 0ded7ac398
1 changed files with 25 additions and 12 deletions

View File

@ -997,34 +997,47 @@ class FloatingIPCloudMixin(_normalize.Normalizer):
def _needs_floating_ip(self, server, nat_destination):
"""Figure out if auto_ip should add a floating ip to this server.
If the server has a public_v4 it does not need a floating ip.
If the server has a floating ip it does not need another one.
If the server does not have a private_v4 it does not need a
If the server does not have a fixed ip address it does not need a
floating ip.
If self.private then the server does not need a floating ip.
If the cloud runs nova, and the server has a private_v4 and not
a public_v4, then the server needs a floating ip.
If the cloud runs nova, and the server has a private address and not a
public address, then the server needs a floating ip.
If the server has a private_v4 and no public_v4 and the cloud has
a network from which floating IPs come that is connected via a
router to the network from which the private_v4 address came,
If the server has a fixed ip address and no floating ip address and the
cloud has a network from which floating IPs come that is connected via
a router to the network from which the fixed ip address came,
then the server needs a floating ip.
If the server has a private_v4 and no public_v4 and the cloud
does not have a network from which floating ips come, or it has
If the server has a fixed ip address and no floating ip address and the
cloud does not have a network from which floating ips come, or it has
one but that network is not connected to the network from which
the server's private_v4 address came via a router, then the
the server's fixed ip address came via a router, then the
server does not need a floating ip.
"""
if not self._has_floating_ips():
return False
if server['public_v4']:
if server['addresses'] is None:
# fetch missing server details, e.g. because
# meta.add_server_interfaces() was not called
server = self.compute.get_server(server)
if server['public_v4'] \
or any([any([address['OS-EXT-IPS:type'] == 'floating'
for address in addresses])
for addresses
in (server['addresses'] or {}).values()]):
return False
if not server['private_v4']:
if not server['private_v4'] \
and not any([any([address['OS-EXT-IPS:type'] == 'fixed'
for address in addresses])
for addresses
in (server['addresses'] or {}).values()]):
return False
if self.private: