kubernetescrossplane

Pass array from crossplane claim to crossplane composition


I am working on claim that will be used by about 8 services org wide, how do i pass the array of env variables to the composition. There seems to be no way of doing this

Here is an example of my claim

apiVersion: app.org.io/v1
kind: XClaim
metadata:
  name: test-app
spec:
  parameters:
    name: test-app
    envVariables:
    - variables:
        foo: bar
        name: precious
        age: 15

Here is an example of my CRD

apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: applambdas.app.org.io
  namespace: crossplane-system
spec:
    group: app.org.io
    names:
        kind: AppLambda
        plural: applambdas
    versions:
        - name: v1
          served: true
          referenceable: true
          schema:
            openAPIV3Schema:
              type: object
              properties:
                spec:
                  type: object
                  properties:
                    parameters:
                      type: object
                      properties:
                        env:
                          type: string
                        envVariables:
                          type: array
                        name:
                          type: string

    claimNames:
      kind: XClaim
      plural: xclaims

Here is an example of my composition

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: lambda
spec:
  compositeTypeRef:
    apiVersion: app.org.io/v1
    kind: AppLambda
  resources:
    - name: lambda-function
      base:
        apiVersion: lambda.aws.upbound.io/v1beta1
        kind: Function
        metadata:
          annotations:
            uptest.upbound.io/timeout: "3600"
          name: lambda
        spec:
          providerConfigRef:
            name: aws-config
          forProvider:
            handler: index.lambda_handler
            packageType: Zip
            region: eu-west-1
            role: arn:aws:iam::xxxxxx:role/crossplane-lambda-test-role
            runtime: python3.9
            s3Bucket: testappbucket-upbound-provider-test-data
            s3Key: function.zip
            timeout: 60
            environment: []
      patches:
        - fromFieldPath: spec.parameters.envVariables[variables]
          toFieldPath: spec.forProvider.environment[variables]

The spec.forProvider.environment dosen't seem to get patched, I have been on this all week, please i need help


Solution

  • In this case, the environment variables are not actually an array. You can see from the crd that variables should be the key to an object, stored underneath a single value environment array.

    spec:
      forProvider:
        environment:
        - variables:
            key: value
    

    So with some small tweaks to your definition and composition, this should be possible:

    apiVersion: apiextensions.crossplane.io/v1
    kind: CompositeResourceDefinition
    
    ...
        envVariables:
          type: object
          additionalProperties:
            type: string
    ...
    
    apiVersion: apiextensions.crossplane.io/v1
    kind: Composition
    ...
        patches:
          - fromFieldPath: spec.parameters.envVariables
            toFieldPath: spec.forProvider.environment[0].variables
    ...
    

    This will let you create a claim like this:

    apiVersion: app.org.io/v1
    kind: XClaim
    metadata:
      name: test-app
    spec:
      parameters:
        name: test-app
        envVariables:
          foo: bar
          name: precious
          age: "15"
    

    Resulting in a function with the appropriate environment variables set. AWS Console Showing Environment Variables

    Note: Environment Variable values must be strings, which is the reason for the validation in the schema and the quotes in the claim.