...
1
16
17 package config
18
19 import (
20 "fmt"
21 "path"
22 "strconv"
23
24 "github.com/spf13/cobra"
25
26 "k8s.io/cli-runtime/pkg/genericiooptions"
27 "k8s.io/client-go/tools/clientcmd"
28 cmdutil "k8s.io/kubectl/pkg/cmd/util"
29 "k8s.io/kubectl/pkg/util/i18n"
30 "k8s.io/kubectl/pkg/util/templates"
31 )
32
33
34 func NewCmdConfig(pathOptions *clientcmd.PathOptions, streams genericiooptions.IOStreams) *cobra.Command {
35 if len(pathOptions.ExplicitFileFlag) == 0 {
36 pathOptions.ExplicitFileFlag = clientcmd.RecommendedConfigPathFlag
37 }
38
39 cmd := &cobra.Command{
40 Use: "config SUBCOMMAND",
41 DisableFlagsInUseLine: true,
42 Short: i18n.T("Modify kubeconfig files"),
43 Long: templates.LongDesc(i18n.T(`
44 Modify kubeconfig files using subcommands like "kubectl config set current-context my-context".
45
46 The loading order follows these rules:
47
48 1. If the --`) + pathOptions.ExplicitFileFlag + i18n.T(` flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
49 2. If $`) + pathOptions.EnvVar + i18n.T(` environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
50 3. Otherwise, `) + path.Join("${HOME}", pathOptions.GlobalFileSubpath) + i18n.T(` is used and no merging takes place.`)),
51 Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
52 }
53
54
55 cmd.PersistentFlags().StringVar(&pathOptions.LoadingRules.ExplicitPath, pathOptions.ExplicitFileFlag, pathOptions.LoadingRules.ExplicitPath, "use a particular kubeconfig file")
56
57
58 cmd.AddCommand(NewCmdConfigView(streams, pathOptions))
59 cmd.AddCommand(NewCmdConfigSetCluster(streams.Out, pathOptions))
60 cmd.AddCommand(NewCmdConfigSetCredentials(streams.Out, pathOptions))
61 cmd.AddCommand(NewCmdConfigSetContext(streams.Out, pathOptions))
62 cmd.AddCommand(NewCmdConfigSet(streams.Out, pathOptions))
63 cmd.AddCommand(NewCmdConfigUnset(streams.Out, pathOptions))
64 cmd.AddCommand(NewCmdConfigCurrentContext(streams.Out, pathOptions))
65 cmd.AddCommand(NewCmdConfigUseContext(streams.Out, pathOptions))
66 cmd.AddCommand(NewCmdConfigGetContexts(streams, pathOptions))
67 cmd.AddCommand(NewCmdConfigGetClusters(streams.Out, pathOptions))
68 cmd.AddCommand(NewCmdConfigGetUsers(streams, pathOptions))
69 cmd.AddCommand(NewCmdConfigDeleteCluster(streams.Out, pathOptions))
70 cmd.AddCommand(NewCmdConfigDeleteContext(streams.Out, streams.ErrOut, pathOptions))
71 cmd.AddCommand(NewCmdConfigDeleteUser(streams, pathOptions))
72 cmd.AddCommand(NewCmdConfigRenameContext(streams.Out, pathOptions))
73
74 return cmd
75 }
76
77 func toBool(propertyValue string) (bool, error) {
78 boolValue := false
79 if len(propertyValue) != 0 {
80 var err error
81 boolValue, err = strconv.ParseBool(propertyValue)
82 if err != nil {
83 return false, err
84 }
85 }
86
87 return boolValue, nil
88 }
89
90 func helpErrorf(cmd *cobra.Command, format string, args ...interface{}) error {
91 cmd.Help()
92 msg := fmt.Sprintf(format, args...)
93 return fmt.Errorf("%s", msg)
94 }
95
View as plain text