You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[feature]: Allow setting defaults for properties in the schema #1200
Is your feature request related to a problem? Please describe.
As far as I can tell there is currently no way to set defaults for properties in the generated OpenAPI schema. This would be extremely useful to have. In our scenario we wanted to create an operator that is compatible with kstatus. For that to work well with brand new Kubernetes resources (ie. initial deploy) you need to make sure that there is an initial status object with at least observedGeneration: 0. If the resource has no status object anything that waits on that resource will immediately report that it is done because the operator itself will never have a chance to write the initial status object. Setting a default resolves this conoundrum.
Describe the solution you would like
Ideally I'd like to be able to set an [DefaultValue(<string>)] attribute on any field that is part of the schema so that this would be outputted to the generated CRD.
Additional Context
I can work around this for now by manually modifying the CRD YAML, but this causes it to drift from the initially generated version so another generate would effectively remove the workaround. This also doesn't work well with the Aspire Hosting integration since that applies the CRD definition on the fly, but only what is generated and there is no opportunity to modify it.
@kimpenhaus Nice, good to hear that this is already being worked on. One thing to note is that while I was working on this I need to set the default on the whole status subresource. Setting only a default for status.observedGeneration to zero didn't fix it for me. But setting it on the status subresource would require setting a whole object. Not sure if that would be supported now.
@kimpenhaus I just tried the prerelease version. One thing that confused me a bit is that previously I used the System.ComponentModel.DataAnnotations set of attributes. That doesn't have a DefaultValue attribute so I had to add a using for KubeOps.Abstractions.Entities.Attributes but that namespace also includes a RequiredAttribute (for example) which is now an ambigious type. Simply removing the using System.ComponentModel.DataAnnotations line fixed that and it looks like the output hasn't really changed so I guess that worked. But I can imagine this being confusing for people.
Activity
kimpenhaus commentedon Jul 15, 2026
@jmezach yes that's absolutely true - I came across this just recently, too
I've started a branch already - addressing this shortcome
see this in particular 😃
jmezach commentedon Jul 15, 2026
@kimpenhaus Nice, good to hear that this is already being worked on. One thing to note is that while I was working on this I need to set the default on the whole
statussubresource. Setting only a default forstatus.observedGenerationto zero didn't fix it for me. But setting it on thestatussubresource would require setting a whole object. Not sure if that would be supported now.kimpenhaus commentedon Jul 15, 2026
@jmezach I've opened pr #1201 - your requirement could be solved by something like:
that should generate this:
if that fits your needs I can complete it and maybe you could test it with the create pre-version.
feat(generator): expand CRD schema metadata support (#1201)
jmezach commentedon Jul 17, 2026
@kimpenhaus I just tried the prerelease version. One thing that confused me a bit is that previously I used the
System.ComponentModel.DataAnnotationsset of attributes. That doesn't have aDefaultValueattribute so I had to add a using forKubeOps.Abstractions.Entities.Attributesbut that namespace also includes aRequiredAttribute(for example) which is now an ambigious type. Simply removing theusing System.ComponentModel.DataAnnotationsline fixed that and it looks like the output hasn't really changed so I guess that worked. But I can imagine this being confusing for people.kimpenhaus commentedon Jul 17, 2026
that's kinda funny - I wasn't even aware that
DataAnnotations.Requiredwould work 😆 guess I will add an info box to the docs.thanks for testing, feedback and pointing to this 🫶