【问题标题】:Kubernetes crd failed to be created using go-client interface使用 go-client 接口创建 Kubernetes crd 失败
【发布时间】:2018-11-01 20:39:37
【问题描述】:

我按照https://github.com/kubernetes/sample-controller 的示例创建了一个 Kubernetes CRD。

我的控制器工作正常,我可以监听 CRD 的创建/更新/删除事件。直到我尝试使用 go-client 接口创建一个对象。

这是我的 CRD。

type MyEndpoint struct {
    metav1.TypeMeta   `json:",inline"`

    // Standard object's metadata.
    // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
    // +optional
    metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
}

我可以毫无问题地使用 kubectl 创建 CRD 定义和创建对象。但是当我使用以下代码创建对象时失败了。

myepDeploy := &crdv1.MyEndpoint{
    TypeMeta: metav1.TypeMeta{
        Kind:       "MyEndpoint",
        APIVersion: "mydom.k8s.io/v1",
    },
    ObjectMeta: metav1.ObjectMeta{
        Name: podName,
        Labels: map[string]string{
            "serviceName": serviceName,
            "nodeIP": nodeName,
            "port": "5000"
        },
    },
}
epClient := myclientset.MycontrollerV1().MyEndpoints("default")
epClient.Create(myepDeploy)

但我收到以下错误:

object *v1.MyEndpoint does not implement the protobuf marshalling interface and cannot be encoded to a protobuf message

我看看其他标准类型,我不知道他们是否实现了这样的接口。我在谷歌上搜索,但没有得到任何运气。

有什么想法吗?请帮忙。顺便说一句,我在 minikube 上运行。

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    对于大多数常见类型和简单类型,编组工作开箱即用。如果结构更复杂,可能需要手动实现编组接口。

    您可以尝试对 MyEndpoint 结构的一部分进行注释,以找出导致问题的确切原因。

    【讨论】:

      【解决方案2】:

      当您的客户端 epClient 尝试将 MyEndpoint 对象编组到 protobuf 时,会发生此错误。这是因为您的 rest 客户端配置。尝试将 Content Type 设置为 "application/json"

      如果您使用以下代码生成配置,请更改内容类型。

      cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
      if err != nil {
          glog.Fatalf("Error building kubeconfig: %s", err.Error())
      }
      
      cfg.ContentType = "application/json"
      
      kubeClient, err := kubernetes.NewForConfig(cfg)
      if err != nil {
          glog.Fatalf("Error building kubernetes clientset: %s", err.Error())
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-01
        • 2018-11-03
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-29
        • 2019-02-12
        相关资源
        最近更新 更多