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 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?