I'm relatively new to golang and need some help pointing to the right direction.
I'm trying to declare a new Deployment
type.
My imports look like:
import (
"encoding/json"
"fmt"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)
When I try to create a Deployment Object like:
test := v1beta1.Deployment{
Spec: v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: "test",
Image: "image_url",
},
},
},
},
},
}
It works, but the Deployment Object that returns doesn't have a Kind
which is necessary to identify the object.
According to https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/extensions/types.go#L162
There's an embedded metav1.TypeMeta
which has the Kind
object that I need. (For reference: https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/types.go#L38)
I tried declaring metav1.TypeMeta
in the struct literal like:
test := v1beta1.Deployment{
metav1.TypeMeta: metav1.TypeMeta{Kind: "Deployment"}
Spec: v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: "test",
Image: "image_url",
},
},
},
},
},
}
But I get a
unknown field '"k8s.io/apimachinery/pkg/apis/meta/v1".TypeMeta' in struct literal of type v1beta1.Deployment
I suspect it is due to metav1.TypeMeta
declaration in the Deployment
struct is an unexported field.
How should I declare Kind?
When using an embedded struct, the key is usually the type name without the package. You can declare your TypeMeta
like this:
test := v1beta1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1beta1",
Kind: "Deployment",
},
}
However, manually setting the TypeMeta
on any Kubernetes API object is usually only necessary if you plan to persist these objects yourself (for example, to generate YAML files).
When using the Kubernetes client API (for example, using the k8s.io/client-go package) to talk to an API server, you will not need the TypeMeta
property, since all API operations are strongly typed anyway and all metadata can safely be inferred. After all, the API version and kind of a v1beta1.Deployment
struct should be (and are, to the client library) obvious.