dotnet-operator-sdk icon indicating copy to clipboard operation
dotnet-operator-sdk copied to clipboard

[bug]: Cannot specify the spec as being required

Open jamescooke-xyz opened this issue 7 months ago • 3 comments

Describe the bug

If I have required properties in my spec, adding the below will validate:

apiVersion: mycrd.io/v1
kind: MyResource
metadata:
  name: example
  namespace: default
spec: {}

But adding this will not:

apiVersion: mycrd.io/v1
kind: MyResource
metadata:
  name: example
  namespace: default
spec:

We need to be able to specify that the spec is required. Either by allowing us to overriding it and explicitly setting it as [Required], or by convention (if a child property is required).

To reproduce

Any CRD created using the Transpiler won't have the spec marked as required.

Expected behavior

An ability to mark the spec marked as required.

Screenshots

No response

Additional Context

No response

jamescooke-xyz avatar May 15 '25 09:05 jamescooke-xyz

If I remember correctly, spec is required by default. At least, back a few versions, when you tried to add "spec" to the required list, the Kubernetes API was complaining about top level required fields are not allowed.

Can you try to add them manually in the generated CRD and install it into a cluster? as far as I know, this should fail.

buehler avatar May 22 '25 12:05 buehler

addition: also you can just use an abstract field in your c# base class, making all inheriting objects implmement a spec.

buehler avatar May 22 '25 12:05 buehler

Spec isn't required by default. If I create a very basic CRD then try and import the following:

apiVersion: mycrd.io/v1
kind: MyResource
metadata:
  name: example
  namespace: default

it will allow the resource to be imported.

If I manually change the crd to specify the spec is required:

        properties: <removed for brevity>
        type: object
        required:
          - spec
    served: true
    storage: true
    subresources:
      status: {}

then kubernetes will not import the 'MyResource' yaml above.

This is what I'm trying to do, but from looking at the Transpiler code, this will never be put out, even if I create an abstract class with spec being required because of the way it is written.

jamescooke-xyz avatar May 23 '25 13:05 jamescooke-xyz