MarshalYAML with Client Go resources

11/15/2019

I'm writing a k8s operator with a CRD. Part of that resource's status field is an enum value, using iota like so:

type Enum int

const (
    Enum1 = iota + 1
    Enum2
    Enum3
    ...
)

I have a function attached to the enum that converts it to a string representation. I want that string representation to be used as the value when client go marshals the enum into the status yaml.

However, when I implement MarshalYAML (exactly as I do in this playground: https://play.golang.org/p/qtfFNoOQ-Dz), client go returns an error:

status.test.phase in body must be of type integer: "string"

(status.test.phase is the location of the enum)

Without the function, it marshals correctly, just with the integer value instead.

What is different, under the hood, between gopkg.in/yaml.v2 in the playground (which works fine) and client go's marshaling system?

-- porgull
client-go
go
kubernetes
yaml

1 Answer

11/18/2019

@MarufTuhin had the right idea - looks like openapi/operator-sdk generate openapi was generating a CRD that expected an integer due to the type of the enum being int.

You can fix this for client-go by adding this kubebuilder comment directly above the enum in the struct (see reference):

// +kubebuilder:validation:Type=string
Enum Enum `json:"enum"`

or by just manually editing the crd generated like so

enum:
  type: string
-- porgull
Source: StackOverflow