Skip to content

Commit

Permalink
Graceful exit when no kubeconfig is configured (#1668)
Browse files Browse the repository at this point in the history
  • Loading branch information
edcdavid authored Nov 29, 2023
1 parent 8bf3fce commit 887a378
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/clientsholder/clientsholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clientsholder
import (
"errors"
"fmt"
"os"
"time"

clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 887a378

Please sign in to comment.