grafana / grafana-app-sdk

An SDK for developing apps for grafana using kubernetes-like storage and operators
Apache License 2.0
48 stars 8 forks source link

Need a way to insert x-kubernetes-preserve-unknown-fields: true into CRD #489

Open bcotton opened 5 days ago

bcotton commented 5 days ago

Gamma needs the ability to accept complex schemas in a CRD that is beyond cue's capability.

For a CRD I would like the ability to have, similar the Crossplane's XRD

                    schema:
                      properties:
                        openAPIV3Schema:
                          description: OpenAPIV3Schema is the OpenAPI v3 schema to
                            use for validation and pruning.
                          type: object
                          x-kubernetes-preserve-unknown-fields: true
                      type: object

to be emitted from cue.

I'm told that

openAPIV3Schema: {
    [string]: _
}

Might do it, but alas, no, I get

openAPIV3Schema:
  additionalProperties: {}
  type: object
IfSentient commented 4 days ago

@bcotton I can't seemto replicate this when using [string]: _, here's my test:

austin@Abra test % cat kinds/test.cue
package kinds

test: {
    kind: "Test"
    group: "test"
    apiResource: {}
    codegen: frontend: false
    codegen: backend: false
    current: "v1"
    versions: {
        "v1": {
            schema: {
                spec: {
                    openAPIV3Schema: {
                        [string]: _
                    }
                }
            }
        }
    }
}
austin@Abra test % grafana-app-sdk version
v0.25.1
austin@Abra test % grafana-app-sdk generate --crdencoding=yaml
 * Writing file pkg/generated/resource/test/v1/test_codec_gen.go
 * Writing file pkg/generated/resource/test/v1/test_object_gen.go
 * Writing file pkg/generated/resource/test/v1/test_schema_gen.go
 * Writing file definitions/test.test.ext.grafana.com.yaml
 * Writing file definitions/test-manifest.yaml
 * Writing file pkg/generated/manifest.go
austin@Abra test % cat definitions/test.test.ext.grafana.com.yaml
kind: CustomResourceDefinition
apiVersion: apiextensions.k8s.io/v1
metadata:
    name: tests.test.ext.grafana.com
spec:
    group: test.ext.grafana.com
    versions:
        - name: v1
          served: true
          storage: true
          schema:
            openAPIV3Schema:
                properties:
                    spec:
                        properties:
                            openAPIV3Schema:
                                type: object
                                x-kubernetes-preserve-unknown-fields: true
                        required:
                            - openAPIV3Schema
                        type: object
                    status:
                        properties:
                            additionalFields:
                                description: additionalFields is reserved for future use
                                type: object
                                x-kubernetes-preserve-unknown-fields: true
                            operatorStates:
                                additionalProperties:
                                    properties:
                                        descriptiveState:
                                            description: descriptiveState is an optional more descriptive state field which has no requirements on format
                                            type: string
                                        details:
                                            description: details contains any extra information that is operator-specific
                                            type: object
                                            x-kubernetes-preserve-unknown-fields: true
                                        lastEvaluation:
                                            description: lastEvaluation is the ResourceVersion last evaluated
                                            type: string
                                        state:
                                            description: |-
                                                state describes the state of the lastEvaluation.
                                                It is limited to three possible states for machine evaluation.
                                            enum:
                                                - success
                                                - in_progress
                                                - failed
                                            type: string
                                    required:
                                        - lastEvaluation
                                        - state
                                    type: object
                                description: |-
                                    operatorStates is a map of operator ID to operator state evaluations.
                                    Any operator which consumes this kind SHOULD add its state evaluation information to this field.
                                type: object
                        type: object
                        x-kubernetes-preserve-unknown-fields: true
                required:
                    - spec
                type: object
          subresources:
            status: {}
    names:
        kind: Test
        plural: tests
    scope: Namespaced

If I change

openAPIV3Schema: {
    [string]: _
}

to

openAPIV3Schema: _

Then I can replicate your issue and I get

                    spec:
                        properties:
                            openAPIV3Schema: {}
                        required:
                            - openAPIV3Schema
                        type: object

I'll look into what's going on in the second case, but can you run my test CUE and see if this works for you? And if it doesn't, please let me know the output of grafana-app-sdk version -v and your OS/version