Openshift API部分源码学习笔记(二)

前面一篇文章将openshift里面启动kubernetes组件部分解析了一下,本文将对openshift启动master服务的代码解析一下

上一篇文章链接:http://www.cnblogs.com/zard/p/7767112.html

源码分析

前面将kubernetes的API Server注册启动命令创建好了之后回到NewCommandStartAllInOne方法中,这次看下NewCommandStartMaster方法,此方法即为获取启动openshift master的命令

--》openshift/origin/pkg/cmd/server/start/start_allinone.go

 

--》openshift/origin/pkg/cmd/server/start/start_master.go

NewCommandStartMaster方法中添加命令的逻辑如下,即为添加了controller和API的两个启动命令

这里我们主要看下API相关的即NewCommandStartMasterAPI方法

--》openshift/origin/pkg/cmd/server/start/start_api.go

 NewCommandStartMasterAPI方法里面主要逻辑如下,即调用StartMaster方法来启动master服务

后面在RunMaster方法里面根据配置信息生产一个master结构对象并调用start方法启动

--》openshift/origin/pkg/cmd/server/start/start_master.go

 start方法里面先根据给定选项来创建openshift和kubernetes的配置信息

然后判断此master server是否有api服务,若有则根据刚刚创建的两个配置文件调用StartAPI方法来启动master上的api部分组件

StartAPI方法会启动那些在master上被认为是API一部分的组件,比如一开始就会根据配置信息来判断是否启动etcd

后面会调用openshiftConfig的run方法来加载组件

--》openshift/origin/pkg/cmd/server/origin/master.go

run方法里面会处理两种API,一种是访问受保护的api(通常这类调用是需要验证的),另一种是不受保护的api,我们这里主要关注那些受保护的api,因为大部分api都是这种

InstallProtectedAPI方法即为安装注册受保护的api,逻辑里首先初始化所有openshift的api

GetRestStorage方法会获取openshift所用到的所有storage,例如projectStorage

然后将所有storage放到一个map中,此map相当于一个openshift的配置文件,记录了所有api路径和此路径所用到的storage

storage := map[string]rest.Storage{
        "images":               imageStorage,
        "imagesignatures":      imageSignatureStorage,
        "imageStreams/secrets": imageStreamSecretsStorage,
        "imageStreams":         imageStreamStorage,
        "imageStreams/status":  imageStreamStatusStorage,
        "imageStreamImports":   imageStreamImportStorage,
        "imageStreamImages":    imageStreamImageStorage,
        "imageStreamMappings":  imageStreamMappingStorage,
        "imageStreamTags":      imageStreamTagStorage,

        "deploymentConfigs":          deployConfigStorage,
        "deploymentConfigs/scale":    deployConfigScaleStorage,
        "deploymentConfigs/status":   deployConfigStatusStorage,
        "deploymentConfigs/rollback": deployConfigRollbackStorage,
        "deploymentConfigs/log":      deploylogregistry.NewREST(configClient, kclient, c.DeploymentLogClient(), kubeletClient),

        // TODO: Deprecate these
        "generateDeploymentConfigs": deployconfiggenerator.NewREST(deployConfigGenerator, c.EtcdHelper.Codec()),
        "deploymentConfigRollbacks": deployrollback.NewDeprecatedREST(deployRollbackClient, c.EtcdHelper.Codec()),

        "processedTemplates": templateregistry.NewREST(),
        "templates":          templateStorage,

        "routes":        routeStorage,
        "routes/status": routeStatusStorage,

        "projects":        projectStorage,
        "projectRequests": projectRequestStorage,

        "hostSubnets":           hostSubnetStorage,
        "netNamespaces":         netNamespaceStorage,
        "clusterNetworks":       clusterNetworkStorage,
        "egressNetworkPolicies": egressNetworkPolicyStorage,

        "users":                userStorage,
        "groups":               groupStorage,
        "identities":           identityStorage,
        "userIdentityMappings": userIdentityMappingStorage,

        "oAuthAuthorizeTokens":      authorizeTokenStorage,
        "oAuthAccessTokens":         accessTokenStorage,
        "oAuthClients":              clientStorage,
        "oAuthClientAuthorizations": clientAuthorizationStorage,

        "resourceAccessReviews":      resourceAccessReviewStorage,
        "subjectAccessReviews":       subjectAccessReviewStorage,
        "localSubjectAccessReviews":  localSubjectAccessReviewStorage,
        "localResourceAccessReviews": localResourceAccessReviewStorage,
        "selfSubjectRulesReviews":    selfSubjectRulesReviewStorage,

        "policies":       policyStorage,
        "policyBindings": policyBindingStorage,
        "roles":          roleStorage,
        "roleBindings":   roleBindingStorage,

        "clusterPolicies":       clusterPolicyStorage,
        "clusterPolicyBindings": clusterPolicyBindingStorage,
        "clusterRoleBindings":   clusterRoleBindingStorage,
        "clusterRoles":          clusterRoleStorage,

        "clusterResourceQuotas":        restInPeace(clusterresourcequotaregistry.NewStorage(c.RESTOptionsGetter)),
        "clusterResourceQuotas/status": updateInPeace(clusterresourcequotaregistry.NewStatusStorage(c.RESTOptionsGetter)),
        "appliedClusterResourceQuotas": appliedclusterresourcequotaregistry.NewREST(
            c.ClusterQuotaMappingController.GetClusterQuotaMapper(), c.Informers.ClusterResourceQuotas().Lister(), c.Informers.Namespaces().Lister()),
    }

拿到了所有storage后便判断是否是v1版本的,若是则安装注册这些api

这里会先调用apiLegacyV1方法通过刚刚得到的storage map得到一个APIGroupVersion结构对象,此对象即为kubernetes的结构对象,然后调用APIGroupVersion的InstallREST方法去安装api

InstallREST方法逻辑很前一篇文章kubernetes里面的一样,这里就不再重复分析,此方法代码如下:

这样openshift master上的api组件就启动成功了

 

posted @ 2017-11-02 17:14  Zard_李  阅读(1180)  评论(0编辑  收藏  举报