k8s1.11命令行——实现源码导读之kubectl

一.源码路径
kubernetes-master/cmd/kubectl/kubectl.go
k8s1.11命令行——实现源码导读之kubectl_第1张图片
二.主要功能实现
kubectl.go中的main函数作为命令行kubectl的主入口,main函数构建了一个commond,然后调用了Execute执行这个命令。
k8s1.11命令行——实现源码导读之kubectl_第2张图片
可以看到,在main函数里,主要实现了以下两步:

 1. 构建cmd命令
 2. 执行该命令

下面我们来看NewDefaultKubectlCommand()方法,这个方法构建了具体的cmd命令。这个方法调用了NewKubectlCommand()方法,在这个方法内,构建了一个kubectl子命令集合(通过cobra脚手架的方式),如下:

groups := templates.CommandGroups{
		{
			Message: "Basic Commands (Beginner):",
			Commands: []*cobra.Command{
				create.NewCmdCreate(f, ioStreams),
				NewCmdExposeService(f, ioStreams),
				NewCmdRun(f, ioStreams),
				set.NewCmdSet(f, ioStreams),
				deprecatedAlias("run-container", NewCmdRun(f, ioStreams)),
			},
		},
		{
			Message: "Basic Commands (Intermediate):",
			Commands: []*cobra.Command{
				NewCmdExplain("kubectl", f, ioStreams),
				get.NewCmdGet("kubectl", f, ioStreams),
				NewCmdEdit(f, ioStreams),
				NewCmdDelete(f, ioStreams),
			},
		},
		{
			Message: "Deploy Commands:",
			Commands: []*cobra.Command{
				rollout.NewCmdRollout(f, ioStreams),
				NewCmdRollingUpdate(f, ioStreams),
				NewCmdScale(f, ioStreams),
				NewCmdAutoscale(f, ioStreams),
			},
		},
		{
			Message: "Cluster Management Commands:",
			Commands: []*cobra.Command{
				NewCmdCertificate(f, ioStreams),
				NewCmdClusterInfo(f, ioStreams),
				NewCmdTop(f, ioStreams),
				NewCmdCordon(f, ioStreams),
				NewCmdUncordon(f, ioStreams),
				NewCmdDrain(f, ioStreams),
				NewCmdTaint(f, ioStreams),
			},
		},
		{
			Message: "Troubleshooting and Debugging Commands:",
			Commands: []*cobra.Command{
				NewCmdDescribe("kubectl", f, ioStreams),
				NewCmdLogs(f, ioStreams),
				NewCmdAttach(f, ioStreams),
				NewCmdExec(f, ioStreams),
				NewCmdPortForward(f, ioStreams),
				NewCmdProxy(f, ioStreams),
				NewCmdCp(f, ioStreams),
				auth.NewCmdAuth(f, ioStreams),
			},
		},
		{
			Message: "Advanced Commands:",
			Commands: []*cobra.Command{
				NewCmdApply("kubectl", f, ioStreams),
				NewCmdPatch(f, ioStreams),
				NewCmdReplace(f, ioStreams),
				wait.NewCmdWait(f, ioStreams),
				NewCmdConvert(f, ioStreams),
			},
		},
		{
			Message: "Settings Commands:",
			Commands: []*cobra.Command{
				NewCmdLabel(f, ioStreams),
				NewCmdAnnotate("kubectl", f, ioStreams),
				NewCmdCompletion(ioStreams.Out, ""),
			},
		},
	}
	groups.Add(cmds)//向父命令中加入子命令集合groups

上面的这些子命令集合共同构成了kubernetes命令行接口,kubernetes提供的命令行接口如下(可用kubectl -h)查看:

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
  run            在集群中运行一个指定的镜像
  set            为 objects 设置一个指定的特征

Basic Commands (Intermediate):
  explain        查看资源的文档
  get            显示一个或更多 resources
  edit           在服务器上编辑一个资源
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
  autoscale      自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量

Cluster Management Commands:
  certificate    修改 certificate 资源.
  cluster-info   显示集群信息
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         标记 node 为 unschedulable
  uncordon       标记 node 为 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:
  describe       显示一个指定 resource 或者 group 的 resources 详情
  logs           输出容器在 pod 中的日志
  attach         Attach 到一个运行中的 container
  exec           在一个 container 中执行一个命令
  port-forward   Forward one or more local ports to a pod
  proxy          运行一个 proxy 到 Kubernetes API server
  cp             复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
  auth           Inspect authorization

Advanced Commands:
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for one condition on one or many resources
  convert        在不同的 API versions 转换配置文件

Settings Commands:
  label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  alpha          Commands for features in alpha
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         修改 kubeconfig 文件
  plugin         Runs a command-line plugin
  version        输出 client 和 server 的版本信息


 

你可能感兴趣的:(k8s1.11源码导读)