likes
comments
collection
share

Kubernetes对象深入学习之二:细说schema.ObjectKind

作者站长头像
站长
· 阅读数 31

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):github.com/zq2599/blog…

runtime.Object、schema.ObjectKind、metav1.TypeMeta三者的关系

  • 首先要梳理清楚runtime.Object、schema.ObjectKind、metav1.TypeMeta这三者的关系,先回顾前文的简图 Kubernetes对象深入学习之二:细说schema.ObjectKind
  • 这里小结一下三者关系:
  1. runtime.Object是个接口,定义了GetObjectKind()方法,返回值是schema.ObjectKind
type Object interface {
	GetObjectKind() schema.ObjectKind
	DeepCopyObject() Object
}
  1. 上述返回值schema.ObjectKind也是一个接口,定义了两个方法:SetGroupVersionKind和GroupVersionKind
type ObjectKind interface {
	// SetGroupVersionKind sets or clears the intended serialized kind of an object. Passing kind nil
	// should clear the current setting.
	SetGroupVersionKind(kind GroupVersionKind)
	// GroupVersionKind returns the stored group, version, and kind of an object, or an empty struct
	// if the object does not expose or provide these fields.
	GroupVersionKind() GroupVersionKind
}
  1. 上面一共提到了两个接口的三个方法,它们都被metav1.TypeMeta实现了 Kubernetes对象深入学习之二:细说schema.ObjectKind
  • 还有一点要注意:TypeMeta的GetObjectKind方法,返回的是它自己!
  • 文章写到这里,突然有了一个小小的顿悟:以后在代码中再看到runtime.Object应该就很淡定了,它可能是任何资源类型,也可能只是个TypeMeta,但是无论如何,能直接从它那里得到的东西很少,因为它只有一个方法定义而已,所以拿到runtime.Object之后,各种转换和判断是少不了的
  • 由此可见核心就是schema.ObjectKind,因为runtime.Object定义的方法要返回它,而它定义的方法又被metav1.TypeMeta实现

了解schema.ObjectKind

  • 先看源码,ObjectKind的源码很简单,就是数据结构GroupVersionKind的获取和设置方法(Java bean的既视感)
type ObjectKind interface {
	// SetGroupVersionKind sets or clears the intended serialized kind of an object. Passing kind nil
	// should clear the current setting.
	SetGroupVersionKind(kind GroupVersionKind)
	// GroupVersionKind returns the stored group, version, and kind of an object, or an empty struct
	// if the object does not expose or provide these fields.
	GroupVersionKind() GroupVersionKind
}
  • 再回顾前文,如下图黄色箭头2,单个对象的数据结构中嵌入了metav1.TypeMeta,这也就说明,对象自已有GroupVersionKind和SetGroupVersionKind这两个方法,我们在程序中可以通过GroupVersionKind方法获取对象的GroupVersionKind信息,也能用SetGroupVersionKind方法设置对象的GroupVersionKind信息 Kubernetes对象深入学习之二:细说schema.ObjectKind

  • 上面反复提到的GroupVersionKind是什么?打开源码一看,原来是基础知识,这是对kubernetes中每一种资源类别的定义

// GroupVersionKind unambiguously identifies a kind.  It doesn't anonymously include GroupVersion
// to avoid automatic coercion.  It doesn't use a GroupVersion to avoid custom marshalling
type GroupVersionKind struct {
	Group   string
	Version string
	Kind    string
}
  • 关于GroupVersionKind的更多信息,请参考《Kubernetes的Group、Version、Resource学习小记》,里面有详细的介绍,这里选一副有代表性的图来说明,如下所示,Group、Version、Kind一目了然 Kubernetes对象深入学习之二:细说schema.ObjectKind
  • 回到TypeMeta源码,看看和GroupVersionKind有关,先看Set方法,可见是用TypeMeta对象的两个字段来保存的,group和version两个字段被拼接到了一起,成为新的APIVersion字段
func (obj *TypeMeta) SetGroupVersionKind(gvk schema.GroupVersionKind) {
	obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
}
  • 再看Get方法,里面会将obj.APIVersion拆分,还原成Group和Version,再重新组装成GroupVersionKind对象返回
func (obj *TypeMeta) GroupVersionKind() schema.GroupVersionKind {
	return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
}
  • 看过上述代码,发现都在操作TypeMeta的APIVersion和Kind字段,那就去看看TypeMeta这个数据结构,如下所示,只有这两个字段
type TypeMeta struct {
	// Kind is a string value representing the REST resource this object represents.
	// Servers may infer this from the endpoint the client submits requests to.
	// Cannot be updated.
	// In CamelCase.
	// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
	// +optional
	Kind string `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"`

	// APIVersion defines the versioned schema of this representation of an object.
	// Servers should convert recognized schemas to the latest internal value, and
	// may reject unrecognized values.
	// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
	// +optional
	APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,2,opt,name=apiVersion"`
}

TypeMeta的重要性

  • 既然每种资源都必须有类型,那么TypeMeta的重要性就不用多说了,在kubernetes源码中暴力搜索一下,先排除单元测试文件,可见有多处会用到,打开一段自动生成的代码,可见复制对象时首先会复制TypeMeta Kubernetes对象深入学习之二:细说schema.ObjectKind
  • 然后再随便打开一个单元测试的代码,如下图黄色箭头,这就很清楚了,再回想之前写的那些代码,原来这个TypeMeta我们一直在用,是如此的属性,每次创建对象的第一步就是TypeMeta对象: Kubernetes对象深入学习之二:细说schema.ObjectKind
  • 至此,关于对象类型和版本的接口schema.ObjectKind就学习完成了,这是平时用client-go开发时最常见的内容了,下一篇会看到ObjectMeta,那里丰富的内容在等着咱们