Kubernetes Runtime在vendor/k8s.io/apimachinery/pkg/runtime中实现,它提供了通用资源类型runtime.Object。
    runtime.Object是Kubernetes类型系统的基石。
    资源对象(Resource Object)都有一个共同的结构runtime.Object。
    runtime.Object被设计为接口,作为资源对象的通用资源对象。

    1. Pod资源对象可以和runtime.Object相互转化 Deployment资源对象可以和runtime.Object相互转化
    2. --------------- ------------------ --------------------
    3. | &core.Pod{} | <----> | runtime.Object | <------> | &apps.Deployment |
    4. --------------- ------------------ --------------------

    runtime.Object结构如下:
    代码路径:vendor/k8s.io/apimachinery/pkg/runtime/interfaces.go

    1. // Object interface must be supported by all API types registered with Scheme. Since objects in a scheme are
    2. // expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
    3. // serializers to set the kind, version, and group the object is represented as. An Object may choose
    4. // to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
    5. type Object interface {
    6. GetObjectKind() schema.ObjectKind
    7. DeepCopyObject() Object
    8. }

    代码路径:vendor/k8s.io/apimachinery/pkg/runtime/schema/interfaces.go

    1. // All objects that are serialized from a Scheme encode their type information. This interface is used
    2. // by serialization to set type information from the Scheme onto the serialized version of an object.
    3. // For objects that cannot be serialized or have unique requirements, this interface may be a no-op.
    4. type ObjectKind interface {
    5. // SetGroupVersionKind sets or clears the intended serialized kind of an object. Passing kind nil
    6. // should clear the current setting.
    7. SetGroupVersionKind(kind GroupVersionKind)
    8. // GroupVersionKind returns the stored group, version, and kind of an object, or an empty struct
    9. // if the object does not expose or provide these fields.
    10. GroupVersionKind() GroupVersionKind
    11. }

    runtime.Object提供了两个方法

    • GetObjectKind() schema.ObjectKind:用于设置并返回GroupVersionKind
    • DeepCopyObject() Object:用于深拷贝当前资源对象并返回

      深拷贝将数据结构重新克隆一份,不与原始对象共享任何内容。

    如何确认一个资源队形是否可以转换为runtime.Object通用资源对象?
    需要确认资源对象是否实现了GetObjectKind和DeepCopyObject方法。
    Kubernetes的每一个资源对象都嵌入了metav1.TypeMeta类型,metav1.TypeMeta实现了GetObjectKind方法,所有资源对象都实现了该方法。
    Kubernetes的每一个资源对象都实现了DeepCodeObject方法,该方法一般被定义在zz_generated.deepcopy.go文件中。

    Kubernetes的任意资源对象可以通过runtime.Object存储他的类型,并允许深度复制操作。
    下面的代码示例将资源对象转换为runtime.Object,然后再转换回资源对象

    1. package main
    2. import (
    3. "reflect"
    4. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    5. "k8s.io/apimachinery/pkg/runtime"
    6. "k8s.io/kubernetes/pkg/apis/core"
    7. )
    8. func main() {
    9. pod := &core.Pod{
    10. TypeMeta: metav1.TypeMeta{
    11. Kind: "Pod",
    12. },
    13. ObjectMeta: metav1.ObjectMeta{
    14. Labels: map[string]string{"foo": "foo"},
    15. },
    16. }
    17. obj := runtime.Object(pod)
    18. pod2, ok := obj.(*core.Pod)
    19. if !ok {
    20. panic("unexpected")
    21. }
    22. if !reflect.DeepEqual(pod, pod2) {
    23. panic("unexpected")
    24. }
    25. }

    导入k8s.io/kubernetes依赖时,会遇到类似下面的错误

    1. k8s.io/api@v0.0.0: reading k8s.io/api/go.mod at revision v0.0.0: unknown revision v0.0.0

    使用replace命令解决

    1. replace (
    2. k8s.io/api => k8s.io/api v0.0.0-20190620084959-7cf5895f2711
    3. k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.0.0-20190620085554-14e95df34f1f
    4. k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20190612205821-1799e75a0719
    5. k8s.io/apiserver => k8s.io/apiserver v0.0.0-20190620085212-47dc9a115b18
    6. k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20190620085706-2090e6d8f84c
    7. k8s.io/client-go => k8s.io/client-go v0.0.0-20190620085101-78d2af792bab
    8. k8s.io/cloud-provider => k8s.io/cloud-provider v0.0.0-20190620090043-8301c0bda1f0
    9. k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.0.0-20190620090013-c9a0fc045dc1
    10. k8s.io/code-generator => k8s.io/code-generator v0.0.0-20190612205613-18da4a14b22b
    11. k8s.io/component-base => k8s.io/component-base v0.0.0-20190620085130-185d68e6e6ea
    12. k8s.io/cri-api => k8s.io/cri-api v0.0.0-20190531030430-6117653b35f1
    13. k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.0.0-20190620090116-299a7b270edc
    14. k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.0.0-20190620085325-f29e2b4a4f84
    15. k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.0.0-20190620085942-b7f18460b210
    16. k8s.io/kube-proxy => k8s.io/kube-proxy v0.0.0-20190620085809-589f994ddf7f
    17. k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.0.0-20190620085912-4acac5405ec6
    18. k8s.io/kubectl => k8s.io/kubectl v0.0.0-20201008135616-e95e378e5972
    19. k8s.io/kubelet => k8s.io/kubelet v0.0.0-20190620085838-f1cb295a73c9
    20. k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.0.0-20190620090156-2138f2c9de18
    21. k8s.io/metrics => k8s.io/metrics v0.0.0-20190620085625-3b22d835f165
    22. k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.0.0-20190620085408-1aef9010884e
    23. )