k8s源碼分析3-kubectl命令行設置7大命令分組

本節重點總結 :

  • 設置cmd工廠函數f,主要是封裝了與kube-apiserver交互客戶端
  • 用cmd工廠函數f創建7大分組命令 ,如下
  1. 基礎初級命令 Basic Commands (Beginner):
  2. 基礎中級命令 Basic Commands (Intermediate):
  3. 部署命令 Deploy Commands:
  4. 集羣管理分組 Cluster Management Commands:
  5. 故障排查和調試 Troubleshooting and Debugging Commands:
  6. 高級命令 Advanced Commands:
  7. 設置命令 Settings Commands

架構圖

kubectl_cmd_group.png

設置參數-替換方法

	flags := cmds.PersistentFlags()
	flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags

	// Normalize all flags that are coming from other packages or pre-configurations
	// a.k.a. change all "_" to "-". e.g. glog package
	flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)

	addProfilingFlags(flags)

	flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code")


設置kubeconfig相關的命令行

	kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
	kubeConfigFlags.AddFlags(flags)
	matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
	matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())

設置cmd工廠函數f,主要是封裝了與kube-apiserver交互客戶端

  • 後面的子命令都使用這個f創建
	f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

創建proxy子命令

	proxyCmd := proxy.NewCmdProxy(f, ioStreams)
	proxyCmd.PreRun = func(cmd *cobra.Command, args []string) {
		kubeConfigFlags.WrapConfigFn = nil
	}

創建7大分組命令

1. 基礎初級命令 Basic Commands (Beginner):

  • 代碼
		{
			Message: "Basic Commands (Beginner):",
			Commands: []*cobra.Command{
				create.NewCmdCreate(f, ioStreams),
				expose.NewCmdExposeService(f, ioStreams),
				run.NewCmdRun(f, ioStreams),
				set.NewCmdSet(f, ioStreams),
			},
		},
  • 對應的輸出

Basic Commands (Beginner):
  create        Create a resource from a file or from stdin.
  expose        Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run           Run a particular image on the cluster
  set           Set specific features on objects
  • 釋義
    • create 代表創建資源
    • expose 將一種資源暴露成service
    • run 運行一個鏡像
    • set 在對象上設置一些功能

2. 基礎中級命令 Basic Commands (Intermediate):

	{
			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),
			},
		},
  • 打印的help效果
Basic Commands (Intermediate):
  explain       Documentation of resources
  get           Display one or many resources
  edit          Edit a resource on the server
  delete        Delete resources by filenames, stdin, resources and names, or by resources and label selector
  • 釋義
    • explain 獲取資源的文檔
    • get 展示資源
    • edit 編輯資源
    • delete 刪除資源

3. 部署命令 Deploy Commands:

		{
			Message: "Deploy Commands:",
			Commands: []*cobra.Command{
				rollout.NewCmdRollout(f, ioStreams),
				scale.NewCmdScale(f, ioStreams),
				autoscale.NewCmdAutoscale(f, ioStreams),
			},
		},
  • 輸出爲
  rollout       Manage the rollout of a resource
  scale         Set a new size for a Deployment, ReplicaSet or Replication Controller
  autoscale     Auto-scale a Deployment, ReplicaSet, or ReplicationController

  • 釋義
    • rollout 滾動更新
    • scale 擴縮容
    • autoscale 自動擴縮容

4. 集羣管理分組 Cluster Management Commands:

		{
			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),
			},
		},
  • 輸出

Cluster Management Commands:
  certificate   Modify certificate resources.
  cluster-info  Display cluster info
  top           Display Resource (CPU/Memory/Storage) usage.
  cordon        Mark node as unschedulable
  uncordon      Mark node as schedulable
  drain         Drain node in preparation for maintenance
  taint         Update the taints on one or more nodes

  • 釋義
    • certificate 管理證書
    • cluster-info 展示集羣信息
    • top 展示資源消耗top
    • cordon 將節點標記爲不可用
    • uncordon 將節點標記爲可用
    • drain 驅逐pod
    • taint 設置節點污點

5.故障排查和調試 Troubleshooting and Debugging Commands:

		{
			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),
				proxyCmd,
				cp.NewCmdCp(f, ioStreams),
				auth.NewCmdAuth(f, ioStreams),
				debug.NewCmdDebug(f, ioStreams),
			},
		},
  • 輸出
Troubleshooting and Debugging Commands:
  describe      Show details of a specific resource or group of resources
  logs          Print the logs for a container in a pod
  attach        Attach to a running container
  exec          Execute a command in a container
  port-forward  Forward one or more local ports to a pod
  proxy         Run a proxy to the Kubernetes API server
  cp            Copy files and directories to and from containers.
  auth          Inspect authorization
  debug         Create debugging sessions for troubleshooting workloads and nodes
  • 釋義
    • describe 展示資源詳情
    • logs 打印pod中容器日誌
    • attach 進入容器
    • exec 在容器中執行命令
    • port-forward 端口轉發
    • proxy 運行代理
    • cp 拷貝文件
    • auth 檢查鑑權
    • debug 打印debug

6. 高級命令 Advanced Commands:

  • 代碼
		{
			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),
				kustomize.NewCmdKustomize(ioStreams),
			},
		},
  • 輸出


Advanced Commands:
  diff          Diff live version against would-be applied version
  apply         Apply a configuration to a resource by filename or stdin
  patch         Update field(s) of a resource
  replace       Replace a resource by filename or stdin
  wait          Experimental: Wait for a specific condition on one or many resources.
  kustomize     Build a kustomization target from a directory or a remote url.

  • 釋義
    • diff 對比當前和應該運行的版本
    • apply 應用變更或配置
    • patch 更新資源的字段
    • replace 替換資源
    • wait 等待資源的特定狀態
    • kustomize 從目錄或遠程 url 構建 kustomization 目標。

7. 設置命令 Settings Commands

  • 代碼
		{
			Message: "Settings Commands:",
			Commands: []*cobra.Command{
				label.NewCmdLabel(f, ioStreams),
				annotate.NewCmdAnnotate("kubectl", f, ioStreams),
				completion.NewCmdCompletion(ioStreams.Out, ""),
			},
		},
  • 輸出
Settings Commands:
  label         Update the labels on a resource
  annotate      Update the annotations on a resource
  completion    Output shell completion code for the specified shell (bash or zsh)

  • 釋義
    • label 打標籤
    • annotate 更新註釋
    • completion 在shell上設置補全

本節重點總結 :

  • 設置cmd工廠函數f,主要是封裝了與kube-apiserver交互客戶端
  • 用cmd工廠函數f創建7大分組命令 ,如下
  1. 基礎初級命令 Basic Commands (Beginner):
  2. 基礎中級命令 Basic Commands (Intermediate):
  3. 部署命令 Deploy Commands:
  4. 集羣管理分組 Cluster Management Commands:
  5. 故障排查和調試 Troubleshooting and Debugging Commands:
  6. 高級命令 Advanced Commands:
  7. 設置命令 Settings Commands
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章