k8s源码结构

源码目录 说明
cmd/ 存放可执行文件的入口代码,每个可执行文件都会对应一个main函数
pkg/ 存放核心库代码,可被项目内部或外部直接引用
vendor/ 存放项目依赖的库代码,一般为每三方库代码
api/ 存放OpenAPI/Swagger文件
build/ 存放构建相关的脚本
test/ 存放测试工具及测试数据
docs/ 存放设计或用户使用文档
hack/ 存放与构建,测试等相关的脚本
third_party/ 存放第三方工具,代码或其他组件
plugin/ 存放Kubernetes插件代码目录,例如认证,授权等相关插件
staging/ 存放部分核心库的暂存目录
translations/ 存放il8n(国际化)语言包的相关文件,可以在不修改内部代码的情况下支持不同语言及地区

k8s.io/kubernetes/cmd/kubectl/kubectl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func main() {
rand.Seed(time.Now().UnixNano()) //随机数种子

command := cmd.NewDefaultKubectlCommand()

// TODO: once we switch everything over to Cobra commands, we can go back to calling
// cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
// normalize func and add the go flag set by hand.
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
// cliflag.InitFlags()
logs.InitLogs()
defer logs.FlushLogs()

if err := command.Execute(); err != nil {
os.Exit(1)
}
}

Makefile: 顶层Makefile文件,描述了整个项目所有代码文件的编译顺序,编译规则及编译后的二进制输出等
Makefile.generated_files:描述了代码生成的逻辑


1
apt install cloc //代码统计工具

结构体,json,protobuf
k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type APIResource struct {

Name string `json:"name" protobuf:"bytes,1,opt,name=name"`

SingularName string `json:"singularName" protobuf:"bytes,6,opt,name=singularName"`

Namespaced bool `json:"namespaced" protobuf:"varint,2,opt,name=namespaced"`

Group string `json:"group,omitempty" protobuf:"bytes,8,opt,name=group"`

Version string `json:"version,omitempty" protobuf:"bytes,9,opt,name=version"`

Kind string `json:"kind" protobuf:"bytes,3,opt,name=kind"`

ShortNames []string `json:"shortNames,omitempty" protobuf:"bytes,5,rep,name=shortNames"`

Categories []string `json:"categories,omitempty" protobuf:"bytes,7,rep,name=categories"`
StorageVersionHash string `json:"storageVersionHash,omitempty" protobuf:"bytes,10,opt,name=storageVersionHash"`
}

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
// staging/src/k8s.io/apimachinery/pkg/runtime/schema/group_version.go
// 标识一个资源
type GroupVersionResource struct {
Group string
Version string
Resource string
}

type GroupKind struct {
Group string
Kind string
}

type GroupVersion struct {
Group string
Version string
}
type GroupResource struct {
Group string
Resource string
}

type GroupVersionKind struct {
Group string
Version string
Kind string
}
Kind与Resource有何区别?

Kind the name of a particular object schema (e.g. the "Cat" and "Dog" kinds would have different attributes and properties)

Resource a representation of a system entity, sent or retrieved as JSON via HTTP to the server. Resources are exposed via:
- Collections - a list of resources of the same type, which may be queryable
- Elements - an individual resource, addressable via a URL
1
2
3
4
5
6
type TypeMeta struct {
// Kind is a string value representing the REST resource this object represents.
Kind string `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"`

APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,2,opt,name=apiVersion"`
}

外部版本的资源定义在vendor/k8s.io/api目录下,完整路径为vendor/k8s.io/api/<group>/<version>/<resource file>

kubectl api-versions列出当前支持的资源组和资源版本
kubectl api-resources列出当前 支持的资源列表

k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/runtime/interfaces.go
1
2
3
4
5
6
7
8
// Object interface must be supported by all API types registered with Scheme. Since objects in a scheme are
// expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
// serializers to set the kind, version, and group the object is represented as. An Object may choose
// to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
type Object interface {
GetObjectKind() schema.ObjectKind
DeepCopyObject() Object
}

目前Kubernetes系统中的所有资源类型都已注册到Scheme资源注册表中,其是一个内存型的注册表
UnversionedType:无版本资源类型
KnownType:有版本资源类型

yamlSerializer使用第三方库gopkg.in/yaml.v2来实现序列化和反序列化操作。

github.com/json-iteractor/go

可通过kubectl convert命令进行资源版本转换

// pkg/util/tail/tail.go
FindTailLineStartIndex