kubernetes scheme原理分析

Kubernetes 的 Scheme 是资源管理的核心,它维护了 Group/Version/Kind (GVK) 与对象类型的映射,支持多版本API的处理。Scheme 包含了 GVK 到类型、类型到 GVK 的映射,以及默认值函数和转换函数。通过 Scheme,可以实现资源对象的创建、序列化和反序列化,以及不同版本间的转换。资源的注册涉及到内部版本和外部版本,通过 SchemeBuilder 注册各种类型的资源模型,确保 API 的正确处理和版本兼容性。

为什么需要Scheme

  • 因为在web开发中随着版本的更新迭代,通常要在系统中维护多个版本的api,多个版本的api在数据结构上往往也各不相同
  • 为了解决上述问题 —— 出现了 Scheme —— 实现 GVK 与 api数据结构的对应

web 请求的处理流程

  • 收到请求后,通常首先是webServer先进行Http协议的处理
  • 解析成基础的webServer内部的一个Http请求对象
  • 该 Http 请求对象持有对应请求的请求头和底层对应的字节序列(从socket流中读取)
  • 接着根据对应的编码格式来进行反序列化
  • 完成从字节序列到当前接口的业务模型的映射, 然后在交给业务逻辑处理
  • 从而最终进行持久化存储, 本文的重点也就在反序列化部分

scheme代码分析

scheme的数据结构

Scheme 定义了资源序列化和反序列化的方法以及资源类型和版本的对应关系;这里我们可以理解成一张纪录表。定义在 k8s.io/apimachinery/pkg/runtime/scheme.go 中。需要关注的 gvkToTypeype 和 typeToGVK 字段

代码路径:staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go

// Scheme defines methods for serializing and deserializing API objects, a type
// registry for converting group, version, and kind information to and from Go
// schemas, and mappings between Go schemas of different versions. A scheme is the
// foundation for a versioned API and versioned configuration over time.
//
// In a Scheme, a Type is a particular Go struct, a Version is a point-in-time
// identifier for a particular representation of that Type (typically backwards
// compatible), a Kind is the unique name for that Type within the Version, and a
// Group identifies a set of Versions, Kinds, and Types that evolve over time. An
// Unversioned Type is one that is not yet formally bound to a type and is promised
// to be backwards compatible (effectively a "v1" of a Type that does not expect
// to break in the future).
//
// Schemes are not expected to change at runtime and are only threadsafe after
// registration is complete.
type Scheme struct {
	// versionMap allows one to figure out the go type of an object with
	// the given version and name.
	gvkToType map[schema.GroupVersionKind]reflect.Type

	// typeToGroupVersion allows one to find metadata for a given go object.
	// The reflect.Type we index by should *not* be a pointer.
	typeToGVK map[reflect.Type][]schema.GroupVersionKind

	// unversionedTypes are transformed without conversion in ConvertToVersion.
	unversionedTypes map[reflect.Type]schema.GroupVersionKind

	// unversionedKinds are the names of kinds that can be created in the context of any group
	// or version
	// TODO: resolve the status of unversioned types.
	unversionedKinds map[string]reflect.Type

	// Map from version and resource to the corresponding func to convert
	// resource field labels in that version to internal vers
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值