DEV Community

Brett Tofel
Brett Tofel

Posted on

5

How to AND NOT something in a CRD

This advice is valid for sure if you're working with a Kubernetes CRD with version like this:

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition

It might be valid for other versions too.

If you want to create a structural CRD, and you want to require something not be there when some other value is set one way or another, here's an example.

                            prometheus:
                              type: object
                              oneOf:
                              - required:
                                - enabled
                                - uri
                                properties:
                                  enabled:
                                    enum:
                                    - true
                              - properties:
                                  enabled:
                                    enum:
                                    - false
                                allOf:
                                - not:
                                    required:
                                      - uri
                              properties:
                                enabled:
                                  description: Whether or not to enable the Presto-Prometheus connector.
                                  type: boolean
                                uri:
                                  description: URI for Prometheus.
                                  type: string
                                  format: uri
                                chunkSizeDuration:
                                  description: Default size of each Prometheus query chunk.

So that means:

We require oneOf the subschemas to be fulfilled. Either enabled is true in which case we require uri be defined. Or, enabled is false in which case, uri is not required. The extra allOf in there, in this case, is simply a way to allow for the not in that 2nd subschema. And just highlight what is meant by subschemas. oneOf starts a list of them. So the first is:

                              - required:
                                - enabled
                                - uri
                                properties:
                                  enabled:
                                    enum:
                                    - true

and the second is:

                              - properties:
                                  enabled:
                                    enum:
                                    - false
                                allOf:
                                - not:
                                    required:
                                      - uri

And to be structural you must list all the properties again, and you can include any items you're not setting requirements on. Which is the rest of the yaml above.

Happy computing.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay