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

Cannot use an empty map with kubebuilder:default marker #550

Closed
Zeroji opened this issue Mar 25, 2021 · 9 comments · Fixed by #863
Closed

Cannot use an empty map with kubebuilder:default marker #550

Zeroji opened this issue Mar 25, 2021 · 9 comments · Fixed by #863
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@Zeroji
Copy link

Zeroji commented Mar 25, 2021

Hi,
I'm using kubebuilder to generate a CRD from the following type definition (minimized):

type CustomSpec struct {
    // +optional
    // +kubebuilder:default={}
    Foo FooType `json:"foo,omitempty"`
}

type FooType struct {
    // +optional
    // +kubebuilder:default=false
    Bar bool `json:"bar,omitempty"`

    // +optional
    // +kubebuilder:default=1
    Baz int `json:"baz,omitempty"`

    // +optional
    // +kubebuilder:default=QUX
    Qux string `json:"qux,omitempty"`
}

This gives the following CRD (comments added manually):

# This is .spec.versions[0].schema.openAPIV3Schema.properties.spec
# Indentation has been brought down
spec:
  properties:
    foo:
      # The following line is not present, but expected
      # default: {}
      properties:
        bar:
          default: false
          type: boolean
        baz:
          default: 1
          type: integer
        qux:
          default: QUX
          type: string
      type: object
  type: object

The issue I'm facing is that if I apply an empty spec for my custom resource, with spec: {} then foo only contains the golang type defaults, e.g. baz is 0 and qux is an empty string.
If I use spec: {foo: {}} then foo is populated with all the correct default values for its properties, however I don't want to force my clients to define foo.

If I modify the CRD manually (after controller-gen, but before kustomize) to add default: {} inside the CRD, everything behaves as expected, and an empty spec gives a correct CR when using kubectl get:

# kubectl get custom/object -o json | jq .spec
{
  "foo": {
    "bar": false,
    "baz": 1,
    "qux": "QUX"
  }
}

The current workaround I've found is to use the following annotation (which is non-ideal):

// +kubebuilder:default={baz: 1}
Foo FooType `json:"foo,omitempty"`

This has the effect of defining foo with the correct defaults, but foo.baz takes the value defined in foo's default over baz's default, so this can override default values on properties if the defaults are changed.

It seems the main issue is that controller-gen parses {} as an empty slice, which json.Marshal then marshalls as null, which gets ignored when writing the CRD. Here's the code that scans the slice, it appears this edge case was considered but not implemented yet:
https://github.com/kubernetes-sigs/controller-tools/blob/master/pkg/markers/parse.go#L297

Hopefully this is fixable without breaking stuff, or maybe a cleaner workaround can be found 🙂

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 23, 2021
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Jul 23, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@MaxThom
Copy link

MaxThom commented Aug 10, 2022

/reopen

@k8s-ci-robot
Copy link
Contributor

@MaxThom: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@MaxThom
Copy link

MaxThom commented Aug 10, 2022

Im facing the same exact issue.

@Danil-Grigorev
Copy link
Member

/reopen

@k8s-ci-robot
Copy link
Contributor

@Danil-Grigorev: Reopened this issue.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants