无法为 Kubernetes API 类型声明声明 Kind 类型。

huangapple go评论90阅读模式
英文:

Unable to declare Kind type for Kubernetes API Type Declarations

问题

我相对于golang还比较新,需要一些帮助指引正确的方向。

我正在尝试声明一个新的Deployment类型。

我的导入部分如下:

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

当我尝试创建一个Deployment对象时,代码如下:

  test := v1beta1.Deployment{
    Spec: v1beta1.DeploymentSpec{
      Template: v1.PodTemplateSpec{
        Spec: v1.PodSpec{
          Containers: []v1.Container{{
            Name:  "test",
            Image: "image_url",
          },
          },
        },
      },
    },
  }

这段代码是可以工作的,但是返回的Deployment对象没有一个必要的Kind字段来标识该对象。

根据https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/extensions/types.go#L162

有一个嵌入的metav1.TypeMeta,其中包含了我需要的Kind对象。(参考:https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/types.go#L38)

我尝试在结构体字面量中声明metav1.TypeMeta,代码如下:

  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",
          },
          },
        },
      },
    },
  }

但是我得到了一个错误信息:

unknown field '"k8s.io/apimachinery/pkg/apis/meta/v1".TypeMeta' in struct literal of type v1beta1.Deployment

我怀疑这是因为在Deployment结构体中声明的metav1.TypeMeta是一个未导出的字段。

我应该如何声明Kind?

英文:

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?

答案1

得分: 2

当使用嵌入式结构时,键通常是类型名称,不包括包名。您可以像这样声明您的TypeMeta

test := v1beta1.Deployment{
  TypeMeta: metav1.TypeMeta{
    APIVersion: "apps/v1beta1",
    Kind:       "Deployment",
  },
}

然而,手动设置任何Kubernetes API对象上的TypeMeta通常只在您计划自己持久化这些对象时才需要(例如,生成YAML文件)。

当使用Kubernetes客户端API(例如,使用k8s.io/client-go包)与API服务器通信时,您不需要TypeMeta属性,因为所有API操作都是强类型的,所有元数据都可以安全地推断出来。毕竟,v1beta1.Deployment结构的API版本和类型对于客户端库来说是明显的(并且确实如此)。

英文:

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.

huangapple
  • 本文由 发表于 2017年4月18日 11:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/43462908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定