配置項(xiàng)定義
文件/kube-apiserver/app/options/options.go
- func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets)
# 配置詳解后續(xù)根據(jù)需要逐步補(bǔ)充,基本配置都在options包里,再使用options包的過程中,首先生成雜亂的配置全量跟默認(rèn)配置,然后再對(duì)這些雜亂的配置進(jìn)行歸類
server啟動(dòng)過程
文件cmd/kube-apiserver/app/server.go
這里創(chuàng)建了比較重要的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的啟動(dòng)配置,裝載了一些攔截器,合并配置跟api handler擴(kuò)展,最終返回一個(gè)aggregatorapiserver.APIAggregator對(duì)象.
server, err := CreateServerChain(completeOptions, stopCh)
if err != nil {
return err
}
// 裝在一些健康檢查handler,初始化路由,審計(jì)backend
prepared, err := server.PrepareRun()
if err != nil {
return err
}
return prepared.Run(stopCh)
}
文件vendor/k8s.io/apiserver/pkg/server/genericapiserver.go
最終執(zhí)行的http server的運(yùn)行,這里定義了如何優(yōu)雅退出邏輯
- func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error
kubeAPIServer創(chuàng)建流程
文件pkg/controlplane/instance.go
這里為整個(gè)創(chuàng)建流程
- func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Instance, error)
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Instance, error) {
// 這里創(chuàng)建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實(shí)現(xiàn)的,整個(gè)apiserver項(xiàng)目里進(jìn)行handler的生成(https://github.com/kubernetes/apiserver)
文章未完結(jié)持續(xù)更新