Skip to content

Commit

Permalink
Add Network feature config options
Browse files Browse the repository at this point in the history
  • Loading branch information
HomayoonAlimohammadi committed Nov 21, 2024
1 parent f2aface commit 17b9576
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,18 @@ 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"

return UserFacingClusterConfig(
local_storage=local_storage,
gateway=gateway,
network=network,
annotations=self._get_valid_annotations(),
cloud_provider=cloud_provider,
)
Expand Down
52 changes: 52 additions & 0 deletions charms/worker/k8s/tests/unit/test_config_options.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 17b9576

Please sign in to comment.