Kubernetes notes (6)--KUBECTL Code Analysis (1)

Source: Internet
Author: User
Tags k8s
This is a creation in Article, where the information may have evolved or changed.

kubectlFunctions that are actually running ( k8s.io/kubernetes/pkg/kubectl/app/kubectl.go ):

func Run() error {    cmd := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, os.Stdout, os.Stderr)    return cmd.Execute()}

NewKubectlCommandThe code ( k8s.io/kubernetes/pkg/kubectl/cmd/cmd.go ):

 //Newkubectlcommand creates the ' kubectl ' command and its nested children.func Newkubectlcommand (f *cmdutil. Factory, in IO. Reader, out, err io. Writer) *cobra.    command {//Parent command to which all subcommands is added. Cmds: = &cobra. command{use: "Kubectl", Short: "Kubectl controls the Kubernetes Cluster Manager", Long: ' Kubectl C Ontrols the Kubernetes cluster Manager. Find more information at https://github.com/kubernetes/kubernetes. ', Run:runhelp, bashcompletionfunction:b Ash_completion_func,} f.bindflags (Cmds. Persistentflags ())//From this point and forward we get warnings on the flags that contain "_" separators Cmds. Setglobalnormalizationfunc (util. Warnwordsepnormalizefunc) Cmds. AddCommand (Newcmdget (f, out)) Cmds. AddCommand (Newcmddescribe (f, out)) Cmds. AddCommand (Newcmdcreate (f, out)) ... return &cmds}  

So cmd := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, os.Stdout, os.Stderr) what you get cmd is a cobra.Command pointer to it.

cmdutil.NewFactoryCode ( k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go ):

Newfactory creates a factory with the default Kubernetes resources defined//if optionalclientconfig are nil, then flags 'll is bound to a new clientcmd. clientconfig.//if Optionalclientconfig is not nil and then this factory would make use of It.func newfactory (Optionalclientco Nfig Clientcmd. ClientConfig) *factory {mapper: = Kubectl. Shortcutexpander{restmapper:api. Restmapper} Flags: = Pflag. Newflagset ("", Pflag. ContinueOnError) flags. Setnormalizefunc (util. WARNWORDSEPNORMALIZEFUNC)//Warn for "_" flags generators: = Map[string]kubectl. generator{"Run/v1": Kubectl. basicreplicationcontroller{}, "Run-pod/v1": Kubectl. basicpod{}, "Service/v1": Kubectl. servicegeneratorv1{}, "Service/v2": Kubectl. servicegeneratorv2{}, "Horizontalpodautoscaler/v1beta1": Kubectl. horizontalpodautoscalerv1beta1{}, "Deployment/v1beta1": Kubectl. Deploymentv1beta1{}, "Job/v1beta1": Kubectl. jobv1beta1{},} clientconfig: = optionalclientconfig if Optionalclientconfig = nil {clientconfig = Defau Ltclientconfig (Flags)} Clients: = Newclientcache (clientconfig) return &factory{...}

Because the cmdutil.NewFactory(nil) parameter is nil , the function is called DefaultClientConfig .

clientcmd.ClientConfigDefined in the k8s.io/kubernetes/pkg/client/unversioned/clientcmd/client_config.go file:

// ClientConfig is used to make it easy to get an api server clienttype ClientConfig interface {    // RawConfig returns the merged result of all overrides    RawConfig() (clientcmdapi.Config, error)    // ClientConfig returns a complete client config    ClientConfig() (*client.Config, error)    // Namespace returns the namespace resulting from the merged    // result of all overrides and a boolean indicating if it was    // overridden    Namespace() (string, bool, error)}

DefaultClientConfigCode ( k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go ):

func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {    loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()    flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")    overrides := &clientcmd.ConfigOverrides{}    flagNames := clientcmd.RecommendedConfigOverrideFlags("")    // short flagnames are disabled by default.  These are here for compatibility with existing scripts    flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"    clientcmd.BindOverrideFlags(overrides, flags, flagNames)    clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)    return clientConfig}

Finally, a DeferredLoadingClientConfig struct (defined in) is returned k8s.io/kubernetes/pkg/client/unversioned/clientcmd/merged_client_builder.go :

// DeferredLoadingClientConfig is a ClientConfig interface that is backed by a set of loading rules// It is used in cases where the loading rules may change after you've instantiated them and you want to be sure that// the most recent rules are used.  This is useful in cases where you bind flags to loading rule parameters before// the parse happens and you want your calling code to be ignorant of how the values are being mutated to avoid// passing extraneous information down a call stacktype DeferredLoadingClientConfig struct {    loadingRules   *ClientConfigLoadingRules    overrides      *ConfigOverrides    fallbackReader io.Reader}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.