Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement helper function to retreive environment varibles #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ example:
| [`getResourceCondition`](example/functions/getResourceCondition) | Helper function to retreive conditions of resources |
| [`getComposedResouce`](example/functions/getComposedResource) | Helper function to retrieve observed composed resources |
| [`getCompositeResource`](example/functions/getCompositeResource) | Helper function to retreive the observed composite resource |
| [`getCompositionEnvVar`](example/functions/getCompositionEnvVar) | Helper function to retreive 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 |

Expand Down
2 changes: 2 additions & 0 deletions example/functions/getCompositionEnvVar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# getCompositionEnvVar
The getCompositionEnvVar function is a helper function used to retrieve [environment variables](https://docs.crossplane.io/latest/concepts/environment-configs/) from the request context. Upon successful retrieval, the function returns a string containing the variable. If no variable with the key exists, an empty string is returned.
31 changes: 31 additions & 0 deletions example/functions/getCompositionEnvVar/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: {{ getCompositionEnvVar . "adminLogin" }}
6 changes: 6 additions & 0 deletions example/functions/getCompositionEnvVar/environment.yaml
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/getCompositionEnvVar/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/getCompositionEnvVar/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 @@ -26,6 +26,7 @@ var funcMaps = []template.FuncMap{
"setResourceNameAnnotation": setResourceNameAnnotation,
"getComposedResource": getComposedResource,
"getCompositeResource": getCompositeResource,
"getCompositionEnvVar": getCompositionEnvVar,
},
}

Expand Down Expand Up @@ -130,3 +131,14 @@ func getCompositeResource(req map[string]any) map[string]any {

return cr
}

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

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

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

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

type want struct {
rsp string
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,
},
},
"NotExistingEnvVar": {
reason: "Should return empty string 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: "",
err: cmpopts.AnyError,
},
},
}

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

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

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