How to define order of fields in CRD

10/26/2021

I have a CRD which contains several fields. like:

            properties:
                fieldA:
                    type: boolean
                fieldB:
                    type: string
                    nullable: false
                fieldC:
                    type: integer
                fieldD:
                    type: string
                    nullable: false

Whenever I create a CR from it

fieldA: true
fieldB: "blabla"
fieldC: 23
fieldD: "blabla2"

then run kubectl get myCR -o yaml, the field order seems random:

fieldD: "blabla2"
fieldB: "blabla"
fieldC: 23
fieldA: true

Is there a way to keep the order defined either in CRD or CR?

-- beatrice
kubernetes

1 Answer

10/26/2021

No.

The fields are part of a key-value-map. And a map stores no information about the order.
You can change it to a list (which ensure the correct order), but this will make things really hard (for you and users of your CRD).

-- akop
Source: StackOverflow