Kubernetes 核心数据结构GVR -Resource

本文详细介绍了Kubernetes中的核心数据结构GVR(Group、Version、Resource),包括Resource的源码分析,资源的外部版本与内部版本的区别,并以Pod资源为例进行深入讲解,帮助读者理解Kubernetes资源管理的本质。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系列文章目录

Kubernetes 核心数据结构GVR
Kubernetes 核心数据结构GVR - Group
Kubernetes 核心数据结构GVR - Version


概述

Kubernetes 是一个完全以资源为中心的系统。其本质是一个资源控制系统,注册、管理、调度资源并维护资源的状态。

一、Resource 是什么?

Kubernetes 将资源进行分组和版本化,形成了 Group(资源组)、Version(资源版本)、Resource(资源)。以下简称为 GVR。

Resource:资源,在 Kubernetes API Server 中也称其为 APIResource。

二、APIResource

1.源码分析

github 地址

// APIResource specifies the name of a resource and whether it is namespaced.
type APIResource struct {
	// name is the plural name of the resource.
	Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
	// singularName is the singular name of the resource.  This allows clients to handle plural and singular opaquely.
	// The singularName is more correct for reporting status on a single item and both singular and plural are allowed
	// from the kubectl CLI interface.
	SingularName string `json:"singularName" protobuf:"bytes,6,opt,name=singularName"`
	// namespaced indicates if a resource is namespaced or not.
	Namespaced bool `json:"namespaced" protobuf:"varint,2,opt,name=namespaced"`
	// group is the preferred group of the resource.  Empty implies the group of the containing resource list.
	// For subresources, this may have a different value, for example: Scale".
	Group string `json:"group,omitempty" protobuf:"bytes,8,opt,name=group"`
	// version is the preferred version of the resource.  Empty implies the version of the containing resource list
	// For subresources, this may have a different value, for example: v1 (while inside a v1beta1 version of the core resource's group)".
	Version string `json:"version,omitempty" protobuf:"bytes,9,opt,name=version"`
	// kind is the kind for the resource (e.g. 'Foo' is the kind for a resource 'foo')
	Kind string `json:"kind" protobuf:"bytes,3,opt,name=kind"`
	// verbs is a list of supported kube verbs (this includes get, list, watch, create,
	// update, patch, delete, deletecollection, and proxy)
	Verbs Verbs `json:"verbs" protobuf:"bytes,4,opt,name=verbs"`
	// shortNames is a list of suggested short names of the resource.
	ShortNames []string `json:"shortNames,omitempty" protobuf:"bytes,5,rep,name=shortNames"`
	// categories is a list of the grouped resources this resource belongs to (e.g. 'all')
	Categories []string `json:"categories,omitempty" protobuf:"bytes,7,rep,name=categories"`
	// The hash value of the storage version, the version this resource is
	// converted to when written to the data store. Value must be treated
	// as opaque by clients. Only equality comparison on the value is valid.
	// This is an alpha feature and may change or be removed in the future.
	// The field is populated by the apiserver only if the
	// StorageVersionHash feature gate is enabled.
	// This field will remain optional even if it graduates.
	// +optional
	StorageVersionHash string `json:"storageVersionHash,omitempty" protobuf:"bytes,10,opt,name=storageVersionHash"`
}

资源数据结构字段说明如下。

  • Name:资源名称。
  • SingularName:资源的单数名称,它必须由小写字母组成,默认使用资源种类(Kind)的小写形式进行命名。例如,Pod资源的单数名称为pod,复数名称为pods。
  • Namespaced:资源是否拥有所属命名空间。
  • Group:资源所在的资源组名称。
  • Version:资源所在的资源版本。
  • Kind:资源种类。
  • Verbs:资源可操作的方法列表,例如get、list、delete、create、update等。
  • ShortNames:资源的简称,例如Pod资源的简称为po。

2.资源外部版本与内部版本

在 Kubernetes 系统中,同一个 Kubernetes 资源对应着两个版本,分为外部版本(External Version)与内部版本(Internal Version)。例如,Deployment资源,它所属的外部版本表现形式为 apps/v1,内部版本表现形式为 apps/__internal。

资源的外部版本和内部版本是需要相互转换的,而用于转换的函数需要事先初始化到资源注册表(Scheme)中。多个外部版本(External Version)之间的资源进行相互转换,都需要通过内部版本(Internal Version)进行中转。这也是Kubernetes能实现多资源版本转换的关键。

不同资源版本包在源码中的引用路径不同,代码示例如下:

// 外部资源版本
corev1 "k8s.io/api/core/v1"
// 内部资源版本
// 在网上查了下,本地代码引不了这个包,有兴趣的可以尝试下
core "k8s.io/kubernetes/pkg/apis/core" 

三、以 Pod 资源举例

资源的外部版本与内部版本的代码定义也不太一样,外部版本的资源需要对外暴露给用户请求的接口,所以资源代码定义了JSON Tags和Proto Tags,用于请求的序列化和反序列化操作。内部版本的资源不对外暴露,所以没有任何的JSON Tags和Proto Tags定义。

Pod资源的外部版本代码定义如下:代码路径:vendor/k8s.io/api/core/v1/types.go

type Pod struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
	Spec PodSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
	Status PodStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

Pod资源的内部版本代码定义如下:代码路径:pkg/apis/core/types.go

type Pod struct {
	metav1.TypeMeta
	metav1.ObjectMeta
	Spec PodSpec
	Status PodStatus
}

Pod资源的外部版本代码定义与内部版本代码定义主要区别在有无 Tags。

总结

本文介绍了 Kubernetes 核心数据结构GVR - Resource 源码分析,列举了资源外部版本与内部版本的关系及区别,并以 Pod 资源举例,加深用户理解,让用户更好的理解 Resource 的概念。

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

k8s-open

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值