【Kubernetes】API Centric 这篇文章中,我们了解了 API 相关的 runtime.SchemeRESTMapper 等数据结构,知道了一个API是如何以 Go Object 的形式被定义,如何被转换成 GVKGVR 的。本文要解决的问题是,ApiServer 作为一个 REST API Server,是如何将这些 API 注册到自己的服务当中的。当一个 REST API Request 到达 ApiServer 之后,到底经历了什么?

REST Storage

 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
type APIGroupVersion struct {
	Storage map[string]rest.Storage
	Root string

	GroupVersion schema.GroupVersion
	OptionsExternalVersion *schema.GroupVersion
	MetaGroupVersion *schema.GroupVersion

	RootScopedKinds sets.String

	Serializer     runtime.NegotiatedSerializer
	ParameterCodec runtime.ParameterCodec

	Typer           runtime.ObjectTyper
	Creater         runtime.ObjectCreater
	Convertor       runtime.ObjectConvertor
	Defaulter       runtime.ObjectDefaulter
	Linker          runtime.SelfLinker
	UnsafeConvertor runtime.ObjectConvertor

	EquivalentResourceRegistry runtime.EquivalentResourceRegistry

	Authorizer authorizer.Authorizer
	Admit admission.Interface

	MinRequestTimeout time.Duration
	OpenAPIModels openapiproto.Models
	MaxRequestBodyBytes int64
}

API Installer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func (g *APIGroupVersion) InstallREST(container *restful.Container) error {
	prefix := path.Join(g.Root, g.GroupVersion.Group, g.GroupVersion.Version)
	installer := &APIInstaller{
		group:             g,
		prefix:            prefix,
		minRequestTimeout: g.MinRequestTimeout,
	}

	apiResources, ws, registrationErrors := installer.Install()
	versionDiscoveryHandler := discovery.NewAPIVersionHandler(g.Serializer, g.GroupVersion, staticLister{apiResources})
	versionDiscoveryHandler.AddToWebService(ws)
	container.Add(ws)
	return utilerrors.NewAggregate(registrationErrors)
}

参考资料