3

While defining a K8 CRD, I need a flexibility to pass any key/value pairs as input while submitting a resource object. https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer

From the link above, if you see properties can only hold cronSpec, image and/or replicas. Can it be free-form? Meaning I can pass any key/value pairs and in my code I get a collection (may be a map) that can contain key/value pairs.

https://stackoverflow.com/users/14044/mike-bryant I tried with this CRD:

schema:
    openAPIV3Schema:
      type: object
      properties:
        apiVersion:
          type: string
        spec:
          type: object
          properties:
            appProperties:
              type: object
              properties:
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      key:
                        type: string
                      value:
                        type: string

with custom object having input like this:

messages:
      - key: "server1"
        value: "ping failed"
      - key: "server2"
        value: "abort"
      - key: "server3"
        value: "succes"

but when I patch the crd with some update status, k8 fails with below error:

kind=Status, message=the server rejected our request due to an error in our request, metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}).:
colossal
  • 529
  • 1
  • 5
  • 22
  • When exactly do you see the error? While deploying? I've created a CRD with your properties and managed to configure it with multiple `key/value` pairs. – kool Nov 16 '20 at 16:29
  • it happens when I try to patch the CustomResource object using this library inside code: https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/Patchable.java#L20 – colossal Nov 16 '20 at 19:41

2 Answers2

0

I think you can use additionalProperties for this.

x-kubernetes-preserve-unknown-fields might also be useful: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#controlling-pruning

Mike Bryant
  • 1,072
  • 9
  • 11
  • I updated my question with what I have tried and it seems like it is working. What I am not sure is that do I still have to use `additionalProperties` and `x-kubernetes-preserve-unknown-fields` ? if yes, do you have running example that you can share please. – colossal Nov 16 '20 at 06:34
0

We can use additionalProperties with type: string for this.

additionalProperties:
    type: string

For example, I'm adding customSelector property to the spec. customSelector is of type object (a map) with strings for key/values.

spec:
    properties:
      customSelector:
        additionalProperties:
          type: string
        description: Free form key value pairs
        type: object
    type: object
nakamume
  • 660
  • 6
  • 11