mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 12:18:16 +00:00 
			
		
		
		
	Support completion for --cluster and --user
With this commit, kubectl is supported completion for `--cluster` and `--user`. ``` $ kubectl --user=<tab> --user=minikube --user=user01 $ kubectl --cluster=<tab> --cluster=cluster01 --cluster=minikube ```
This commit is contained in:
		@@ -76,10 +76,26 @@ __kubectl_get_namespaces()
 | 
				
			|||||||
    fi
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__kubectl_get_contexts()
 | 
					__kubectl_config_get_contexts()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    __kubectl_parse_config "contexts"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					__kubectl_config_get_clusters()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    __kubectl_parse_config "clusters"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					__kubectl_config_get_users()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    __kubectl_parse_config "users"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# $1 has to be "contexts", "clusters" or "users"
 | 
				
			||||||
 | 
					__kubectl_config_get()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    local template kubectl_out
 | 
					    local template kubectl_out
 | 
				
			||||||
    template="{{ range .contexts  }}{{ .name }} {{ end }}"
 | 
					    template="{{ range .$1  }}{{ .name }} {{ end }}"
 | 
				
			||||||
    if kubectl_out=$(kubectl config $(__kubectl_override_flags) -o template --template="${template}" view 2>/dev/null); then
 | 
					    if kubectl_out=$(kubectl config $(__kubectl_override_flags) -o template --template="${template}" view 2>/dev/null); then
 | 
				
			||||||
        COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
 | 
					        COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
@@ -171,7 +187,7 @@ __custom_func() {
 | 
				
			|||||||
            return
 | 
					            return
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        kubectl_config_use-context)
 | 
					        kubectl_config_use-context)
 | 
				
			||||||
            __kubectl_get_contexts
 | 
					            __kubectl_config_get_contexts
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        *)
 | 
					        *)
 | 
				
			||||||
@@ -224,6 +240,15 @@ __custom_func() {
 | 
				
			|||||||
    `
 | 
					    `
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						bash_completion_flags = map[string]string{
 | 
				
			||||||
 | 
							"namespace": "__kubectl_get_namespaces",
 | 
				
			||||||
 | 
							"context":   "__kubectl_config_get_contexts",
 | 
				
			||||||
 | 
							"cluster":   "__kubectl_config_get_clusters",
 | 
				
			||||||
 | 
							"user":      "__kubectl_config_get_users",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewKubectlCommand creates the `kubectl` command and its nested children.
 | 
					// NewKubectlCommand creates the `kubectl` command and its nested children.
 | 
				
			||||||
func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
 | 
					func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
 | 
				
			||||||
	// Parent command to which all subcommands are added.
 | 
						// Parent command to which all subcommands are added.
 | 
				
			||||||
@@ -330,24 +355,16 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	templates.ActsAsRootCommand(cmds, filters, groups...)
 | 
						templates.ActsAsRootCommand(cmds, filters, groups...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cmds.Flag("namespace") != nil {
 | 
						for name, completion := range bash_completion_flags {
 | 
				
			||||||
		if cmds.Flag("namespace").Annotations == nil {
 | 
							if cmds.Flag(name) != nil {
 | 
				
			||||||
			cmds.Flag("namespace").Annotations = map[string][]string{}
 | 
								if cmds.Flag(name).Annotations == nil {
 | 
				
			||||||
 | 
									cmds.Flag(name).Annotations = map[string][]string{}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		cmds.Flag("namespace").Annotations[cobra.BashCompCustom] = append(
 | 
								cmds.Flag(name).Annotations[cobra.BashCompCustom] = append(
 | 
				
			||||||
			cmds.Flag("namespace").Annotations[cobra.BashCompCustom],
 | 
									cmds.Flag(name).Annotations[cobra.BashCompCustom],
 | 
				
			||||||
			"__kubectl_get_namespaces",
 | 
									completion,
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if cmds.Flag("context") != nil {
 | 
					 | 
				
			||||||
		if cmds.Flag("context").Annotations == nil {
 | 
					 | 
				
			||||||
			cmds.Flag("context").Annotations = map[string][]string{}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		cmds.Flag("context").Annotations[cobra.BashCompCustom] = append(
 | 
					 | 
				
			||||||
			cmds.Flag("context").Annotations[cobra.BashCompCustom],
 | 
					 | 
				
			||||||
			"__kubectl_get_contexts",
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), out, err))
 | 
						cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), out, err))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user