From db266978862d3202e35eccff520ca61896fb8f9d Mon Sep 17 00:00:00 2001 From: Davlet Panech Date: Mon, 7 Oct 2024 15:02:06 -0400 Subject: [PATCH] minikube: better error handling * stx control stop: don't auto-start minikube * return non-zero to shell when minikube profile is stopped Closes-Bug: 2083862 Signed-off-by: Davlet Panech Change-Id: Idd8dcc382c189fa36d40eb9b544e83a84a7ce1d5 --- stx/lib/stx/minikube.py | 7 +++++++ stx/lib/stx/stx_control.py | 23 ++++++++--------------- stx/lib/stx/stx_shell.py | 9 ++++----- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/stx/lib/stx/minikube.py b/stx/lib/stx/minikube.py index 3fc7b619..b5b5c276 100644 --- a/stx/lib/stx/minikube.py +++ b/stx/lib/stx/minikube.py @@ -167,6 +167,13 @@ class MinikubeCtl(object): ) return False + def require_started(self): + """ + Throw an exception if profile is not started + """ + if not self.is_started(): + raise MinikubeProfileNotRunning(self.minikube_profile) + def start(self): """ Starts the Minikube profile if it's not already started. diff --git a/stx/lib/stx/stx_control.py b/stx/lib/stx/stx_control.py index 1aa5b662..b41cc258 100644 --- a/stx/lib/stx/stx_control.py +++ b/stx/lib/stx/stx_control.py @@ -24,7 +24,6 @@ import time from stx.k8s import KubeHelper from stx.minikube import MinikubeCtl from stx.minikube import MinikubeProfileNotFoundError -from stx.minikube import MinikubeProfileNotRunning from stx import helper # pylint: disable=E0611 from stx import stx_shell @@ -295,7 +294,7 @@ stx-pkgbuilder/configmap/') timeout = 5 * 60 deadline = time.time() + timeout if self.config.use_minikube: - self.minikube_ctl.start() + self.minikube_ctl.require_started() helm_status = self.k8s.helm_release_exists(self.projectname) if helm_status: @@ -343,19 +342,11 @@ stx-pkgbuilder/configmap/') self.shell.cmd_control_enter(args) def handleStatusTask(self): - try: - if self.config.use_minikube: - if not self.minikube_ctl.is_started(): - raise MinikubeProfileNotRunning(self.config.minikube_profile) - - self.k8s.get_helm_info() - self.k8s.get_deployment_info() - self.k8s.get_pods_info() - - except Exception as e: - self.logger.error( - "Error starting minikube_ctl: %s", str(e), - ) + if self.config.use_minikube: + self.minikube_ctl.require_started() + self.k8s.get_helm_info() + self.k8s.get_deployment_info() + self.k8s.get_pods_info() def run_pod_cmd(self, podname, maincmd, remotecmd): # Run command on pod in this format: kubectl+maincmd+podname+remotecmd @@ -456,7 +447,9 @@ no lat container is available!') self.logger.error(helper.help_control()) except MinikubeProfileNotFoundError as e: self.logger.error(str(e), exc_info=True) + sys.exit(1) except Exception as e: self.logger.error( 'Error executing control task: %s', str(e), exc_info=True ) + sys.exit(1) diff --git a/stx/lib/stx/stx_shell.py b/stx/lib/stx/stx_shell.py index ea9ecaca..6d6175b9 100644 --- a/stx/lib/stx/stx_shell.py +++ b/stx/lib/stx/stx_shell.py @@ -19,7 +19,6 @@ import sys from stx.k8s import KubeHelper from stx.minikube import MinikubeCtl -from stx.minikube import MinikubeProfileNotRunning from stx import utils # pylint: disable=E0611 @@ -109,23 +108,23 @@ class HandleShellTask(object): if container not in self.all_container_names: self.logger.error("--%s must be one of: %s", container_arg, self.all_container_names) - sys.exit(1) + sys.exit(125) try: if self.config.use_minikube: - if not self.minikube_ctl.is_started(): - raise MinikubeProfileNotRunning(self.config.minikube_profile) + self.minikube_ctl.require_started() shell_command = self.create_shell_command(container, command, no_tty) self.logger.debug('Running command: %s', shell_command) shell_status = subprocess.call(shell_command, shell=True) - print(shell_status) sys.exit(shell_status) except RuntimeError as e: self.logger.error(str(e)) self.logger.error("To check the status of the pods, run: 'stx control status'.") self.logger.error("To start the build pod, run: 'stx control start --wait'.") + sys.exit(125) except Exception as e: self.logger.error(str(e), exc_info=True) + sys.exit(125) def cmd_shell(self, args): self.logger.setLevel(args.loglevel)