立即前往

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 智算采购季 热销S6云服务器2核4G限时88元/年起,部分主机可加赠对象存储组合包!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 一键部署Llama3大模型学习机 0代码一键部署,预装最新主流大模型Llama3与StableDiffusion
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 产品能力
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      Kubernetes RESTMapper源码分析

      首页 知识中心 软件开发 文章详情页

      Kubernetes RESTMapper源码分析

      2024-11-21 09:56:12 阅读次数:21

      Resource

      1、概述

      在 kubernetes API中,我们经常使用属于 GVK 或者 GVR 来区分特定的 kubernetes 资源。其中 GVK 是 Group Version Kind 的简称,而 GVR 则是 Group Version Resource 的简称。

      Kind 是 API “顶级”资源对象的类型,每个资源对象都需要 Kind 来区分它自身代表的资源类型,例如,对于一个 Pod 的例子:

      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          app: nfs-client-provisioner
        name: nfs-client-provisioner-6b7577544d-lngg2
        namespace: default
      .......

      其中 kind 字段即代表该资源对象的类型。一般来说,在 kubernetes API 中有三种不同的 Kind:

      • 单个资源对象的类型,最典型的就是刚才例子中提到的 Pod;
      • 资源对象的列表类型,例如 PodList 以及 NodeList 等;
      • 特殊类型以及非持久化操作的类型,很多这种类型的资源是 subresource, 例如用于绑定资源的 /binding、更新资源状态的 /status 以及读写资源实例数量的 /scale。

      需要注意的是,同 Kind 不只可以出现在同一分组的不同版本中,如 apps/v1beta1 与 apps/v1,它还可能出现在不同的分组中,例如 Deployment 开始以 alpha 的特性出现在 extensions 分组,GA 之后被推进到 apps 组,所以为了严格区分不同的 Kind,需要组合 API Group、API Version 与 Kind 成为 GVK。

      Resource 则是通过 HTTP 协议以 JSON 格式发送或者读取的资源展现形式,可以以单个资源对象展现,也可以以列表的形式展现。要正确的请求资源对象,API-Server 必须知道 apiVersion 与请求的资源,这样 API-Server 才能正确地解码请求信息,这些信息正是处于请求的资源路径中。一般来说,把 API Group、API Version 以及 Resource 组合成为 GVR 可以区分特定的资源请求路径,例如 /apis/batch/v1/jobs 就是请求所有的 jobs 信息。

      GVR 常用于组合成 RESTful API 请求路径。例如,针对应用程序 v1 部署的 RESTful API 请求如下所示:

      GET /apis/apps/v1/namespaces/{namespace}/deployments/{name}

      通过获取资源的 JSON 或 YAML 格式的序列化对象,进而从资源的类型信息中可以获得该资源的 GVK。RESTMapper作为GVK到GVR的映射,通过 GVK 信息则可以获取要读取的资源对象的 GVR,进而构建 RESTful API 请求获取对应的资源。Kubernetes 定义了 RESTMapper 接口并带默认带有实现 DefaultRESTMapper。

      RESTMapper作为GVK到GVR的映射,其主要作用是在ListerWatcher时, 根据Schema定义的类型GVK解析出GVR, 向apiserver发起http请求获取资源, 然后watch。

      2、RESTMapper源码分析

      2.1 什么是RESTMapper

       先来看来什么是RESTMapper。RESTMapper是一个interface,定义在/pkg/api/meta/interfaces.go中:

      // RESTMapper allows clients to map resources to kind, and map kind and version
      // to interfaces for manipulating those objects. It is primarily intended for
      // consumers of Kubernetes compatible REST APIs as defined in docs/devel/api-conventions.md.
      //
      // The Kubernetes API provides versioned resources and object kinds which are scoped
      // to API groups. In other words, kinds and resources should not be assumed to be
      // unique across groups.
      //
      // TODO: split into sub-interfaces
      type RESTMapper interface {
      	// KindFor takes a partial resource and returns the single match.  Returns an error if there are multiple matches
      	KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error)
      
      	// KindsFor takes a partial resource and returns the list of potential kinds in priority order
      	KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error)
      
      	// ResourceFor takes a partial resource and returns the single match.  Returns an error if there are multiple matches
      	ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error)
      
      	// ResourcesFor takes a partial resource and returns the list of potential resource in priority order
      	ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error)
      
      	// RESTMapping identifies a preferred resource mapping for the provided group kind.
      	RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error)
      	// RESTMappings returns all resource mappings for the provided group kind if no
      	// version search is provided. Otherwise identifies a preferred resource mapping for
      	// the provided version(s).
      	RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error)
      
      	ResourceSingularizer(resource string) (singular string, err error)
      }

      关于RESTMapper的注释非常重要,“RESTMapper allows clients to map resources to kind, and map kind and version to interfaces for manipulating those objects”。也就是说,RESTMapper映射是指GVR(GroupVersionResource)和GVK(GroupVersionKind)的关系,可以通过GVR找到合适的GVK,并可以通过GVK生成一个RESTMapping。

      2.2 什么是RESTMapping

      再来看来RESTMapping,同样定义在/pkg/api/meta/interfaces.go中:

      // RESTMapping contains the information needed to deal with objects of a specific
      // resource and kind in a RESTful manner.
      type RESTMapping struct {
      	// Resource is the GroupVersionResource (location) for this endpoint
      	Resource schema.GroupVersionResource
      
      	// GroupVersionKind is the GroupVersionKind (data format) to submit to this endpoint
      	GroupVersionKind schema.GroupVersionKind
      
      	// Scope contains the information needed to deal with REST Resources that are in a resource hierarchy
      	Scope RESTScope
      }

      RESTMapping包含Resource名称(GVR),及其对应的GVK,还有一个Scope(标明资源是否为root或者namespaced)。

      那么RESTMapping怎么用呢?

      比如/pkg/apiserver/api_installer.go中就有使用到RESTMapping中的Scope用来生成合适的URL(RESTScopeNameRoot和RESTScopeNameNamespace处理不同,详见以后对Apiserver的分析)。 

      2.3 什么是RESTScope

      这里一并把RESTScope介绍掉,因为RESTScope接口也定义在/pkg/api/meta/interfaces.go中:

      type RESTScopeName string
      
      const (
      	RESTScopeNameNamespace RESTScopeName = "namespace"
      	RESTScopeNameRoot      RESTScopeName = "root"
      )
      
      // RESTScope contains the information needed to deal with REST resources that are in a resource hierarchy
      type RESTScope interface {
      	// Name of the scope
      	Name() RESTScopeName
      }

      目前有两种类型RESTScope:namespace和root,RESTScopeNamespace表明该资源是在Namespace下的,如pods,rc等;RESTScopeRoot标明资源是全局的,如nodes, pv等。RESTScope具体由restScope之实现。restScope定义在/pkg/api/meta/restmapper.go中,逻辑比较简单,这里就不在详细分析。

      // Implements RESTScope interface
      type restScope struct {
      	name RESTScopeName
      }
      
      func (r *restScope) Name() RESTScopeName {
      	return 
      }
      
      var RESTScopeNamespace = &restScope{
      	name: RESTScopeNameNamespace,
      }
      
      var RESTScopeRoot = &restScope{
      	name: RESTScopeNameRoot,
      }

      2.4 DefaultRESTMapper

      DefaultRESTMapper实现了RESTMapper interface。为什么称为DefaultRESTMapper呢,因为DefaultRESTMapper定义了defaultGroupVersions。DefaultRESTMapper定义在/pkg/api/meta/restmapper.go中:

      //DefaultRESTMapper中的resource是指GVR,kind是指GVK
      //singular和plural都是GVR,singular指资源的单数形式,plural指资源的复数形式
      type DefaultRESTMapper struct {
      	defaultGroupVersions []schema.GroupVersion
      
      	resourceToKind       map[schema.GroupVersionResource]schema.GroupVersionKind
      	kindToPluralResource map[schema.GroupVersionKind]schema.GroupVersionResource
      	kindToScope          map[schema.GroupVersionKind]RESTScope
      	singularToPlural     map[schema.GroupVersionResource]schema.GroupVersionResource
      	pluralToSingular     map[schema.GroupVersionResource]schema.GroupVersionResource
      }
      
      var _ RESTMapper = &DefaultRESTMapper{}

      现在来详细分析DefaultRESTMapper的字段的涵义。

      • defaultGroupVersions: 默认的GroupVersion,如v1,apps/v1等,一般一个DefaultRESTMapper只设一个默认的GroupVersion;
      • resourceToKind:GVR(单数,复数)到GVK的map;
      • kindToPluralResource:GVK到GVR(复数)的map;
      • kindToScope:GVK到Scope的map;
      • singularToPlural:GVR(单数)到GVR(复数)的map;
      • pluralToSingular:GVR(复数)到GVR(单数)的map;

      下面来分析DefaultRESTMapper的重要方法的实现。

      1)NewDefaultRESTMapper方法

      NewDefaultRESTMapper方法生成一个新的DefaultRESTMapper。

      func NewDefaultRESTMapper(defaultGroupVersions []schema.GroupVersion) *DefaultRESTMapper {
      	resourceToKind := make(map[schema.GroupVersionResource]schema.GroupVersionKind)
      	kindToPluralResource := make(map[schema.GroupVersionKind]schema.GroupVersionResource)
      	kindToScope := make(map[schema.GroupVersionKind]RESTScope)
      	singularToPlural := make(map[schema.GroupVersionResource]schema.GroupVersionResource)
      	pluralToSingular := make(map[schema.GroupVersionResource]schema.GroupVersionResource)
      	// TODO: verify name mappings work correctly when versions differ
      
      	return &DefaultRESTMapper{
      		resourceToKind:       resourceToKind,
      		kindToPluralResource: kindToPluralResource,
      		kindToScope:          kindToScope,
      		defaultGroupVersions: defaultGroupVersions,
      		singularToPlural:     singularToPlural,
      		pluralToSingular:     pluralToSingular,
      	}
      }

      2)AddSpecific(kind schema.GroupVersionKind, plural, singular schema.GroupVersionResource, scope RESTScope)   

      AddSpecific方法主要是把具体的GVK、GVR和scope对应值加入到DefaultRESTMapper对应的字段中。

      func (m *DefaultRESTMapper) AddSpecific(kind schema.GroupVersionKind, plural, singular schema.GroupVersionResource, scope RESTScope) {
      	m.singularToPlural[singular] = plural
      	m.pluralToSingular[plural] = singular
      
      	m.resourceToKind[singular] = kind
      	m.resourceToKind[plural] = kind
      
      	m.kindToPluralResource[kind] = plural
      	m.kindToScope[kind] = scope
      }

      3)Add(kind schema.GroupVersionKind, scope RESTScope)

      Add方法根据的具体的GVK获取对应GVR单数和复数值,并将GVK、GVR和scope对应值加入到DefaultRESTMapper对应的字段中。

      func (m *DefaultRESTMapper) Add(kind schema.GroupVersionKind, scope RESTScope) {
      	plural, singular := UnsafeGuessKindToResource(kind)
      	m.AddSpecific(kind, plural, singular, scope)
      }

      UnsafeGuessKindToResource方法可以根据GVK获取对应GVR单数和复数值。

      // unpluralizedSuffixes is a list of resource suffixes that are the same plural and singular
      // This is only is only necessary because some bits of code are lazy and don't actually use the RESTMapper like they should.
      // TODO eliminate this so that different callers can correctly map to resources.  This probably means updating all
      // callers to use the RESTMapper they mean.
      var unpluralizedSuffixes = []string{
      	"endpoints",
      }
      
      // UnsafeGuessKindToResource converts Kind to a resource name.
      // Broken. This method only "sort of" works when used outside of this package.  It assumes that Kinds and Resources match
      // and they aren't guaranteed to do so.
      func UnsafeGuessKindToResource(kind schema.GroupVersionKind) ( /*plural*/ schema.GroupVersionResource /*singular*/, schema.GroupVersionResource) {
      	kindName := kind.Kind
      	if len(kindName) == 0 {
      		return schema.GroupVersionResource{}, schema.GroupVersionResource{}
      	}
      	singularName := strings.ToLower(kindName)
      	// GVR(单数)
      	singular := kind.GroupVersion().WithResource(singularName)
      
      	// unfluralized后缀是一组复数和单数相同的资源
      	for _, skip := range unpluralizedSuffixes {
      		if strings.HasSuffix(singularName, skip) {
      			return singular, singular
      		}
      	}
      
      	// 组织GVR(复数)
      	switch string(singularName[len(singularName)-1]) {
      	case "s":
      		return kind.GroupVersion().WithResource(singularName + "es"), singular
      	case "y":
      		return kind.GroupVersion().WithResource(strings.TrimSuffix(singularName, "y") + "ies"), singular
      	}
      
      	return kind.GroupVersion().WithResource(singularName + "s"), singular
      }

      4)ResourceFor方法

      ResourceFor()通过GVR(信息不一定要全)找到一个最匹配的已注册的GVR(m.pluralToSingular)。规则如下:

      • 如果参数GVR没有有Resource,则返回错误。
      • 如果参数GVR限定Group,Version和Resource,则匹配Group,Version和Resource;
      • 如果参数GVR限定Group和Resource,则匹配Group和Resource;
      • 如果参数GVR限定Version和Resource,则匹配Version和Resource;
      • 如果参数GVR只有Resource,则匹配Resource。
      • 如果系统中存在多个匹配,则返回错误(系统现在还不支持在不同的Group中定义相同的type)。
      // 找到最匹配的注册GVR
      func (m *DefaultRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
      	resources, err := m.ResourcesFor(resource)
      	if err != nil {
      		return schema.GroupVersionResource{}, err
      	}
      	if len(resources) == 1 {
      		return resources[0], nil
      	}
      
      	return schema.GroupVersionResource{}, &AmbiguousResourceError{PartialResource: resource, MatchingResources: resources}
      }
      
      func (m *DefaultRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
      	// 获取GVR,使资源小写,并将内部版本转换为""
      	resource := coerceResourceForMatching(input)
      
      	hasResource := len(resource.Resource) > 0
      	hasGroup := len(resource.Group) > 0
      	hasVersion := len(resource.Version) > 0
      
      	// 资源必须存在
      	if !hasResource {
      		return nil, fmt.Errorf("a resource must be present, got: %v", resource)
      	}
      
      	ret := []schema.GroupVersionResource{}
      	switch {
      	case hasGroup && hasVersion:
      		// 完全限定,比较GVR
      		// fully qualified.  Find the exact match
      		for plural, singular := range m.pluralToSingular {
      			if singular == resource {
      				ret = append(ret, plural)
      				break
      			}
      			if plural == resource {
      				ret = append(ret, plural)
      				break
      			}
      		}
      
      	case hasGroup:
      		// 只限定GR,比较GR
      		// given a group, prefer an exact match.  If you don't find one, resort to a prefix match on group
      		foundExactMatch := false
      		requestedGroupResource := resource.GroupResource()
      		for plural, singular := range m.pluralToSingular {
      			if singular.GroupResource() == requestedGroupResource {
      				foundExactMatch = true
      				ret = append(ret, plural)
      			}
      			if plural.GroupResource() == requestedGroupResource {
      				foundExactMatch = true
      				ret = append(ret, plural)
      			}
      		}
      
      		// 只限定G,比较Group
      		// if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
      		// storageclass.storage.k8s.io
      		if !foundExactMatch {
      			for plural, singular := range m.pluralToSingular {
      				if !strings.HasPrefix(plural.Group, requestedGroupResource.Group) {
      					continue
      				}
      				if singular.Resource == requestedGroupResource.Resource {
      					ret = append(ret, plural)
      				}
      				if plural.Resource == requestedGroupResource.Resource {
      					ret = append(ret, plural)
      				}
      			}
      
      		}
      
      	case hasVersion:
      		// 限定VR,比较VR
      		for plural, singular := range m.pluralToSingular {
      			if singular.Version == resource.Version && singular.Resource == resource.Resource {
      				ret = append(ret, plural)
      			}
      			if plural.Version == resource.Version && plural.Resource == resource.Resource {
      				ret = append(ret, plural)
      			}
      		}
      
      	default:
      		// 只比较Resource,根据Resource匹配
      		for plural, singular := range m.pluralToSingular {
      			if singular.Resource == resource.Resource {
      				ret = append(ret, plural)
      			}
      			if plural.Resource == resource.Resource {
      				ret = append(ret, plural)
      			}
      		}
      	}
      
      	if len(ret) == 0 {
      		return nil, &NoResourceMatchError{PartialResource: resource}
      	}
      
      	sort.Sort(resourceByPreferredGroupVersion{ret, m.defaultGroupVersions})
      	return ret, nil
      }
      
      // 使资源小写,并将内部版本转换为未指定的(遗留行为)
      // coerceResourceForMatching makes the resource lower case and converts internal versions to unspecified (legacy behavior)
      func coerceResourceForMatching(resource schema.GroupVersionResource) schema.GroupVersionResource {
      	resource.Resource = strings.ToLower(resource.Resource)
      	if resource.Version == runtime.APIVersionInternal {
      		resource.Version = ""
      	}
      
      	return resource
      }

      5)KindFor方法

      KindFor()通过GVR(信息不一定要全)找到一个最匹配的已注册的GVK。规则和ResourceFor()一样:

      • 如果参数GVR没有有Resource,则返回错误。
      • 如果参数GVR限定Group,Version和Resource,则匹配Group,Version和Resource;
      • 如果参数GVR限定Group和Resource,则匹配Group和Resource;
      • 如果参数GVR限定Version和Resource,则匹配Version和Resource;
      • 如果参数GVR只有Resource,则匹配Resource。
      • 如果系统中存在多个匹配,则返回错误(系统现在还不支持在不同的Group中定义相同的type)。

      注意:维护m.resourceToKind关系时,GVR单数和复数都会和GVK做映射,具体逻辑参见AddSpecific方法。

      // 根据GVR找到最匹配GVK
      func (m *DefaultRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
      	kinds, err := m.KindsFor(resource)
      	if err != nil {
      		return schema.GroupVersionKind{}, err
      	}
      	if len(kinds) == 1 {
      		return kinds[0], nil
      	}
      
      	return schema.GroupVersionKind{}, &AmbiguousResourceError{PartialResource: resource, MatchingKinds: kinds}
      }
      
      func (m *DefaultRESTMapper) KindsFor(input schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
      	resource := coerceResourceForMatching(input)
      
      	hasResource := len(resource.Resource) > 0
      	hasGroup := len(resource.Group) > 0
      	hasVersion := len(resource.Version) > 0
      
      	if !hasResource {
      		return nil, fmt.Errorf("a resource must be present, got: %v", resource)
      	}
      
      	ret := []schema.GroupVersionKind{}
      	switch {
      	// fully qualified.  Find the exact match
      	case hasGroup && hasVersion:
      		kind, exists := m.resourceToKind[resource]
      		if exists {
      			ret = append(ret, kind)
      		}
      
      	case hasGroup:
      		foundExactMatch := false
      		requestedGroupResource := resource.GroupResource()
      		for currResource, currKind := range m.resourceToKind {
      			if currResource.GroupResource() == requestedGroupResource {
      				foundExactMatch = true
      				ret = append(ret, currKind)
      			}
      		}
      
      		// if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
      		// storageclass.storage.k8s.io
      		if !foundExactMatch {
      			for currResource, currKind := range m.resourceToKind {
      				if !strings.HasPrefix(currResource.Group, requestedGroupResource.Group) {
      					continue
      				}
      				if currResource.Resource == requestedGroupResource.Resource {
      					ret = append(ret, currKind)
      				}
      			}
      
      		}
      
      	case hasVersion:
      		for currResource, currKind := range m.resourceToKind {
      			if currResource.Version == resource.Version && currResource.Resource == resource.Resource {
      				ret = append(ret, currKind)
      			}
      		}
      
      	default:
      		for currResource, currKind := range m.resourceToKind {
      			if currResource.Resource == resource.Resource {
      				ret = append(ret, currKind)
      			}
      		}
      	}
      
      	if len(ret) == 0 {
      		return nil, &NoResourceMatchError{PartialResource: input}
      	}
      
      	sort.Sort(kindByPreferredGroupVersion{ret, m.defaultGroupVersions})
      	return ret, nil
      }

      6)ResourceSingularizer方法

      将资源名称从复数转换为单数,如果系统中存在多个匹配,则返回错误。

      // 将资源名称从复数转换为单数
      // ResourceSingularizer implements RESTMapper
      // It converts a resource name from plural to singular (e.g., from pods to pod)
      func (m *DefaultRESTMapper) ResourceSingularizer(resourceType string) (string, error) {
      	partialResource := schema.GroupVersionResource{Resource: resourceType}
      	resources, err := m.ResourcesFor(partialResource)
      	if err != nil {
      		return resourceType, err
      	}
      
      	singular := schema.GroupVersionResource{}
      	for _, curr := range resources {
      		currSingular, ok := m.pluralToSingular[curr]
      		if !ok {
      			continue
      		}
      		if singular.Empty() {
      			singular = currSingular
      			continue
      		}
      
      		if currSingular.Resource != singular.Resource {
      			return resourceType, fmt.Errorf("multiple possible singular resources (%v) found for %v", resources, resourceType)
      		}
      	}
      
      	if singular.Empty() {
      		return resourceType, fmt.Errorf("no singular of resource %v has been defined", resourceType)
      	}
      
      	return singular.Resource, nil
      }

      7)RESTMapping方法

      根据GVK获取RESTMapping,RESTMapping()的参数是GK和versions,通常的做法是把一个GVK直接拆成GK和Version,然后获取mapping。

      // RESTMapping returns a struct representing the resource path and conversion interfaces a
      // RESTClient should use to operate on the provided group/kind in order of versions. If a version search
      // order is not provided, the search order provided to DefaultRESTMapper will be used to resolve which
      // version should be used to access the named group/kind.
      func (m *DefaultRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
      	mappings, err := m.RESTMappings(gk, versions...)
      	if err != nil {
      		return nil, err
      	}
      	if len(mappings) == 0 {
      		return nil, &NoKindMatchError{GroupKind: gk, SearchedVersions: versions}
      	}
      	// since we rely on RESTMappings method
      	// take the first match and return to the caller
      	// as this was the existing behavior.
      	return mappings[0], nil
      }
      
      // RESTMappings returns the RESTMappings for the provided group kind. If a version search order
      // is not provided, the search order provided to DefaultRESTMapper will be used.
      func (m *DefaultRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
      	mappings := make([]*RESTMapping, 0)
      	// DefaultRESTMapper对象已注册最匹配的GVK
      	potentialGVK := make([]schema.GroupVersionKind, 0)
      	hadVersion := false
      
      	// 选择一个合适的版本, 找到DefaultRESTMapper对象最匹配的已注册的GVK
      	// Pick an appropriate version
      	for _, version := range versions {
      		if len(version) == 0 || version == runtime.APIVersionInternal {
      			continue
      		}
      		currGVK := gk.WithVersion(version)
      		hadVersion = true
      		if _, ok := m.kindToPluralResource[currGVK]; ok {
      			potentialGVK = append(potentialGVK, currGVK)
      			break
      		}
      	}
      	// version不满足条件的话,使用DefaultRESTMapper对象默认GV
      	// Use the default preferred versions
      	if !hadVersion && len(potentialGVK) == 0 {
      		for _, gv := range m.defaultGroupVersions {
      			if gv.Group != gk.Group {
      				continue
      			}
      			potentialGVK = append(potentialGVK, gk.WithVersion(gv.Version))
      		}
      	}
      
      	if len(potentialGVK) == 0 {
      		return nil, &NoKindMatchError{GroupKind: gk, SearchedVersions: versions}
      	}
      
      	for _, gvk := range potentialGVK {
      		// 确保有GVR(复数)
      		//Ensure we have a REST mapping
      		res, ok := m.kindToPluralResource[gvk]
      		if !ok {
      			continue
      		}
      
      		// 确保有rest scope
      		// Ensure we have a REST scope
      		scope, ok := m.kindToScope[gvk]
      		if !ok {
      			return nil, fmt.Errorf("the provided version %q and kind %q cannot be mapped to a supported scope", gvk.GroupVersion(), gvk.Kind)
      		}
      
      		mappings = append(mappings, &RESTMapping{
      			Resource:         res,
      			GroupVersionKind: gvk,
      			Scope:            scope,
      		})
      	}
      
      	if len(mappings) == 0 {
      		return nil, &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: gk.Group, Resource: gk.Kind}}
      	}
      	return mappings, nil
      }

      RESTMapping()的流程如下:

      • 构造GVK:使用GK和Versions,或GK和DefaultGroupVersions,构造GVK;
      • 获取GVR:从kindToPluralResource中获取GVR;
      • 获取scope:从kindToScope中获取scope;
      • 组装成RESTMapping并返回。

      3、总结

      RESTMapper可以从GVK获取GVR,并生成一个RESTMapping来处理该GVR。RESTMapping中有GVK、GVR、Scope信息。

      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_15481067/11748693,作者:人艰不拆_zmc,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:深入理解Java中的反射与动态代理

      下一篇:如何在Java中实现智能合成与语音识别

      相关文章

      2025-03-25 08:08:18

      spring核心IOC(解耦,DI,Bean对象)

      spring核心IOC(解耦,DI,Bean对象)

      2025-03-25 08:08:18
      Autowired , bean , IOC , Resource , 扫描 , 注解 , 默认
      2024-12-13 06:53:39

      redis消息队列-redis stream详细说明

      Redis Stream 是 Redis 4.0 版本引入的一种新的数据类型,它主要设计用于构建消息队列和事件驱动的应用程序。Redis Stream 可以看作是一个动态增长的列表,用于存储按时间顺序排列的消息。这种数据结构非常适合用来实现异步通信、日志记录、实时监控等多种应用场景。

      2024-12-13 06:53:39
      Consumer , Redis , Stream , 内存 , 场景 , 消息
      2024-07-12 07:07:05

      React工作34:radio回显

      React工作34:radio回显

      2024-07-12 07:07:05
      javascript , 前端
      2024-06-26 06:19:51

      【Win 10 应用开发】在代码中加载文本资源

      在 XAML 中使用 uid 加载资源虽然方便,但是它有个缺点——不同控件有不同的属性,有时候不太方便匹配,当然了,如果你的资源所针对的控件类型不多,那是无所谓的。

      2024-06-26 06:19:51
      Resource , UWP
      2024-06-18 07:20:39

      说说 C# 9 新特性的实际运用

      说说 C# 9 新特性的实际运用

      2024-06-18 07:20:39
      Resource , UWP
      2024-06-13 08:12:14

      高级指引——手动创建节点分组 Group

      高级指引——手动创建节点分组 Group

      2024-06-13 08:12:14
      分组 , 节点
      2023-07-06 09:51:42

      Spring中的Resouce资源

      Resource 在 Java 当做资源使用 URL 进行表示。Spring 将对物理资源的访问方式抽象成了 Resouce, 我们可以通过 Spring 提供的接口来访问磁盘文件等数据。针对不同的资源采取了不同的实现方式。Resouce

      2023-07-06 09:51:42
      Resource , Spring
      2023-06-20 09:11:19

      Spring中@Autowired和@Resource的区别及详细使用

      @Autowired是Spring提供的 @Resource是J2EE提供的 @Autowired: 获得对象的方式称之为byType(通过类型),如果需要指定名字,那么需要使@Qualifier(“

      2023-06-20 09:11:19
      Autowired , Resource , spring
      2023-06-19 06:57:41

      Spring Security OAuth2之resource_id配置与验证

      一、resource_id的作用Spring Security OAuth2 架构上分为Authorization Server认证服务器和Resource Server资源服务器。我们可以为每一个Resource Server(一个微服务

      2023-06-19 06:57:41
      Resource , Security , Spring
      2023-06-16 06:09:45

      @Autowired与@Resource区别

      @Resource和@Autowired都可以作为注入属性的修饰,在接口仅有单一实现类时,两个注解的修饰效果相同,可以互相替换,不影响使用。

      2023-06-16 06:09:45
      java , Resource , spring
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      32777

      阅读量

      4830022

      查看更多

      最新文章

      【Win 10 应用开发】在代码中加载文本资源

      2024-06-26 06:19:51

      说说 C# 9 新特性的实际运用

      2024-06-18 07:20:39

      Spring中的Resouce资源

      2023-07-06 09:51:42

      Spring中@Autowired和@Resource的区别及详细使用

      2023-06-20 09:11:19

      Spring Security OAuth2之resource_id配置与验证

      2023-06-19 06:57:41

      Spring系列之注解@Autowired和@Resource的区别

      2023-06-07 07:32:48

      查看更多

      热门文章

      Spring系列之注解@Autowired和@Resource的区别

      2023-06-07 07:32:48

      Spring中@Autowired和@Resource的区别及详细使用

      2023-06-20 09:11:19

      Spring Security OAuth2之resource_id配置与验证

      2023-06-19 06:57:41

      Spring中的Resouce资源

      2023-07-06 09:51:42

      【Win 10 应用开发】在代码中加载文本资源

      2024-06-26 06:19:51

      说说 C# 9 新特性的实际运用

      2024-06-18 07:20:39

      查看更多

      热门标签

      java Java python 编程开发 开发语言 代码 算法 线程 html Python 数组 C++ javascript c++ 元素
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      Spring中@Autowired和@Resource的区别及详细使用

      Spring Security OAuth2之resource_id配置与验证

      【Win 10 应用开发】在代码中加载文本资源

      说说 C# 9 新特性的实际运用

      Spring中的Resouce资源

      Spring系列之注解@Autowired和@Resource的区别

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 权益商城
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 权益商城
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号