diff --git a/charms/worker/k8s/charmcraft.yaml b/charms/worker/k8s/charmcraft.yaml index c5b3ceab..61bd0519 100644 --- a/charms/worker/k8s/charmcraft.yaml +++ b/charms/worker/k8s/charmcraft.yaml @@ -189,6 +189,11 @@ config: default: false description: | Enable/Disable the gateway feature on the cluster. + network-enabled: + type: boolean + default: true + description: | + Enables or disables the network feature. actions: get-kubeconfig: diff --git a/charms/worker/k8s/src/charm.py b/charms/worker/k8s/src/charm.py index 7794ac84..4b686bd7 100755 --- a/charms/worker/k8s/src/charm.py +++ b/charms/worker/k8s/src/charm.py @@ -418,6 +418,10 @@ def _assemble_cluster_config(self) -> UserFacingClusterConfig: enabled=self.config.get("gateway-enabled"), ) + network = NetworkConfig( + enabled=self.config.get("network-enabled"), + ) + cloud_provider = None if self.xcp.has_xcp: cloud_provider = "external" @@ -425,6 +429,7 @@ def _assemble_cluster_config(self) -> UserFacingClusterConfig: return UserFacingClusterConfig( local_storage=local_storage, gateway=gateway, + network=network, annotations=self._get_valid_annotations(), cloud_provider=cloud_provider, ) diff --git a/charms/worker/k8s/tests/unit/test_config_options.py b/charms/worker/k8s/tests/unit/test_config_options.py new file mode 100644 index 00000000..13bb4f49 --- /dev/null +++ b/charms/worker/k8s/tests/unit/test_config_options.py @@ -0,0 +1,52 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +# Learn more about testing at: https://juju.is/docs/sdk/testing + +# pylint: disable=duplicate-code,missing-function-docstring +"""Unit tests.""" + + +from pathlib import Path + +import ops +import ops.testing +import pytest +from charm import K8sCharm + + +@pytest.fixture(params=["worker", "control-plane"]) +def harness(request): + """Craft a ops test harness. + + Args: + request: pytest request object + """ + meta = Path(__file__).parent / "../../charmcraft.yaml" + if request.param == "worker": + meta = Path(__file__).parent / "../../../charmcraft.yaml" + harness = ops.testing.Harness(K8sCharm, meta=meta.read_text()) + harness.begin() + harness.charm.is_worker = request.param == "worker" + yield harness + harness.cleanup() + + +def test_configure_network_options(harness): + """Test configuring the network options. + + Args: + harness: the harness under test + """ + if harness.charm.is_worker: + pytest.skip("Not applicable on workers") + + harness.disable_hooks() + + harness.update_config({"network-enabled": False}) + ufcg = harness.charm._assemble_cluster_config() + assert not ufcg.network.enabled, "Network should be disabled" + + harness.update_config({"network-enabled": True}) + ufcg = harness.charm._assemble_cluster_config() + assert ufcg.network.enabled, "Network should be enabled" diff --git a/pyproject.toml b/pyproject.toml index 363ba3ba..bffeb7fe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,4 +86,4 @@ max-complexity = 10 skip = "build,lib,venv,icon.svg,.tox,.git,.mypy_cache,.ruff_cache,.coverage" [tool.pyright] -extraPaths = ["./charms/worker/k8s/lib"] +extraPaths = ["./charms/worker/k8s/lib", "./charms/worker/k8s/src"]