Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
* add a "network type" param to the lb test
* move header bump to separate PR
* update ipv6 cidr
  • Loading branch information
petrutlucian94 committed Jan 8, 2025
1 parent 3ecb70e commit ad7bf49
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
38 changes: 23 additions & 15 deletions tests/integration/tests/test_loadbalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2025 Canonical, Ltd.
#
import logging
from enum import Enum
from pathlib import Path
from typing import List

Expand All @@ -12,11 +13,17 @@
LOG = logging.getLogger(__name__)


class K8sNetType(Enum):
ipv4 = "ipv4"
ipv6 = "ipv6"
dualstack = "dualstack"


@pytest.mark.node_count(2)
@pytest.mark.tags(tags.PULL_REQUEST)
@pytest.mark.disable_k8s_bootstrapping()
def test_loadbalancer_ipv4(instances: List[harness.Instance]):
_test_loadbalancer(instances, ipv6=False)
_test_loadbalancer(instances, k8s_net_type=K8sNetType.ipv4)


@pytest.mark.node_count(2)
Expand All @@ -26,7 +33,7 @@ def test_loadbalancer_ipv6_only(instances: List[harness.Instance]):
pytest.xfail(
"Cilium ipv6 only unsupported: https://github.com/cilium/cilium/issues/15082"
)
_test_loadbalancer(instances, ipv6=True)
_test_loadbalancer(instances, k8s_net_type=K8sNetType.ipv6)


@pytest.mark.node_count(2)
Expand All @@ -35,30 +42,31 @@ def test_loadbalancer_ipv6_only(instances: List[harness.Instance]):
@pytest.mark.dualstack()
@pytest.mark.network_type("dualstack")
def test_loadbalancer_ipv6_dualstack(instances: List[harness.Instance]):
_test_loadbalancer(instances, ipv6=True, dualstack=True)
_test_loadbalancer(instances, ipv6=True, k8s_net_type=K8sNetType.dualstack)


def _test_loadbalancer(instances: List[harness.Instance], ipv6=False, dualstack=False):
def _test_loadbalancer(instances: List[harness.Instance], k8s_net_type: K8sNetType):
instance = instances[0]
tester_instance = instances[1]

if ipv6:
bootstrap_args = []
if dualstack:
bootstrap_config = (MANIFESTS_DIR / "bootstrap-dualstack.yaml").read_text()
else:
bootstrap_config = (MANIFESTS_DIR / "bootstrap-ipv6-only.yaml").read_text()
bootstrap_args += ["--address", "::/0"]
if k8s_net_type == K8sNetType.ipv6:
bootstrap_config = (MANIFESTS_DIR / "bootstrap-ipv6-only.yaml").read_text()
instance.exec(
["k8s", "bootstrap", "--file", "-", "--address", "::/0"],
input=str.encode(bootstrap_config),
)
elif k8s_net_type == K8sNetType.dualstack:
bootstrap_config = (MANIFESTS_DIR / "bootstrap-dualstack.yaml").read_text()
instance.exec(
["k8s", "bootstrap", "--file", "-", *bootstrap_args],
["k8s", "bootstrap", "--file", "-"],
input=str.encode(bootstrap_config),
)
else:
instance.exec(["k8s", "bootstrap"])

lb_cidrs = []

def get_lb_cidr(ipv6_cidr):
def get_lb_cidr(ipv6_cidr: bool):
instance_default_ip = util.get_default_ip(instance, ipv6=ipv6_cidr)
tester_instance_default_ip = util.get_default_ip(
tester_instance, ipv6=ipv6_cidr
Expand All @@ -70,9 +78,9 @@ def get_lb_cidr(ipv6_cidr):
)
return lb_cidr

if dualstack or not ipv6:
if k8s_net_type in (K8sNetType.ipv4, K8sNetType.dualstack):
lb_cidrs.append(get_lb_cidr(ipv6_cidr=False))
if ipv6:
if k8s_net_type in (K8sNetType.ipv6, K8sNetType.dualstack):
lb_cidrs.append(get_lb_cidr(ipv6_cidr=True))
lb_cidr_str = ",".join(lb_cidrs)

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/tests/test_util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def join_cluster(instance: harness.Instance, join_token: str):


def is_ipv6(ip: str) -> bool:
addr = ipaddress.ip_address(ip.strip("'"))
addr = ipaddress.ip_address(ip)
return isinstance(addr, ipaddress.IPv6Address)


Expand Down Expand Up @@ -535,15 +535,15 @@ def find_suitable_cidr(parent_cidr: str, excluded_ips: List[str]):
net = ipaddress.ip_network(parent_cidr, False)
ipv6 = isinstance(net, ipaddress.IPv6Network)
if ipv6:
ip_range = 124
ip_range = 126
else:
ip_range = 30

# Starting from the first IP address from the parent cidr,
# we search for a /30 cidr block(4 total ips, 2 available)
# that doesn't contain the excluded ips to avoid collisions
# /30 because this is the smallest CIDR cilium hands out IPs from.
# For ipv6, we use a /124 block that contains 16 total ips.
# For ipv6, we use a /126 block that contains 4 total ips.
for i in range(4, 255, 4):
lb_net = ipaddress.ip_network(f"{str(net[0]+i)}/{ip_range}", False)

Expand Down

0 comments on commit ad7bf49

Please sign in to comment.