douyiji3919 2018-02-19 07:34
浏览 186
已采纳

如何在Kubernetes代码中满足&deployment满足类型runtime.Object?

In kubectl/run.go in Kubernetes code, the Generate function has a result list of these two types:

runtime.Object, error

The last line of the function is:

return &deployment, nil

runtime is imported:

k8s.io/apimachinery/pkg/runtime

I got runtime by running go get on that import statement, and Object is defined in interfaces.go:

type Object interface {
    GetObjectKind() schema.ObjectKind
    DeepCopyObject() Object
}

(And I found the same code on the web here.)

The address operator creates a pointer... more specifically, the Go spec states:

For an operand x of type T, the address operation &x generates a pointer of type *T to x.

and pointers have a type distinct from their base type:

A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer.

How does &deployment satisfy the runtime.Object type?

My best guess so far is that deployment implements the runtime.Object interface, and mapping &deployment to runtime.Object satisfies this rule of assignability:

T is an interface type and x implements T.

and that a return statement statement mapping to a result list type is equivalent to assignment in this respect. Is this correct? If not, is there another part of the specification or documentation that explains it?

  • 写回答

1条回答 默认 最新

  • dongyu2047 2018-02-19 07:56
    关注

    deployment is a local variable, its declaration:

    deployment := extensionsv1beta1.Deployment{
        // ...
    }
    

    Where extensionsv1beta1 from the imports:

    import (
        // ...
        extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
        // ...
    )
    

    Doc of extensionsv1beta1.Deployment. Its definition is:

    type Deployment struct {
        metav1.TypeMeta `json:",inline"`
        // ...other fields...
    }
    

    It embeds metav1.TypeMeta, which has a method GetObjectKind() method with pointer receiver. This means a pointer to Deployment also has this method, because Spec: Struct types:

    Given a struct type S and a defined type T, promoted methods are included in the method set of the struct as follows:

    • If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.

    And Deployment has a "direct" DeepCopyObject() method, again with pointer receiver. So the method set of *Deployment contains this method.

    And last quoting Spec: Interface types:

    An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface.

    So this means the method set of *Deployment has all the methods defined by Object, or in other words: the method set of *Deployment is a superset of the method set of Object, so *Deployment implements Object.

    deployment is of type extensionsv1beta1.Deployment, which means &deployment is of type *extensionsv1beta1.Deployment, which we showed above that it implements Object; so the value &deployment can be assigned to or be stored in a variable of type Object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发