From 887a378c0f223dd0d19e1fd6ead77cca323fdf15 Mon Sep 17 00:00:00 2001 From: David Elie-Dit-Cosaque <86730676+edcdavid@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:03:04 -0600 Subject: [PATCH] Graceful exit when no kubeconfig is configured (#1668) --- internal/clientsholder/clientsholder.go | 7 ++++++- main.go | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/clientsholder/clientsholder.go b/internal/clientsholder/clientsholder.go index 0aee59c60..066f28930 100644 --- a/internal/clientsholder/clientsholder.go +++ b/internal/clientsholder/clientsholder.go @@ -19,6 +19,7 @@ package clientsholder import ( "errors" "fmt" + "os" "time" clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" @@ -146,10 +147,14 @@ func ClearTestClientsHolder() { // GetClientsHolder returns the singleton ClientsHolder object. func GetClientsHolder(filenames ...string) *ClientsHolder { + const exitUsage = 2 if clientsHolder.ready { return &clientsHolder } - + if len(filenames) == 0 { + logrus.Errorf("Please provide a valid kubeconfig. Either set the KUBECONFIG environment variable or alternatively copy a kube config to $HOME/.kube/config") + os.Exit(exitUsage) + } clientsHolder, err := newClientsHolder(filenames...) if err != nil { logrus.Panic("Failed to create k8s clients holder: ", err) diff --git a/main.go b/main.go index ccd3da137..bdc7e7f60 100644 --- a/main.go +++ b/main.go @@ -110,11 +110,19 @@ func getK8sClientsConfigFileNames() []string { params := configuration.GetTestParameters() fileNames := []string{} if params.Kubeconfig != "" { + // Add the kubeconfig path fileNames = append(fileNames, params.Kubeconfig) } if params.Home != "" { kubeConfigFilePath := filepath.Join(params.Home, ".kube", "config") - fileNames = append(fileNames, kubeConfigFilePath) + // Check if the kubeconfig path exists + if _, err := os.Stat(kubeConfigFilePath); err == nil { + log.Infof("kubeconfig path %s is present", kubeConfigFilePath) + // Only add the kubeconfig to the list of paths if it exists, since it is not added by the user + fileNames = append(fileNames, kubeConfigFilePath) + } else { + log.Infof("kubeconfig path %s is not present", kubeConfigFilePath) + } } return fileNames