Skip to content

Commit 793b299

Browse files
committed
improve kube config generation and fix its tests
1 parent a2b28d1 commit 793b299

File tree

3 files changed

+58
-92
lines changed

3 files changed

+58
-92
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ruff = "*"
5656

5757
[tool.pixi.tasks]
5858
test = "pytest tests"
59+
cov = "pytest --cov=gaiaflow --cov-report=html"
5960
format = "isort src/gaiaflow && ruff format src/gaiaflow"
6061
check = "ruff check src/gaiaflow"
6162
check_fix = "ruff check src/gaiaflow --fix"

src/gaiaflow/managers/helpers.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -477,31 +477,24 @@ def __init__(self, gaiaflow_path: Path, os_type: str):
477477

478478
def create_inline(self):
479479
kube_config = Path.home() / ".kube" / "config"
480-
backup_config = kube_config.with_suffix(".backup")
481-
482-
self._backup_kube_config(kube_config, backup_config)
483-
self._patch_kube_config(kube_config)
484-
self._write_inline(kube_config)
485-
486-
if backup_config.exists():
487-
shutil.copy(backup_config, kube_config)
488-
backup_config.unlink()
489-
log_info("Reverted kube config to original state.")
490-
491-
def _backup_kube_config(self, kube_config: Path, backup_config: Path):
492-
if kube_config.exists():
493-
with open(kube_config, "r") as f:
494-
config_data = yaml.safe_load(f)
495-
with open(backup_config, "w") as f:
496-
yaml.dump(config_data, f)
497-
498-
def _patch_kube_config(self, kube_config: Path):
499480
if not kube_config.exists():
481+
log_info("No kube config found, skipping inline creation.")
500482
return
501483

502-
with open(kube_config, "r") as f:
503-
config_data = yaml.safe_load(f)
484+
filename = self.gaiaflow_path / "_docker" / "kube_config_inline"
485+
self._write_inline(filename)
486+
487+
with open(filename, "r") as f:
488+
try:
489+
config_data = yaml.safe_load(f) or {}
490+
except yaml.YAMLError as e:
491+
log_error(f"Failed to parse kube config: {e}")
492+
return
493+
494+
self._patch_kube_config(config_data, filename)
504495

496+
def _patch_kube_config(self, config_data: dict, filename: Path) -> dict:
497+
log_info("Patching kube config file...")
505498
for cluster in config_data.get("clusters", []):
506499
cluster_info = cluster.get("cluster", {})
507500
if self.os_type == "windows":
@@ -515,11 +508,11 @@ def _patch_kube_config(self, kube_config: Path):
515508
cluster_info["server"] = "https://192.168.49.2:8443"
516509
cluster_info["insecure-skip-tls-verify"] = True
517510

518-
with open(kube_config, "w") as f:
511+
with open(filename, "w") as f:
519512
yaml.dump(config_data, f)
513+
log_info(f"Patched file written to {filename}")
520514

521-
def _write_inline(self, kube_config: Path):
522-
filename = self.gaiaflow_path / "_docker" / "kube_config_inline"
515+
def _write_inline(self, filename: Path):
523516
log_info("Creating kube config inline file...")
524517
with open(filename, "w") as f:
525518
subprocess.call(

tests/managers/test_helpers.py

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -236,88 +236,60 @@ def tearDown(self):
236236
self.tmpdir.cleanup()
237237

238238
def _write_kube_config(self, data):
239-
kube_dir = Path.home() / ".kube"
240-
kube_dir.mkdir(exist_ok=True)
241-
kube_config = kube_dir / "config"
242-
with open(kube_config, "w") as f:
243-
yaml.dump(data, f)
244-
return kube_config
239+
inline_file = self.gaia_path / "_docker" / "kube_config_inline"
240+
inline_file.parent.mkdir(parents=True, exist_ok=True)
241+
with open(inline_file, "w") as f:
242+
yaml.safe_dump(data, f)
243+
return inline_file
245244

246245
@patch("subprocess.call", return_value=0)
247-
def test_write_inline_creates_file(self, _):
248-
kube_config = self._write_kube_config({"clusters": []})
249-
self.helper._write_inline(kube_config)
250-
out_file = self.gaia_path / "_docker" / "kube_config_inline"
251-
self.assertTrue(out_file.exists())
252-
253-
def test_backup_and_patch_config(self):
254-
kube_config = self._write_kube_config(
255-
{"clusters": [{"cluster": {"server": "127.0.0.1"}}]}
256-
)
257-
backup = kube_config.with_suffix(".backup")
258-
self.helper._backup_kube_config(kube_config, backup)
259-
self.assertTrue(backup.exists())
246+
def test_write_inline_creates_file(self, mock_call):
247+
inline_file = self.gaia_path / "_docker" / "kube_config_inline"
248+
self.helper._write_inline(inline_file)
249+
self.assertTrue(inline_file.exists())
250+
mock_call.assert_called_once()
260251

261-
self.helper._patch_kube_config(kube_config)
262-
patched = yaml.safe_load(open(kube_config))
263-
self.assertIn("clusters", patched)
264252

265253
@patch("subprocess.call", return_value=0)
266254
@patch("gaiaflow.managers.helpers.is_wsl", return_value=False)
267-
def test_create_inline_linux(self, *_):
268-
helper = KubeConfigHelper(self.gaia_path, os_type="linux")
269-
self._write_kube_config({"clusters": [{"cluster": {"server": "127.0.0.1"}}]})
270-
helper.create_inline()
271-
self.assertTrue(
272-
(self.gaia_path / "_docker" / "kube_config_inline").exists()
273-
)
255+
def test_create_inline_linux(self, mock_is_wsl, mock_call):
256+
fake_data = {"clusters": [{"cluster": {"server": "127.0.0.1"}}]}
257+
with patch.object(
258+
self.helper,
259+
"_write_inline",
260+
side_effect=lambda f: self._write_kube_config(fake_data),
261+
):
262+
self.helper.create_inline()
263+
inline_file = self.gaia_path / "_docker" / "kube_config_inline"
264+
self.assertTrue(inline_file.exists())
265+
data = yaml.safe_load(open(inline_file))
266+
self.assertEqual(data["clusters"][0]["cluster"]["server"], "127.0.0.1")
274267

268+
@patch("subprocess.call", return_value=0)
275269
@patch("gaiaflow.managers.helpers.is_wsl", return_value=False)
276-
def test_create_inline_windows_branch(self, _):
270+
def test_patch_windows_branch(self, *_):
277271
helper = KubeConfigHelper(self.gaia_path, os_type="windows")
278-
kube_config = self._write_kube_config({"clusters": []})
279-
backup_config = kube_config.with_suffix(".backup")
280-
backup_config.write_text("backup")
281-
282-
with (
283-
patch("shutil.copy") as mock_copy,
284-
patch.object(Path, "unlink") as mock_unlink,
285-
):
286-
helper.create_inline()
287-
288-
mock_copy.assert_called_once_with(backup_config, kube_config)
289-
mock_unlink.assert_called_once()
272+
kube_config = self._write_kube_config({"clusters": [{"cluster": {"server": "127.0.0.1"}}]})
273+
helper._patch_kube_config(yaml.safe_load(open(kube_config)), kube_config)
274+
data = yaml.safe_load(open(kube_config))
275+
cluster = data["clusters"][0]["cluster"]
276+
self.assertEqual(cluster["server"], "host.docker.internal")
277+
self.assertTrue(cluster["insecure-skip-tls-verify"])
290278

291-
@patch("gaiaflow.managers.helpers.is_wsl", return_value=True)
292-
def test_create_inline_wsl_branch(self, _):
293-
helper = KubeConfigHelper(self.gaia_path, os_type="linux")
294-
kube_config = self._write_kube_config({"clusters": []})
295-
backup_config = kube_config.with_suffix(".backup")
296-
backup_config.write_text("backup")
297-
298-
with (
299-
patch("shutil.copy") as mock_copy,
300-
patch.object(Path, "unlink") as mock_unlink,
301-
):
302-
helper.create_inline()
303-
304-
mock_copy.assert_called_once_with(backup_config, kube_config)
305-
mock_unlink.assert_called_once()
306279

280+
@patch("subprocess.call", return_value=0)
307281
@patch("gaiaflow.managers.helpers.is_wsl", return_value=True)
308-
def test_patch_wsl(self, _):
282+
def test_patch_wsl_branch(self, *_):
309283
helper = KubeConfigHelper(self.gaia_path, os_type="linux")
310-
config = self._write_kube_config({"clusters": [{"cluster": {"server": "localhost"}}]})
311-
helper._patch_kube_config(config)
312-
data = yaml.safe_load(open(config))
313-
assert data["clusters"][0]["cluster"]["insecure-skip-tls-verify"]
284+
inline_file = self._write_kube_config(
285+
{"clusters": [{"cluster": {"server": "localhost"}}]}
286+
)
287+
helper._patch_kube_config(yaml.safe_load(open(inline_file)), inline_file)
288+
data = yaml.safe_load(open(inline_file))
289+
cluster = data["clusters"][0]["cluster"]
290+
self.assertEqual(cluster["server"], "https://192.168.49.2:8443")
291+
self.assertTrue(cluster["insecure-skip-tls-verify"])
314292

315-
def test_patch_windows(self):
316-
helper = KubeConfigHelper(self.gaia_path, os_type="windows")
317-
config = self._write_kube_config({"clusters": [{"cluster": {"server": "127.0.0.1"}}]})
318-
helper._patch_kube_config(config)
319-
data = yaml.safe_load(open(config))
320-
assert data["clusters"][0]["cluster"]["server"] == "host.docker.internal"
321293

322294
class TestBaseDockerHandler(unittest.TestCase):
323295
def setUp(self):

0 commit comments

Comments
 (0)