Skip to content
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,10 @@ conditions:
| [`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 retreive conditions of resources |
| [`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 retreive the observed composite resource |
| [`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 |

Expand Down
52 changes: 52 additions & 0 deletions example/functions/getExtraResources/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-extra-resources
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: meta.gotemplating.fn.crossplane.io/v1alpha1
kind: ExtraResources
requirements:
bucket:
apiVersion: s3.aws.upbound.io/v1beta1
kind: Bucket
matchName: my-awesome-{{ .observed.composite.resource.spec.environment }}-bucket
{{ $someExtraResources := getExtraResources . "bucket" }}
{{- range $i, $extraResource := default (list) $someExtraResources }}
---
apiVersion: kubernetes.crossplane.io/v1alpha1
kind: Object
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: bucket-configmap-{{ $i }}
spec:
forProvider:
manifest:
apiVersion: v1
kind: Configmap
metadata:
name: {{ $extraResource.resource.metadata.name }}-bucket
data:
bucket: {{ $extraResource.resource.status.atProvider.id }}
providerConfigRef:
name: "kubernetes"
{{- end }}
---
apiVersion: example.crossplane.io/v1beta1
kind: XR
status:
dummy: cool-status
13 changes: 13 additions & 0 deletions example/functions/getExtraResources/extraResources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: s3.aws.upbound.io/v1beta1
kind: Bucket
metadata:
labels:
testing.upbound.io/example-name: bucket-notification
name: my-awesome-dev-bucket
spec:
forProvider:
region: us-west-1
status:
atProvider:
id: random-bucket-id
6 changes: 6 additions & 0 deletions example/functions/getExtraResources/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.9.0
6 changes: 6 additions & 0 deletions example/functions/getExtraResources/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
environment: dev
6 changes: 3 additions & 3 deletions example/functions/getResourceCondition/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ spec:
apiVersion: {{ .observed.composite.resource.apiVersion }}
kind: {{ .observed.composite.resource.kind }}
status:
# Retreive condition of XR
# retrieve condition of XR
compositeCondition:
{{ getResourceCondition "Ready" .observed.composite | toYaml | nindent 4 }}

# Retreive non existing condition of a resource just returns an Unknown condition
# retrieve non existing condition of a resource just returns an Unknown condition
compositeConditionNotFound:
{{ getResourceCondition "Other" .observed.composite | toYaml | nindent 4 }}

# You can also retreive conditions of composed resources the same way
# You can also retrieve conditions of composed resources the same way
composedCondition:
{{ getResourceCondition "Ready" .observed.resources.project | toYaml | nindent 4 }}

Expand Down
11 changes: 11 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,
"getExtraResources": getExtraResources,
},
}

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

return cr
}

func getExtraResources(req map[string]any, name string) []any {
var ers []any
path := fmt.Sprintf("extraResources[%s].items", name)
if err := fieldpath.Pave(req).GetValueInto(path, &ers); err != nil {
return nil
}

return ers
}
84 changes: 84 additions & 0 deletions function_maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,87 @@ func Test_getCompositeResource(t *testing.T) {
})
}
}

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

type want struct {
rsp []any
}

completeResource := map[string]any{
"apiVersion": "dbforpostgresql.azure.upbound.io/v1beta1",
"kind": "FlexibleServer",
"spec": map[string]any{
"forProvider": map[string]any{
"storageMb": "32768",
},
},
"status": map[string]any{
"atProvider": map[string]any{
"id": "abcdef",
},
},
}

cases := map[string]struct {
reason string
args args
want want
}{
"RetrieveCompleteResource": {
reason: "Should successfully retrieve the complete resource",
args: args{
req: map[string]any{
"extraResources": map[string]any{
"flexserver": map[string]any{
"items": []any{
completeResource,
},
},
},
},
name: "flexserver",
},
want: want{
rsp: []any{
completeResource,
},
},
},
"ResourceNotFound": {
reason: "Should return empty list if no extra resources are found",
args: args{
req: map[string]any{
"extraResources": map[string]any{
"flexserver": map[string]any{
"items": []any{},
},
},
},
name: "flexserver",
},
want: want{rsp: []any{}},
},
"NoExtraResources": {
reason: "Should return nil if no extra resources are available",
args: args{
req: map[string]any{},
name: "flexserver",
},
want: want{rsp: nil},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := getExtraResources(tc.args.req, tc.args.name)
if diff := cmp.Diff(tc.want.rsp, got); diff != "" {
t.Errorf("%s\ngetExtraResources(...): -want rsp, +got rsp:\n%s", tc.reason, diff)
}
})
}
}