From ff675c6c0b3ceaa9abeed84abc5d7eb790e23f74 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Tue, 10 Sep 2019 15:22:41 +0200 Subject: [PATCH 1/3] Fix for missing external networks Due to recent changes in out openstack the builds for stable koris version failed with an error: No external network found This fixes this issue. The solution chosen here is first to change ext01 to bgp-noris. Also, if this is not found, it could be detected from the config and finally, if there is an external network, it will be chosen. Only in the rarest case, the code should stop with `No external network found`. --- koris/cloud/openstack.py | 24 ++++++++++++++++-------- tests/test_openstack.py | 6 +++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/koris/cloud/openstack.py b/koris/cloud/openstack.py index db2ac0a..be51025 100644 --- a/koris/cloud/openstack.py +++ b/koris/cloud/openstack.py @@ -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 @@ -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 @@ -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 @@ -1131,8 +1134,13 @@ def _get_ext_net(self): The external network as OpenStack Network Object. """ - ext_net = OSNetwork.find_external_network(self.conn) - if ext_net is None: + ext_net = OSNetwork.find_external_network( + self.conn, + fallback=self.config.get('private_net', + {}).get('subnet', + {}).get('router', {}).get('name') + ) + if not ext_net: LOGGER.error("No external network found") raise RuntimeError("no external network found") diff --git a/tests/test_openstack.py b/tests/test_openstack.py index f7b2856..4cbebb0 100644 --- a/tests/test_openstack.py +++ b/tests/test_openstack.py @@ -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): @@ -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(): @@ -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(): From 59c9afbd030991d5eacbc6e895bb3bcd39272df7 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Tue, 10 Sep 2019 15:25:43 +0200 Subject: [PATCH 2/3] Add ubuntu version to the image name This is because now images are published with a new schema: koris-ubuntu-16.04-2019-09-10 Or: koris-ubuntu-18.04-2019-09-10 This allows us to smoothly transition between Ubuntu versions. --- Makefile | 3 ++- tests/koris_test.yml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 738a350..73aff31 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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) diff --git a/tests/koris_test.yml b/tests/koris_test.yml index 3343605..9fcef88 100644 --- a/tests/koris_test.yml +++ b/tests/koris_test.yml @@ -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 From e60d937276ddc1b88592dfa2406095af3f08a9e5 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Tue, 10 Sep 2019 17:43:03 +0200 Subject: [PATCH 3/3] Store fallback parameter in a variable This allows specifying it in the exception and logging message. --- koris/cloud/openstack.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/koris/cloud/openstack.py b/koris/cloud/openstack.py index be51025..ee30714 100644 --- a/koris/cloud/openstack.py +++ b/koris/cloud/openstack.py @@ -1133,16 +1133,17 @@ def _get_ext_net(self): Returns: The external network as OpenStack Network Object. """ + fallback = self.config.get('private_net', + {}).get('subnet', + {}).get('router', {}).get('name') ext_net = OSNetwork.find_external_network( - self.conn, - fallback=self.config.get('private_net', - {}).get('subnet', - {}).get('router', {}).get('name') - ) + self.conn, fallback=fallback) if not ext_net: - LOGGER.error("No external network found") - raise RuntimeError("no external network found") + 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