generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add helper functions for retrieving composed and composite resources #81
Open
jtucci
wants to merge
6
commits into
crossplane-contrib:main
Choose a base branch
from
jtucci:jtucci/add-helper-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc62196
Add helper functions for retrieving composed and composite resources
jon-tucci 32976d1
Merge branch 'main' into jtucci/add-helper-functions
jtucci 866718f
Fixes naming in example
jtucci a8f6519
Remove unnecessary Sprintf
jtucci b38472e
re-add import that was accidentally removed
jtucci 0a79ad8
Fix typo in example
jtucci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,8 @@ | |
go.work | ||
|
||
# Binaries | ||
function-go-templating | ||
function-go-templating | ||
|
||
# IDE | ||
.idea | ||
.idea/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# getComposedResource | ||
The getComposedResource function is a utility function used to facilitate the retrieval of composed resources within templated configurations, specifically targeting observed resources. By accepting a function request map and a resource name, it navigates the complex structure of a request to fetch the specified composed resource, making it easier and more user-friendly to access nested data. If the resource is found, it returns a map containing the resource's manifest; if not, it returns nil, indicating the resource does not exist or is inaccessible through the given path. | ||
## Usage | ||
|
||
|
||
Examples: | ||
|
||
```golang | ||
// Retrieve the observed resource named "flexServer" from the function request | ||
{{ flexServer := getComposedResource . "flexServer" }} | ||
|
||
// Extract values from the observed resource | ||
{{ $flexServerID := get $flexServer.status.atProvider "id" }} | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
apiVersion: apiextensions.crossplane.io/v1 | ||
kind: Composition | ||
metadata: | ||
name: example-function-get-composed-resource | ||
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: | | ||
--- | ||
# Create an initial composed resource for which we will retrieve a value | ||
apiVersion: dbforpostgresql.azure.upbound.io/v1beta1 | ||
kind: FlexibleServer | ||
metadata: | ||
annotations: | ||
{{ setResourceNameAnnotation "flexserver" }} | ||
gotemplating.fn.crossplane.io/ready: "False" | ||
spec: | ||
forProvider: | ||
storageMb: 32768 | ||
providerConfigRef: | ||
name: my-provider-cfg | ||
--- | ||
# Use getComposedResource to retrieve the observed resource named "flexServer" | ||
{{ $flexServer := getComposedResource . "flexServer" }} | ||
apiVersion: dbforpostgresql.azure.upbound.io/v1beta1 | ||
kind: FlexibleServerConfiguration | ||
metadata: | ||
annotations: | ||
{{ setResourceNameAnnotation "flexServerConfig" }} | ||
gotemplating.fn.crossplane.io/ready: "False" | ||
spec: | ||
forProvider: | ||
# Populate the field using the observed status of the retrieved resource | ||
serverId: {{ get $flexServer.status "id" }} | ||
providerConfigRef: | ||
name: my-provider-cfg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apiVersion: dbforpostgresql.azure.upbound.io/v1beta1 | ||
kind: FlexibleServer | ||
metadata: | ||
annotations: | ||
crossplane.io/composition-resource-name: flexServer | ||
labels: | ||
crossplane.io/composite: example | ||
spec: | ||
forProvider: | ||
storageMb: 32768 | ||
providerConfigRef: | ||
name: my-provider-cfg | ||
status: | ||
atProvider: | ||
id: abcdef | ||
conditions: | ||
- type: Ready | ||
status: "True" | ||
reason: "foo" | ||
lastTransitionTime: "2023-11-03T09:07:31Z" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# getCompositeResource | ||
The getCompositeResource function is a utility function used to facilitate the retrieval of a composite resources (XR) within templated configurations. Upon successful retrieval, the function returns a map containing the observed composite resource's manifest. If the resource cannot be located or is unreachable, it returns nil, indicating the absence or inaccessibility of the composite resource. | ||
## Usage | ||
|
||
|
||
Examples: | ||
Given the following XR spec | ||
```yaml | ||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: XR | ||
metadata: | ||
name: example | ||
spec: | ||
name: "example" | ||
location: "eastus" | ||
|
||
``` | ||
```golang | ||
// Retrieve the observed composite resource (XR) from the function request | ||
{{ $xr := getCompositeResource . }} | ||
|
||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: ExampleResource | ||
// 'Patch' values from the composite resource into the composed resource | ||
spec: | ||
forProvider: | ||
name: {{ get $xr.spec "name" }} | ||
location: {{ get $xr.spec "location" }} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apiVersion: apiextensions.crossplane.io/v1 | ||
kind: Composition | ||
metadata: | ||
name: example-function-get-composite-resource | ||
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: | | ||
--- | ||
# Use a getCompositeResource to retrieve the XR | ||
{{ $xr := getCompositeResource . }} | ||
apiVersion: dbforpostgresql.azure.upbound.io/v1beta1 | ||
kind: FlexibleServer | ||
metadata: | ||
annotations: | ||
{{ setResourceNameAnnotation "flexserver" }} | ||
gotemplating.fn.crossplane.io/ready: "False" | ||
spec: | ||
forProvider: | ||
# Use the XR object to set values | ||
adminLogin: {{ get $xr.spec "adminLogin" }} | ||
location: {{ get $xr.spec "location" }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: XR | ||
metadata: | ||
name: example | ||
spec: | ||
adminLogin: "admin" | ||
location: "eastus" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the resource was named
flexserver
all lowercase instead offlexServer
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed :)