Skip to content

Commit

Permalink
Support the external-cloud-provider on bootstrap of k8s (#68)
Browse files Browse the repository at this point in the history
* Support the external-cloud-provider on bootstrap of k8s

* Use new datastore status
  • Loading branch information
addyess authored Apr 21, 2024
1 parent 873c263 commit d961ec8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,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,
ControlPlaneNodeJoinConfig,
Expand Down Expand Up @@ -97,6 +98,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 @@ -182,6 +185,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 @@ -190,8 +195,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 @@ -233,6 +237,7 @@ def _bootstrap_k8s_snap(self):

bootstrap_config = BootstrapConfig()
self._configure_datastore(bootstrap_config)
self._configure_cloud_provider(bootstrap_config)
bootstrap_config.service_cidr = self.config["service-cidr"]
bootstrap_config.control_plane_taints = self.config["register-with-taints"].split()
bootstrap_config.extra_sans = [_get_public_address()]
Expand Down Expand Up @@ -294,6 +299,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

0 comments on commit d961ec8

Please sign in to comment.