kubernetes-sigs / controller-tools

Tools to use with the controller-runtime libraries
Apache License 2.0
736 stars 420 forks source link

Controller-gen conflicting types in allOf branches in schema: string vs object #577

Closed aybansal-sudo closed 3 years ago

aybansal-sudo commented 3 years ago

Hi, I use kubebuilder to build api using crds.

my types.go file contains: // +kubebuilder:pruning:PreserveUnknownFields // +kubebuilder:validation:Type=object RequestPayload json.RawMessage json:"requestPayload"

when running the following command to generate crd: controller-gen "crd:crdVersions={v1},trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases I am receiving the following error. conflicting types in allOf branches in schema: string vs object

use case here is : CR Spec will contain a type(string) and request Payload field (arbitrary JObject). Struct of req payload is unknown and depends on the type field.

I tried using runtime.RawExtension, although I was able to submit the cr, spec.RequestPayload field was empty object. Any ideas on how to fix this?

yann-soubeyrand commented 3 years ago

Hi @aybansal-sudo, did you find the solution to your problem?

yann-soubeyrand commented 3 years ago

The solution which worked for me is to use version 0.6.2 of controller-tools (I changed the version in the Makefile generated by kubebuilder and removed bin/controller-gen) and wrap my json.RawMessage field in a struct like this:

type RequestPayload struct {
    //+kubebuilder:validation:Type=object
    //+kubebuilder:validation:Schemaless
    //+kubebuilder:pruning:PreserveUnknownFields
    json.RawMessage `json:",inline"`
}