Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+kubebuilder:validation:Required marker not working #599

Closed
parthyadav3105 opened this issue Aug 23, 2021 · 1 comment
Closed

+kubebuilder:validation:Required marker not working #599

parthyadav3105 opened this issue Aug 23, 2021 · 1 comment

Comments

@parthyadav3105
Copy link

What happened
On using +kubebuilder:validation:Required marker on fields(or packages both) does not adds that field to required list in the generated crd output.

Example:

type ImageSpec struct {
	// +kubebuilder:validation:Required
	Foo string `json:"foo,omitempty"`
}

output crd:


---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.4.1
  creationTimestamp: null
  name: images.hello.io.kupenstack.io
spec:
  group: hello.io.kupenstack.io
  names:
    kind: Image
    listKind: ImageList
    plural: images
    singular: image
  scope: Namespaced
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        description: Image is the Schema for the images API
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            description: ImageSpec defines the desired state of Image
            properties:
              foo:
                type: string
            type: object
          status:
            description: ImageStatus defines the observed state of Image
            type: object
        type: object
    served: true
    storage: true
    subresources:
      status: {}
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

What you expected to happen:
According to reference documentation foo field in above crd should be a required field as shown bellow:


---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.4.1
  creationTimestamp: null
  name: images.hello.io.kupenstack.io
spec:
  group: hello.io.kupenstack.io
  names:
    kind: Image
    listKind: ImageList
    plural: images
    singular: image
  scope: Namespaced
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        description: Image is the Schema for the images API
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            description: ImageSpec defines the desired state of Image
            properties:
              foo:
                type: string
            type: object
            required:
            - foo
          status:
            description: ImageStatus defines the observed state of Image
            type: object
        type: object
        required:
        - spec
    served: true
    storage: true
    subresources:
      status: {}
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

Environment:
Controller-gen version: v0.4.1
Kubernetes version: Server Version: v1.21.4

@parthyadav3105
Copy link
Author

Update:
The omitempty tag in Foo string `json:"foo,omitempty"` had more precedence than the kubebuilder marker +kubebuilder:validation:Required therefore making the resultant generated crd field to be optional.

The +kubebuilder:validation:Required marker does works as desired when omitempty is not used.

I am not able to recall if docs(https://book.kubebuilder.io) mentions it, but I hope it does.

With that I mark this issue to be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant