From b3aefd97428ca547de3a81f26dd23a10e6b7f8f0 Mon Sep 17 00:00:00 2001 From: Justin Ross Date: Tue, 16 Jan 2024 12:48:35 -0500 Subject: [PATCH] Use the Minikube context instead of run_steps_minikube --- python/skewer/main.py | 60 ++++++++++++++++------------------ python/skewer/planocommands.py | 16 ++++----- python/skewer/tests.py | 12 ++++--- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/python/skewer/main.py b/python/skewer/main.py index 4731c94..e269d01 100644 --- a/python/skewer/main.py +++ b/python/skewer/main.py @@ -22,7 +22,7 @@ from plano import * __all__ = [ - "generate_readme", "run_steps_minikube", "run_steps", "Minikube", + "generate_readme", "run_steps", "Minikube", ] standard_text = read_yaml(join(get_parent_dir(__file__), "standardtext.yaml")) @@ -109,33 +109,6 @@ def await_console_ok(): await_http_ok("service/skupper", "https://{}:8010/", user="admin", password=password) -def run_steps_minikube(skewer_file, debug=False): - notice("Running steps on Minikube") - - check_environment() - check_program("minikube") - - model = Model(skewer_file) - model.check() - - kube_sites = [x for _, x in model.sites if x.platform == "kubernetes"] - kubeconfigs = list() - - make_dir("/tmp/skewer", quiet=True) - - with Minikube(): - for site in kube_sites: - kubeconfig = site.env["KUBECONFIG"].replace("~", "/tmp/skewer") - site.env["KUBECONFIG"] = kubeconfig - - kubeconfigs.append(kubeconfig) - - with site: - run("minikube -p skewer update-context") - check_file(ENV["KUBECONFIG"]) - - run_steps(skewer_file, kubeconfigs=kubeconfigs, debug=debug) - def run_steps(skewer_file, kubeconfigs=[], debug=False): notice(f"Running steps (skewer_file='{skewer_file}', kubeconfigs={kubeconfigs})") @@ -371,7 +344,7 @@ def get_github_owner_repo(): return path.split("/", 1) if result.scheme in ("http", "https") and result.netloc == "github.com": - path = remove_prefix(result.path, "/", result.path) + path = remove_prefix(result.path, "/") return path.split("/", 1) @@ -663,17 +636,42 @@ def check(self): check_unknown_attributes(self) class Minikube: + def __init__(self, skewer_file): + self.skewer_file = skewer_file + self.kubeconfigs = list() + def __enter__(self): + notice("Starting Minikube") + + check_environment() check_program("minikube") + make_dir("/tmp/skewer", quiet=True) run("minikube -p skewer start --auto-update-drivers false") - make_dir("/tmp/skewer", quiet=True) tunnel_output_file = open("/tmp/skewer/minikube-tunnel-output", "w") - self.tunnel = start("minikube -p skewer tunnel", output=tunnel_output_file) + model = Model(self.skewer_file) + model.check() + + kube_sites = [x for _, x in model.sites if x.platform == "kubernetes"] + + for site in kube_sites: + kubeconfig = site.env["KUBECONFIG"].replace("~", "/tmp/skewer") + site.env["KUBECONFIG"] = kubeconfig + + self.kubeconfigs.append(kubeconfig) + + with site: + run("minikube -p skewer update-context") + check_file(ENV["KUBECONFIG"]) + + return self + def __exit__(self, exc_type, exc_value, traceback): + notice("Stopping Minikube") + stop(self.tunnel) run("minikube -p skewer delete") diff --git a/python/skewer/planocommands.py b/python/skewer/planocommands.py index 9c692f3..54ee9cc 100644 --- a/python/skewer/planocommands.py +++ b/python/skewer/planocommands.py @@ -53,11 +53,11 @@ """.strip() @command -def generate(): +def generate(output="README.md"): """ Generate README.md from the data in skewer.yaml """ - generate_readme("skewer.yaml", "README.md") + generate_readme("skewer.yaml", output) @command def render(verbose=False, quiet=False): @@ -88,7 +88,8 @@ def run_(*kubeconfigs, debug=False): Run the example steps """ if not kubeconfigs: - run_steps_minikube("skewer.yaml", debug=debug) + with Minikube("skewer.yaml") as mk: + run_steps("skewer.yaml", mk.kubeconfigs, debug=debug) else: run_steps("skewer.yaml", kubeconfigs, debug=debug) @@ -98,18 +99,15 @@ def demo(*kubeconfigs, debug=False): Run the example steps and pause for a demo before cleaning up """ with working_env(SKEWER_DEMO=1): - if not kubeconfigs: - run_steps_minikube("skewer.yaml", debug=debug) - else: - run_steps("skewer.yaml", kubeconfigs, debug=debug) + run_(*kubeconfigs, debug=debug) @command def test_(debug=False): """ Test README generation and run the steps """ - generate_readme("skewer.yaml", make_temp_file()) - run_steps_minikube("skewer.yaml", debug=debug) + generate(output=make_temp_file()) + run_(debug=debug) @command def update_skewer(): diff --git a/python/skewer/tests.py b/python/skewer/tests.py index 75551e1..d7c5151 100644 --- a/python/skewer/tests.py +++ b/python/skewer/tests.py @@ -43,22 +43,24 @@ def generate_readme_(): check_file("README.md") @test -def run_steps(): +def run_steps_(): with working_dir("example"): - run_steps_minikube("skewer.yaml", debug=True) + with Minikube("skewer.yaml") as mk: + run_steps("skewer.yaml", kubeconfigs=mk.kubeconfigs, debug=True) @test def run_steps_demo(): with working_dir("example"): - with working_env(SKEWER_DEMO=1, SKEWER_DEMO_NO_WAIT=1): - run_steps_minikube("skewer.yaml", debug=True) + with Minikube("skewer.yaml") as mk: + run_steps("skewer.yaml", kubeconfigs=mk.kubeconfigs, debug=True) @test def run_steps_debug(): with working_dir("example"): with expect_error(): with working_env(SKEWER_FAIL=1): - run_steps_minikube("skewer.yaml", debug=True) + with Minikube("skewer.yaml") as mk: + run_steps("skewer.yaml", kubeconfigs=mk.kubeconfigs, debug=True) if __name__ == "__main__": import sys