mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 07:03:31 +00:00
add flag to manage $KUBECONFIG files
This commit is contained in:
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -31,6 +32,7 @@ import (
|
||||
type pathOptions struct {
|
||||
local bool
|
||||
global bool
|
||||
envvar bool
|
||||
specifiedFile string
|
||||
}
|
||||
|
||||
@@ -47,8 +49,9 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
|
||||
}
|
||||
|
||||
// file paths are common to all sub commands
|
||||
cmd.PersistentFlags().BoolVar(&pathOptions.local, "local", true, "use the .kubeconfig in the current directory")
|
||||
cmd.PersistentFlags().BoolVar(&pathOptions.local, "local", false, "use the .kubeconfig in the current directory")
|
||||
cmd.PersistentFlags().BoolVar(&pathOptions.global, "global", false, "use the .kubeconfig from "+os.Getenv("HOME"))
|
||||
cmd.PersistentFlags().BoolVar(&pathOptions.envvar, "envvar", false, "use the .kubeconfig from $KUBECONFIG")
|
||||
cmd.PersistentFlags().StringVar(&pathOptions.specifiedFile, "kubeconfig", "", "use a particular .kubeconfig file")
|
||||
|
||||
cmd.AddCommand(NewCmdConfigView(out, pathOptions))
|
||||
@@ -65,18 +68,53 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
|
||||
func (o *pathOptions) getStartingConfig() (*clientcmdapi.Config, string, error) {
|
||||
filename := ""
|
||||
config := clientcmdapi.NewConfig()
|
||||
switch {
|
||||
case o.global:
|
||||
filename = os.Getenv("HOME") + "/.kube/.kubeconfig"
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
|
||||
case len(o.specifiedFile) > 0:
|
||||
if len(o.specifiedFile) > 0 {
|
||||
filename = o.specifiedFile
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
}
|
||||
|
||||
if o.global {
|
||||
if len(filename) > 0 {
|
||||
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
|
||||
}
|
||||
|
||||
filename = os.Getenv("HOME") + "/.kube/.kubeconfig"
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
}
|
||||
|
||||
if o.envvar {
|
||||
if len(filename) > 0 {
|
||||
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
|
||||
}
|
||||
|
||||
filename = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
||||
if len(filename) == 0 {
|
||||
return nil, "", fmt.Errorf("environment variable %v does not have a value", clientcmd.RecommendedConfigPathEnvVar)
|
||||
}
|
||||
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
}
|
||||
|
||||
if o.local {
|
||||
if len(filename) > 0 {
|
||||
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
|
||||
}
|
||||
|
||||
case o.local:
|
||||
filename = ".kubeconfig"
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
|
||||
}
|
||||
|
||||
// no specific flag was set, first attempt to use the envvar, then use local
|
||||
if len(filename) == 0 {
|
||||
if len(os.Getenv(clientcmd.RecommendedConfigPathEnvVar)) > 0 {
|
||||
filename = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
} else {
|
||||
filename = ".kubeconfig"
|
||||
config = getConfigFromFileOrDie(filename)
|
||||
}
|
||||
}
|
||||
|
||||
return config, filename, nil
|
||||
|
@@ -19,6 +19,7 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -82,7 +83,7 @@ func (o *viewOptions) getStartingConfig() (*clientcmdapi.Config, string, error)
|
||||
switch {
|
||||
case o.merge:
|
||||
loadingRules := clientcmd.NewClientConfigLoadingRules()
|
||||
loadingRules.EnvVarPath = ""
|
||||
loadingRules.EnvVarPath = os.Getenv("KUBECONFIG")
|
||||
loadingRules.CommandLinePath = o.pathOptions.specifiedFile
|
||||
|
||||
overrides := &clientcmd.ConfigOverrides{}
|
||||
|
Reference in New Issue
Block a user