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 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{
// ...
}
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
Sand a defined typeT, promoted methods are included in the method set of the struct as follows:
- If
Scontains an embedded fieldT, the method sets ofSand*Sboth include promoted methods with receiverT. The method set of*Salso 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With