Skip to content

Repository files navigation

function-extra-resources

CI

A function for selecting extra resources via composition functions in Go.

Using function-extra-resources

Please see the example in ./example

function-extra-resources is generally most useful in tandem with a function that can reference the many resources like function-go-templating.

Creating objects from other's found in the local cluster.

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}}

Crossplane compatibility

A single build works on Crossplane v1.20+ and v2.x. It requests extra resources over the deprecated extra_resources protocol fields, which Crossplane v2 still resolves, rather than the required_resources fields that only exist on v2.

namespace behaves differently across the two majors, because Crossplane v1 has no notion of a namespace on an extra resource selector:

  • On v2, matches are filtered server-side and only the namespace's resources are sent to the function.
  • On v1, the namespace is dropped from the request. Selector matches are resolved across every namespace and filtered down by the function, so the result is the same but the full cluster-wide match still crosses the wire — which can exceed the 4MB default gRPC receive limit. Raise it with --max-recv-message-size if you hit it.
  • On v1, a Reference to a namespaced resource cannot be resolved at all, since the lookup is by name only. Such a reference fails rather than resolving to the wrong object.

In both cases namespace only applies to namespaced kinds. Cluster-scoped objects — EnvironmentConfig among them — have an empty namespace, so setting one selects nothing.

Local dev.

Air

air is not strictly necessary, but helpful.

Installing 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 ./example

Crossplane Function Basics

This function uses Go, Docker, and the Crossplane CLI.

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

# Run tests - see fn_test.go
$ go test ./...

# Build the function's runtime image - see Dockerfile
$ docker build . --tag=runtime

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages