Skip to content

Commit a0f9060

Browse files
fix(providers): support per-run isolated KUBECONFIG in vcluster credential resolution
1 parent a053999 commit a0f9060

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

devops_bench/providers/vcluster.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ def _is_allowlisted_context(context_name: str, kubeconfig_path: str) -> bool:
134134

135135
def _default_kubeconfig_path(cluster_name: str) -> str:
136136
"""Determine default target kubeconfig path for a virtual cluster."""
137+
env_kube = get_env("KUBECONFIG")
138+
if env_kube:
139+
try:
140+
resolved_env = str(Path(env_kube).expanduser().resolve())
141+
default_kube = str(Path("~/.kube/config").expanduser().resolve())
142+
if resolved_env != default_kube:
143+
return resolved_env
144+
except Exception:
145+
pass
137146
return str(Path(tempfile.gettempdir()) / f"vcluster-{cluster_name}-kubeconfig.yaml")
138147

139148

@@ -223,6 +232,7 @@ def ensure_cluster_credentials(
223232
) from exc
224233
f.write(kubeconfig_yaml)
225234
_log.info("Wrote virtual cluster kubeconfig to %s (mode 0600)", resolved_target)
235+
os.environ["KUBECONFIG"] = str(resolved_target)
226236

227237
return ClusterInfo.from_dict(
228238
{

tests/unit/providers/test_vcluster_provider.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import json
20+
import os
2021
import re
2122
import tempfile
2223
from pathlib import Path
@@ -143,6 +144,18 @@ def test_vcluster_resolve_variables_parallel_kubeconfig(
143144
run_kubeconfig = str(tmp_path / "run-kubeconfig")
144145
monkeypatch.setenv("KUBECONFIG", run_kubeconfig)
145146

147+
variables = VClusterProvider().resolve_variables(ctx, {})
148+
assert variables["kubeconfig_path"] == str(Path(run_kubeconfig).resolve())
149+
150+
151+
def test_vcluster_resolve_variables_default_kubeconfig_falls_back_to_temp(
152+
ctx: ResolveContext,
153+
fake_kubeconfig: str,
154+
monkeypatch: pytest.MonkeyPatch,
155+
) -> None:
156+
monkeypatch.setenv("HOST_KUBECONFIG", fake_kubeconfig)
157+
monkeypatch.setenv("KUBECONFIG", "~/.kube/config")
158+
146159
variables = VClusterProvider().resolve_variables(ctx, {})
147160
assert variables["kubeconfig_path"] == str(
148161
Path(tempfile.gettempdir()) / "vcluster-test-cluster-kubeconfig.yaml"
@@ -169,6 +182,7 @@ def test_vcluster_ensure_cluster_credentials(tmp_path: Path) -> None:
169182
assert info.location == "local"
170183
assert info.project == "local-vcluster"
171184
assert info.kubeconfig_path == str(target_path.resolve())
185+
assert os.environ.get("KUBECONFIG") == str(target_path.resolve())
172186

173187
assert target_path.exists()
174188
assert "https://127.0.0.1:8443" in target_path.read_text(encoding="utf-8")

0 commit comments

Comments
 (0)