Add ModelConfig 40/10540/1
authorDavid Garcia <david.garcia@canonical.com>
Wed, 10 Mar 2021 19:00:53 +0000 (20:00 +0100)
committergarciadav <david.garcia@canonical.com>
Wed, 24 Mar 2021 14:09:39 +0000 (15:09 +0100)
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 <david.garcia@canonical.com>
(cherry picked from commit a71d4a04c1e8ad3ffe1a129024e6dbc14d6d3bd5)

n2vc/config.py [new file with mode: 0644]
n2vc/k8s_juju_conn.py
n2vc/libjuju.py
n2vc/n2vc_juju_conn.py
n2vc/tests/unit/test_k8s_juju_conn.py
n2vc/tests/unit/test_libjuju.py

diff --git a/n2vc/config.py b/n2vc/config.py
new file mode 100644 (file)
index 0000000..59a74be
--- /dev/null
@@ -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("_", "-")
index e3d8a67..5b158d7 100644 (file)
@@ -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
@@ -102,8 +103,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"])
@@ -111,8 +111,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,
index 161370b..e5a8c61 100644 (file)
@@ -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)
index 5435c4e..6e2e9d4 100644 (file)
@@ -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
index ae262b2..b182d8c 100644 (file)
@@ -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,
index 9ab6ddb..ad0933c 100644 (file)
@@ -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()