0

I'm trying to use oneOf to validate my Kubernetes CRDs and still couldn't make it work. I have the following CRD defined.

        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                type:
                  type: string
                parameters:
                  type: array
                  items:
                    type: object
                    properties:
                      parameterName:
                        type: string
                      parameterValue:
                        type: string
                policySpec:
                  type: object
                  properties:
                    json:
                      x-kubernetes-preserve-unknown-fields: true
                      type: object
                      description: this is arbitrary JSON
                template:
                  type: string

I want to add oneOf validation to parameters, policySpec and template so the user can specify only one of those. I followed the docs related to oneOf but couldn't figure it out still.

xref for docs: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema

Also found a similar stackoverflow question with no proper answer : Properly using oneOf in a Kubernetes CRD OpenAPI schema

Any K8s folks out there who can help ? Thanks in advance

Lahiru Udayanga
  • 309
  • 4
  • 13

1 Answers1

0

Providing oneOf alongside the properties of the parent should result in what you're looking for:

openAPIV3Schema:
  type: object
  properties:
    spec:
      type: object
      oneOf:
        - required: ["parameters"]
        - required: ["policySpec"]
        - required: ["template"]
      properties:
        type:
          type: string
        parameters:
          type: array
          items:
            type: object
            properties:
              parameterName:
                type: string
              parameterValue:
                type: string
        policySpec:
          type: object
          properties:
            json:
              x-kubernetes-preserve-unknown-fields: true
              type: object
              description: this is arbitrary JSON
        template:
          type: string
Dale
  • 1
  • 3