kubectl源码解析
目录
kubectl源码阅读
版本 v1.16.6
kubectl命令的创建
kubernetes/cmd/kubectl/kubectl.go
|
|
重点 : command := cmd.NewDefaultKubectlCommand()
kubernetes/pkg/kubectl/cmd/cmd.go
|
|
return调用NewDefaultKubectlCommandWithArgs方法传递参数讲解
-
NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes)
- plugin.ValidPluginFilenamePrefixes 为定义的变量
1 2
位于Kubernetes\vendor\k8s.io\kubectl\pkg\cmd\plugin\plugin.go文本中定义 ValidPluginFilenamePrefixes = []string{"kubectl"}
- 函数
1 2 3 4 5 6
// NewDefaultPluginHandler实例化DefaultPluginHandler,使用给定的文件名前缀列表来标识有效的插件文件名。 func NewDefaultPluginHandler(validPrefixes []string) *DefaultPluginHandler { return &DefaultPluginHandler{ ValidPrefixes: validPrefixes, } }
- 返回值 为DefaultPluginHandler结构体指针
1 2 3 4 5 6 7 8 9 10
// DefaultPluginHandler implements PluginHandler type DefaultPluginHandler struct { ValidPrefixes []string } 此结构体实现了pluginHandler接口中定义的方法 // 存在于给定文件名,或布尔值为false。查找将遍历给定前缀列表,以识别有效的插件文件名。返回与前缀匹配的第一个文件路径。 Lookup(filename string) (string, bool) // ecute接收可执行文件的文件路径、参数片和环境变量片,以传递给可执行文件。 Execute(executablePath string, cmdArgs, environment []string) error
实际调用NewDefaultKubectlCommandWithArgs函数
|
|
其中参数pluginHandler为接口类型,定义如下:
|
|
重点:
cmd := NewKubectlCommand(in, out, errout)
-
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
//newkubectl命令创建“kubectl”命令及其嵌套子命令 func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command { //作用创建kubectl命令 ... // 添加标志位,如--kubeconfig、--cache-dir、--client-certificate、--client-key、--token、--as、--as-group、--username、--password、--cluster、--user、--namespace、--context、--server、--insecure-skip-tls-verify、--certificate-authority、--request-timeout等标志位 kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() kubeConfigFlags.AddFlags(flags) //添加标志位--match-server-version 要求服务器版本必须与客户端版本一致 matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags()) //将标志位添加到全局标志位 cmds.PersistentFlags().AddGoFlagSet(flag.CommandLine) //使用工厂函数创建factory的接口,由结构体factoryImpl实现此接口的所有方法,如:DynamicClient、KubernetesClientSet、RESTClient、NewBuilder、ClientForMapping、UnstructuredClientForMapping、Validator、OpenAPISchema f := cmdutil.NewFactory(matchVersionKubeConfigFlags) ... //将系统的标准输入、标准输出、标准错误封装成一个结构体IOStreams ioStreams := genericclioptions.IOStreams{In: in, Out: out, ErrOut: err} // 所有子命令都要加入的父命令 groups := templates.CommandGroups{ { //入门级命令有 create、expose、run、set Message: "Basic Commands (Beginner):", Commands: []*cobra.Command{ create.NewCmdCreate(f, ioStreams), expose.NewCmdExposeService(f, ioStreams), run.NewCmdRun(f, ioStreams), set.NewCmdSet(f, ioStreams), }, }, { //初中级命令 explain、get、edit、delete Message: "Basic Commands (Intermediate):", Commands: []*cobra.Command{ explain.NewCmdExplain("kubectl", f, ioStreams), get.NewCmdGet("kubectl", f, ioStreams), edit.NewCmdEdit(f, ioStreams), delete.NewCmdDelete(f, ioStreams), }, }, { //部署相关命令 rollout rollingupdate(已被官方移除)、scale、autoscale Message: "Deploy Commands:", Commands: []*cobra.Command{ rollout.NewCmdRollout(f, ioStreams), rollingupdate.NewCmdRollingUpdate(f, ioStreams), scale.NewCmdScale(f, ioStreams), autoscale.NewCmdAutoscale(f, ioStreams), }, }, { //集群管理命令 certificates、clusterinfo、top、cordon、uncordon、drain、taint Message: "Cluster Management Commands:", Commands: []*cobra.Command{ certificates.NewCmdCertificate(f, ioStreams), clusterinfo.NewCmdClusterInfo(f, ioStreams), top.NewCmdTop(f, ioStreams), drain.NewCmdCordon(f, ioStreams), drain.NewCmdUncordon(f, ioStreams), drain.NewCmdDrain(f, ioStreams), taint.NewCmdTaint(f, ioStreams), }, }, { //故障处理和Debugging命令 describe、logs、attach、exec、port-forward、proxy、cp、auth Message: "Troubleshooting and Debugging Commands:", Commands: []*cobra.Command{ describe.NewCmdDescribe("kubectl", f, ioStreams), logs.NewCmdLogs(f, ioStreams), attach.NewCmdAttach(f, ioStreams), cmdexec.NewCmdExec(f, ioStreams), portforward.NewCmdPortForward(f, ioStreams), proxy.NewCmdProxy(f, ioStreams), cp.NewCmdCp(f, ioStreams), auth.NewCmdAuth(f, ioStreams), }, }, { //高级命令 diff、apply、patch、replace、wait、convert、kustomize Message: "Advanced Commands:", Commands: []*cobra.Command{ diff.NewCmdDiff(f, ioStreams), apply.NewCmdApply("kubectl", f, ioStreams), patch.NewCmdPatch(f, ioStreams), replace.NewCmdReplace(f, ioStreams), wait.NewCmdWait(f, ioStreams), convert.NewCmdConvert(f, ioStreams), kustomize.NewCmdKustomize(ioStreams), }, }, { //设置类命令 label、annotate、completion Message: "Settings Commands:", Commands: []*cobra.Command{ label.NewCmdLabel(f, ioStreams), annotate.NewCmdAnnotate("kubectl", f, ioStreams), completion.NewCmdCompletion(ioStreams.Out, ""), }, }, } ... //添加--kubeconfig 的falg参数 kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() kubeConfigFlags.AddFlags(flags) matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags()) ... //其他类型命令 config、plugin、version、api-resources、api-versions cmds.AddCommand(cmdconfig.NewCmdConfig(f, clientcmd.NewDefaultPathOptions(), ioStreams)) cmds.AddCommand(plugin.NewCmdPlugin(f, ioStreams)) cmds.AddCommand(version.NewCmdVersion(f, ioStreams)) cmds.AddCommand(apiresources.NewCmdAPIVersions(f, ioStreams)) cmds.AddCommand(apiresources.NewCmdAPIResources(f, ioStreams)) cmds.AddCommand(options.NewCmdOptions(ioStreams.Out)) return cmds }
kubectl create子命令详解
create 命令创建
|
|
o.RunCreate(f, cmd)
|
|
createAndRefresh(info) 主要创建逻辑
|
|
子命令 create namespace
|
|
options.Complete(f, cmd, args)
|
|
options.Run()
|
|
obj, err := o.StructuredGenerator.StructuredGenerate()
|
|
其他的子命令过程都类似,只有细微的差别