diff --git a/content/master/concepts/environment-configs.md b/content/master/concepts/environment-configs.md index 74dc3f13f..4e6339e28 100644 --- a/content/master/concepts/environment-configs.md +++ b/content/master/concepts/environment-configs.md @@ -196,16 +196,90 @@ spec: - type: Selector selector: matchLabels: - - key: my-label-key + - key: my-first-label-key type: Value - value: my-label-value - - key: my-label-key + value: my-first-label-value + - key: my-second-label-key type: FromCompositeFieldPath valueFromFieldPath: spec.parameters.deploy resources: # Removed for brevity ``` +By default, Crossplane errors out if a +{{}}valueFromFieldPath{{}} +field doesn't exist in the composite resource at runtime. +Set the +{{}}fromFieldPathPolicy{{}} +field to `Optional` to ignore fields that don't exist. + +```yaml {label="byLabelOptional",copy-lines="all"} +apiVersion: apiextensions.crossplane.io/v1 +kind: Composition +metadata: + name: example-composition +spec: + environment: + environmentConfigs: + - type: Selector + selector: + matchLabels: + - key: my-first-label-key + type: Value + value: my-first-label-value + - key: my-second-label-key + type: FromCompositeFieldPath + valueFromFieldPath: spec.parameters.deploy + fromFieldPathPolicy: Optional + resources: + # Removed for brevity +``` + +Set a default value for an optional label by setting the +{{}}value{{}} for the +{{}}key{{}} first, then +define the +{{}}Optional{{}} label. + +For example, this Composition defines +{{}}value: my-default-value{{}} +for the key {{}}my-second-label-key{{}}. +If the label +{{}}my-second-label-key{{}} +exists, Crossplane uses the value from the label instead. + +```yaml {label="byLabelOptionalDefault",copy-lines="all"} +apiVersion: apiextensions.crossplane.io/v1 +kind: Composition +metadata: + name: example-composition +spec: + environment: + environmentConfigs: + - type: Selector + selector: + matchLabels: + - key: my-first-label-key + type: Value + value: my-label-value + - key: my-second-label-key + type: Value + value: my-default-value + - key: my-second-label-key + type: FromCompositeFieldPath + valueFromFieldPath: spec.parameters.deploy + fromFieldPathPolicy: Optional + resources: + # Removed for brevity +``` + +{{}} +Crossplane applies values in order. The value of the last key defined always takes precedence. + +Defining the default value _after_ the label always overwrites the label +value. +{{< /hint >}} + #### Manage selector results Selecting environments by labels may return more than one environment.