client-go:1 环境准备

1 环境准备

1 源码下载

直接 Clone 官方 Kubernetes 源码即可获得代码:

$ git clone https://github.com/kubernetes/kubernetes.git

但是由于 Kubernetes 源码非常大,而且我们访问 GitHub 又比较慢,为解决这个问题我们可以使用加速器进行 Clone,只需要将 github.com 替换成 github.com.cnpmjs.org 即可:

$ git clone https://github.com.cnpmjs.org/kubernetes/kubernetes.git

Clone 完成后本地目录如下所示:

[11:28:01 root@go kubernetes]#tree . -L 1
.
├── api
├── build
├── CHANGELOG
├── CHANGELOG.md -> CHANGELOG/README.md
├── cluster
├── cmd
├── code-of-conduct.md
├── CONTRIBUTING.md
├── docs
├── go.mod
├── go.sum
├── hack
├── LICENSE
├── LICENSES
├── logo
├── Makefile -> build/root/Makefile
├── Makefile.generated_files -> build/root/Makefile.generated_files
├── OWNERS
├── OWNERS_ALIASES
├── pkg
├── plugin
├── README.md
├── SECURITY_CONTACTS
├── staging
├── SUPPORT.md
├── test
├── third_party
└── vendor

我们可以看到 Kubernetes 源码目录非常多,本身也是一个几百万行代码的大型项目,我们也不可能把每一段代码都讲解到,主要是围绕 Kubernetes 的主线来讲解,我们先看一下几个主要的目录:

目录名 用途
cmd 每个组件代码入口(main函数)
pkg 各个组件的具体功能实现
staging 已经分库的项目
vendor 依赖

我们知道 Kubernetes 的各个组件都是二进制文件,这些二进制文件的入口就在 cmd 目录下面,比如 kube-apiserver、kubeadmin、kubectl、kubelet 等等,都分别在 cmd 目录下面有一个入口文件,在入口文件的 main 函数中都是通过 Cobra 这个 CLI 工具来进行初始化的,所以的配置参数或者配置文件也是通过 Cobra 来进行解析的。

# 我们可以查看到 cmd 目录下有很多熟悉的一些 K8S 中的组件名称
[11:28:21 root@go kubernetes]#ll cmd/
total 4
drwxr-xr-x 2 root root  52 Nov 21 11:27 clicheck
drwxr-xr-x 2 root root 129 Nov 21 11:27 cloud-controller-manager
drwxr-xr-x 2 root root  46 Nov 21 11:27 dependencycheck
drwxr-xr-x 2 root root  49 Nov 21 11:27 dependencyverifier
drwxr-xr-x 2 root root  33 Nov 21 11:27 gendocs
drwxr-xr-x 2 root root  85 Nov 21 11:27 genkubedocs
drwxr-xr-x 2 root root  29 Nov 21 11:27 genman
drwxr-xr-x 2 root root  34 Nov 21 11:27 genswaggertypedocs
drwxr-xr-x 2 root root  49 Nov 21 11:27 genutils
drwxr-xr-x 2 root root  33 Nov 21 11:27 genyaml
drwxr-xr-x 2 root root  45 Nov 21 11:27 importverifier
drwxr-xr-x 4 root root  89 Nov 21 11:27 kubeadm
drwxr-xr-x 3 root root  51 Nov 21 11:27 kube-apiserver
drwxr-xr-x 3 root root  60 Nov 21 11:27 kube-controller-manager
drwxr-xr-x 2 root root  38 Nov 21 11:27 kubectl
drwxr-xr-x 2 root root  46 Nov 21 11:27 kubectl-convert
drwxr-xr-x 3 root root  49 Nov 21 11:27 kubelet
drwxr-xr-x 2 root root  42 Nov 21 11:27 kubemark
drwxr-xr-x 3 root root  47 Nov 21 11:27 kube-proxy
drwxr-xr-x 3 root root  51 Nov 21 11:27 kube-scheduler
drwxr-xr-x 2 root root  22 Nov 21 11:27 linkcheck
-rw-r--r-- 1 root root 190 Nov 21 11:27 OWNERS
drwxr-xr-x 2 root root  47 Nov 21 11:27 preferredimports

并且在 kube-apiserver 中可以看到有一个apiserver.go 文件

[11:31:17 root@go kubernetes]#ll cmd/kube-apiserver/
total 8
-rw-r--r-- 1 root root 1178 Nov 21 11:27 apiserver.go
drwxr-xr-x 4 root root  120 Nov 21 11:27 app
-rw-r--r-- 1 root root  395 Nov 21 11:27 OWNERS

# 查看  apiserver.go 文件,main 直接调用 NewAPIServerCommand 
[11:32:08 root@go kubernetes]#cat cmd/kube-apiserver/apiserver.go 
/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// apiserver is the main api server and master for the cluster.
// it is responsible for serving the cluster management API.
package main

import (
    "os"

    "k8s.io/component-base/cli"
    _ "k8s.io/component-base/logs/json/register"          // for JSON log format registration
    _ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
    _ "k8s.io/component-base/metrics/prometheus/version"  // for version metric registration
    "k8s.io/kubernetes/cmd/kube-apiserver/app"
)

func main() {
    command := app.NewAPIServerCommand()
    code := cli.Run(command)
    os.Exit(code)
}

而 pkg 目录下面就是各个组件的具体功能的实现,比如 kube-apiserver 的认证鉴权准入控制器等功能都是在该目录下面实现的。比如 cmd 是程序的入口然后核心的代码都会去调用 pkg 中代码

# 这里查看 pkg 目录
[11:37:47 root@go kubernetes]#ll pkg/
total 36
drwxr-xr-x 11 root root  186 Nov 21 11:27 api
drwxr-xr-x 25 root root 4096 Nov 21 11:27 apis
drwxr-xr-x  4 root root   60 Nov 21 11:27 auth
drwxr-xr-x  2 root root   71 Nov 21 11:27 capabilities
drwxr-xr-x  5 root root   67 Nov 21 11:27 client
drwxr-xr-x  3 root root   68 Nov 21 11:27 cloudprovider
drwxr-xr-x  3 root root   19 Nov 21 11:27 cluster
drwxr-xr-x 32 root root 4096 Nov 21 11:27 controller
drwxr-xr-x  5 root root  301 Nov 21 11:27 controlplane
drwxr-xr-x  7 root root  239 Nov 21 11:27 credentialprovider
drwxr-xr-x  2 root root   44 Nov 21 11:27 features
drwxr-xr-x  2 root root   65 Nov 21 11:27 fieldpath
drwxr-xr-x  3 root root   35 Nov 21 11:27 generated
drwxr-xr-x  6 root root  147 Nov 21 11:27 kubeapiserver
drwxr-xr-x  3 root root   73 Nov 21 11:27 kubectl
drwxr-xr-x 47 root root 4096 Nov 21 11:27 kubelet
drwxr-xr-x  2 root root  117 Nov 21 11:27 kubemark
-rw-r--r--  1 root root  234 Nov 21 11:27 OWNERS
drwxr-xr-x  4 root root  161 Nov 21 11:27 printers
drwxr-xr-x  6 root root   97 Nov 21 11:27 probe
drwxr-xr-x 13 root root 4096 Nov 21 11:27 proxy
drwxr-xr-x  3 root root   16 Nov 21 11:27 quota
drwxr-xr-x 22 root root 4096 Nov 21 11:27 registry
drwxr-xr-x  2 root root   74 Nov 21 11:27 routes
drwxr-xr-x  9 root root 4096 Nov 21 11:27 scheduler
drwxr-xr-x  4 root root   61 Nov 21 11:27 security
drwxr-xr-x  2 root root  115 Nov 21 11:27 securitycontext
drwxr-xr-x  2 root root  182 Nov 21 11:27 serviceaccount
drwxr-xr-x 33 root root 4096 Nov 21 11:27 util
drwxr-xr-x 32 root root 4096 Nov 21 11:27 volume
drwxr-xr-x  3 root root   21 Nov 21 11:27 windows

staging 这里的代码都存放在独立的仓库中,以引用包的方式添加到项目中来。

[11:39:00 root@go kubernetes]#ll staging/src/k8s.io/
total 16
drwxr-xr-x 26 root root 4096 Nov 21 11:27 api
drwxr-xr-x  9 root root  271 Nov 21 11:27 apiextensions-apiserver
drwxr-xr-x  5 root root  213 Nov 21 11:27 apimachinery
drwxr-xr-x  5 root root  236 Nov 21 11:27 apiserver
drwxr-xr-x 22 root root 4096 Nov 21 11:27 client-go
drwxr-xr-x  5 root root  211 Nov 21 11:27 cli-runtime
drwxr-xr-x 14 root root 4096 Nov 21 11:27 cloud-provider
drwxr-xr-x  5 root root  208 Nov 21 11:27 cluster-bootstrap
drwxr-xr-x  8 root root  329 Nov 21 11:27 code-generator
drwxr-xr-x 13 root root  323 Nov 21 11:27 component-base
drwxr-xr-x  9 root root  272 Nov 21 11:27 component-helpers
drwxr-xr-x  8 root root  252 Nov 21 11:27 controller-manager
drwxr-xr-x  4 root root  194 Nov 21 11:27 cri-api
drwxr-xr-x  4 root root  229 Nov 21 11:27 csi-translation-lib
drwxr-xr-x  6 root root  224 Nov 21 11:27 kube-aggregator
drwxr-xr-x  4 root root  197 Nov 21 11:27 kube-controller-manager
drwxr-xr-x  7 root root  236 Nov 21 11:27 kubectl
drwxr-xr-x  5 root root  208 Nov 21 11:27 kubelet
drwxr-xr-x  4 root root  197 Nov 21 11:27 kube-proxy
drwxr-xr-x  5 root root  213 Nov 21 11:27 kube-scheduler
drwxr-xr-x  8 root root  227 Nov 21 11:27 legacy-cloud-providers
drwxr-xr-x  5 root root  206 Nov 21 11:27 metrics
drwxr-xr-x  3 root root 4096 Nov 21 11:27 mount-utils
drwxr-xr-x 10 root root  296 Nov 21 11:27 pod-security-admission
drwxr-xr-x  7 root root  236 Nov 21 11:27 sample-apiserver
drwxr-xr-x  5 root root  205 Nov 21 11:27 sample-cli-plugin
drwxr-xr-x  7 root root  283 Nov 21 11:27 sample-controller

vendor 下面就是项目依赖的第三方包

[11:39:05 root@go kubernetes]#ll vendor/
total 152
drwxr-xr-x   3 root root     23 Nov 21 11:27 bitbucket.org
drwxr-xr-x   3 root root     16 Nov 21 11:27 cloud.google.com
drwxr-xr-x 108 root root   4096 Nov 21 11:27 github.com
drwxr-xr-x   4 root root     31 Nov 21 11:27 go.etcd.io
drwxr-xr-x   3 root root     15 Nov 21 11:27 golang.org
drwxr-xr-x   3 root root     16 Nov 21 11:27 gonum.org
drwxr-xr-x   7 root root     78 Nov 21 11:27 google.golang.org
drwxr-xr-x   9 root root    276 Nov 21 11:27 go.opencensus.io
drwxr-xr-x   5 root root     46 Nov 21 11:27 go.opentelemetry.io
drwxr-xr-x  10 root root    130 Nov 21 11:27 gopkg.in
drwxr-xr-x   7 root root    104 Nov 21 11:27 go.starlark.net
drwxr-xr-x   5 root root     47 Nov 21 11:27 go.uber.org
drwxr-xr-x   7 root root   4096 Nov 21 11:27 k8s.io
-rw-r--r--   1 root root 141423 Nov 21 11:27 modules.txt
-rw-r--r--   1 root root    105 Nov 21 11:27 OWNERS
drwxr-xr-x   7 root root    107 Nov 21 11:27 sigs.k8s.io
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇