Skip to content

Commit

Permalink
(reedjosh) Initial build.
Browse files Browse the repository at this point in the history
Signed-off-by: reedjosh <josh@thereedfamily.rocks>
  • Loading branch information
reedjosh committed Mar 25, 2024
1 parent 730efa7 commit c3a8e87
Show file tree
Hide file tree
Showing 19 changed files with 1,726 additions and 682 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ env:
# XPKG_ACCESS_ID and XPKG_TOKEN secrets are set in the GitHub respository (or
# organization) settings. Create a token at https://accounts.upbound.io.
XPKG_ACCESS_ID: ${{ secrets.XPKG_ACCESS_ID }}
XPKG_TOKEN: ${{ secrets.XPKG_TOKEN }}

# The package to push, without a version tag. The default matches GitHub. For
# example xpkg.upbound.io/crossplane/function-template-go.
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

# Go workspace file
go.work
tmp
113 changes: 96 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,105 @@
# function-template-go
# function-extra-resources
[![CI](https://github.com/crossplane/function-template-go/actions/workflows/ci.yml/badge.svg)](https://github.com/crossplane/function-template-go/actions/workflows/ci.yml)

A template for writing a [composition function][functions] in [Go][go].
A function for selecting extra resources via [composition function][functions]s in [Go][go].

To learn how to use this template:
## Using `function-extra-resources`

* [Follow the guide to writing a composition function in Go][function guide]
* [Learn about how composition functions work][functions]
* [Read the function-sdk-go package documentation][package docs]
Please see the example in `./examples`

If you just want to jump in and get started:
`function-extra-resources` is generally most useful in tandem with a function that can reference the many resources like
`function-go-templating`.

1. Replace `function-template-go` with your function in `go.mod`,
`package/crossplane.yaml`, and any Go imports. (You can also do this
automatically by running the `./init.sh <function-name>` script.)
1. Update `input/v1beta1/` to reflect your desired input (and run `go generate`)
1. Add your logic to `RunFunction` in `fn.go`
1. Add tests for your logic in `fn_test.go`
1. Update this file, `README.md`, to be about your function!
### Creating objects from other's found in the local cluster.
``` yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-environment-configs
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: pull-extra-resources
functionRef:
name: function-extra-resources
input:
apiVersion: extra-resources.fn.crossplane.io/v1beta1
kind: Input
spec:
extraResources:
- kind: XCluster
into: XCluster
apiVersion: example.crossplane.io/v1
type: Selector
selector:
maxMatch: 2
minMatch: 1
matchLabels:
- key: type
type: Value
value: cluster
- step: go-templating
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
{{- $XClusters := index (index .context "apiextensions.crossplane.io/extra-resources") "XCluster" }}
{{- range $i, $A := $XClusters }}
---
apiVersion: vault.upbound.io/v1beta1
kind: VaultRole
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: {{index (index $A "metadata") "name"}}
spec:
forProvider:
{{- end}}
```
## Installing the `function-extra-resources` Function into a Cluster

``` shell
cat <<EOF | kubectl apply -f -
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-extra-resources
spec:
package: xpkg.upbound.io/crossplane-contrib/function-extra-resources:latest
EOF
```

## Local dev.

### Air

`air` is not strictly necessary, but helpful.

Installing [air](https://github.com/cosmtrek/air) allows quick iterative local development.

`air` is a live reloader that watches for local file changes.

Once installed, running

`air -- --insecure --debug --address localhost:9443`.

Shoud get the function process/server build and running to serve CLI function requests.

### After locally serving function-extra-resources

`./run.sh` will use the crossplane CLI to run our basic example in `./examples`

### Crossplane Function Basics

This template uses [Go][go], [Docker][docker], and the [Crossplane CLI][cli] to
build functions.
This function uses [Go][go], [Docker][docker], and the [Crossplane CLI][cli].

```shell
# Run code generation - see input/generate.go
Expand All @@ -41,4 +120,4 @@ $ crossplane xpkg build -f package --embed-runtime-image=runtime
[function guide]: https://docs.crossplane.io/knowledge-base/guides/write-a-composition-function-in-go
[package docs]: https://pkg.go.dev/github.com/crossplane/function-sdk-go
[docker]: https://www.docker.com
[cli]: https://docs.crossplane.io/latest/cli
[cli]: https://docs.crossplane.io/latest/cli
5 changes: 5 additions & 0 deletions air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# air.toml
root = "."
tmp_dir = "tmp"
build_cmd = "go build -o ./tmp/main ."
run_cmd = "./tmp/main"
53 changes: 48 additions & 5 deletions example/composition.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-template-go
name: function-environment-configs
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: run-the-template
- step: pull-extra-resources
functionRef:
name: function-template-go
name: function-extra-resources
input:
apiVersion: template.fn.crossplane.io/v1beta1
apiVersion: extra-resources.fn.crossplane.io/v1beta1
kind: Input
example: "Hello world"
spec:
extraResources:
- kind: EnvironmentConfig
into: envConfs
apiVersion: apiextensions.crossplane.io/v1alpha1
type: Selector
selector:
maxMatch: 2
minMatch: 1
matchLabels:
- key: type
type: Value
value: cluster
- kind: XCluster
into: XCluster
apiVersion: example.crossplane.io/v1
type: Selector
selector:
maxMatch: 2
minMatch: 1
matchLabels:
- key: type
type: Value
value: cluster
- step: go-templating
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
{{- $XClusters := index (index .context "apiextensions.crossplane.io/extra-resources") "XCluster" }}
{{- range $i, $A := $XClusters }}
---
apiVersion: vault.upbound.io/v1beta1
kind: VaultRole
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: {{index (index $A "metadata") "name"}}
spec:
forProvider:
{{- end}}
47 changes: 47 additions & 0 deletions example/extraResources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apiVersion: apiextensions.crossplane.io/v1alpha1
kind: EnvironmentConfig
metadata:
name: test-x1
labels:
type: cluster
metaName: test
data:
realName: test-x1
---
apiVersion: apiextensions.crossplane.io/v1alpha1
kind: EnvironmentConfig
metadata:
name: test-y1
labels:
type: cluster
metaName: test
data:
realName: test-y1
---
apiVersion: example.crossplane.io/v1
kind: XCluster
metadata:
name: net-staging-blue
labels:
type: cluster
spec:
compositionRef:
name: compositecluster.example.crossplane.io
compositionSelector:
matchLabels:
provider: aws
compositionUpdatePolicy: Automatic
---
apiVersion: example.crossplane.io/v1
kind: XCluster
metadata:
name: net-staging-green
labels:
type: cluster
spec:
compositionRef:
name: compositecluster.example.crossplane.io
compositionSelector:
matchLabels:
provider: aws
compositionUpdatePolicy: Automatic
11 changes: 9 additions & 2 deletions example/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-template-go
name: function-extra-resources
annotations:
# This tells crossplane beta render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
# This is ignored when using the Development runtime.
package: function-template-go
package: xpkg.upbound.io/crossplane-contrib/function-extra-resources:latest
---
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
Loading

0 comments on commit c3a8e87

Please sign in to comment.