From a71d4a04c1e8ad3ffe1a129024e6dbc14d6d3bd5 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Wed, 10 Mar 2021 20:00:53 +0100 Subject: [PATCH] Add ModelConfig This class maps the model-config from the vca_config. That config will be passed every time a model is added. Change-Id: I0a1e47ba7d708f3514c64a6f20d410a21fe8ea1d Signed-off-by: David Garcia --- n2vc/config.py | 84 +++++++++++++++++++++++++++ n2vc/k8s_juju_conn.py | 7 +-- n2vc/libjuju.py | 10 +--- n2vc/n2vc_juju_conn.py | 14 +---- n2vc/tests/unit/test_k8s_juju_conn.py | 3 +- n2vc/tests/unit/test_libjuju.py | 8 --- 6 files changed, 94 insertions(+), 32 deletions(-) create mode 100644 n2vc/config.py diff --git a/n2vc/config.py b/n2vc/config.py new file mode 100644 index 0000000..59a74be --- /dev/null +++ b/n2vc/config.py @@ -0,0 +1,84 @@ +# Copyright 2021 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +MODEL_CONFIG_KEYS = [ + "agent-metadata-url", + "agent-stream", + "apt-ftp-proxy", + "apt-http-proxy", + "apt-https-proxy", + "apt-mirror", + "apt-no-proxy", + "automatically-retry-hooks", + "backup-dir", + "cloudinit-userdata", + "container-image-metadata-url", + "container-image-stream", + "container-inherit-properties", + "container-networking-method", + "default-series", + "default-space", + "development", + "disable-network-management", + "egress-subnets", + "enable-os-refresh-update", + "enable-os-upgrade", + "fan-config", + "firewall-mode", + "ftp-proxy", + "http-proxy", + "https-proxy", + "ignore-machine-addresses", + "image-metadata-url", + "image-stream", + "juju-ftp-proxy", + "juju-http-proxy", + "juju-https-proxy", + "juju-no-proxy", + "logforward-enabled", + "logging-config", + "lxd-snap-channel", + "max-action-results-age", + "max-action-results-size", + "max-status-history-age", + "max-status-history-size", + "net-bond-reconfigure-delay", + "no-proxy", + "provisioner-harvest-mode", + "proxy-ssh", + "snap-http-proxy", + "snap-https-proxy", + "snap-store-assertions", + "snap-store-proxy", + "snap-store-proxy-url", + "ssl-hostname-verification", + "test-mode", + "transmit-vendor-metrics", + "update-status-hook-interval", +] + + +class ModelConfig(dict): + prefix = "model_config_" + + def __init__(self, config: dict): + for key, value in config.items(): + if ( + key.startswith(self.prefix) + and self._get_renamed_key(key) in MODEL_CONFIG_KEYS + ): + self.__setitem__(self._get_renamed_key(key), value) + + def _get_renamed_key(self, key): + return key.replace(self.prefix, "").replace("_", "-") diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 85e24f2..0e9d547 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -20,6 +20,7 @@ import tempfile import binascii import base64 +from n2vc.config import ModelConfig from n2vc.exceptions import K8sException, N2VCBadArgumentsException from n2vc.k8s_conn import K8sConnector from n2vc.kubectl import Kubectl, CORE_CLIENT, RBAC_CLIENT @@ -97,8 +98,7 @@ class K8sJujuConnector(K8sConnector): ) port = vca_config["port"] if "port" in vca_config else 17070 url = "{}:{}".format(vca_config["host"], port) - enable_os_upgrade = vca_config.get("enable_os_upgrade", True) - apt_mirror = vca_config.get("apt_mirror", None) + model_config = ModelConfig(vca_config) username = vca_config["user"] secret = vca_config["secret"] ca_cert = base64_to_cacert(vca_config["ca_cert"]) @@ -106,8 +106,7 @@ class K8sJujuConnector(K8sConnector): self.libjuju = Libjuju( endpoint=url, api_proxy=None, # Not needed for k8s charms - enable_os_upgrade=enable_os_upgrade, - apt_mirror=apt_mirror, + model_config=model_config, username=username, password=secret, cacert=ca_cert, diff --git a/n2vc/libjuju.py b/n2vc/libjuju.py index 7a73033..98e31d8 100644 --- a/n2vc/libjuju.py +++ b/n2vc/libjuju.py @@ -32,6 +32,7 @@ from juju.controller import Controller from juju.client import client from juju import tag +from n2vc.config import ModelConfig from n2vc.juju_watcher import JujuModelWatcher from n2vc.provisioner import AsyncSSHProvisioner from n2vc.n2vc_conn import N2VCConnector @@ -63,8 +64,7 @@ class Libjuju: log: logging.Logger = None, db: dict = None, n2vc: N2VCConnector = None, - apt_mirror: str = None, - enable_os_upgrade: bool = True, + model_config: ModelConfig = {}, ): """ Constructor @@ -99,11 +99,7 @@ class Libjuju: self.n2vc = n2vc # Generate config for models - self.model_config = {} - if apt_mirror: - self.model_config["apt-mirror"] = apt_mirror - self.model_config["enable-os-refresh-update"] = enable_os_upgrade - self.model_config["enable-os-upgrade"] = enable_os_upgrade + self.model_config = model_config self.loop.set_exception_handler(self.handle_exception) self.creating_model = asyncio.Lock(loop=self.loop) diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index 7714d4e..313430d 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -24,6 +24,7 @@ import asyncio import logging import os +from n2vc.config import ModelConfig from n2vc.exceptions import ( N2VCBadArgumentsException, N2VCException, @@ -149,15 +150,7 @@ class N2VCJujuConnector(N2VCConnector): ) self.api_proxy = None - if "enable_os_upgrade" in vca_config: - self.enable_os_upgrade = vca_config["enable_os_upgrade"] - else: - self.enable_os_upgrade = True - - if "apt_mirror" in vca_config: - self.apt_mirror = vca_config["apt_mirror"] - else: - self.apt_mirror = None + model_config = ModelConfig(vca_config) self.cloud = vca_config.get('cloud') self.k8s_cloud = None @@ -179,8 +172,6 @@ class N2VCJujuConnector(N2VCConnector): self.libjuju = Libjuju( endpoint=self.url, api_proxy=self.api_proxy, - enable_os_upgrade=self.enable_os_upgrade, - apt_mirror=self.apt_mirror, username=self.username, password=self.secret, cacert=self.ca_cert, @@ -188,6 +179,7 @@ class N2VCJujuConnector(N2VCConnector): log=self.log, db=self.db, n2vc=self, + model_config=model_config, ) # create juju pub key file in lcm container at diff --git a/n2vc/tests/unit/test_k8s_juju_conn.py b/n2vc/tests/unit/test_k8s_juju_conn.py index ae262b2..b182d8c 100644 --- a/n2vc/tests/unit/test_k8s_juju_conn.py +++ b/n2vc/tests/unit/test_k8s_juju_conn.py @@ -111,8 +111,7 @@ class K8sJujuConnInitSuccessTestCase(asynctest.TestCase): mock_libjuju.assert_called_once_with( endpoint="1.1.1.1:17070", api_proxy=None, # Not needed for k8s charms - enable_os_upgrade=True, - apt_mirror=None, + model_config={}, username="user", password="secret", cacert=mock_base64_to_cacert.return_value, diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index 9ab6ddb..ad0933c 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -66,8 +66,6 @@ class LibjujuTestCase(asynctest.TestCase): log=None, db={"get_one": []}, n2vc=n2vc, - apt_mirror="192.168.0.100", - enable_os_upgrade=True, ) logging.disable(logging.CRITICAL) loop.run_until_complete(self.libjuju.disconnect()) @@ -105,8 +103,6 @@ class LibjujuInitTestCase(asynctest.TestCase): log=None, db={"get_one": []}, n2vc=self.n2vc, - apt_mirror="192.168.0.100", - enable_os_upgrade=True, ) mock_update_endpoints.assert_called_once_with([self.endpoint]) mock__get_api_endpoints_db.assert_called_once() @@ -128,8 +124,6 @@ class LibjujuInitTestCase(asynctest.TestCase): log=None, db={"get_one": []}, n2vc=self.n2vc, - apt_mirror="192.168.0.100", - enable_os_upgrade=True, ) mock_update_endpoints.assert_not_called() mock__get_api_endpoints_db.assert_called_once() @@ -151,8 +145,6 @@ class LibjujuInitTestCase(asynctest.TestCase): log=None, db={"get_one": []}, n2vc=self.n2vc, - apt_mirror="192.168.0.100", - enable_os_upgrade=True, ) mock_update_endpoints.assert_called_once_with([self.endpoint]) mock__get_api_endpoints_db.assert_called_once() -- 2.25.1