Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
will not exit when no kubeconfig found (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashbhutwala authored and operator-r2d2 committed Jun 27, 2019
1 parent dce10bd commit 1636b39
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
25 changes: 10 additions & 15 deletions pkg/synopsysctl/cmd_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package synopsysctl
import (
"fmt"
"os"
"strings"

homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
Expand All @@ -48,23 +49,17 @@ var rootCmd = &cobra.Command{
return nil
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Set the Log Level
lvl, err := log.ParseLevel(logLevelCtl)
if err != nil {
log.Errorf("ctl-log-Level '%s' is not a valid level: %s", logLevelCtl, err)
}
log.SetLevel(lvl)
if !cmd.Flags().Lookup("kubeconfig").Changed { // if kubeconfig wasn't set, check the environ
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
kubeconfig = kubeconfigEnvVal
}
mockModeFlagExists := cmd.Flags().Lookup("mock")
mockMode := false
if mockModeFlagExists != nil && mockModeFlagExists.Changed {
mockMode = true
}
// Sets kubeconfig and initializes resource client libraries
if err := setResourceClients(); err != nil {
log.Error(err)
os.Exit(1)
nativeMode := strings.Contains(cmd.CommandPath(), "native")
// only set resource clients if we are not in mock mode and we are not in native mode
if !mockMode && !nativeMode {
callSetResourceClients()
}
return nil
return parseLogLevelAndKubeConfig(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("must specify sub-command")
Expand Down
25 changes: 25 additions & 0 deletions pkg/synopsysctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
operatorutil "github.com/blackducksoftware/synopsys-operator/pkg/util"
util "github.com/blackducksoftware/synopsys-operator/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -48,6 +49,30 @@ var alertClient *alertclientset.Clientset
var blackDuckClient *blackduckclientset.Clientset
var opsSightClient *opssightclientset.Clientset

func parseLogLevelAndKubeConfig(cmd *cobra.Command) error {
// Set the Log Level
lvl, err := log.ParseLevel(logLevelCtl)
if err != nil {
log.Errorf("ctl-log-Level '%s' is not a valid level: %s", logLevelCtl, err)
return err
}
log.SetLevel(lvl)
if !cmd.Flags().Lookup("kubeconfig").Changed { // if kubeconfig wasn't set, check the environ
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
kubeconfig = kubeconfigEnvVal
}
}
return nil
}

func callSetResourceClients() {
// Sets kubeconfig and initializes resource client libraries
if err := setResourceClients(); err != nil {
log.Error(err)
os.Exit(1)
}
}

// setResourceClients sets the global variables for the Kuberentes rest config
// and the resource clients
func setResourceClients() error {
Expand Down

0 comments on commit 1636b39

Please sign in to comment.