Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,19 @@ conditions:

## Additional functions

| Name | Description |
|------------------------------------------------------------------|--------------------------------------------------------------|
| [`randomChoice`](example/inline) | Randomly selects one of a given strings |
| [`toYaml`](example/functions/toYaml) | Marshals any object into a YAML string |
| [`fromYaml`](example/functions/fromYaml) | Unmarshals a YAML string into an object |
| [`getResourceCondition`](example/functions/getResourceCondition) | Helper function to retrieve conditions of resources |
| [`getComposedResource`](example/functions/getComposedResource) | Helper function to retrieve observed composed resources |
| [`getCompositeResource`](example/functions/getCompositeResource) | Helper function to retrieve the observed composite resource |
| [`getExtraResources`](example/functions/getExtraResources) | Helper function to retrieve extra resources |
| [`setResourceNameAnnotation`](example/inline) | Returns the special resource-name annotation with given name |
| [`include`](example/functions/include) | Outputs template as a string |
| Name | Description |
| -------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| [`randomChoice`](example/inline) | Randomly selects one of a given strings |
| [`toYaml`](example/functions/toYaml) | Marshals any object into a YAML string |
| [`fromYaml`](example/functions/fromYaml) | Unmarshals a YAML string into an object |
| [`getResourceCondition`](example/functions/getResourceCondition) | Helper function to retrieve conditions of resources |
| [`getComposedResource`](example/functions/getComposedResource) | Helper function to retrieve observed composed resources |
| [`getCompositeResource`](example/functions/getCompositeResource) | Helper function to retrieve the observed composite resource |
| [`getExtraResources`](example/functions/getExtraResources) | Helper function to retrieve extra resources |
| [`getCompositeResource`](example/functions/getCompositeResource) | Helper function to retrieve the observed composite resource |
| [`getCompositionEnvironment`](example/functions/getCompositionEnvironment) | Helper function to retrieve an environment variable from the request context |
| [`setResourceNameAnnotation`](example/inline) | Returns the special resource-name annotation with given name |
| [`include`](example/functions/include) | Outputs template as a string |

## Developing this function

Expand Down
2 changes: 2 additions & 0 deletions example/functions/getCompositionEnvironment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# getCompositionEnvironment
The getCompositionEnvironment function is a helper function used to retrieve [environment variables](https://docs.crossplane.io/latest/composition/environment-configs) from the request context. Upon successful retrieval, the function returns the value. If no variable with the key exists, nil is returned.
31 changes: 31 additions & 0 deletions example/functions/getCompositionEnvironment/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-function-get-composition-env-var
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
---
apiVersion: dbforpostgresql.azure.upbound.io/v1beta1
kind: FlexibleServer
metadata:
annotations:
{{ setResourceNameAnnotation "flexserver" }}
gotemplating.fn.crossplane.io/ready: "False"
spec:
forProvider:

# Retrieve AdminLogin from request context
adminLogin: {{ getCompositionEnvironment . "adminLogin" }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: apiextensions.crossplane.io/v1alpha1
kind: EnvironmentConfig
metadata:
name: example-environment
data:
adminLogin: admin
6 changes: 6 additions & 0 deletions example/functions/getCompositionEnvironment/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.5.0
10 changes: 10 additions & 0 deletions example/functions/getCompositionEnvironment/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
environment:
environmentConfigs:
- type: Reference
ref:
name: example-environment
12 changes: 12 additions & 0 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var funcMaps = []template.FuncMap{
"getComposedResource": getComposedResource,
"getCompositeResource": getCompositeResource,
"getExtraResources": getExtraResources,
"getCompositionEnvironment": getCompositionEnvironment,
},
}

Expand Down Expand Up @@ -143,3 +144,14 @@ func getExtraResources(req map[string]any, name string) []any {

return ers
}

func getCompositionEnvironment(req map[string]any, name string) (any, error) {
path := fmt.Sprintf(`context["apiextensions.crossplane.io/environment"]["%s"]`, name)

env, err := fieldpath.Pave(req).GetValue(path)
if err != nil {
return nil, err
}

return env, nil
}
120 changes: 120 additions & 0 deletions function_maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,123 @@ func Test_getExtraResources(t *testing.T) {
})
}
}

func Test_getCompositionEnvironment(t *testing.T) {
type args struct {
req map[string]any
name string
}

type want struct {
rsp any
err error
}

cases := map[string]struct {
reason string
args args
want want
}{
"RetrieveCompositionEnvVar": {
reason: "Should successfully retrieve a composition env var",
args: args{
req: map[string]any{
"context": map[string]any{
"apiextensions.crossplane.io/environment": map[string]any{
"test": "abc",
},
},
},
name: "test",
},
want: want{
rsp: "abc",
err: nil,
},
},
"RetrieveCompositionEnvVarCaseInsensitive": {
reason: "Should successfully retrieve a composition env var case insensitive",
args: args{
req: map[string]any{
"context": map[string]any{
"apiextensions.crossplane.io/environment": map[string]any{
"TEST": "abc",
},
},
},
name: "TEST",
},
want: want{
rsp: "abc",
err: nil,
},
},
"RetrieveCompositionEnvVarList": {
reason: "Should successfully retrieve a composition env var as a list",
args: args{
req: map[string]any{
"context": map[string]any{
"apiextensions.crossplane.io/environment": map[string]any{
"test": []string{"abc"},
},
},
},
name: "test",
},
want: want{
rsp: []string{"abc"},
err: nil,
},
},
"RetrieveCompositionEnvVarMap": {
reason: "Should successfully retrieve a composition env var as a map",
args: args{
req: map[string]any{
"context": map[string]any{
"apiextensions.crossplane.io/environment": map[string]any{
"test": map[string]any{
"key": "abc",
},
},
},
},
name: "test",
},
want: want{
rsp: map[string]any{
"key": "abc",
},
err: nil,
},
},
"NotExistingEnvVar": {
reason: "Should return nil when env var does not exist",
args: args{
req: map[string]any{
"context": map[string]any{
"apiextensions.crossplane.io/environment": map[string]any{},
},
},
name: "test",
},
want: want{
rsp: nil,
err: cmpopts.AnyError,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
rsp, err := getCompositionEnvironment(tc.args.req, tc.args.name)

if diff := cmp.Diff(tc.want.rsp, rsp, protocmp.Transform()); diff != "" {
t.Errorf("%s\ngetCompositionEnvironment(...): -want rsp, +got rsp:\n%s", tc.reason, diff)
}

if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("%s\ntgetCompositionEnvironment(...): -want err, +got err:\n%s", tc.reason, diff)
}
})
}
}