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.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?