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 20, 2024
1 parent 17d8a37 commit 5fb6f64
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
36 changes: 33 additions & 3 deletions charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,38 @@ config:
For more information, see the upstream Kubernetes documentation about
taints:
https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/
pod-cidr:
type: string
default: "10.1.0.0/16"
description: |
Comma-separated CIDR blocks for IP addresses that can be assigned
to pods within the cluster. Can contain at most 2 blocks, one for IPv4
and one for IPv6.
After deployment it is only possible to increase the size of
the IP range. It is not possible to change or shrink the address
range after deployment.
Examples:
- "192.0.2.0/24"
- "2001:db8::/32"
- "192.0.2.0/24,2001:db8::/32"
- "2001:db8::/32,192.0.2.0/24"
service-cidr:
type: string
default: 10.152.183.0/24
description: |
CIDR to use for Kubernetes services. After deployment it is
only possible to increase the size of the IP range. It is not possible to
change or shrink the address range after deployment.
Comma-separated CIDR blocks for IP addresses that can be assigned
to services within the cluster. Can contain at most 2 blocks, one for IPv4
and one for IPv6.
After deployment it is only possible to increase the size of
the IP range. It is not possible to change or shrink the address
range after deployment.
Examples:
- "192.0.2.0/24"
- "2001:db8::/32"
- "192.0.2.0/24,2001:db8::/32"
- "2001:db8::/32,192.0.2.0/24"
local-storage-enabled:
type: boolean
default: true
Expand All @@ -184,6 +209,11 @@ config:
"Retain". If set to "Delete", the storage will be deleted when the
PersistentVolumeClaim is deleted. If set to "Retain", the storage will
be retained when the PersistentVolumeClaim is deleted.
network-enabled:
type: boolean
default: true
description: |
Enables or disables the network feature.
actions:
get-kubeconfig:
Expand Down
8 changes: 7 additions & 1 deletion charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ def _bootstrap_k8s_snap(self):
bootstrap_config = BootstrapConfig.construct()
self._configure_datastore(bootstrap_config)
bootstrap_config.cluster_config = self._assemble_cluster_config()
bootstrap_config.service_cidr = str(self.config["service-cidr"])
bootstrap_config.pod_cidr = str(self.config.get("pod-cidr"))
bootstrap_config.service_cidr = str(self.config.get("service-cidr"))
bootstrap_config.control_plane_taints = str(self.config["register-with-taints"]).split()
bootstrap_config.extra_sans = [_get_public_address()]
bootstrap_config.extra_node_kube_controller_manager_args = {
Expand Down Expand Up @@ -413,12 +414,17 @@ def _assemble_cluster_config(self) -> UserFacingClusterConfig:
# https://github.com/canonical/k8s-operator/pull/169/files#r1847378214
)

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,
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 5fb6f64

Please sign in to comment.