在kubernetes代码中,& deployment如何满足类型runtime.Object? [英] How can &deployment satisfy type runtime.Object in kubernetes code?

查看:369
本文介绍了在kubernetes代码中,& deployment如何满足类型runtime.Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

kubectl /在Kubernetes代码中运行.go 生成函数有一个这两种类型的结果列表:

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

runtime.Object, error

该函数的最后一行是:

return &deployment, nil

运行时已导入:

k8s.io/apimachinery/pkg/runtime

我有 runtime 在该import语句上运行 go get ,并在 interfaces.go <中定义Object / code>:

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.)

地址运算符创建指针...更具体地说,Go规范说明:

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


对于类型为T的操作数x,地址操作& x生成类型为* T的指针x。

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

和指针的类型与其基类型不同


指针类型表示指向变量的所有指针的集合给定类型,称为指针的基本类型。

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

& deployment 满足 runtime.Object 类型?

到目前为止,我最好的猜测是 deployment 实现 runtime.Object 接口,并将& deployment 映射到 runtime.Object 满足这个可转让性规则

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是接口类型,x实现T.

T is an interface type and x implements T.

并且映射到结果列表类型的return语句等效于此方面的赋值。它是否正确?如果没有,是否有其他部分说明或文档解释它?

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?

推荐答案

部署是一个局部变量,它的声明:

deployment is a local variable, its declaration:

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

其中 extensionsv1beta1 来自进口:

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

extensionsv1beta1.Deployment 的文件。它的定义是:

Doc of extensionsv1beta1.Deployment. Its definition is:

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

嵌入 metav1.TypeMeta ,它有一个方法 带指针接收器的GetObjectKind() 方法。这意味着指向部署的指针也有此方法,因为规范:结构类型:

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:


给定结构类型 S 和定义的类型 T ,提升的方法包含在结构的方法集中,如下所示:

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


  • 如果 S 包含嵌入字段 T ,则方法集 S * S 都包含提升方法,接收方 T * S 的方法集还包括带接收方 * T 的推广方法。

  • 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.

部署有直接< A HREF = https://godoc.org/k8s.io/api/extensions/v1beta1#Deployment.DeepCopyObject 相对= nofollow noreferrer> DeepCopyObject() 方法,再次使用指针接收器。所以方法集 *部署包含此方法。

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

最后引用规范:接口类型:


接口类型指定方法集称为 interface 接口类型的变量可以存储任何类型的值,其方法集是接口的任何超集。这样的类型被称为实现接口

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.

所以这意味着 * Deployment 的方法集具有 Object ,或换句话说: * Deployment 的方法集是<$ c $方法集的超集c>对象,所以 *部署实现对象

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.

部署的类型为 extensionsv1beta1.Deployment ,这意味着 & deployment 的类型为 * extensionsv1beta1.Deployment ,我们在上面显示它实现了 Object ;所以可以将值& deployment 分配给或存储在 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.

这篇关于在kubernetes代码中,&amp; deployment如何满足类型runtime.Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆