kubernetes中apiserver的解析

1. 在主節點master中注冊api入口

// kubernetes/pkg/master/master.go

kubernets/pkg/master/master.go

func New(c *Config)(*Master, error) {

? ? ? m.InstallAPIs(c.ExtraConfig.APIResourceConfigSource, c.GenericConfig.RESTOptionsGetter, restStorageProviders...)

}

InstallAPIs方法中的第一個參數用來驗證api的版本信息,后面的參數都與存儲有關。

type APIResourceConfigSourceinterface {

VersionEnabled(version schema.GroupVersion) bool

AnyVersionForGroupEnabled(group string) bool

}

2.?根據Config往APIGroupsInfo內增加組信息,然后通過InstallAPIGroups進行注冊

// InstallAPIs will install the APIs for the restStorageProviders if they are enabled.

func (m *Master) InstallAPIs(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter, restStorageProviders ...RESTStorageProvider) {

/*用傳入的版本信息和存儲信息構建apiGroupsInfo,結構如下:

(APIGroupInfo類型的很多參數都是schema.GroupVersion類型,這種類型里面只有兩個string類型的字段Group和Version。)

type APIGroupInfo struct {

PrioritizedVersions []schema.GroupVersion

// 從版本到資源再到存儲的map

VersionedResourcesStorageMap map[string]map[string]rest.Storage

// OptionsExternalVersion controls the APIVersion used for common objects in the // schema like api.Status, api.DeleteOptions, and metav1.ListOptions. Other implementors may // define a version "v1beta1" but want to use the Kubernetes "v1" internal objects. // If nil, defaults to groupMeta.GroupVersion.?

OptionsExternalVersion *schema.GroupVersion

// 默認值為 "meta.k8s.io/v1" and is the scheme group version used to decode // common API implementations like ListOptions.?

MetaGroupVersion *schema.GroupVersion

// Scheme includes all of the types used by this group and how to convert between them (or // to convert objects from outside of this group that are accepted in this API).

Scheme *runtime.Scheme

// NegotiatedSerializer controls how this group encodes and decodes data

NegotiatedSerializer runtime.NegotiatedSerializer

// ParameterCodec performs conversions for query parameters passed to API calls ParameterCodec runtime.ParameterCodec

}

*/

apiGroupsInfo := []genericapiserver.APIGroupInfo{}

......

for i :=range apiGroupsInfo {

if err := m.GenericAPIServer.InstallAPIGroup(&apiGroupsInfo[i]); err != nil {

glog.Fatalf("Error in registering group versions: %v", err)

}

}

}

3. k8s.io/apiserver/pkg/server/genericapiserver.go

// Exposes the given api group in the API.

func (s *GenericAPIServer) InstallAPIGroup(apiGroupInfo *APIGroupInfo) error {

......

if err := s.installAPIResources(APIGroupPrefix, apiGroupInfo); err != nil {

return err

}

// 在/apis/添加一個枚舉出group中所有支持version的api接口

......

s.DiscoveryGroupManager.AddGroup(apiGroup)

s.Handler.GoRestfulContainer.Add(discovery.NewAPIGroupHandler(s.Serializer, apiGroup).WebService())

......

}

4.?k8s.io/apiserver/pkg/server/genericapiserver.go

// installAPIResources is a private method for installing the REST storage backing each api groupversionresource

func (s *GenericAPIServer) installAPIResources(apiPrefix string, apiGroupInfo *APIGroupInfo) error {

......

apiGroupVersion := s.getAPIGroupVersion(apiGroupInfo, groupVersion, apiPrefix)

......

if err := apiGroupVersion.InstallREST(s.Handler.GoRestfulContainer); err != nil {

return fmt.Errorf("unable to setup API %v: %v", apiGroupInfo, err)

}

}

return nil

}

首先獲得apiGroupVersion(v1beta)等信息,然后由apiGroupVersion調用InstallREST方法完成后續。

5.?k8s.io/apiserver/pkg/endpoints/groupversion.go

//?InstallREST將REST handlers(storage, watch, proxy and redirect)注冊到一個restful容器中

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,

enableAPIResponseCompression: g.EnableAPIResponseCompression,

}

apiResources, ws, registrationErrors := installer.Install()

versionDiscoveryHandler := discovery.NewAPIVersionHandler(g.Serializer, g.GroupVersion, staticLister{apiResources})

versionDiscoveryHandler.AddToWebService(ws)

container.Add(ws)

return utilerrors.NewAggregate(registrationErrors)

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。