diff --git a/.github/workflows/e2e-test.yaml b/.github/workflows/e2e-test.yaml index 06fd19e9..d8e66175 100644 --- a/.github/workflows/e2e-test.yaml +++ b/.github/workflows/e2e-test.yaml @@ -64,7 +64,7 @@ jobs: echo "Running kubecm add..." echo "********************************************************************************" bin/kubecm add -cf 2nd-kind - bin/kubecm add -cf 3rd-kind --context-name 3rd + bin/kubecm add -cf 3rd-kind --context-prefix 3rd echo "********************************************************************************" echo "Running kubecm merge multiple kubeconfig..." echo "********************************************************************************" @@ -85,7 +85,7 @@ jobs: echo "********************************************************************************" echo "Running kubecm switch..." echo "********************************************************************************" - bin/kubecm s 3rd + bin/kubecm s 3rd-kind-3rd-kind echo "********************************************************************************" echo "Running kubecm delete..." echo "********************************************************************************" diff --git a/cmd/add.go b/cmd/add.go index f1b14dbf..923db6c3 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -7,6 +7,7 @@ import ( "os" "reflect" "strconv" + "strings" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" @@ -36,10 +37,12 @@ func (ac *AddCommand) Init() { }, Example: addExample(), } - ac.command.Flags().StringP("file", "f", "", "Path to merge kubeconfig files") - ac.command.Flags().String("context-name", "", "override context name when add kubeconfig context") - ac.command.PersistentFlags().BoolP("cover", "c", false, "Overwrite local kubeconfig files") - ac.command.PersistentFlags().Bool("select-context", false, "select the context to be added") + ac.command.Flags().StringP("file", "f", "", "path to merge kubeconfig files") + ac.command.Flags().String("context-prefix", "", "add a prefix before context name") + ac.command.Flags().String("context-name", "", "override context name when add kubeconfig context, when context-name is set, context-prefix and context-template parameters will be ignored") + ac.command.PersistentFlags().BoolP("cover", "c", false, "overwrite local kubeconfig files") + ac.command.Flags().Bool("select-context", false, "select the context to be added") + ac.command.Flags().StringSlice("context-template", []string{"context"}, "define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace") _ = ac.command.MarkFlagRequired("file") ac.AddCommands(&DocsCommand{}) } @@ -47,11 +50,21 @@ func (ac *AddCommand) Init() { func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { file, _ := ac.command.Flags().GetString("file") cover, _ := ac.command.Flags().GetBool("cover") + contextPrefix, _ := ac.command.Flags().GetString("context-prefix") contextName, _ := ac.command.Flags().GetString("context-name") selectContext, _ := ac.command.Flags().GetBool("select-context") + contextTemplate, _ := ac.command.Flags().GetStringSlice("context-template") var newConfig *clientcmdapi.Config - var err error + + if contextName != "" { + contextTemplate = []string{} + contextPrefix = contextName + } + err := validateContextTemplate(contextTemplate) + if err != nil { + return err + } if file == "-" { // from stdin @@ -75,7 +88,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { } } - err = AddToLocal(newConfig, file, contextName, cover, selectContext) + err = AddToLocal(newConfig, file, contextPrefix, cover, selectContext, contextTemplate) if err != nil { return err } @@ -83,7 +96,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { } // AddToLocal add kubeConfig to local -func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool, selectContext bool) error { +func AddToLocal(newConfig *clientcmdapi.Config, path, contextPrefix string, cover bool, selectContext bool, contextTemplate []string) error { oldConfig, err := clientcmd.LoadFromFile(cfgFile) if err != nil { return err @@ -93,7 +106,7 @@ func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool fileName: getFileName(path), } // merge context loop - outConfig, err := kco.handleContexts(oldConfig, newName, selectContext) + outConfig, err := kco.handleContexts(oldConfig, contextPrefix, selectContext, contextTemplate) if err != nil { return err } @@ -121,21 +134,22 @@ func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool return nil } -func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, contextName string, selectContext bool) (*clientcmdapi.Config, error) { +func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, contextPrefix string, selectContext bool, contextTemplate []string) (*clientcmdapi.Config, error) { newConfig := clientcmdapi.NewConfig() - var index int var newName string + generatedName := make(map[string]int) + for name, ctx := range kc.config.Contexts { - if len(kc.config.Contexts) > 1 { - if contextName == "" { - newName = fmt.Sprintf("%s-%s", kc.fileName, HashSufString(name)) - } else { - newName = fmt.Sprintf("%s-%d", contextName, index) - } - } else if contextName == "" { - newName = name - } else { - newName = contextName + newName = kc.generateContextName(name, ctx, contextTemplate) + if contextPrefix != "" { + newName = strings.TrimSuffix(fmt.Sprintf("%s-%s", contextPrefix, newName), "-") + } + + // prevent generate duplicate context name + // for example: set --context-template user,cluster, the context1 and context2 have the same user and cluster + generatedName[newName]++ + if generatedName[newName] > 1 { + newName = fmt.Sprintf("%s-%d", newName, generatedName[newName]) } if selectContext { @@ -159,12 +173,32 @@ func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, conte itemConfig := kc.handleContext(oldConfig, newName, ctx) newConfig = appendConfig(newConfig, itemConfig) fmt.Printf("Add Context: %s \n", newName) - index++ } outConfig := appendConfig(oldConfig, newConfig) return outConfig, nil } +func (kc *KubeConfigOption) generateContextName(name string, ctx *clientcmdapi.Context, contextTemplate []string) string { + valueMap := map[string]string{ + Filename: kc.fileName, + Context: name, + User: ctx.AuthInfo, + Cluster: ctx.Cluster, + Namespace: ctx.Namespace, + } + + var contextValues []string + for _, value := range contextTemplate { + if v, ok := valueMap[value]; ok { + if v != "" { + contextValues = append(contextValues, v) + } + } + } + + return strings.Join(contextValues, "-") +} + func checkContextName(name string, oldConfig *clientcmdapi.Config) bool { if _, ok := oldConfig.Contexts[name]; ok { return true @@ -223,15 +257,19 @@ func (kc *KubeConfigOption) handleContext(oldConfig *clientcmdapi.Config, func addExample() string { return ` -Note: If -c is set and more than one context is added to the kubeconfig file, the following will occur: -- If --context-name is set, the context will be generated as , ... -- If --context-name is not set, it will be generated as where {hash} is the MD5 hash of the file name. - # Merge test.yaml with $HOME/.kube/config kubecm add -f test.yaml -# Merge test.yaml with $HOME/.kube/config and rename context name -kubecm add -cf test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm add -cf test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm add -f test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm add -f test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and override context name, it's useful if there is only one context in the kubeconfig file +kubecm add -f test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm add -f test.yaml --select-context # Add kubeconfig from stdin -cat /etc/kubernetes/admin.conf | kubecm add -f - +cat /etc/kubernetes/admin.conf | kubecm add -f - ` } diff --git a/cmd/add_test.go b/cmd/add_test.go index 69ba68e5..4a361103 100644 --- a/cmd/add_test.go +++ b/cmd/add_test.go @@ -62,23 +62,23 @@ var ( AuthInfos: map[string]*clientcmdapi.AuthInfo{ "black-user": {Token: "black-token"}, "red-user": {Token: "red-token"}, - "red-user-7f65b9cc8f": {Token: "red-token"}, - "black-user-gtch2cf96d": {Token: "black-token"}, + "red-user-cbc897d6ch": {Token: "red-token"}, + "black-user-d2m9fd8b7d": {Token: "black-token"}, "not-exist": {Token: "not-exist-token"}, }, Clusters: map[string]*clientcmdapi.Cluster{ "pig-cluster": {Server: "http://pig.org:8080"}, "cow-cluster": {Server: "http://cow.org:8080"}, - "cow-cluster-7f65b9cc8f": {Server: "http://cow.org:8080"}, - "pig-cluster-gtch2cf96d": {Server: "http://pig.org:8080"}, + "cow-cluster-cbc897d6ch": {Server: "http://cow.org:8080"}, + "pig-cluster-d2m9fd8b7d": {Server: "http://pig.org:8080"}, "not-exist": {Server: "http://not.exist:8080"}, }, Contexts: map[string]*clientcmdapi.Context{ - "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, - "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, - "test-d2m9fd8b7d": {AuthInfo: "black-user-gtch2cf96d", Cluster: "pig-cluster-gtch2cf96d", Namespace: "saw-ns"}, - "test-cbc897d6ch": {AuthInfo: "red-user-7f65b9cc8f", Cluster: "cow-cluster-7f65b9cc8f", Namespace: "hammer-ns"}, - "test-2h6782585t": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"}, + "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, + "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, + "root-context": {AuthInfo: "black-user-d2m9fd8b7d", Cluster: "pig-cluster-d2m9fd8b7d", Namespace: "saw-ns"}, + "federal-context": {AuthInfo: "red-user-cbc897d6ch", Cluster: "cow-cluster-cbc897d6ch", Namespace: "hammer-ns"}, + "not-exist-context": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"}, }, } singleTestConfig = clientcmdapi.Config{ @@ -110,6 +110,57 @@ var ( }, } renameSingleTestConfig = clientcmdapi.Config{ + AuthInfos: map[string]*clientcmdapi.AuthInfo{ + "black-user": {Token: "black-token"}, + "red-user": {Token: "red-token"}, + "single-user": {Token: "single-token"}, + }, + Clusters: map[string]*clientcmdapi.Cluster{ + "pig-cluster": {Server: "http://pig.org:8080"}, + "cow-cluster": {Server: "http://cow.org:8080"}, + "single-cluster": {Server: "http://single:8080"}, + }, + Contexts: map[string]*clientcmdapi.Context{ + "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, + "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, + "rename-single-context": {AuthInfo: "single-user", Cluster: "single-cluster", Namespace: "single-ns"}, + }, + } + contextTemplateTestConfig = clientcmdapi.Config{ + AuthInfos: map[string]*clientcmdapi.AuthInfo{ + "black-user": {Token: "black-token"}, + "red-user": {Token: "red-token"}, + "single-user": {Token: "single-token"}, + }, + Clusters: map[string]*clientcmdapi.Cluster{ + "pig-cluster": {Server: "http://pig.org:8080"}, + "cow-cluster": {Server: "http://cow.org:8080"}, + "single-cluster": {Server: "http://single:8080"}, + }, + Contexts: map[string]*clientcmdapi.Context{ + "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, + "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, + "test-single-user-single-cluster": {AuthInfo: "single-user", Cluster: "single-cluster", Namespace: "single-ns"}, + }, + } + contextTemplateAndPrefixTestConfig = clientcmdapi.Config{ + AuthInfos: map[string]*clientcmdapi.AuthInfo{ + "black-user": {Token: "black-token"}, + "red-user": {Token: "red-token"}, + "single-user": {Token: "single-token"}, + }, + Clusters: map[string]*clientcmdapi.Cluster{ + "pig-cluster": {Server: "http://pig.org:8080"}, + "cow-cluster": {Server: "http://cow.org:8080"}, + "single-cluster": {Server: "http://single:8080"}, + }, + Contexts: map[string]*clientcmdapi.Context{ + "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, + "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, + "demo-single-user-single-cluster": {AuthInfo: "single-user", Cluster: "single-cluster", Namespace: "single-ns"}, + }, + } + contextNameTestConfig = clientcmdapi.Config{ AuthInfos: map[string]*clientcmdapi.AuthInfo{ "black-user": {Token: "black-token"}, "red-user": {Token: "red-token"}, @@ -123,7 +174,7 @@ var ( Contexts: map[string]*clientcmdapi.Context{ "root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, "federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, - "rename": {AuthInfo: "single-user", Cluster: "single-cluster", Namespace: "single-ns"}, + "demo": {AuthInfo: "single-user", Cluster: "single-cluster", Namespace: "single-ns"}, }, } ) @@ -192,8 +243,9 @@ func TestKubeConfig_handleContexts(t *testing.T) { fileName string } type args struct { - oldConfig *clientcmdapi.Config - newName string + oldConfig *clientcmdapi.Config + contextPrefix string + contextTemplate []string } tests := []struct { name string @@ -203,9 +255,12 @@ func TestKubeConfig_handleContexts(t *testing.T) { wantErr bool }{ // TODO: Add test cases. - {"not have new context name", fields{config: newConfig, fileName: "test"}, args{&oldTestConfig, ""}, &mergedConfig, false}, - {"single context name", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, ""}, &mergeSingleTestConfig, false}, - {"single context name - new", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "rename"}, &renameSingleTestConfig, false}, + {"not have new context name", fields{config: newConfig, fileName: "test"}, args{&oldTestConfig, "", []string{"context"}}, &mergedConfig, false}, + {"single context name", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "", []string{"context"}}, &mergeSingleTestConfig, false}, + {"single context name - new", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "rename", []string{"context"}}, &renameSingleTestConfig, false}, + {"set context template", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "", []string{"filename", "user", "cluster"}}, &contextTemplateTestConfig, false}, + {"set context template and context prefix", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "demo", []string{"user", "cluster"}}, &contextTemplateAndPrefixTestConfig, false}, + {"set context name", fields{config: singleConfig, fileName: "test"}, args{&oldTestConfig, "demo", []string{}}, &contextNameTestConfig, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -213,7 +268,7 @@ func TestKubeConfig_handleContexts(t *testing.T) { config: tt.fields.config, fileName: tt.fields.fileName, } - got, err := kc.handleContexts(tt.args.oldConfig, tt.args.newName, false) + got, err := kc.handleContexts(tt.args.oldConfig, tt.args.contextPrefix, false, tt.args.contextTemplate) if (err != nil) != tt.wantErr { t.Errorf("handleContexts() error = %v, wantErr %v", err, tt.wantErr) return @@ -265,7 +320,7 @@ func TestAddToLocal(t *testing.T) { } // Test AddToLocal function - err = AddToLocal(newConfig, tempFile.Name(), "", true, false) + err = AddToLocal(newConfig, tempFile.Name(), "", true, false, []string{"context"}) if err != nil { t.Fatalf("Failed to add to local: %v", err) } @@ -280,3 +335,84 @@ func TestAddToLocal(t *testing.T) { t.Fatalf("Failed to find 'test-context' in the loaded config") } } + +func TestGenerateContextName(t *testing.T) { + type fields struct { + config *clientcmdapi.Config + fileName string + } + type args struct { + name string + ctx *clientcmdapi.Context + contextTemplate []string + } + tests := []struct { + name string + fields fields + args args + want string + }{ + { + name: "all attributes", + fields: fields{ + config: &clientcmdapi.Config{}, + fileName: "test-file", + }, + args: args{ + name: "test-context", + ctx: &clientcmdapi.Context{ + AuthInfo: "test-user", + Cluster: "test-cluster", + Namespace: "test-namespace", + }, + contextTemplate: []string{"filename", "context", "user", "cluster", "namespace"}, + }, + want: "test-file-test-context-test-user-test-cluster-test-namespace", + }, + { + name: "partial attributes", + fields: fields{ + config: &clientcmdapi.Config{}, + fileName: "test-file", + }, + args: args{ + name: "test-context", + ctx: &clientcmdapi.Context{ + AuthInfo: "test-user", + Cluster: "test-cluster", + Namespace: "test-namespace", + }, + contextTemplate: []string{"filename", "user", "namespace"}, + }, + want: "test-file-test-user-test-namespace", + }, + { + name: "no attributes", + fields: fields{ + config: &clientcmdapi.Config{}, + fileName: "test-file", + }, + args: args{ + name: "test-context", + ctx: &clientcmdapi.Context{ + AuthInfo: "test-user", + Cluster: "test-cluster", + Namespace: "test-namespace", + }, + contextTemplate: []string{}, + }, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + kc := &KubeConfigOption{ + config: tt.fields.config, + fileName: tt.fields.fileName, + } + if got := kc.generateContextName(tt.args.name, tt.args.ctx, tt.args.contextTemplate); got != tt.want { + t.Errorf("generateContextName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/cloud_add.go b/cmd/cloud_add.go index 553fa8ca..4a9223c5 100644 --- a/cmd/cloud_add.go +++ b/cmd/cloud_add.go @@ -37,6 +37,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error regionID, _ := ca.command.Flags().GetString("region_id") cover, _ := ca.command.Flags().GetBool("cover") selectContext, _ := ca.command.Flags().GetBool("select-context") + contextTemplate, _ := ca.command.Flags().GetStringSlice("context-template") var num int if provider == "" { num = selectCloud(Clouds, "Select Cloud") @@ -75,7 +76,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext) + err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -88,7 +89,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, fmt.Sprintf("alicloud-%s", clusterID), "", cover, selectContext) + err = AddToLocal(newConfig, fmt.Sprintf("alicloud-%s", clusterID), "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -128,7 +129,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext) + err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -141,7 +142,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, fmt.Sprintf("tencent-%s", clusterID), "", cover, selectContext) + err = AddToLocal(newConfig, fmt.Sprintf("tencent-%s", clusterID), "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -170,7 +171,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext) + err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -183,7 +184,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, fmt.Sprintf("rancher-%s", clusterID), "", cover, selectContext) + err = AddToLocal(newConfig, fmt.Sprintf("rancher-%s", clusterID), "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -220,7 +221,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - err = AddToLocal(newConfig, fmt.Sprintf("aws-%s", clusterID), "", cover, selectContext) + err = AddToLocal(newConfig, fmt.Sprintf("aws-%s", clusterID), "", cover, selectContext, contextTemplate) if err != nil { return err } @@ -280,7 +281,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext) + return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext, contextTemplate) } subscriptionList, err := azure.ListSubscriptions() @@ -333,7 +334,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error if err != nil { return err } - return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext) + return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext, contextTemplate) } return nil diff --git a/cmd/merge.go b/cmd/merge.go index 1ed994ce..c90d0751 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -30,7 +30,9 @@ func (mc *MergeCommand) Init() { } mc.command.Flags().StringP("folder", "f", "", "KubeConfig folder") mc.command.Flags().BoolP("assumeyes", "y", false, "skip interactive file overwrite confirmation") - mc.command.PersistentFlags().Bool("select-context", false, "select the context to be merged") + mc.command.Flags().String("context-prefix", "", "add a prefix before context name") + mc.command.Flags().Bool("select-context", false, "select the context to be merged") + mc.command.Flags().StringSlice("context-template", []string{"context"}, "define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace") //_ = mc.command.MarkFlagRequired("folder") mc.AddCommands(&DocsCommand{}) } @@ -38,10 +40,17 @@ func (mc *MergeCommand) Init() { func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error { files := args folder, _ := mc.command.Flags().GetString("folder") + contextPrefix, _ := mc.command.Flags().GetString("context-prefix") selectContext, _ := mc.command.Flags().GetBool("select-context") + contextTemplate, _ := mc.command.Flags().GetStringSlice("context-template") + + err := validateContextTemplate(contextTemplate) + if err != nil { + return err + } if folder != "" { - folder, err := CheckAndTransformFilePath(folder) + folder, err = CheckAndTransformFilePath(folder) if err != nil { return err } @@ -63,7 +72,7 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error { config: loadConfig, fileName: getFileName(yaml), } - outConfigs, err = kco.handleContexts(outConfigs, "", selectContext) + outConfigs, err = kco.handleContexts(outConfigs, contextPrefix, selectContext, contextTemplate) if err != nil { return err } @@ -79,7 +88,7 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error { cover := BoolUI(fmt.Sprintf("Are you sure you want to overwrite the 「%s」 file?", cfgFile)) confirm, _ = strconv.ParseBool(cover) } - err := WriteConfig(confirm, cfgFile, outConfigs) + err = WriteConfig(confirm, cfgFile, outConfigs) if err != nil { return err } @@ -121,5 +130,13 @@ kubecm merge 1st.yaml 2nd.yaml 3rd.yaml kubecm merge -f dir # Merge KubeConfig in the dir directory to the specified file. kubecm merge -f dir --config kubecm.config +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm merge test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm merge test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm merge test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm merge test.yaml --select-context ` } diff --git a/cmd/root.go b/cmd/root.go index 49d2d5f1..45e6cc77 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -68,6 +68,8 @@ func (cli *Cli) setFlags() { } flags := cli.rootCmd.PersistentFlags() flags.StringVar(&cfgFile, "config", kubeconfig, "path of kubeconfig") + // let the `make doc-gen` command generate consistent output rather than parsing different $HOME environment variables for different users. + flags.Lookup("config").DefValue = "$HOME/.kube/config" flags.IntVarP(&uiSize, "ui-size", "u", 4, "number of list items to show in menu at once") flags.BoolVarP(&silenceTable, "silence-table", "s", false, "enable/disable output of context table on successful config update") flags.BoolVarP(&macNotify, "mac-notify", "m", false, "enable to display Mac notification banner") diff --git a/cmd/utils.go b/cmd/utils.go index 18000548..d6854e82 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -45,6 +45,14 @@ type Namespaces struct { Default bool } +const ( + Filename = "filename" + Context = "context" + User = "user" + Cluster = "cluster" + Namespace = "namespace" +) + // SelectRunner interface - For better unit testing type SelectRunner interface { Run() (int, string, error) @@ -464,3 +472,12 @@ func MacNotifier(msg string) error { func isMacOs() bool { return r.GOOS == "darwin" } + +func validateContextTemplate(contextTemplate []string) error { + for _, value := range contextTemplate { + if value != Filename && value != Context && value != User && value != Cluster && value != Namespace { + return errors.New("the available values for context-template are: filename, user, cluster, context, namespace") + } + } + return nil +} diff --git a/cmd/utils_test.go b/cmd/utils_test.go index a5d2ccf3..ddb0f881 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -412,3 +412,43 @@ func TestPromptUI(t *testing.T) { }) } } + +func TestValidateContextTemplate(t *testing.T) { + type args struct { + contextTemplate []string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "valid context template", + args: args{ + contextTemplate: []string{Filename, Context, User, Cluster, Namespace}, + }, + wantErr: false, + }, + { + name: "invalid context template", + args: args{ + contextTemplate: []string{"invalid"}, + }, + wantErr: true, + }, + { + name: "empty context template", + args: args{ + contextTemplate: []string{}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := validateContextTemplate(tt.args.contextTemplate); (err == nil) == tt.wantErr { + t.Errorf("validateContextTemplate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/docs/en-us/cli/kubecm.md b/docs/en-us/cli/kubecm.md index dd3a4a7e..9dc52855 100644 --- a/docs/en-us/cli/kubecm.md +++ b/docs/en-us/cli/kubecm.md @@ -21,7 +21,7 @@ KubeConfig Manager. ### Options ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -h, --help help for kubecm -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update diff --git a/docs/en-us/cli/kubecm_add.md b/docs/en-us/cli/kubecm_add.md index 284573a8..cc46e154 100644 --- a/docs/en-us/cli/kubecm_add.md +++ b/docs/en-us/cli/kubecm_add.md @@ -14,33 +14,39 @@ kubecm add [flags] ``` -Note: If -c is set and more than one context is added to the kubeconfig file, the following will occur: -- If --context-name is set, the context will be generated as , ... -- If --context-name is not set, it will be generated as where {hash} is the MD5 hash of the file name. - # Merge test.yaml with $HOME/.kube/config kubecm add -f test.yaml -# Merge test.yaml with $HOME/.kube/config and rename context name -kubecm add -cf test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm add -cf test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm add -f test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm add -f test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and override context name, it's useful if there is only one context in the kubeconfig file +kubecm add -f test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm add -f test.yaml --select-context # Add kubeconfig from stdin -cat /etc/kubernetes/admin.conf | kubecm add -f - +cat /etc/kubernetes/admin.conf | kubecm add -f - ``` ### Options ``` - --context-name string override context name when add kubeconfig context - -c, --cover Overwrite local kubeconfig files - -f, --file string Path to merge kubeconfig files - -h, --help help for add - --select-context select the context to be added + --context-name string override context name when add kubeconfig context, when context-name is set, context-prefix and context-template parameters will be ignored + --context-prefix string add a prefix before context name + --context-template strings define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace (default [context]) + -c, --cover overwrite local kubeconfig files + -f, --file string path to merge kubeconfig files + -h, --help help for add + --select-context select the context to be added ``` ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_add_docs.md b/docs/en-us/cli/kubecm_add_docs.md index 57055958..1a4a2854 100644 --- a/docs/en-us/cli/kubecm_add_docs.md +++ b/docs/en-us/cli/kubecm_add_docs.md @@ -30,8 +30,8 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") - -c, --cover Overwrite local kubeconfig files + --config string path of kubeconfig (default "$HOME/.kube/config") + -c, --cover overwrite local kubeconfig files -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_alias.md b/docs/en-us/cli/kubecm_alias.md index b14e35b7..94e402de 100644 --- a/docs/en-us/cli/kubecm_alias.md +++ b/docs/en-us/cli/kubecm_alias.md @@ -31,7 +31,7 @@ $ kubecm alias -o bash ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_alias_docs.md b/docs/en-us/cli/kubecm_alias_docs.md index aee4a4df..45d18f68 100644 --- a/docs/en-us/cli/kubecm_alias_docs.md +++ b/docs/en-us/cli/kubecm_alias_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_clear.md b/docs/en-us/cli/kubecm_clear.md index 051c426b..f7f4b012 100644 --- a/docs/en-us/cli/kubecm_clear.md +++ b/docs/en-us/cli/kubecm_clear.md @@ -14,7 +14,7 @@ kubecm clear ``` -# Clear lapsed context, cluster and user (default is /Users/guoxd/.kube/config) +# Clear lapsed context, cluster and user (default is /Users/I576375/.kube/config) kubecm clear # Customised clear lapsed files kubecm clear config.yaml test.yaml @@ -30,7 +30,7 @@ kubecm clear config.yaml test.yaml ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_clear_docs.md b/docs/en-us/cli/kubecm_clear_docs.md index 3f430233..d0816443 100644 --- a/docs/en-us/cli/kubecm_clear_docs.md +++ b/docs/en-us/cli/kubecm_clear_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_cloud.md b/docs/en-us/cli/kubecm_cloud.md index 468bbd7f..fc2b2067 100644 --- a/docs/en-us/cli/kubecm_cloud.md +++ b/docs/en-us/cli/kubecm_cloud.md @@ -18,7 +18,7 @@ Manage kubeconfig from cloud ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_cloud_add.md b/docs/en-us/cli/kubecm_cloud_add.md index 13f85ad3..9573caa4 100644 --- a/docs/en-us/cli/kubecm_cloud_add.md +++ b/docs/en-us/cli/kubecm_cloud_add.md @@ -60,7 +60,7 @@ kubecm cloud add --provider alibabacloud --cluster_id=xxxxxx ``` --cluster_id string kubernetes cluster id - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner --provider string public cloud --region_id string cloud region id diff --git a/docs/en-us/cli/kubecm_cloud_docs.md b/docs/en-us/cli/kubecm_cloud_docs.md index b1877475..0d0109e5 100644 --- a/docs/en-us/cli/kubecm_cloud_docs.md +++ b/docs/en-us/cli/kubecm_cloud_docs.md @@ -31,7 +31,7 @@ kubecm add docs ``` --cluster_id string kubernetes cluster id - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner --provider string public cloud --region_id string cloud region id diff --git a/docs/en-us/cli/kubecm_cloud_list.md b/docs/en-us/cli/kubecm_cloud_list.md index 2e953db0..97f9ff4d 100644 --- a/docs/en-us/cli/kubecm_cloud_list.md +++ b/docs/en-us/cli/kubecm_cloud_list.md @@ -45,7 +45,7 @@ kubecm cloud list --provider alibabacloud --cluster_id=xxxxxx ``` --cluster_id string kubernetes cluster id - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner --provider string public cloud --region_id string cloud region id diff --git a/docs/en-us/cli/kubecm_create.md b/docs/en-us/cli/kubecm_create.md index 974cb8ab..07f73260 100644 --- a/docs/en-us/cli/kubecm_create.md +++ b/docs/en-us/cli/kubecm_create.md @@ -38,7 +38,7 @@ kubecm create --user test --namespace default --cluster-role view --context-name ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_delete.md b/docs/en-us/cli/kubecm_delete.md index 732417cc..267bb2d6 100644 --- a/docs/en-us/cli/kubecm_delete.md +++ b/docs/en-us/cli/kubecm_delete.md @@ -32,7 +32,7 @@ kubecm delete my-context1 my-context2 ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_delete_docs.md b/docs/en-us/cli/kubecm_delete_docs.md index dbf3fc85..8246a063 100644 --- a/docs/en-us/cli/kubecm_delete_docs.md +++ b/docs/en-us/cli/kubecm_delete_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_docs.md b/docs/en-us/cli/kubecm_docs.md index 25fe9de3..9c747495 100644 --- a/docs/en-us/cli/kubecm_docs.md +++ b/docs/en-us/cli/kubecm_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_export.md b/docs/en-us/cli/kubecm_export.md index 7b23cfae..d0951427 100644 --- a/docs/en-us/cli/kubecm_export.md +++ b/docs/en-us/cli/kubecm_export.md @@ -31,7 +31,7 @@ kubecm export -f myconfig.yaml my-context1 my-context2 ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_export_docs.md b/docs/en-us/cli/kubecm_export_docs.md index 4159f08c..53eaff98 100644 --- a/docs/en-us/cli/kubecm_export_docs.md +++ b/docs/en-us/cli/kubecm_export_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_list.md b/docs/en-us/cli/kubecm_list.md index ef57a551..d74fb855 100644 --- a/docs/en-us/cli/kubecm_list.md +++ b/docs/en-us/cli/kubecm_list.md @@ -33,7 +33,7 @@ kubecm ls kind k3s ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_list_docs.md b/docs/en-us/cli/kubecm_list_docs.md index 106156c8..a360492c 100644 --- a/docs/en-us/cli/kubecm_list_docs.md +++ b/docs/en-us/cli/kubecm_list_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_merge.md b/docs/en-us/cli/kubecm_merge.md index 6ea55b15..a3455fbf 100644 --- a/docs/en-us/cli/kubecm_merge.md +++ b/docs/en-us/cli/kubecm_merge.md @@ -20,22 +20,32 @@ kubecm merge 1st.yaml 2nd.yaml 3rd.yaml kubecm merge -f dir # Merge KubeConfig in the dir directory to the specified file. kubecm merge -f dir --config kubecm.config +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm merge test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm merge test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm merge test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm merge test.yaml --select-context ``` ### Options ``` - -y, --assumeyes skip interactive file overwrite confirmation - -f, --folder string KubeConfig folder - -h, --help help for merge - --select-context select the context to be merged + -y, --assumeyes skip interactive file overwrite confirmation + --context-prefix string add a prefix before context name + --context-template strings define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace (default [context]) + -f, --folder string KubeConfig folder + -h, --help help for merge + --select-context select the context to be merged ``` ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_merge_docs.md b/docs/en-us/cli/kubecm_merge_docs.md index bbe2575e..2b9baf53 100644 --- a/docs/en-us/cli/kubecm_merge_docs.md +++ b/docs/en-us/cli/kubecm_merge_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_rename.md b/docs/en-us/cli/kubecm_rename.md index f4043ff4..fc0f6d98 100644 --- a/docs/en-us/cli/kubecm_rename.md +++ b/docs/en-us/cli/kubecm_rename.md @@ -28,7 +28,7 @@ kubecm rename ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_rename_docs.md b/docs/en-us/cli/kubecm_rename_docs.md index 8f7a1e5b..852ffb09 100644 --- a/docs/en-us/cli/kubecm_rename_docs.md +++ b/docs/en-us/cli/kubecm_rename_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_switch.md b/docs/en-us/cli/kubecm_switch.md index f358be13..cf60a30c 100644 --- a/docs/en-us/cli/kubecm_switch.md +++ b/docs/en-us/cli/kubecm_switch.md @@ -32,7 +32,7 @@ kubecm switch dev ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_switch_docs.md b/docs/en-us/cli/kubecm_switch_docs.md index b26405b2..801238de 100644 --- a/docs/en-us/cli/kubecm_switch_docs.md +++ b/docs/en-us/cli/kubecm_switch_docs.md @@ -30,7 +30,7 @@ kubecm add docs ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/en-us/cli/kubecm_version.md b/docs/en-us/cli/kubecm_version.md index 2ae31a0a..f0702b54 100644 --- a/docs/en-us/cli/kubecm_version.md +++ b/docs/en-us/cli/kubecm_version.md @@ -19,7 +19,7 @@ kubecm version [flags] ### Options inherited from parent commands ``` - --config string path of kubeconfig (default "/Users/guoxd/.kube/config") + --config string path of kubeconfig (default "$HOME/.kube/config") -m, --mac-notify enable to display Mac notification banner -s, --silence-table enable/disable output of context table on successful config update -u, --ui-size int number of list items to show in menu at once (default 4) diff --git a/docs/zh-cn/cli/kubecm_add.md b/docs/zh-cn/cli/kubecm_add.md index 34e72b29..615d8643 100644 --- a/docs/zh-cn/cli/kubecm_add.md +++ b/docs/zh-cn/cli/kubecm_add.md @@ -10,32 +10,35 @@ kubecm add [flags] ``` ->注意:如果 `-c` 被设置,且添加的 kubeconfig 文件中有**超过一个** context,会出现如下情况: ->- 如果设置了 `--context-name`,则 context 会以 ``, `` 的形式产生 ->- 如果没有设置 `--context-name`,则会以 `` 的方式展示,其中 `{hash}` 是文件名的 MD5 哈希值 - ### 示例 ``` - # Merge test.yaml with $HOME/.kube/config kubecm add -f test.yaml -# Merge test.yaml with $HOME/.kube/config and rename context name -kubecm add -cf test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm add -cf test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm add -f test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm add -f test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and override context name, it's useful if there is only one context in the kubeconfig file +kubecm add -f test.yaml --context-name test +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm add -f test.yaml --select-context # Add kubeconfig from stdin -cat /etc/kubernetes/admin.conf | kubecm add -f - - +cat /etc/kubernetes/admin.conf | kubecm add -f - ``` ### 选项 ``` - --context-name string override context name when add kubeconfig context - -c, --cover Overwrite local kubeconfig files - -f, --file string Path to merge kubeconfig files - - -h, --help help for add - --select-context select the context to be added + --context-name string override context name when add kubeconfig context, when context-name is set, context-prefix and context-template parameters will be ignored + --context-prefix string add a prefix before context name + --context-template strings define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace (default [context]) + -c, --cover overwrite local kubeconfig files + -f, --file string path to merge kubeconfig files + -h, --help help for add + --select-context select the context to be added ``` ### 全局选项 diff --git a/docs/zh-cn/cli/kubecm_merge.md b/docs/zh-cn/cli/kubecm_merge.md index 035b0697..ae2acbe0 100644 --- a/docs/zh-cn/cli/kubecm_merge.md +++ b/docs/zh-cn/cli/kubecm_merge.md @@ -13,23 +13,31 @@ kubecm merge [flags] ### 示例 ``` - # Merge multiple kubeconfig kubecm merge 1st.yaml 2nd.yaml 3rd.yaml # Merge KubeConfig in the dir directory kubecm merge -f dir # Merge KubeConfig in the dir directory to the specified file. kubecm merge -f dir --config kubecm.config - +# Merge test.yaml with $HOME/.kube/config and add a prefix before context name +kubecm merge test.yaml --context-prefix test +# Merge test.yaml with $HOME/.kube/config and define the attributes used for composing the context name +kubecm merge test.yaml --context-template user,cluster +# Merge test.yaml with $HOME/.kube/config, define the attributes used for composing the context name and add a prefix before context name +kubecm merge test.yaml --context-template user,cluster --context-prefix demo +# Merge test.yaml with $HOME/.kube/config and select the context to be added in interactive mode +kubecm merge test.yaml --select-context ``` ### 选项 ``` - -y, --assumeyes skip interactive file overwrite confirmation - -f, --folder string KubeConfig folder - -h, --help help for merge - --select-context select the context to be merged + -y, --assumeyes skip interactive file overwrite confirmation + --context-prefix string add a prefix before context name + --context-template strings define the attributes used for composing the context name, available values: filename, user, cluster, context, namespace (default [context]) + -f, --folder string KubeConfig folder + -h, --help help for merge + --select-context select the context to be merged ``` ### 全局选项