Skip to content

Commit

Permalink
Use kubeconfig flag set at root for subcommands (#575)
Browse files Browse the repository at this point in the history
* Use kubeconfig flag set at root for subcommands

* Expand shell variable for user's home directory
  • Loading branch information
yankcrime authored and kensipe committed Jul 16, 2019
1 parent d9b5a33 commit bdca4c4
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 188 deletions.
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ require (
github.com/go-test/deep v1.0.1
github.com/gobuffalo/envy v1.6.15 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
Expand All @@ -33,21 +31,17 @@ require (
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v0.9.2 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.2.0 // indirect
github.com/prometheus/procfs v0.0.0-20190209105433-f8d8b3f739bd // indirect
github.com/rogpeppe/go-internal v1.2.2 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d // indirect
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a // indirect
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138
google.golang.org/appengine v1.5.0 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
Expand Down
62 changes: 62 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pkg/kudoctl/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func newGetCmd() *cobra.Command {
},
}

getCmd.Flags().StringVar(&options.KubeConfigPath, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config")
getCmd.Flags().StringVar(&options.Namespace, "namespace", "default", "The namespace where the operator watches for changes.")

return getCmd
Expand Down
28 changes: 3 additions & 25 deletions pkg/kudoctl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ package get
import (
"fmt"
"log"
"os"

"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo"
"github.com/pkg/errors"
"github.com/spf13/viper"

"github.com/xlab/treeprint"
"k8s.io/client-go/tools/clientcmd"

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

// Options defines configuration options for the get command
type Options struct {
KubeConfigPath string
Namespace string
Namespace string
}

// DefaultOptions initializes the get command options to its defaults
Expand All @@ -33,7 +29,7 @@ func Run(args []string, options *Options) error {
return err
}

kc, err := kudo.NewClient(options.Namespace, options.KubeConfigPath)
kc, err := kudo.NewClient(options.Namespace, viper.GetString("kubeconfig"))
if err != nil {
return errors.Wrap(err, "creating kudo client")
}
Expand Down Expand Up @@ -61,24 +57,6 @@ func validate(args []string, options *Options) error {
return fmt.Errorf("expecting \"instances\" and not \"%s\"", args[0])
}

// 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)
}
options.KubeConfigPath = configPath
if err := check.ValidateKubeConfigPath(options.KubeConfigPath); err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}
_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "getting config failed")
}

return nil

}
Expand Down
1 change: 0 additions & 1 deletion pkg/kudoctl/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func newInstallCmd() *cobra.Command {
SilenceUsage: true,
}

installCmd.Flags().StringVar(&options.KubeConfigPath, "kubeconfig", "", "The file path to Kubernetes configuration file. (default \"$HOME/.kube/config\")")
installCmd.Flags().StringVar(&options.InstanceName, "instance", "", "The instance name. (default to Operator name)")
installCmd.Flags().StringVar(&options.Namespace, "namespace", "default", "The namespace used for the package installation. (default \"default\"")
installCmd.Flags().StringArrayVarP(&parameters, "parameter", "p", nil, "The parameter name and value separated by '='")
Expand Down
29 changes: 2 additions & 27 deletions pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import (
"github.com/kudobuilder/kudo/pkg/kudoctl/bundle"
"github.com/kudobuilder/kudo/pkg/kudoctl/bundle/finder"
"github.com/kudobuilder/kudo/pkg/kudoctl/http"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/check"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/repo"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
"github.com/spf13/viper"
)

// Options defines configuration options for the install command
type Options struct {
InstanceName string
KubeConfigPath string
Namespace string
Parameters map[string]string
PackageVersion string
Expand Down Expand Up @@ -48,24 +46,6 @@ func validate(args []string, options *Options) error {
return fmt.Errorf("expecting exactly one argument - name of the package or path to install")
}

// 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)
}
options.KubeConfigPath = configPath
if err := check.ValidateKubeConfigPath(options.KubeConfigPath); err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}
_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "getting config failed")
}

return nil
}

Expand Down Expand Up @@ -111,12 +91,7 @@ func installOperator(operatorArgument string, options *Options) error {
return errors.WithMessage(err, "could not build operator repository")
}

_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "getting config failed")
}

kc, err := kudo.NewClient(options.Namespace, options.KubeConfigPath)
kc, err := kudo.NewClient(options.Namespace, viper.GetString("kubeconfig"))
if err != nil {
return errors.Wrap(err, "creating kudo client")
}
Expand Down
37 changes: 7 additions & 30 deletions pkg/kudoctl/cmd/plan/plan_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package plan
import (
"encoding/json"
"fmt"
"os"
"time"

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

kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xlab/treeprint"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -19,12 +16,13 @@ import (
)

type historyOptions struct {
instance string
kubeConfigPath string
namespace string
instance string
namespace string
}

var defaultHistoryOptions = &historyOptions{}
var (
defaultHistoryOptions = &historyOptions{}
)

// NewPlanHistoryCmd creates a command that shows the plan history of an instance.
func NewPlanHistoryCmd() *cobra.Command {
Expand All @@ -41,7 +39,6 @@ func NewPlanHistoryCmd() *cobra.Command {
}

listCmd.Flags().StringVar(&options.instance, "instance", "", "The instance name.")
listCmd.Flags().StringVar(&options.kubeConfigPath, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config")
listCmd.Flags().StringVar(&options.namespace, "namespace", "default", "The namespace where the operator watches for changes.")

return listCmd
Expand All @@ -54,26 +51,6 @@ 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)
}
options.kubeConfigPath = configPath
if err := check.ValidateKubeConfigPath(options.kubeConfigPath); err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}

_, err = cmd.Flags().GetString("kubeconfig")
// TODO: wrong flag
if err != nil || instanceFlag == "" {
return fmt.Errorf("flag Error: %v", err)
}

err = planHistory(args, options)
if err != nil {
return fmt.Errorf("client Error: %v", err)
Expand All @@ -83,7 +60,7 @@ func runHistory(cmd *cobra.Command, args []string, options *historyOptions) erro

func planHistory(args []string, options *historyOptions) error {

config, err := clientcmd.BuildConfigFromFlags("", options.kubeConfigPath)
config, err := clientcmd.BuildConfigFromFlags("", viper.GetString("kubeconfig"))
if err != nil {
return err
}
Expand Down
33 changes: 5 additions & 28 deletions pkg/kudoctl/cmd/plan/plan_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"encoding/json"
"fmt"
"log"
"os"

kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/check"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xlab/treeprint"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -18,9 +16,8 @@ import (
)

type statusOptions struct {
instance string
kubeConfigPath string
namespace string
instance string
namespace string
}

var defaultStatusOptions = &statusOptions{}
Expand All @@ -33,14 +30,13 @@ func NewPlanStatusCmd() *cobra.Command {
Short: "Shows the status of all plans to an particular instance.",
Long: `
# View plan status
kudoctl plan status --instance=<instanceName> --kubeconfig=<$HOME/.kube/config>`,
kudoctl plan status --instance=<instanceName>`,
RunE: func(cmd *cobra.Command, args []string) error {
return runStatus(cmd, args, options)
},
}

statusCmd.Flags().StringVar(&options.instance, "instance", "", "The instance name available from 'kubectl get instances'")
statusCmd.Flags().StringVar(&options.kubeConfigPath, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config")
statusCmd.Flags().StringVar(&options.namespace, "namespace", "default", "The namespace where the instance is running.")

return statusCmd
Expand All @@ -53,25 +49,6 @@ 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)
}
options.kubeConfigPath = configPath
if err := check.ValidateKubeConfigPath(options.kubeConfigPath); err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}

_, err = cmd.Flags().GetString("kubeconfig")
if err != nil || instanceFlag == "" {
return fmt.Errorf("flag Error: Please set kubeconfig flag, e.g. \"--kubeconfig=<$HOME/.kube/config>\"")
}

err = planStatus(options)
if err != nil {
return fmt.Errorf("client Error: %v", err)
Expand All @@ -83,7 +60,7 @@ func planStatus(options *statusOptions) error {

tree := treeprint.New()

config, err := clientcmd.BuildConfigFromFlags("", options.kubeConfigPath)
config, err := clientcmd.BuildConfigFromFlags("", viper.GetString("kubeconfig"))
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/kudoctl/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"os"

"github.com/kudobuilder/kudo/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// NewKudoctlCmd creates a new root command for kudoctl
Expand Down Expand Up @@ -46,6 +49,9 @@ and serves as an API aggregation layer.
cmd.AddCommand(newPlanCmd())
cmd.AddCommand(newTestCmd())
cmd.AddCommand(newVersionCmd())
cmd.PersistentFlags().String("kubeconfig", os.Getenv("HOME")+"/.kube/config", "Path to your Kubernetes configuration file")
viper.BindEnv("kubeconfig")
viper.BindPFlag("kubeconfig", cmd.PersistentFlags().Lookup("kubeconfig"))

return cmd
}
38 changes: 0 additions & 38 deletions pkg/kudoctl/util/check/check.go

This file was deleted.

Loading

0 comments on commit bdca4c4

Please sign in to comment.