- if not reuse_cluster_uuid:
- # This is a new cluster, so bootstrap it
-
- cluster_uuid = str(uuid.uuid4())
-
- # Add k8s cloud to Juju (unless it's microk8s)
-
- # 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)
-
- await self.add_k8s(k8s_cloud, k8s_creds)
-
- # Bootstrap Juju controller
- self.bootstrap(k8s_cloud, cluster_uuid)
- else:
- # k8s_cloud = 'microk8s-test'
- k8s_cloud = "{}-k8s".format(namespace)
-
- await self.add_k8s(k8s_cloud, k8s_creds)
-
- await self.bootstrap(k8s_cloud, cluster_uuid)
-
- # Get the controller information
-
- # Parse ~/.local/share/juju/controllers.yaml
- # controllers.testing.api-endpoints|ca-cert|uuid
- with open(os.path.expanduser(
- "~/.local/share/juju/controllers.yaml"
- )) as f:
- controllers = yaml.load(f, Loader=yaml.Loader)
- controller = controllers['controllers'][cluster_uuid]
- endpoints = controller['api-endpoints']
- self.juju_endpoint = endpoints[0]
- self.juju_ca_cert = controller['ca-cert']
-
- # Parse ~/.local/share/juju/accounts
- # controllers.testing.user|password
- with open(os.path.expanduser(
- "~/.local/share/juju/accounts.yaml"
- )) as f:
- controllers = yaml.load(f, Loader=yaml.Loader)
- controller = controllers['controllers'][cluster_uuid]
-
- self.juju_user = controller['user']
- self.juju_secret = controller['password']
-
- print("user: {}".format(self.juju_user))
- print("secret: {}".format(self.juju_secret))
- print("endpoint: {}".format(self.juju_endpoint))
- print("ca-cert: {}".format(self.juju_ca_cert))
-
- # raise Exception("EOL")
-
- self.juju_public_key = None
-
- config = {
- 'endpoint': self.juju_endpoint,
- 'username': self.juju_user,
- 'secret': self.juju_secret,
- 'cacert': self.juju_ca_cert,
- 'namespace': namespace,
- 'microk8s': microk8s,
- }
-
- # Store the cluster configuration so it
- # can be used for subsequent calls
- await self.set_config(cluster_uuid, config)
-
- else:
- # This is an existing cluster, so get its config
- cluster_uuid = reuse_cluster_uuid
-
- config = self.get_config(cluster_uuid)
-
- self.juju_endpoint = config['endpoint']
- self.juju_user = config['username']
- self.juju_secret = config['secret']
- self.juju_ca_cert = config['cacert']
- self.juju_public_key = None
-
+ ###################################################
+ # TODO: Make it idempotent, calling add-k8s and #
+ # bootstrap whenever reuse_cluster_uuid is passed #
+ # as parameter #
+ # `init_env` is called to initialize the K8s #
+ # cluster for juju. If this initialization fails, #
+ # it can be called again by LCM with the param #
+ # reuse_cluster_uuid, e.g. to try to fix it. #
+ ###################################################
+
+ # This is a new cluster, so bootstrap it
+
+ cluster_uuid = reuse_cluster_uuid or str(uuid.uuid4())
+
+ # Is a local k8s cluster?
+ localk8s = self.is_local_k8s(k8s_creds)
+
+ # If the k8s is external, the juju controller needs a loadbalancer
+ loadbalancer = False if localk8s else True
+
+ # Name the new k8s cloud
+ k8s_cloud = "k8s-{}".format(cluster_uuid)
+
+ self.log.debug("Adding k8s cloud {}".format(k8s_cloud))
+ await self.add_k8s(k8s_cloud, k8s_creds)
+
+ # Bootstrap Juju controller
+ self.log.debug("Bootstrapping...")
+ await self.bootstrap(k8s_cloud, cluster_uuid, loadbalancer)
+ self.log.debug("Bootstrap done.")
+
+ # Get the controller information
+
+ # Parse ~/.local/share/juju/controllers.yaml
+ # controllers.testing.api-endpoints|ca-cert|uuid
+ self.log.debug("Getting controller endpoints")
+ with open(os.path.expanduser("~/.local/share/juju/controllers.yaml")) as f:
+ controllers = yaml.load(f, Loader=yaml.Loader)
+ controller = controllers["controllers"][cluster_uuid]
+ endpoints = controller["api-endpoints"]
+ juju_endpoint = endpoints[0]
+ juju_ca_cert = controller["ca-cert"]
+
+ # Parse ~/.local/share/juju/accounts
+ # controllers.testing.user|password
+ self.log.debug("Getting accounts")
+ with open(os.path.expanduser("~/.local/share/juju/accounts.yaml")) as f:
+ controllers = yaml.load(f, Loader=yaml.Loader)
+ controller = controllers["controllers"][cluster_uuid]
+
+ juju_user = controller["user"]
+ juju_secret = controller["password"]
+
+ config = {
+ "endpoint": juju_endpoint,
+ "username": juju_user,
+ "secret": juju_secret,
+ "cacert": juju_ca_cert,
+ "loadbalancer": loadbalancer,
+ }
+
+ # Store the cluster configuration so it
+ # can be used for subsequent calls
+ self.log.debug("Setting config")
+ await self.set_config(cluster_uuid, config)
+
+ # Test connection
+ controller = await self.get_controller(cluster_uuid)
+ await controller.disconnect()
+
+ # TODO: Remove these commented lines
+ # raise Exception("EOL")
+ # self.juju_public_key = None