Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the external-cloud-provider on bootstrap of k8s #68

Merged
merged 8 commits into from
Apr 21, 2024
2 changes: 2 additions & 0 deletions charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ provides:
requires:
etcd:
interface: etcd
external-cloud-provider:
interface: external_cloud_provider
3 changes: 2 additions & 1 deletion charms/worker/k8s/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cosl == 0.0.8
pydantic == 1.*
charm-lib-reconciler @ git+https://github.com/charmed-kubernetes/charm-lib-reconciler@main
charm-lib-contextual-status @ git+https://github.com/charmed-kubernetes/charm-lib-contextual-status@main
charm-lib-node-base @ git+https://github.com/charmed-kubernetes/layer-kubernetes-node-base@main#subdirectory=ops
charm-lib-node-base @ git+https://github.com/charmed-kubernetes/layer-kubernetes-node-base@main#subdirectory=ops
charm-lib-interface-external-cloud-provider @ git+https://github.com/charmed-kubernetes/charm-lib-interface-external-cloud-provider@main
21 changes: 19 additions & 2 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import yaml
from charms.contextual_status import WaitingStatus, on_error
from charms.grafana_agent.v0.cos_agent import COSAgentProvider
from charms.interface_external_cloud_provider import ExternalCloudProvider
from charms.k8s.v0.k8sd_api_manager import (
BootstrapConfig,
CreateClusterRequest,
Expand Down Expand Up @@ -85,6 +86,8 @@ def __init__(self, *args):
super().__init__(*args)
factory = UnixSocketConnectionFactory(unix_socket=K8SD_SNAP_SOCKET, timeout=320)
self.api_manager = K8sdAPIManager(factory)
xcp_relation = "external-cloud-provider" if self.is_control_plane else ""
self.xcp = ExternalCloudProvider(self, xcp_relation)
self.cos = COSIntegration(self)
self.reconciler = Reconciler(self, self._reconcile)
self.distributor = TokenDistributor(self, self.get_node_name(), self.api_manager)
Expand Down Expand Up @@ -170,6 +173,8 @@ def get_node_name(self) -> str:
Returns:
the hostname of the machine.
"""
if self.xcp.name == "aws":
return socket.getfqdn().lower()
return socket.gethostname().lower()

def get_cloud_name(self) -> str:
Expand All @@ -178,8 +183,7 @@ def get_cloud_name(self) -> str:
Returns:
the cloud hosting the machine.
"""
# TODO: adjust to detect the correct cloud
return ""
return self.xcp.name or ""

@on_error(ops.BlockedStatus("Failed to install k8s snap."), SnapError)
def _install_k8s_snap(self):
Expand Down Expand Up @@ -221,6 +225,7 @@ def _bootstrap_k8s_snap(self):

bootstrap_config = BootstrapConfig()
self._configure_datastore(bootstrap_config)
self._configure_cloud_provider(bootstrap_config)

status.add(ops.MaintenanceStatus("Bootstrapping Cluster"))

Expand Down Expand Up @@ -284,6 +289,18 @@ def _configure_datastore(self, config: BootstrapConfig):
elif datastore == "dqlite":
log.info("Using dqlite as datastore")

def _configure_cloud_provider(self, config: BootstrapConfig):
"""Configure the cloud-provider for the Kubernetes cluster.

Args:
config (BootstrapConfig): The bootstrap configuration object for
the Kubernetes cluster that is being configured. This object
will be modified in-place.
"""
if self.xcp.has_xcp:
log.info("Using external as cloud-provider")
config.cloud_provider = "external"

def _revoke_cluster_tokens(self):
"""Revoke tokens for the units in the cluster and k8s-cluster relations.

Expand Down
Loading