Skip to content

Commit

Permalink
Merge pull request #17 from negz/release-prep
Browse files Browse the repository at this point in the history
Update README, add example
  • Loading branch information
negz authored Nov 2, 2023
2 parents 82d1287 + 25c6232 commit dc5213c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
70 changes: 33 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# function-dummy
[![CI](https://github.com/crossplane-contrib/function-dummy/actions/workflows/ci.yml/badge.svg)](https://github.com/crossplane-contrib/function-dummy/actions/workflows/ci.yml) ![GitHub release (latest SemVer)](https://img.shields.io/github/release/crossplane-contrib/function-dummy)

A [Crossplane] Composition Function that returns what you tell it to.

## What is this?
This [composition function][docs-functions] returns whatever you tell it to.

This [Composition Function][function-design] just returns whatever
`RunFunctionResponse` you tell it to. You provide a `RunFunctionResponse` in the
Function's input. The `desired` object will be merged onto any desired state
passed in the `RunFunctionRequest` using [`proto.Merge`][merge] semantics.

This Function is mostly useful for testing Crossplane itself.

Note that this is a beta-style Function. It won't work with Crossplane v1.13 or
earlier - it targets the [implementation of Functions][function-pr] coming with
Crossplane v1.14 in late October.
Provide a YAML-serialized [`RunFunctionResponse`][bsr] as the function's input.
The response's `desired` object will be merged onto any desired state that was
passed to the function using [`proto.Merge`][merge] semantics.

Here's an example:

Expand All @@ -24,8 +17,8 @@ metadata:
name: test-crossplane
spec:
compositeTypeRef:
apiVersion: database.example.com/v1alpha1
kind: NoSQL
apiVersion: example.crossplane.io/v1beta1
kind: XR
pipeline:
- step: return-some-results
functionRef:
Expand All @@ -37,7 +30,7 @@ spec:
desired:
composite:
resource:
spec:
status:
widgets: 200
connectionDetails:
very: secret
Expand All @@ -53,35 +46,38 @@ spec:
message: "I did the thing!"
```
## Developing
See the [example](example) directory for an example you can run locally using
the Crossplane CLI:
```shell
$ crossplane beta render xr.yaml composition.yaml functions.yaml
```

See the [composition functions documentation][docs-functions] to learn more
about `crossplane beta render`.

This Function doesn't use the typical Crossplane build submodule and Makefile,
since we'd like Functions to have a less heavyweight developer experience.
It mostly relies on regular old Go tools:
## Developing this function

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

```shell
# Run code generation - see input/generate.go
$ go generate ./...

# Run tests
$ go test -cover ./...
? github.com/crossplane-contrib/function-dummy/input/v1beta1 [no test files]
ok github.com/crossplane-contrib/function-dummy 0.006s coverage: 25.8% of statements
# Run tests - see fn_test.go
$ go test ./...

# Lint the code
$ docker run --rm -v $(pwd):/app -v ~/.cache/golangci-lint/v1.54.2:/root/.cache -w /app golangci/golangci-lint:v1.54.2 golangci-lint run
# Build the function's runtime image - see Dockerfile
$ docker build . --tag=runtime

# Build a Docker image - see Dockerfile
$ docker build .
# Build a function package - see package/crossplane.yaml
$ crossplane xpkg build -f package --embed-runtime-image=runtime
```

This Function is pushed to `xpkg.upbound.io/crossplane-contrib/function-dummy`.
At the time of writing it's pushed manually via `docker push` using
`docker-credential-up` from https://github.com/upbound/up/.

[Crossplane]: https://crossplane.io
[function-design]: https://github.com/crossplane/crossplane/blob/3996f20/design/design-doc-composition-functions.md
[function-pr]: https://github.com/crossplane/crossplane/pull/4500
[docs-composition]: https://docs.crossplane.io/v1.13/getting-started/provider-aws-part-2/#create-a-deployment-template
[#2581]: https://github.com/crossplane/crossplane/issues/2581
[merge]: https://pkg.go.dev/github.com/golang/protobuf/proto#Merge
[docs-functions]: https://docs.crossplane.io/v1.14/concepts/composition-functions/
[bsr]: https://buf.build/crossplane/crossplane/docs/main:apiextensions.fn.proto.v1beta1#apiextensions.fn.proto.v1beta1.RunFunctionResponse
[merge]: https://pkg.go.dev/github.com/golang/protobuf/proto#Merge
[go]: https://go.dev
[docker]: https://www.docker.com
[cli]: https://docs.crossplane.io/latest/cli
36 changes: 36 additions & 0 deletions example/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-patch-and-transform
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: return-some-results
functionRef:
name: function-dummy
input:
apiVersion: dummy.fn.crossplane.io/v1beta1
kind: Response
response:
desired:
composite:
resource:
status:
widgets: 200
connectionDetails:
very: secret
resources:
cool-resource:
resource:
apiVersion: example.org/v1
kind: Composed
spec:
forProvider:
widgets: 200
ready: READY_TRUE
results:
- severity: SEVERITY_NORMAL
message: "I did the thing!"
7 changes: 7 additions & 0 deletions example/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-dummy
spec:
package: xpkg.upbound.io/crossplane-contrib/function-dummy:v0.2.1
6 changes: 6 additions & 0 deletions example/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
metadata:
name: example-xr
spec: {} # This function ignores the XR. It only cares about its Composition input.

0 comments on commit dc5213c

Please sign in to comment.