k8s源码阅读(2) - kube-apiserver

配置项定义

文件/kube-apiserver/app/options/options.go

  • func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets)
# 配置详解后续根据需要逐步补充,基本配置都在options包里,再使用options包的过程中,首先生成杂乱的配置全量跟默认配置,然后再对这些杂乱的配置进行归类

server启动过程

文件cmd/kube-apiserver/app/server.go

这里创建了比较重要的api handler CreateKubeAPIServer后文详解

  • func Run(completeOptions completedServerRunOptions, stopCh <-chan struct{}) error
func Run(completeOptions completedServerRunOptions, stopCh <-chan struct{}) error {
    // To help debugging, immediately log version
    klog.Infof("Version: %+v", version.Get())
    // 这里初始化了server的启动配置,装载了一些拦截器,合并配置跟api handler扩展,最终返回一个aggregatorapiserver.APIAggregator对象.
    server, err := CreateServerChain(completeOptions, stopCh)
    if err != nil {
        return err
    }
    // 装在一些健康检查handler,初始化路由,审计backend
    prepared, err := server.PrepareRun()
    if err != nil {
        return err
    }

    return prepared.Run(stopCh)
}

文件vendor/k8s.io/apiserver/pkg/server/genericapiserver.go

最终执行的http server的运行,这里定义了如何优雅退出逻辑

  • func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error

kubeAPIServer创建流程

文件pkg/controlplane/instance.go

这里为整个创建流程

  • func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Instance, error)
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Instance, error) {
    // 这里创建GenericAPIServer在下面展开说
    s, err := c.GenericConfig.New("kube-apiserver", delegationTarget)
    if err != nil {
        return nil, err
    }
    ...
    // 这里生成最终返回的instance下面展开说
    m := &Instance{
        GenericAPIServer:          s,
        ClusterAuthenticationInfo: c.ExtraConfig.ClusterAuthenticationInfo,
    }
    ...
    restStorageProviders := []RESTStorageProvider{
        apiserverinternalrest.StorageProvider{},
        authenticationrest.RESTStorageProvider{Authenticator: c.GenericConfig.Authentication.Authenticator, APIAudiences: c.GenericConfig.Authentication.APIAudiences},
        authorizationrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorization.Authorizer, RuleResolver: c.GenericConfig.RuleResolver},
        autoscalingrest.RESTStorageProvider{},
        batchrest.RESTStorageProvider{},
        certificatesrest.RESTStorageProvider{},
        coordinationrest.RESTStorageProvider{},
        discoveryrest.StorageProvider{},
        networkingrest.RESTStorageProvider{},
        noderest.RESTStorageProvider{},
        policyrest.RESTStorageProvider{},
        rbacrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorization.Authorizer},
        schedulingrest.RESTStorageProvider{},
        storagerest.RESTStorageProvider{},
        flowcontrolrest.RESTStorageProvider{},
        // keep apps after extensions so legacy clients resolve the extensions versions of shared resource names.
        // See https://github.com/kubernetes/kubernetes/issues/42392
        appsrest.StorageProvider{},
        admissionregistrationrest.RESTStorageProvider{},
        eventsrest.RESTStorageProvider{TTL: c.ExtraConfig.EventTTL},
    }
    // 装载api
    if err := m.InstallAPIs(c.ExtraConfig.APIResourceConfigSource, c.GenericConfig.RESTOptionsGetter, restStorageProviders...); err != nil {
        return nil, err
    }


    

    return m, nil
}

  • func (m *Instance) InstallAPIs(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter, restStorageProviders ...RESTStorageProvider) error
// 这里是装载api的过程,其中serverstorage.APIResourceConfigSource配置的定义可以看cmd/kube-apiserver/app/server.go里的func CreateKubeAPIServerConfig, func buildGenericConfig 这里装载了openapi生成的k8s-api的接口定义,k8s的open api是基于https://github.com/emicklei/go-restful实现的,整个apiserver项目里进行handler的生成(https://github.com/kubernetes/apiserver)

文章未完结持续更新

推荐阅读更多精彩内容