mirror of
https://github.com/techno-tim/k3s-ansible.git
synced 2026-09-14 08:03:19 +02:00
feat(k3s-server): add kube_vip_enabled and metal_lb_enabled switches
- gate the control-plane VIP and kube-vip service LB on kube_vip_enabled - gate the MetalLB manifest and pool on metal_lb_enabled - add argument_specs entries in both roles - document both switches in sample inventory and README - update metal_lb deploy-condition test and add a kube-vip deploy-condition test
This commit is contained in:
committed by
Techno Tim
parent
4dc333a7d3
commit
1687a3b098
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Regression test for the kube-vip deploy conditions.
|
||||
|
||||
The control-plane VIP (roles/k3s_server/tasks/vip.yml) and the kube-vip
|
||||
service load balancer (roles/k3s_server/tasks/kube-vip.yml) are both included
|
||||
from roles/k3s_server/tasks/main.yml. Before this switch existed the VIP
|
||||
include had no gate and always ran, so a user who wanted no kube-vip (single
|
||||
node or external LB) could not opt out.
|
||||
|
||||
This test loads the real `when` expressions from the k3s_server task file and
|
||||
evaluates them against representative variable sets, asserting that:
|
||||
|
||||
- the control-plane VIP is deployed only when kube_vip_enabled is true;
|
||||
- the service load balancer is deployed only when kube_vip_enabled is true
|
||||
AND kube_vip_lb_ip_range is defined.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
import yaml
|
||||
from jinja2 import Environment
|
||||
|
||||
VIP_WHEN = "kube_vip_enabled"
|
||||
KUBE_VIP_WHEN = "kube_vip_enabled and kube_vip_lb_ip_range is defined"
|
||||
|
||||
|
||||
def repo_root():
|
||||
return subprocess.check_output(
|
||||
["git", "rev-parse", "--show-toplevel"], text=True
|
||||
).strip()
|
||||
|
||||
|
||||
def fail(message):
|
||||
raise SystemExit("kube-vip deploy condition test failed: " + message)
|
||||
|
||||
|
||||
def extract_when(path, task_name):
|
||||
"""Return the `when:` expression string for the named task."""
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
doc = yaml.safe_load(handle)
|
||||
for task in doc:
|
||||
if task.get("name") == task_name:
|
||||
when = task.get("when")
|
||||
return (when or "").strip()
|
||||
return None
|
||||
|
||||
|
||||
def evaluate(when, variables):
|
||||
"""Evaluate a `when` expression against variables using Jinja2."""
|
||||
env = Environment()
|
||||
|
||||
def fake_bool(value):
|
||||
# Minimal stand-in for Ansible's truthiness filter used by `| bool`.
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if value is None:
|
||||
return False
|
||||
return str(value).lower() in ("1", "true", "yes", "on")
|
||||
|
||||
env.filters["bool"] = fake_bool
|
||||
template = env.from_string("{{ " + when + " }}")
|
||||
rendered = template.render(**variables)
|
||||
# The expression renders to the literal strings "True"/"False".
|
||||
if rendered == "True":
|
||||
return True
|
||||
if rendered == "False":
|
||||
return False
|
||||
fail("condition did not render to a boolean: {0!r}".format(rendered))
|
||||
|
||||
|
||||
def assert_deployment(when, variables, expected, label):
|
||||
result = evaluate(when, variables)
|
||||
verdict = "deploy" if result else "skip"
|
||||
expected_verdict = "deploy" if expected else "skip"
|
||||
if result != expected:
|
||||
fail(
|
||||
"{0}: expected to {1} but the condition chose to {2} "
|
||||
"(vars: {3})".format(label, expected_verdict, verdict, variables)
|
||||
)
|
||||
|
||||
|
||||
def scenarios():
|
||||
"""Yield (variables, expected_vip, expected_service_lb, label) pairs."""
|
||||
yield (
|
||||
# Default inventory: kube-vip enabled, no service LB range.
|
||||
{
|
||||
"kube_vip_enabled": True,
|
||||
},
|
||||
True,
|
||||
False,
|
||||
"default: kube-vip VIP only",
|
||||
)
|
||||
yield (
|
||||
# kube-vip owns the service LB range too.
|
||||
{
|
||||
"kube_vip_enabled": True,
|
||||
"kube_vip_lb_ip_range": "192.168.30.80-192.168.30.90",
|
||||
},
|
||||
True,
|
||||
True,
|
||||
"kube-vip VIP and service LB",
|
||||
)
|
||||
yield (
|
||||
# Explicitly disabled, no LB range.
|
||||
{
|
||||
"kube_vip_enabled": False,
|
||||
},
|
||||
False,
|
||||
False,
|
||||
"kube_vip_enabled: false",
|
||||
)
|
||||
yield (
|
||||
# Explicitly disabled even when the LB range is present.
|
||||
{
|
||||
"kube_vip_enabled": False,
|
||||
"kube_vip_lb_ip_range": "192.168.30.80-192.168.30.90",
|
||||
},
|
||||
False,
|
||||
False,
|
||||
"kube_vip_enabled: false despite LB range",
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
root = repo_root()
|
||||
server_tasks = os.path.join(root, "roles", "k3s_server", "tasks", "main.yml")
|
||||
|
||||
vip_when = extract_when(server_tasks, "Deploy vip manifest")
|
||||
kube_vip_when = extract_when(server_tasks, "Deploy kube-vip manifest")
|
||||
|
||||
if vip_when is None:
|
||||
fail("could not find 'Deploy vip manifest' when condition")
|
||||
if kube_vip_when is None:
|
||||
fail("could not find 'Deploy kube-vip manifest' when condition")
|
||||
|
||||
if vip_when != VIP_WHEN:
|
||||
fail(
|
||||
"k3s_server/tasks/main.yml 'Deploy vip manifest' when condition "
|
||||
"changed unexpectedly:\n"
|
||||
" expected: {0}\n got: {1}".format(VIP_WHEN, vip_when)
|
||||
)
|
||||
if kube_vip_when != KUBE_VIP_WHEN:
|
||||
fail(
|
||||
"k3s_server/tasks/main.yml 'Deploy kube-vip manifest' when "
|
||||
"condition changed unexpectedly:\n"
|
||||
" expected: {0}\n got: {1}".format(KUBE_VIP_WHEN, kube_vip_when)
|
||||
)
|
||||
|
||||
for variables, expected_vip, expected_service_lb, label in scenarios():
|
||||
assert_deployment(vip_when, variables, expected_vip, "vip " + label)
|
||||
assert_deployment(
|
||||
kube_vip_when,
|
||||
variables,
|
||||
expected_service_lb,
|
||||
"service_lb " + label,
|
||||
)
|
||||
|
||||
print("kube-vip deploy condition regression test passed for all scenarios")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -10,7 +10,8 @@ scenario.
|
||||
|
||||
This test loads the real `when` expressions from both task files and evaluates
|
||||
them against representative variable sets, asserting MetalLB is deployed in
|
||||
every topology except when kube-vip owns the VIP range or Cilium BGP is enabled.
|
||||
every topology except when MetalLB is explicitly disabled, kube-vip owns the
|
||||
VIP range, or Cilium BGP is enabled.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
@@ -23,6 +24,7 @@ import yaml
|
||||
from jinja2 import Environment
|
||||
|
||||
METALLB_WHEN = (
|
||||
"metal_lb_enabled and "
|
||||
"kube_vip_lb_ip_range is not defined and "
|
||||
"not (cilium_bgp | default(false) | bool)"
|
||||
)
|
||||
@@ -88,6 +90,7 @@ def scenarios():
|
||||
yield (
|
||||
# Default Flannel inventory (all.yml sets cilium_bgp: false).
|
||||
{
|
||||
"metal_lb_enabled": True,
|
||||
"cilium_bgp": False,
|
||||
"cilium_iface": None,
|
||||
},
|
||||
@@ -98,6 +101,7 @@ def scenarios():
|
||||
# Calico CNI with no Cilium variable in scope (issue #644): cilium_bgp
|
||||
# is genuinely undefined, so `default(false)` must keep MetalLB on.
|
||||
{
|
||||
"metal_lb_enabled": True,
|
||||
"calico_iface": "eth1",
|
||||
},
|
||||
True,
|
||||
@@ -106,6 +110,7 @@ def scenarios():
|
||||
yield (
|
||||
# Cilium CNI with BGP disabled: MetalLB must still be deployed.
|
||||
{
|
||||
"metal_lb_enabled": True,
|
||||
"cilium_bgp": False,
|
||||
"cilium_iface": "eth1",
|
||||
},
|
||||
@@ -115,6 +120,7 @@ def scenarios():
|
||||
yield (
|
||||
# Cilium CNI with BGP enabled: Cilium provides the LB, skip MetalLB.
|
||||
{
|
||||
"metal_lb_enabled": True,
|
||||
"cilium_bgp": True,
|
||||
"cilium_iface": "eth1",
|
||||
},
|
||||
@@ -124,12 +130,21 @@ def scenarios():
|
||||
yield (
|
||||
# kube-vip is the load balancer provider: skip MetalLB.
|
||||
{
|
||||
"metal_lb_enabled": True,
|
||||
"kube_vip_lb_ip_range": "192.168.30.80-192.168.30.90",
|
||||
"cilium_bgp": False,
|
||||
},
|
||||
False,
|
||||
"kube-vip owns the VIP range",
|
||||
)
|
||||
yield (
|
||||
# MetalLB explicitly disabled (external LB / single node): skip.
|
||||
{
|
||||
"metal_lb_enabled": False,
|
||||
},
|
||||
False,
|
||||
"metal_lb_enabled: false (external LB)",
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user