douzi8127 2017-04-18 03:05
浏览 564

无法为Kubernetes API类型声明声明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条回答 默认 最新

  • duanlinjian5819 2017-04-18 14:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料