环境配置

This is an alpha feature. Crossplane may change or drop this feature at any time.

This feature was introduced in v1.11.

For more information read the Crossplane feature lifecycle.

crossplane EnvironmentConfig 是一种类似于集群范围的 ConfigMap 的资源,由 Composition 使用。Composition 可以使用环境来存储单个资源的信息或引用 patches.

crossplane 支持多个 EnvironmentConfigs,每个都是唯一的数据存储。

Crossplane 创建复合资源时,会合并相关 Composition 中引用的所有 EnvironmentConfigs,并为该复合资源创建一个唯一的内存环境。

Composition 资源可以在其独特的内存环境中读写数据。

Important
每个 Composition 资源的内存环境都是唯一的。 一个 Composite 资源无法读取另一个 Composite 资源环境中的数据。

启用环境配置

EnvironmentConfigs 是一项 alpha 功能,默认情况下不会启用。

通过更改 crossplane pod 设置 并启用--参数参数。

 1$ kubectl edit deployment crossplane --namespace crossplane-system
 2apiVersion: apps/v1
 3kind: Deployment
 4spec:
 5# Removed for brevity
 6  template:
 7    spec:
 8      containers:
 9      - args:
10        - core
11        - start
12        - --enable-environment-configs
Tip
crossplane 安装指南](https://crossplane.devops.gold/master/software/install/#feature-flags) 介绍了如何启用功能标志,如–启用环境配置等功能标志。

创建环境配置

一个 环境配置有一个对象字段、数据.

环境配置支持数据字段内的任何数据。

下面是一个示例环境配置.

 1apiVersion: apiextensions.crossplane.io/v1alpha1
 2kind: EnvironmentConfig
 3metadata:
 4  name: example-environment
 5data:
 6  locations:
 7    us: us-east-2
 8    eu: eu-north-1
 9  key1: value1
10  key2: value2
11  key3:
12    - item1
13    - item2

选择环境配置

选择要在 Composition 内部引用的 EnvironmentConfigs环境字段。

环境配置 environmentConfigs字段是该 Composition 可以引用的环境列表。

通过以下方式选择环境参考选择器.

A参考通过名称来选择环境。选择器根据标签应用于环境。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8    - type: Reference
 9      ref:
10        name: example-environment
11    - type: Selector
12      selector:
13        matchLabels:
14      # Removed for brevity

如果一个 Composition 被引用多个环境配置crossplane 会按照列出的顺序将它们合并在一起。

Note
如果多个environmentConfigs如果多个 environmentConfigs 使用相同的键,则 Composition 会引用最后列出的环境的值。

按名称选择

通过名称选择环境类型: 参考.

定义对象和对象和名称与环境的确切名称相匹配。

例如,选择环境配置名为example-environment

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8    - type: Reference
 9      ref:
10        name: example-environment

按标签选择

通过带有类型: 选择器.

定义 选择器对象。

匹配标签matchLabels对象包含一个要匹配的标签列表。

选择标签需要同时匹配标签和 key 的值。

匹配标签值时,请提供一个精确的值,并用类型: Values并在Values字段中提供要匹配的值。

crossplane 还可以根据 Composition 资源中的输入匹配标签值。 使用类型: FromCompositeFieldPath并在字段中提供要匹配的字段。字段中提供要匹配的字段。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8    - type: Selector
 9      selector: 
10        matchLabels:
11          - key: my-label-key
12            type: Value
13            value: my-label-value
14          - key: my-label-key
15            type: FromCompositeFieldPath
16            valueFromFieldPath: spec.parameters.deploy
17  resources:
18  # Removed for brevity

管理选择器结果

按标签选择环境可能会返回多个环境。 Composition 会按环境名称对所有结果进行排序,并只引用排序列表中的第一个环境。

设置 模式模式: 多重以返回所有匹配的环境。 使用模式: 单返回单个环境。

Note

排序和选择模式仅适用于单个类型: 选择器.

这不会改变 Composition 合并多个环境配置.

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8    - type: Selector
 9      selector:
10        mode: Multiple
11        matchLabels:
12          - key: my-label-key
13            type: Value
14            value: my-label-value
15          - key: my-label-key
16            type: FromCompositeFieldPath
17            valueFromFieldPath: spec.parameters.deploy
18    - type: Selector
19      selector:
20        mode: Single
21        matchLabels:
22          - key: my-other-label-key
23            type: Value
24            value: my-other-label-value
25          - key: my-other-label-key
26            type: FromCompositeFieldPath
27            valueFromFieldPath: spec.parameters.deploy

被引用时模式: 多重时,使用maxMatch并定义返回环境的最大数量。

使用 minMatch 并定义返回环境的最小数量。

Composition 按名称的字母顺序对返回的环境排序。 使用sortByFieldPath并定义要排序的字段。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8    - type: Selector
 9      selector:
10        mode: Multiple
11        maxMatch: 4
12        sortByFieldPath: metadata.annotations[sort.by/weight]
13        matchLabels:
14          - key: my-label-key
15            type: Value
16            value: my-label-value
17          - key: my-label-key
18            type: FromCompositeFieldPath
19            valueFromFieldPath: spec.parameters.deploy

选择的环境匹配标签中列出的任何其他环境进行合并。环境配置.

可选的选择器标签

默认情况下,如果一个valueFromFieldPath字段不存在于 Composition 资源中时,Crossplane 会出错。

添加fromFieldPathPolicy作为 可选以忽略不存在的字段。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8      - type: Selector
 9        selector:
10          matchLabels:
11            - key: my-first-label-key
12              type: Value
13              value: my-first-label-value
14            - key: my-second-label-key
15              type: FromCompositeFieldPath
16              valueFromFieldPath: spec.parameters.deploy
17              fromFieldPathPolicy: Optional
18  resources:
19  # Removed for brevity

为可选标签设置默认值,方法是设置默认值Values键的默认值的默认值,然后定义可选标签。

例如,该 Composition 定义了Values: my-default-value为键 my-second-label-key 的值。如果标签my-second-label-key存在,则 crossplane 会引用该标签的值。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3metadata:
 4  name: example-composition
 5spec:
 6  environment:
 7    environmentConfigs:
 8      - type: Selector
 9        selector:
10          matchLabels:
11            - key: my-first-label-key
12              type: Value
13              value: my-label-value
14            - key: my-second-label-key
15              type: Value
16              value: my-default-value
17            - key: my-second-label-key
18              type: FromCompositeFieldPath
19              valueFromFieldPath: spec.parameters.deploy
20              fromFieldPathPolicy: Optional
21  resources:
22  # Removed for brevity
Warning

crossplane 按顺序应用 Values 值,最后定义的键值总是优先。

在标签之后定义默认值总是会覆盖标签值。

使用 EnvironmentConfigs 进行修补

当 crossplane 创建或更新复合资源时,Crossplane 会将所有指定的 EnvironmentConfigs 合并到内存环境中。

复合资源可以在环境配置和复合资源之间或环境配置和复合资源内部定义的单个资源之间读写数据。

Tip
补丁和转换 文档中有关环境配置补丁类型的信息。

修补一个 Composition 资源

要给 Composition 资源打补丁,请引用补丁内的环境.

被引用到复合字段路径将数据从内存环境复制到 Composition 资源。 使用从复合字段路径将数据从 Composition 资源复制到内存环境。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3# Removed for Brevity
 4spec:
 5  environment:
 6  # Removed for Brevity
 7      patches:
 8      - type: ToCompositeFieldPath
 9        fromFieldPath: tags
10        toFieldPath: metadata.labels[envTag]
11      - type: FromCompositeFieldPath
12        fromFieldPath: metadata.name
13        toFieldPath: newEnvironmentKey

单个资源可以使用写入内存环境的任何数据。

对个别资源进行修补

要为单个资源打补丁,请在补丁内,使用环境字段路径将数据从资源复制到内存环境。 使用 从环境字段路径将数据从内存环境复制到资源。

 1apiVersion: apiextensions.crossplane.io/v1
 2kind: Composition
 3# Removed for Brevity
 4spec:
 5  environment:
 6  # Removed for Brevity
 7  resources:
 8  # Removed for Brevity
 9    - name: vpc
10      base:
11        apiVersion: ec2.aws.upbound.io/v1beta1
12        kind: VPC
13        spec:
14          forProvider:
15            cidrBlock: 172.16.0.0/16
16      patches:
17        - type: ToEnvironmentFieldPath
18          fromFieldPath: status.atProvider.id
19          toFieldPath: vpcId
20        - type: FromEnvironmentFieldPath
21          fromFieldPath: tags
22          toFieldPath: spec.forProvider.tags

补丁和转换](https://crossplane.devops.gold/master/concepts/patch-and-transform/) 文档中有更多关于修补单个资源的信息。