Kubernetes crd无法使用go-client界面创建

时间:2018-05-22 10:27:00

标签: kubernetes

我按照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"`
}

我可以创建CRD定义并使用kubectl创建对象而没有任何问题。但是当我使用以下代码创建对象时,我失败了。

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上运行。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

当您的客户epClient试图将MyEndpoint对象编组为protobuf时,发生此错误。这是因为您的其余客户端配置。尝试将内容类型设置为"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())
}