Skip to content

Commit

Permalink
Merge pull request #300 from hemanthnakkina/local-mode-petname
Browse files Browse the repository at this point in the history
Make cloud names unique instead of constants
  • Loading branch information
gboutry authored Jul 31, 2024
2 parents c4f789e + def60fb commit a9909b8
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 95 deletions.
17 changes: 13 additions & 4 deletions sunbeam-python/sunbeam/commands/juju.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
ResultType,
convert_proxy_to_model_configs,
)
from sunbeam.jobs.deployment import Deployment
from sunbeam.jobs.deployments import DeploymentsConfig
from sunbeam.jobs.juju import (
CONTROLLER_MODEL,
ApplicationNotFoundException,
Expand Down Expand Up @@ -2050,19 +2050,24 @@ class SaveControllerStep(BaseStep, JujuStepHelper):

def __init__(
self,
deployment: Deployment,
controller: str | None,
deployment_name: str,
deployments_config: DeploymentsConfig,
data_location: Path,
is_external: bool = False,
force: bool = False,
):
super().__init__(
"Save controller information",
"Saving controller information locally",
)
self.deployment = deployment
self.deployment_name = deployment_name
self.deployments_config = deployments_config
self.deployment = self.deployments_config.get_deployment(self.deployment_name)
self.controller = controller
self.data_location = data_location
self.is_external = is_external
self.force = force

def _get_controller(self, name: str) -> JujuController | None:
try:
Expand All @@ -2087,7 +2092,10 @@ def is_skip(self, status: Status | None = None) -> Result:
if self.controller_details is None:
return Result(ResultType.FAILED, f"Controller {self.controller} not found")

if self.controller_details == self.deployment.juju_controller:
if (
not self.force
and self.controller_details == self.deployment.juju_controller
):
return Result(ResultType.SKIPPED)

return Result(ResultType.COMPLETED)
Expand All @@ -2109,4 +2117,5 @@ def run(self, status: Status | None) -> Result:
user="admin", password=password
)

self.deployments_config.write()
return Result(ResultType.COMPLETED)
22 changes: 14 additions & 8 deletions sunbeam-python/sunbeam/commands/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
UnsupportedKubeconfigException,
run_sync,
)
from sunbeam.jobs.k8s import K8S_CLOUD, K8S_KUBECONFIG_KEY, validate_cidr_or_ip_range
from sunbeam.jobs.k8s import (
CREDENTIAL_SUFFIX,
K8S_CLOUD_SUFFIX,
K8S_KUBECONFIG_KEY,
validate_cidr_or_ip_range,
)
from sunbeam.jobs.manifest import Manifest
from sunbeam.jobs.questions import (
PromptQuestion,
Expand All @@ -62,7 +67,6 @@
K8S_APP_TIMEOUT = 180 # 3 minutes, managing the application should be fast
K8S_UNIT_TIMEOUT = 1200 # 20 minutes, adding / removing units can take a long time
K8S_ENABLE_ADDONS_TIMEOUT = 180 # 3 minutes
CREDENTIAL_SUFFIX = "-creds"
K8SD_SNAP_SOCKET = "/var/snap/k8s/common/var/lib/k8sd/state/control.socket"


Expand Down Expand Up @@ -348,12 +352,12 @@ def run(self, status: Status | None = None) -> Result:
class AddK8SCloudStep(BaseStep, JujuStepHelper):
_KUBECONFIG = K8S_KUBECONFIG_KEY

def __init__(self, client: Client, jhelper: JujuHelper):
def __init__(self, deployment: Deployment, jhelper: JujuHelper):
super().__init__("Add K8S cloud", "Adding K8S cloud to Juju controller")
self.client = client
self.client = deployment.get_client()
self.jhelper = jhelper
self.name = K8S_CLOUD
self.credential_name = f"{K8S_CLOUD}{CREDENTIAL_SUFFIX}"
self.cloud_name = f"{deployment.name}{K8S_CLOUD_SUFFIX}"
self.credential_name = f"{self.cloud_name}{CREDENTIAL_SUFFIX}"

def is_skip(self, status: Status | None = None) -> Result:
"""Determines if the step should be skipped or not.
Expand All @@ -364,7 +368,7 @@ def is_skip(self, status: Status | None = None) -> Result:
clouds = run_sync(self.jhelper.get_clouds())
LOG.debug(f"Clouds registered in the controller: {clouds}")
# TODO(hemanth): Need to check if cloud credentials are also created?
if f"cloud-{self.name}" in clouds.keys():
if f"cloud-{self.cloud_name}" in clouds.keys():
return Result(ResultType.SKIPPED)

return Result(ResultType.COMPLETED)
Expand All @@ -374,7 +378,9 @@ def run(self, status: Status | None = None) -> Result:
try:
kubeconfig = read_config(self.client, self._KUBECONFIG)
run_sync(
self.jhelper.add_k8s_cloud(self.name, self.credential_name, kubeconfig)
self.jhelper.add_k8s_cloud(
self.cloud_name, self.credential_name, kubeconfig
)
)
except (ConfigItemNotFoundException, UnsupportedKubeconfigException) as e:
LOG.debug("Failed to add k8s cloud to Juju controller", exc_info=True)
Expand Down
12 changes: 6 additions & 6 deletions sunbeam-python/sunbeam/commands/microk8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
run_sync,
)
from sunbeam.jobs.k8s import (
MICROK8S_CLOUD,
CREDENTIAL_SUFFIX,
K8S_CLOUD_SUFFIX,
MICROK8S_KUBECONFIG_KEY,
validate_cidr_or_ip_range,
)
Expand All @@ -51,7 +52,6 @@
APPLICATION = "microk8s"
MICROK8S_APP_TIMEOUT = 180 # 3 minutes, managing the application should be fast
MICROK8S_UNIT_TIMEOUT = 1200 # 20 minutes, adding / removing units can take a long time
CREDENTIAL_SUFFIX = "-creds"
MICROK8S_CONFIG_KEY = "TerraformVarsMicrok8s"
MICROK8S_ADDONS_CONFIG_KEY = "TerraformVarsMicrok8sAddons"

Expand Down Expand Up @@ -205,14 +205,14 @@ def get_unit_timeout(self) -> int:
class AddMicrok8sCloudStep(BaseStep, JujuStepHelper):
_CONFIG = MICROK8S_KUBECONFIG_KEY

def __init__(self, client: Client, jhelper: JujuHelper):
def __init__(self, deployment: Deployment, jhelper: JujuHelper):
super().__init__(
"Add MicroK8S cloud", "Adding MicroK8S cloud to Juju controller"
)
self.client = client
self.client = deployment.get_client()
self.jhelper = jhelper
self.cloud_name = MICROK8S_CLOUD
self.credential_name = f"{MICROK8S_CLOUD}{CREDENTIAL_SUFFIX}"
self.cloud_name = f"{deployment.name}{K8S_CLOUD_SUFFIX}"
self.credential_name = f"{self.cloud_name}{CREDENTIAL_SUFFIX}"

def is_skip(self, status: Optional[Status] = None) -> Result:
"""Determines if the step should be skipped or not.
Expand Down
10 changes: 5 additions & 5 deletions sunbeam-python/sunbeam/commands/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from sunbeam.clusterd.client import Client
from sunbeam.clusterd.service import ConfigItemNotFoundException
from sunbeam.commands.juju import JujuStepHelper
from sunbeam.commands.k8s import CREDENTIAL_SUFFIX
from sunbeam.commands.terraform import TerraformException, TerraformHelper
from sunbeam.jobs.common import (
RAM_32_GB_IN_KB,
Expand All @@ -37,8 +36,9 @@
update_config,
update_status_background,
)
from sunbeam.jobs.deployment import Deployment
from sunbeam.jobs.juju import JujuHelper, JujuWaitException, TimeoutException, run_sync
from sunbeam.jobs.k8s import K8SHelper
from sunbeam.jobs.k8s import CREDENTIAL_SUFFIX, K8SHelper
from sunbeam.jobs.manifest import Manifest
from sunbeam.jobs.questions import (
PromptQuestion,
Expand Down Expand Up @@ -107,7 +107,7 @@ class DeployControlPlaneStep(BaseStep, JujuStepHelper):

def __init__(
self,
client: Client,
deployment: Deployment,
tfhelper: TerraformHelper,
jhelper: JujuHelper,
manifest: Manifest,
Expand All @@ -121,7 +121,7 @@ def __init__(
"Deploying OpenStack Control Plane",
"Deploying OpenStack Control Plane to Kubernetes (this may take a while)",
)
self.client = client
self.client = deployment.get_client()
self.tfhelper = tfhelper
self.jhelper = jhelper
self.manifest = manifest
Expand All @@ -131,7 +131,7 @@ def __init__(
self.proxy_settings = proxy_settings or {}
self.force = force
self.model = OPENSTACK_MODEL
self.cloud = K8SHelper.get_cloud()
self.cloud = K8SHelper.get_cloud(deployment.name)

def get_storage_tfvars(self, storage_nodes: list[dict]) -> dict:
"""Create terraform variables related to storage."""
Expand Down
6 changes: 3 additions & 3 deletions sunbeam-python/sunbeam/features/observability/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(
self.manifest = self.feature.manifest
self.client = self.feature.deployment.get_client()
self.model = OBSERVABILITY_MODEL
self.cloud = K8SHelper.get_cloud()
self.cloud = K8SHelper.get_cloud(self.feature.deployment.name)

def run(self, status: Optional[Status] = None) -> Result:
"""Execute configuration using terraform."""
Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(
self.manifest = self.feature.manifest
self.client = self.feature.deployment.get_client()
self.model = OBSERVABILITY_MODEL
self.cloud = K8SHelper.get_cloud()
self.cloud = K8SHelper.get_cloud(self.feature.deployment.name)

def run(self, status: Optional[Status] = None) -> Result:
"""Execute configuration using terraform."""
Expand Down Expand Up @@ -325,7 +325,7 @@ def __init__(
self.manifest = self.feature.manifest
self.jhelper = jhelper
self.model = OBSERVABILITY_MODEL
self.cloud = K8SHelper.get_cloud()
self.cloud = K8SHelper.get_cloud(self.feature.deployment.name)

def run(self, status: Optional[Status] = None) -> Result:
"""Execute configuration using terraform."""
Expand Down
2 changes: 0 additions & 2 deletions sunbeam-python/sunbeam/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

LOG = logging.getLogger(__name__)
DEFAULT_CONFIG = {
"juju.cloud.type": "manual",
"juju.cloud.name": "sunbeam",
"daemon.group": "snap_daemon",
"daemon.debug": False,
"k8s.provider": "microk8s",
Expand Down
9 changes: 9 additions & 0 deletions sunbeam-python/sunbeam/jobs/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def switch(self, name: str) -> None:
self.active = name
self.write()

def get_minimal_info(self) -> dict:
"""Get deployments config with minimal information."""
self_dict = self.model_dump()
# self_dict has deployments with Deployment dict but not of provider
# so workaround to add each deployment based on provider
deployments = [d.model_dump(include={"name", "type"}) for d in self.deployments]
self_dict["deployments"] = deployments
return self_dict


def deployment_path(snap: Snap) -> Path:
"""Path to deployments configuration."""
Expand Down
12 changes: 4 additions & 8 deletions sunbeam-python/sunbeam/jobs/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
LOG = logging.getLogger(__name__)

# --- Microk8s specific
MICROK8S_CLOUD = "sunbeam-microk8s"
MICROK8S_KUBECONFIG_KEY = "Microk8sConfig"
MICROK8S_DEFAULT_STORAGECLASS = "microk8s-hostpath"
METALLB_ANNOTATION = "metallb.universe.tf/loadBalancerIPs"

# --- K8s specific
K8S_CLOUD = "sunbeam-k8s"
K8S_DEFAULT_STORAGECLASS = "csi-rawfile-default"
K8S_KUBECONFIG_KEY = "K8SKubeConfig"
SERVICE_LB_ANNOTATION = "io.cilium/lb-ipam-ips"

SUPPORTED_K8S_PROVIDERS = ["k8s", "microk8s"]
CREDENTIAL_SUFFIX = "-creds"
K8S_CLOUD_SUFFIX = "-k8s"


def validate_cidr_or_ip_range(ip_ranges: str):
Expand Down Expand Up @@ -69,13 +69,9 @@ def get_provider(cls) -> str:
return provider

@classmethod
def get_cloud(cls) -> str:
def get_cloud(cls, deployment_name: str) -> str:
"""Return cloud name matching provider."""
match cls.get_provider():
case "k8s":
return K8S_CLOUD
case _:
return MICROK8S_CLOUD
return f"{deployment_name}{K8S_CLOUD_SUFFIX}"

@classmethod
def get_default_storageclass(cls) -> str:
Expand Down
Loading

0 comments on commit a9909b8

Please sign in to comment.