温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

构建API Server的详细步骤

发布时间:2021-09-09 13:39:29 来源:亿速云 阅读:218 作者:chen 栏目:云计算

本篇内容介绍了“构建API Server的详细步骤”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

API Server利用GenericAPIServer框架依次创建了api_extension_server\api_server\aggregator_server来处理所有请求,server构建的核心逻辑如下:

func CreateServerChain(runOptions *options.ServerRunOptions, stopCh <-chan struct{}) (*genericapiserver.GenericAPIServer, error) {

   // api server的config结构

   kubeAPIServerConfig, sharedInformers, versionedInformers, insecureServingOptions, serviceResolver, err := CreateKubeAPIServerConfig(runOptions, nodeTunneler, proxyTransport)

   if err != nil {

      return nil, err

   }

 

   // extension server的config结构

   apiExtensionsConfig, err := createAPIExtensionsConfig(*kubeAPIServerConfig.GenericConfig, versionedInformers, runOptions)

   if err != nil {

      return nil, err

   }

 

   // 1. 创建api extensions server

   apiExtensionsServer, err := createAPIExtensionsServer(apiExtensionsConfig, genericapiserver.EmptyDelegate)

   if err != nil {

      return nil, err

   }

 

   // 2. 创建api server,delegate到extensions server上(注意,第二个参数)

   kubeAPIServer, err := CreateKubeAPIServer(kubeAPIServerConfig, apiExtensionsServer.GenericAPIServer, sharedInformers, versionedInformers)

   if err != nil {

      return nil, err

   }

 

   .....

 

   // otherwise go down the normal path of standing the aggregator up in front of the API server

   // this wires up openapi

   kubeAPIServer.GenericAPIServer.PrepareRun()

 

   // This will wire up openapi for extension api server

   apiExtensionsServer.GenericAPIServer.PrepareRun()

 

   ....

   // 3. 创建aggregator server, delegate到api server上

   aggregatorServer, err := createAggregatorServer(aggregatorConfig, kubeAPIServer.GenericAPIServer, apiExtensionsServer.Informers)

   if err != nil {

      // we don't need special handling for innerStopCh because the aggregator server doesn't create any go routines

      return nil, err

   }

 

   ....

   return aggregatorServer.GenericAPIServer, nil

}

当中,三个server创建的过程大体类似。以下是API Server创建方法CreateKubeAPIServer的核心逻辑:

func CreateKubeAPIServer(kubeAPIServerConfig *master.Config, delegateAPIServer genericapiserver.DelegationTarget, sharedInformers informers.SharedInformerFactory, versionedInformers clientgoinformers.SharedInformerFactory) (*master.Master, error) {

   kubeAPIServer, err := kubeAPIServerConfig.Complete(versionedInformers).New(delegateAPIServer)

   if err != nil {

      return nil, err

   }

   kubeAPIServer.GenericAPIServer.AddPostStartHook("start-kube-apiserver-informers", func(context genericapiserver.PostStartHookContext) error {

      sharedInformers.Start(context.StopCh)

      return nil

   })

 

   return kubeAPIServer, nil

}

 

// kubeAPIServerConfig.Complete(versionedInformers).New(delegateAPIServer)中的New方法如下所示:

func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Master, error) {

   ...

 

   // 通过Config创建APIServer对应的GenericAPIServer对象

   s, err := c.GenericConfig.New("kube-apiserver", delegationTarget)

   if err != nil {

      return nil, err

   }

 

   ....

   m := &Master{

      GenericAPIServer: s,

   }

 

   // 将所有REST资源(Pods/Deployments)的Storage接口实现类都install到master内含的GenericAPIServer上

   restStorageProviders := []RESTStorageProvider{

      authenticationrest.RESTStorageProvider{Authenticator: c.GenericConfig.Authenticator},

      authorizationrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorizer, RuleResolver: c.GenericConfig.RuleResolver},

      autoscalingrest.RESTStorageProvider{},

      batchrest.RESTStorageProvider{},

      certificatesrest.RESTStorageProvider{},

      extensionsrest.RESTStorageProvider{},

      networkingrest.RESTStorageProvider{},

      policyrest.RESTStorageProvider{},

      rbacrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorizer},

      schedulingrest.RESTStorageProvider{},

      settingsrest.RESTStorageProvider{},

      storagerest.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.RESTStorageProvider{},

      admissionregistrationrest.RESTStorageProvider{},

   }

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

 

   ...

   return m, nil

}

经过以上几个步骤,API Server就完成了所有资源的install,可以开始监听并响应请求。

“构建API Server的详细步骤”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI