Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent default merging when specifying a specific kubeconfig for config view #4662

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions pkg/kubectl/cmd/config/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

type viewOptions struct {
pathOptions *pathOptions
merge bool
merge util.BoolFlag
}

func NewCmdConfigView(out io.Writer, pathOptions *pathOptions) *cobra.Command {
Expand All @@ -48,7 +49,9 @@ Examples:
// Show only local ./.kubeconfig settings
$ kubectl config view --local`,
Run: func(cmd *cobra.Command, args []string) {
printer, _, err := util.PrinterForCommand(cmd)
options.complete()

printer, _, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
glog.FatalDepth(1, err)
}
Expand All @@ -63,13 +66,26 @@ Examples:
},
}

util.AddPrinterFlags(cmd)
cmdutil.AddPrinterFlags(cmd)
// Default to yaml
cmd.Flags().Set("output", "yaml")
cmd.Flags().BoolVar(&options.merge, "merge", true, "merge together the full hierarchy of .kubeconfig files")

options.merge.Default(true)
cmd.Flags().Var(&options.merge, "merge", "merge together the full hierarchy of .kubeconfig files")
return cmd
}

func (o *viewOptions) complete() bool {
// if --kubeconfig, --global, or --local is specified, then merging doesn't make sense since you're declaring precise intent
if (len(o.pathOptions.specifiedFile) > 0) || o.pathOptions.global || o.pathOptions.local {
if !o.merge.Provided() {
o.merge.Set("false")
}
}

return true
}

func (o viewOptions) loadConfig() (*clientcmdapi.Config, error) {
err := o.validate()
if err != nil {
Expand All @@ -87,7 +103,7 @@ func (o viewOptions) validate() error {
// getStartingConfig returns the Config object built from the sources specified by the options, the filename read (only if it was a single file), and an error if something goes wrong
func (o *viewOptions) getStartingConfig() (*clientcmdapi.Config, string, error) {
switch {
case o.merge:
case o.merge.Value():
loadingRules := clientcmd.NewClientConfigLoadingRules()
loadingRules.EnvVarPath = os.Getenv("KUBECONFIG")
loadingRules.CommandLinePath = o.pathOptions.specifiedFile
Expand Down