Kubernetes crd failed to be created using go-client interface

5/22/2018

I created a Kubernetes CRD following the example at https://github.com/kubernetes/sample-controller.

My controller works fine, and I can listen on the create/update/delete events of my CRD. Until I tried to create an object using go-client interface.

This is my 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"`
}

I can create the CRD definition and create object using kubectl without any problems. But I got failure when I use following code to create the object.

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)

But I got following error:

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

I take a look at other standard types, I don't see if they implemented such interface. I searched on google, but not getting any luck.

Any ideas? Please help. BTW, I am running on minikube.

-- Toby Xu
kubernetes

2 Answers

5/23/2018

For most common types and for simple types marshalling works out of the box. In case of more complex structure, you may need to implement marshalling interface manually.

You may try to comment part of the MyEndpoint structure to find out what exactly caused the problem.

-- VAS
Source: StackOverflow

11/1/2018

This error is occurred when your client epClient trying to marshal the MyEndpoint object to protobuf. This is because of your rest client config. Try setting Content Type is "application/json".

If you are using below code to generate config, then change the content type.

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())
}
-- nightfury1204
Source: StackOverflow