Juju/k8s fixes 91/8291/1
authorAdam Israel <adam.israel@canonical.com>
Mon, 2 Dec 2019 21:33:05 +0000 (16:33 -0500)
committerAdam Israel <adam.israel@canonical.com>
Mon, 2 Dec 2019 21:33:05 +0000 (16:33 -0500)
Fix how non-microk8s clusters are bootstrapped

Change-Id: Idf771fdd4d14f2876e0107f1d636eabc5169e170
Signed-off-by: Adam Israel <adam.israel@canonical.com>
n2vc/k8s_juju_conn.py
tests/test_k8s_juju_conn.py

index 3acc10e..1f7cc00 100644 (file)
@@ -69,6 +69,7 @@ class K8sJujuConnector(K8sConnector):
         self.models = {}
         self.log = logging.getLogger(__name__)
 
+        self.juju_command = juju_command
         self.juju_secret = ""
 
         self.info('K8S Juju connector initialized')
@@ -94,10 +95,6 @@ class K8sJujuConnector(K8sConnector):
         Bootstrapping cannot be done, by design, through the API. We need to
         use the CLI tools.
         """
-        # TODO: The path may change
-        jujudir = "/usr/local/bin"
-
-        self.k8scli = "{}/juju".format(jujudir)
 
         """
         WIP: Workflow
@@ -128,30 +125,19 @@ class K8sJujuConnector(K8sConnector):
 
             # Add k8s cloud to Juju (unless it's microk8s)
 
-            # Convert to a dict
-            # k8s_creds = yaml.safe_load(k8s_creds)
-
             # Does the kubeconfig contain microk8s?
             microk8s = self.is_microk8s_by_credentials(k8s_creds)
 
-            if not microk8s:
-                # Name the new k8s cloud
-                k8s_cloud = "{}-k8s".format(namespace)
-
-                print("Adding k8s cloud {}".format(k8s_cloud))
-                await self.add_k8s(k8s_cloud, k8s_creds)
+            # Name the new k8s cloud
+            k8s_cloud = "{}-k8s".format(namespace)
 
-                # Bootstrap Juju controller
-                print("Bootstrapping...")
-                await self.bootstrap(k8s_cloud, cluster_uuid)
-                print("Bootstrap done.")
-            else:
-                # k8s_cloud = 'microk8s-test'
-                k8s_cloud = "{}-k8s".format(namespace)
+            print("Adding k8s cloud {}".format(k8s_cloud))
+            await self.add_k8s(k8s_cloud, k8s_creds)
 
-                await self.add_k8s(k8s_cloud, k8s_creds)
-
-                await self.bootstrap(k8s_cloud, cluster_uuid)
+            # Bootstrap Juju controller
+            print("Bootstrapping...")
+            await self.bootstrap(k8s_cloud, cluster_uuid, microk8s)
+            print("Bootstrap done.")
 
             # Get the controller information
 
@@ -333,7 +319,7 @@ class K8sJujuConnector(K8sConnector):
         # Get or create the model, based on the namespace the cluster was
         # instantiated with.
         namespace = self.get_namespace(cluster_uuid)
-        namespace = "gitlab-demo"
+
         self.log.debug("Checking for model named {}".format(namespace))
         model = await self.get_model(namespace, cluster_uuid=cluster_uuid)
         if not model:
@@ -668,8 +654,9 @@ class K8sJujuConnector(K8sConnector):
 
         :returns: True if successful, otherwise raises an exception.
         """
-        cmd = [self.k8scli, "add-k8s", "--local", cloud_name]
 
+        cmd = [self.juju_command, "add-k8s", "--local", cloud_name]
+        print(cmd)
         p = subprocess.run(
             cmd,
             stdout=subprocess.PIPE,
@@ -711,7 +698,8 @@ class K8sJujuConnector(K8sConnector):
     async def bootstrap(
         self,
         cloud_name: str,
-        cluster_uuid: str
+        cluster_uuid: str,
+        microk8s: bool
     ) -> bool:
         """Bootstrap a Kubernetes controller
 
@@ -719,9 +707,18 @@ class K8sJujuConnector(K8sConnector):
 
         :param cloud_name str: The name of the cloud.
         :param cluster_uuid str: The UUID of the cluster to bootstrap.
+        :param microk8s bool: If this is a microk8s cluster.
         :returns: True upon success or raises an exception.
         """
-        cmd = [self.k8scli, "bootstrap", cloud_name, cluster_uuid]
+
+        if microk8s:
+            cmd = [self.juju_command, "bootstrap", cloud_name, cluster_uuid]
+        else:
+            """
+            For non-microk8s clusters, specify that the controller service is using a LoadBalancer.
+            """
+            cmd = [self.juju_command, "bootstrap", cloud_name, cluster_uuid, "--config", "controller-service-type=loadbalancer"]
+
         print("Bootstrapping controller {} in cloud {}".format(
             cluster_uuid, cloud_name
         ))
@@ -753,7 +750,7 @@ class K8sJujuConnector(K8sConnector):
         :returns: True upon success or raises an exception.
         """
         cmd = [
-            self.k8scli,
+            self.juju_command,
             "destroy-controller",
             "--destroy-all-models",
             "--destroy-storage",
@@ -971,7 +968,7 @@ class K8sJujuConnector(K8sConnector):
         """
 
         # Remove the bootstrapped controller
-        cmd = [self.k8scli, "remove-k8s", "--client", cloud_name]
+        cmd = [self.juju_command, "remove-k8s", "--client", cloud_name]
         p = subprocess.run(
             cmd,
             stdout=subprocess.PIPE,
@@ -984,7 +981,7 @@ class K8sJujuConnector(K8sConnector):
             raise Exception(p.stderr)
 
         # Remove the cloud from the local config
-        cmd = [self.k8scli, "remove-cloud", "--client", cloud_name]
+        cmd = [self.juju_command, "remove-cloud", "--client", cloud_name]
         p = subprocess.run(
             cmd,
             stdout=subprocess.PIPE,
index c272429..95b5f38 100644 (file)
@@ -62,24 +62,27 @@ async def main():
     fs.fs_connect(storage)
 
     client = n2vc.k8s_juju_conn.K8sJujuConnector(
-        kubectl_command = '/bin/true',
-        fs = fs,
+        kubectl_command='/snap/bin/kubectl',
+        juju_command='/snap/bin/juju',
+        fs=fs,
+        db=None,
     )
 
     # kubectl config view --raw
     # microk8s.config
 
     # if microk8s then
-    kubecfg = subprocess.getoutput('microk8s.config')
+    kubecfg = subprocess.getoutput('microk8s.config')
     # else
-    # kubecfg.subprocess.getoutput('kubectl config view --raw')
-    
-    k8screds = yaml.load(kubecfg, Loader=yaml.FullLoader)
+    kubecfg = subprocess.getoutput('kubectl config view --raw')
+    # print(kubecfg)
+
+    # k8screds = yaml.load(kubecfg, Loader=yaml.FullLoader)
     namespace = 'testing'
     kdu_model = "./tests/bundles/k8s-zookeeper.yaml"
 
     """init_env"""
-    cluster_uuid, _ = await client.init_env(k8screds, namespace, reuse_cluster_uuid=reuse_cluster_uuid)
+    cluster_uuid, _ = await client.init_env(kubecfg, namespace, reuse_cluster_uuid=reuse_cluster_uuid)
     print(cluster_uuid)
 
     if not reuse_cluster_uuid: