From d0e05e23074ab3f0fcebf502887ed12479d84683 Mon Sep 17 00:00:00 2001 From: Adam Dyess Date: Tue, 16 Apr 2024 16:31:47 -0500 Subject: [PATCH 1/2] Support the external-cloud-provider on bootstrap of k8s --- charms/worker/k8s/charmcraft.yaml | 2 ++ charms/worker/k8s/requirements.txt | 3 ++- charms/worker/k8s/src/charm.py | 21 +++++++++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/charms/worker/k8s/charmcraft.yaml b/charms/worker/k8s/charmcraft.yaml index f83d801e..bcf31003 100644 --- a/charms/worker/k8s/charmcraft.yaml +++ b/charms/worker/k8s/charmcraft.yaml @@ -98,3 +98,5 @@ provides: requires: etcd: interface: etcd + external-cloud-provider: + interface: external_cloud_provider diff --git a/charms/worker/k8s/requirements.txt b/charms/worker/k8s/requirements.txt index a4473a04..9acee428 100644 --- a/charms/worker/k8s/requirements.txt +++ b/charms/worker/k8s/requirements.txt @@ -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 \ No newline at end of file +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 diff --git a/charms/worker/k8s/src/charm.py b/charms/worker/k8s/src/charm.py index c346cc04..ca98b8a1 100755 --- a/charms/worker/k8s/src/charm.py +++ b/charms/worker/k8s/src/charm.py @@ -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, @@ -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) @@ -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: @@ -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): @@ -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")) @@ -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. From bb02bcd23e90630b3ac8afb19a2487a4050ddf0b Mon Sep 17 00:00:00 2001 From: Adam Dyess Date: Fri, 19 Apr 2024 22:28:45 -0500 Subject: [PATCH 2/2] Use new datastore status --- tests/integration/test_etcd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_etcd.py b/tests/integration/test_etcd.py index f2936d7d..db015f96 100644 --- a/tests/integration/test_etcd.py +++ b/tests/integration/test_etcd.py @@ -40,4 +40,4 @@ async def test_etcd_datastore(kubernetes_cluster: model.Model): status = json.loads(result.results["stdout"]) assert status["ready"], "Cluster isn't ready" assert status["datastore"]["type"] == "external", "Not bootstrapped against etcd" - assert status["datastore"]["external-url"] == f"https://{etcd.public_address}:{etcd_port}" + assert status["datastore"]["servers"] == [f"https://{etcd.public_address}:{etcd_port}"]