Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Merge branch 'fix-missing-external-network' into 'dev'
Browse files Browse the repository at this point in the history
Fix missing external network

See merge request PI/koris!142
  • Loading branch information
obitech committed Sep 11, 2019
2 parents b672915 + e60d937 commit 88f43d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CLUSTER_NAME ?= koris-pipeline-$(REV_NUMBER)$(BUILD_SUFFIX)
KUBECONFIG ?= $(CLUSTER_NAME)-admin.conf
CIDR ?= 192.168.1.0\/16
CONFIG_FILE ?= tests/koris_test.yml
UBUNTU_VER ?= 16.04

BROWSER := $(PY) -c "$$BROWSER_PYSCRIPT"

Expand Down Expand Up @@ -386,7 +387,7 @@ security-checks-nodes:
@kubectl logs kube-bench-node --kubeconfig=${KUBECONFIG}

update-config: KEY ?= kube ## create a test configuration file
update-config: IMAGE ?= $(shell openstack image list -c Name -f value --sort name:desc | grep 'koris-[[:digit:]]' | head -n 1)
update-config: IMAGE ?= $(shell openstack image list -c Name -f value --sort name:desc | grep 'koris-ubuntu-${UBUNTU_VER}-[[:digit:]]' | head -n 1)
update-config:
@sed -i "s/%%CLUSTER_NAME%%/$(CLUSTER_NAME)/g" $(CONFIG_FILE)
@sed -i "s/%%LATEST_IMAGE%%/$(IMAGE)/g" $(CONFIG_FILE)
Expand Down
31 changes: 20 additions & 11 deletions koris/cloud/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,8 @@ def get_or_create(self):

# pylint: disable=inconsistent-return-statements
@staticmethod
def find_external_network(conn, default="ext02", fallback="ext01"):
def find_external_network(conn, default="ext02", fallback='bgp-noris',
autodetect=True):
"""Finds and returns an external network in OpenStack.
This function will look for all external networks, then try to find the
Expand All @@ -992,6 +993,8 @@ def find_external_network(conn, default="ext02", fallback="ext01"):
default (str): The default external network to use.
fallback (str): The fallback external network to use in case the default
is not found.
autodetect (bool): If network isn't given with router in the
config and the default is not found try and find one.
Returns:
An :class:`OpenStackAPI.network.v2.network` object or None if no external
Expand All @@ -1000,13 +1003,13 @@ def find_external_network(conn, default="ext02", fallback="ext01"):

# Retrieve all external networks as list

# import pdb; pdb.set_trace()
ext_networks = list(conn.network.networks(is_router_external=True))

for net_name in [default, fallback]:
nets = [x for x in ext_networks if x.name == net_name]
if nets:
return nets[0]
nets = [x for x in ext_networks if x.name in [default, fallback]]
if nets:
return nets[0]
if autodetect and ext_networks:
return ext_networks[0]


class OSSubnet: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -1130,11 +1133,17 @@ def _get_ext_net(self):
Returns:
The external network as OpenStack Network Object.
"""

ext_net = OSNetwork.find_external_network(self.conn)
if ext_net is None:
LOGGER.error("No external network found")
raise RuntimeError("no external network found")
fallback = self.config.get('private_net',
{}).get('subnet',
{}).get('router', {}).get('name')

ext_net = OSNetwork.find_external_network(
self.conn, fallback=fallback)
if not ext_net:
msg = (f"Could not find any external network "
"({fallback} specified for router isn't found either.)")
LOGGER.error(msg)
raise RuntimeError(msg)

return ext_net

Expand Down
2 changes: 1 addition & 1 deletion tests/koris_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
master_flavor: 'ECS.GP1.2-8'
node_flavor: 'ECS.C1.4-8'
version:
k8s: "1.13.6"
k8s: "1.13.10"
# If your project has multiple networks you MUST specify the subnetwork too
# don't change this unless you know what you are doing
# per default koris will create a network for your cluster and route
Expand Down
6 changes: 3 additions & 3 deletions tests/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def no_networks(*args, **kwargs):


def other_networks(*args, **kwargs):
return [Network("hello", "ajsdlk")]
return [Network("hello", "bgp-noris")]


def valid_plus_other(*args, **kwargs):
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_other_networks():
conn = MagicMock()
conn.network.networks = other_networks

assert OSNetwork.find_external_network(conn) is None
assert OSNetwork.find_external_network(conn).name == "hello"


def test_valid_plus_other_networks():
Expand All @@ -70,7 +70,7 @@ def test_fallback_networks():
conn = MagicMock()
conn.network.networks = valid_fallback

assert OSNetwork.find_external_network(conn).name == "ext01"
assert OSNetwork.find_external_network(conn).name == "hello"


def test_get_connection():
Expand Down

0 comments on commit 88f43d1

Please sign in to comment.