Operator-sdk: 'generate openapi' - All fields are set as required in the CRD when all fields are optional in the Go types

Created on 27 Jun 2019  路  6Comments  路  Source: operator-framework/operator-sdk

Bug Report

For the Jaeger Operator, pretty much all fields except the CR's ObjectMeta/Name are optional. When running the OpenAPI generator, we see that all fields are in the required list in the CRD, even though they are all tagged with // +optional above their types.

We see that the optional tag is indeed working, because we see the zz_generated.openapi.go reflect the change: when a field is not tagged as optional, it's included in the Required list.

We also noticed that when all fields are optional, the Required property in the SchemaProps type is omitted.

Environment

  • operator-sdk version: v0.8.1, commit: 33b3bfe10176f8647f5354516fff29dea42b6342
  • go version: go1.12.5 linux/amd64
  • Kubernetes version information:
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.2", GitCommit:"66049e3b21efe110454d67df4fa62b08ea79a19b", GitTreeState:"clean", BuildDate:"2019-05-16T16:14:56Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
  • Kubernetes cluster kind: minikube

  • Are you writing your operator in ~ansible, helm~, or go?

Additional context
This can be reproduced by running make generate in the following Jaeger Operator branch:
https://github.com/jpkrohling/jaeger-operator/tree/466-ValidateCR

blocked kinbug

Most helpful comment

Right now the crd generator requires a field to have both the +optional comment tag plus json:"<field-name>,omitempty" to not mark a field as required in the validation block.

However this should be fixed in controller-tools ~v0.1.11~ master with the markers
// +kubebuilder:validation:[Required/Optional].
See https://github.com/kubernetes-sigs/controller-tools/issues/137 and https://github.com/kubernetes-sigs/controller-tools/pull/211

~Going to update to controller-tools v0.1.11 to try and fix this.~

Edit: The above fix isn't in v0.1.11. It's most likely going to come about in v0.2.0 which should be released soon.
Will try to bump the controller-tools dependency to the latest master to see if we don't have any breaking changes in the CRD generator.

@estroz Yet another +kubebuilder CRD generation tag that needs to be documented upstream.
ref: https://github.com/kubernetes-sigs/kubebuilder/issues/815

All 6 comments

Right now the crd generator requires a field to have both the +optional comment tag plus json:"<field-name>,omitempty" to not mark a field as required in the validation block.

However this should be fixed in controller-tools ~v0.1.11~ master with the markers
// +kubebuilder:validation:[Required/Optional].
See https://github.com/kubernetes-sigs/controller-tools/issues/137 and https://github.com/kubernetes-sigs/controller-tools/pull/211

~Going to update to controller-tools v0.1.11 to try and fix this.~

Edit: The above fix isn't in v0.1.11. It's most likely going to come about in v0.2.0 which should be released soon.
Will try to bump the controller-tools dependency to the latest master to see if we don't have any breaking changes in the CRD generator.

@estroz Yet another +kubebuilder CRD generation tag that needs to be documented upstream.
ref: https://github.com/kubernetes-sigs/kubebuilder/issues/815

Waiting on the upgrade to controller-tools v0.2.0 to get this fixed.
Latest upstream release is currently v0.2.0-beta.2.
Related: https://github.com/operator-framework/operator-sdk/pull/1512

I can confirm that adding omitempty is indeed a workaround for this.

is there a way to hide a field from spec by specifying any annotation?

Without the omitempty I was unable to use the +optional to remove the required Spec fields.

I also attempted to solely use the kube builder optional validation, however, it was not supported as it expected a key=value

Any suggestions to utilise the optional kube builder validation flag?

Hi @xUnholy,

This issue is closed. If you are needing some help, could you please one a new and provide the required information for we are able to help you with?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielsig727 picture danielsig727  路  4Comments

hasbro17 picture hasbro17  路  3Comments

lsalazar1 picture lsalazar1  路  4Comments

magescher picture magescher  路  3Comments

flickerfly picture flickerfly  路  5Comments