Skip to content

Commit

Permalink
Look for KUBECONFIG environment variable for 'get' and 'plan' (#478)
Browse files Browse the repository at this point in the history
The `get` and `plan` KUDO commands should also honour the $KUBECONFIG
environment variable, if it's set.
  • Loading branch information
yankcrime authored and alenkacz committed Jul 2, 2019
1 parent 46287db commit 8c5300c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
39 changes: 13 additions & 26 deletions pkg/kudoctl/cmd/get/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,24 @@ import (
"fmt"
"log"
"os"
"os/user"

"github.com/spf13/cobra"
"github.com/xlab/treeprint"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/clientcmd"

"path/filepath"

kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"

"github.com/kudobuilder/kudo/pkg/kudoctl/util/check"
)

var (
kubeConfig string
namespace string
)

const (
defaultConfigPath = ".kube/config"
)

// NewGetInstancesCmd creates a command that lists the instances in the cluster
func NewGetInstancesCmd() *cobra.Command {
getCmd := &cobra.Command{
Expand All @@ -47,13 +42,22 @@ func NewGetInstancesCmd() *cobra.Command {

func run(cmd *cobra.Command, args []string) {

mustKubeConfig()

_, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
log.Printf("Flag Error: %v", err)
}

// If the $KUBECONFIG environment variable is set, use that
if len(os.Getenv("KUBECONFIG")) > 0 {
kubeConfig = os.Getenv("KUBECONFIG")
}

check.KubeConfigLocationOrDefault(kubeConfig)

if err := check.ValidateKubeConfigPath(kubeConfig); err != nil {
log.Printf("Could not check kubeconfig path: %v", err)
}

p, err := getInstances()
if err != nil {
log.Printf("Error: %v", err)
Expand Down Expand Up @@ -110,20 +114,3 @@ func getInstances() ([]string, error) {

return instanceList, nil
}

// mustKubeConfig checks if the kubeconfig file exists.
func mustKubeConfig() {
// if kubeConfig is not specified, search for the default kubeconfig file under the $HOME/.kube/config.
if len(kubeConfig) == 0 {
usr, err := user.Current()
if err != nil {
fmt.Printf("Error: failed to determine user's home dir: %v", err)
}
kubeConfig = filepath.Join(usr.HomeDir, defaultConfigPath)
}

_, err := os.Stat(kubeConfig)
if err != nil && os.IsNotExist(err) {
fmt.Printf("Error: failed to find the kubeconfig file (%v): %v", kubeConfig, err)
}
}
6 changes: 6 additions & 0 deletions pkg/kudoctl/cmd/plan/plan_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plan
import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/kudobuilder/kudo/pkg/kudoctl/util/check"
Expand Down Expand Up @@ -53,6 +54,11 @@ func runHistory(cmd *cobra.Command, args []string, options *historyOptions) erro
return fmt.Errorf("flag Error: Please set instance flag, e.g. \"--instance=<instanceName>\"")
}

// If the $KUBECONFIG environment variable is set, use that
if len(os.Getenv("KUBECONFIG")) > 0 {
options.kubeConfigPath = os.Getenv("KUBECONFIG")
}

configPath, err := check.KubeConfigLocationOrDefault(options.kubeConfigPath)
if err != nil {
return fmt.Errorf("error when getting default kubeconfig path: %+v", err)
Expand Down
6 changes: 6 additions & 0 deletions pkg/kudoctl/cmd/plan/plan_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"os"

kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/check"
Expand Down Expand Up @@ -52,6 +53,11 @@ func runStatus(cmd *cobra.Command, args []string, options *statusOptions) error
return fmt.Errorf("flag Error: Please set instance flag, e.g. \"--instance=<instanceName>\"")
}

// If the $KUBECONFIG environment variable is set, use that
if len(os.Getenv("KUBECONFIG")) > 0 {
options.kubeConfigPath = os.Getenv("KUBECONFIG")
}

configPath, err := check.KubeConfigLocationOrDefault(options.kubeConfigPath)
if err != nil {
return fmt.Errorf("error when getting default kubeconfig path: %+v", err)
Expand Down

0 comments on commit 8c5300c

Please sign in to comment.