- # 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"]
- self.juju_endpoint = endpoints[0]
- self.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]
-
- self.juju_user = controller["user"]
- self.juju_secret = controller["password"]
-
- # 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,
- "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)
-
- # Login to the k8s cluster
- if not self.authenticated:
- await self.login(cluster_uuid)
-
- # We're creating a new cluster
- # print("Getting model {}".format(self.get_namespace(cluster_uuid),
- # cluster_uuid=cluster_uuid))
- # model = await self.get_model(
- # self.get_namespace(cluster_uuid),
- # cluster_uuid=cluster_uuid
- # )
-
- # Disconnect from the model
- # if model and model.is_connected():
- # await model.disconnect()
-
- return cluster_uuid, True
+ default_storage_class = kubectl.get_default_storage_class()
+ self.log.debug("Default storage class: {}".format(default_storage_class))
+ await libjuju.add_k8s(
+ name=cluster_uuid,
+ rbac_id=rbac_id,
+ token=token,
+ client_cert_data=client_cert_data,
+ configuration=kubectl.configuration,
+ storage_class=default_storage_class,
+ credential_name=self._get_credential_name(cluster_uuid),
+ )
+ self.log.debug("K8s cluster added to juju controller")
+ return cluster_uuid, True
+ except Exception as e:
+ self.log.error("Error initializing k8scluster: {}".format(e), exc_info=True)
+ if len(cleanup_data) > 0:
+ self.log.debug("Cleaning up created resources in k8s cluster...")
+ for item in cleanup_data:
+ delete_function = item["delete"]
+ delete_args = item["args"]
+ delete_function(*delete_args)
+ self.log.debug("Cleanup finished")
+ raise e