self.models = {}
self.log = logging.getLogger(__name__)
+ self.juju_command = juju_command
self.juju_secret = ""
self.info('K8S Juju connector initialized')
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
# 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
# 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:
: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,
async def bootstrap(
self,
cloud_name: str,
- cluster_uuid: str
+ cluster_uuid: str,
+ microk8s: bool
) -> bool:
"""Bootstrap a Kubernetes controller
: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
))
:returns: True upon success or raises an exception.
"""
cmd = [
- self.k8scli,
+ self.juju_command,
"destroy-controller",
"--destroy-all-models",
"--destroy-storage",
"""
# 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,
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,
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: