将数组从Crossplane声明传递到Crossplane组合。

huangapple go评论87阅读模式
英文:

Pass array from crossplane claim to crossplane composition

问题

我正在处理一个将被约8个服务机构广泛使用的声明,我该如何将环境变量数组传递给组合?似乎没有办法做到这一点。

这是我的声明示例:

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

这是我的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

这是我的组合示例:

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]

spec.forProvider.environment 似乎没有被修补,我已经在这上面花了整整一周,请帮帮我。

英文:

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

答案1

得分: 1

在这种情况下,环境变量实际上不是一个数组。你可以从 crd 中看到,variables 应该是一个对象的键,存储在一个名为 environment 的单个值数组下面。

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

所以通过对你的定义和组合进行一些小的调整,这是可能的:

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
...

这将允许你创建这样的声明:

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

从而得到一个设置了适当环境变量的函数。
AWS控制台显示环境变量

注意:环境变量的值必须是字符串,这是模式验证和声明中引号的原因。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月10日 20:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定