-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nic Cope <[email protected]>
- Loading branch information
Showing
15 changed files
with
659 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
name: Bug Report | ||
about: Help us diagnose and fix bugs in this Function | ||
labels: bug | ||
--- | ||
<!-- | ||
Thank you for helping to improve Crossplane! | ||
Please be sure to search for open issues before raising a new one. We use issues | ||
for bug reports and feature requests. Please find us at https://slack.crossplane.io | ||
for questions, support, and discussion. | ||
--> | ||
|
||
### What happened? | ||
<!-- | ||
Please let us know what behaviour you expected and how this Function diverged | ||
from that behaviour. | ||
--> | ||
|
||
|
||
### How can we reproduce it? | ||
<!-- | ||
Help us to reproduce your bug as succinctly and precisely as possible. Artifacts | ||
such as example manifests or a script that triggers the issue are highly | ||
appreciated! | ||
--> | ||
|
||
### What environment did it happen in? | ||
Function version: | ||
|
||
<!-- | ||
Include at least the version or commit of Crossplane you were running. Consider | ||
also including your: | ||
* Cloud provider or hardware configuration | ||
* Kubernetes version (use `kubectl version`) | ||
* Kubernetes distribution (e.g. Tectonic, GKE, OpenShift) | ||
* OS (e.g. from /etc/os-release) | ||
* Kernel (e.g. `uname -a`) | ||
--> |
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,24 @@ | ||
--- | ||
name: Feature Request | ||
about: Help us make this Function more useful | ||
labels: enhancement | ||
--- | ||
<!-- | ||
Thank you for helping to improve Crossplane! | ||
Please be sure to search for open issues before raising a new one. We use issues | ||
for bug reports and feature requests. Please find us at https://slack.crossplane.io | ||
for questions, support, and discussion. | ||
--> | ||
|
||
### What problem are you facing? | ||
<!-- | ||
Please tell us a little about your use case - it's okay if it's hypothetical! | ||
Leading with this context helps frame the feature request so we can ensure we | ||
implement it sensibly. | ||
---> | ||
|
||
### How could this Function help solve your problem? | ||
<!-- | ||
Let us know how you think this Function could help with your use case. | ||
--> |
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 @@ | ||
<!-- | ||
Thank you for helping to improve Crossplane! | ||
Please read through https://git.io/fj2m9 if this is your first time opening a | ||
Crossplane pull request. Find us in https://slack.crossplane.io/messages/dev if | ||
you need any help contributing. | ||
--> | ||
|
||
### Description of your changes | ||
|
||
<!-- | ||
Briefly describe what this pull request does, and how it is covered by tests. | ||
Be proactive - direct your reviewers' attention to anything that needs special | ||
consideration. | ||
You MUST either [x] check or ~strikethrough~ every item in the checklist below. | ||
We love pull requests that fix an open issue. If yours does, use the below line | ||
to indicate which issue it fixes, for example "Fixes #500". | ||
--> | ||
|
||
Fixes # | ||
|
||
I have: | ||
|
||
- [ ] Read and followed Crossplane's [contribution process]. | ||
- [ ] Added or updated unit tests for my change. | ||
|
||
[contribution process]: https://git.io/fj2m9 | ||
[docs]: https://docs.crossplane.io/contribute/contribute |
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,166 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- release-* | ||
pull_request: {} | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
description: Package version (e.g. v0.1.0) | ||
required: false | ||
|
||
env: | ||
# Common versions | ||
PYTHON_VERSION: '3.11.5' | ||
DOCKER_BUILDX_VERSION: 'v0.11.2' | ||
|
||
# These environment variables are important to the Crossplane CLI install.sh | ||
# script. They determine what version it installs. | ||
XP_CHANNEL: master # TODO(negz): Pin to stable once v1.14 is released. | ||
XP_VERSION: current # TODO(negz): Pin to a version once v1.14 is released. | ||
|
||
# This CI job will automatically push new builds to xpkg.upbound.io if the | ||
# 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 }} | ||
|
||
# The package to push, without a version tag. The default matches GitHub. For | ||
# example xpkg.upbound.io/crossplane/function-template-go. | ||
XPKG: xpkg.upbound.io/${{ github.repository}} | ||
|
||
# The package version to push. The default is 0.0.0-gitsha. | ||
XPKG_VERSION: ${{ inputs.version }} | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ env.PYTHON_VERSION }} | ||
|
||
- name: Setup Hatch | ||
run: pipx install hatch==1.7.0 | ||
|
||
- name: Lint | ||
run: hatch run lint:check | ||
|
||
unit-test: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ env.PYTHON_VERSION }} | ||
|
||
- name: Setup Hatch | ||
run: pipx install hatch==1.7.0 | ||
|
||
- name: Run Unit Tests | ||
run: hatch run test:unit | ||
|
||
# We want to build most packages for the amd64 and arm64 architectures. To | ||
# speed this up we build single-platform packages in parallel. We then upload | ||
# those packages to GitHub as a build artifact. The push job downloads those | ||
# artifacts and pushes them as a single multi-platform package. | ||
build: | ||
runs-on: ubuntu-22.04 | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
arch: | ||
- amd64 | ||
- arm64 | ||
steps: | ||
- name: Setup QEMU | ||
uses: docker/setup-qemu-action@v3 | ||
with: | ||
platforms: all | ||
|
||
- name: Setup Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
with: | ||
version: ${{ env.DOCKER_BUILDX_VERSION }} | ||
install: true | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
# We ask Docker to use GitHub Action's native caching support to speed up | ||
# the build, per https://docs.docker.com/build/cache/backends/gha/. | ||
- name: Build Runtime | ||
id: image | ||
uses: docker/build-push-action@v5 | ||
with: | ||
context: . | ||
platforms: linux/${{ matrix.arch }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max | ||
target: image | ||
build-args: | ||
PYTHON_VERSION=${{ env.PYTHON_VERSION }} | ||
outputs: type=docker,dest=runtime-${{ matrix.arch }}.tar | ||
|
||
- name: Setup the Crossplane CLI | ||
run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh" | ||
|
||
- name: Build Package | ||
run: ./crossplane xpkg build --package-file=${{ matrix.arch }}.xpkg --package-root=package/ --embed-runtime-image-tarball=runtime-${{ matrix.arch }}.tar | ||
|
||
- name: Upload Single-Platform Package | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: packages | ||
path: "*.xpkg" | ||
if-no-files-found: error | ||
retention-days: 1 | ||
|
||
# This job downloads the single-platform packages built by the build job, and | ||
# pushes them as a multi-platform package. We only push the package it the | ||
# XPKG_ACCESS_ID and XPKG_TOKEN secrets were provided. | ||
push: | ||
runs-on: ubuntu-22.04 | ||
needs: | ||
- build | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Download Single-Platform Packages | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: packages | ||
path: . | ||
|
||
- name: Setup the Crossplane CLI | ||
run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh" | ||
|
||
- name: Login to Upbound | ||
uses: docker/login-action@v3 | ||
if: env.XPKG_ACCESS_ID != '' | ||
with: | ||
registry: xpkg.upbound.io | ||
username: ${{ secrets.XPKG_ACCESS_ID }} | ||
password: ${{ secrets.XPKG_TOKEN }} | ||
|
||
# If a version wasn't explicitly passed as a workflow_dispatch input we | ||
# default to version v0.0.0-<git-commit-date>-<git-short-sha>, for example | ||
# v0.0.0-20231101115142-1091066df799. This is a simple implementation of | ||
# Go's pseudo-versions: https://go.dev/ref/mod#pseudo-versions. | ||
- name: Set Default Multi-Platform Package Version | ||
if: env.XPKG_VERSION == '' | ||
run: echo "XPKG_VERSION=v0.0.0-$(date -d@$(git show -s --format=%ct) +%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)" >> $GITHUB_ENV | ||
|
||
- name: Push Multi-Platform Package to Upbound | ||
if: env.XPKG_ACCESS_ID != '' | ||
run: "./crossplane --verbose xpkg push --package-files $(echo *.xpkg|tr ' ' ,) ${{ env.XPKG }}:${{ env.XPKG_VERSION }}" |
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,39 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
# It's important that this is Debian 12 to match the distroless image. | ||
FROM debian:12-slim AS build | ||
|
||
RUN --mount=type=cache,target=/var/lib/apt/lists \ | ||
--mount=type=cache,target=/var/cache/apt \ | ||
rm -f /etc/apt/apt.conf.d/docker-clean \ | ||
&& apt-get update \ | ||
&& apt-get install --no-install-recommends --yes python3-venv git | ||
|
||
# Don't write .pyc bytecode files. These speed up imports when the program is | ||
# loaded. There's no point doing that in a container where they'll never be | ||
# persisted across restarts. | ||
ENV PYTHONDONTWRITEBYTECODE=true | ||
|
||
# Use Hatch to build a wheel. The build stage must do this in a venv because | ||
# Debian doesn't have a hatch package, and it won't let you install one globally | ||
# using pip. | ||
WORKDIR /build | ||
RUN --mount=target=. \ | ||
--mount=type=cache,target=/root/.cache/pip \ | ||
python3 -m venv /venv/build \ | ||
&& /venv/build/bin/pip install hatch \ | ||
&& /venv/build/bin/hatch build -t wheel /whl | ||
|
||
# Create a fresh venv and install only the function wheel into it. | ||
RUN --mount=type=cache,target=/root/.cache/pip \ | ||
python3 -m venv /venv/fn \ | ||
&& /venv/fn/bin/pip install /whl/*.whl | ||
|
||
# Copy the function venv to our runtime stage. It's important that the path be | ||
# the same as in the build stage, to avoid shebang paths and symlinks breaking. | ||
FROM gcr.io/distroless/python3-debian12 AS image | ||
WORKDIR / | ||
COPY --from=build /venv/fn /venv/fn | ||
EXPOSE 9443 | ||
USER nonroot:nonroot | ||
ENTRYPOINT ["/venv/fn/bin/function"] |
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 |
---|---|---|
@@ -1,2 +1,39 @@ | ||
# function-template-python | ||
A template for writing a composition function in Python | ||
[![CI](https://github.com/crossplane/function-template-python/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 [Python][python]. | ||
|
||
To learn how to use this template: | ||
|
||
* [Learn about how composition functions work][functions] | ||
|
||
If you just want to jump in and get started: | ||
|
||
1. Replace `function-template-python` with your function's name in | ||
`pyproject.toml` and `package/crossplane.yaml`. | ||
1. Add your logic to `RunFunction` in `function/fn.py` | ||
1. Add tests for your logic in `test/test_fn.py` | ||
1. Update this file, `README.md`, to be about your function! | ||
|
||
This template uses [Python][python], [Docker][docker], and the [Crossplane | ||
CLI][cli] to build functions. | ||
|
||
```shell | ||
# Lint the code - see pyproject.toml | ||
hatch run lint:check | ||
|
||
# Run unit tests - see tests/test_fn.py | ||
hatch run test:unit | ||
|
||
# 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 | ||
``` | ||
|
||
[functions]: https://docs.crossplane.io/latest/concepts/composition-functions | ||
[python]: https://python.io | ||
[package docs]: https://pkg.go.dev/github.com/crossplane/function-sdk-go | ||
[docker]: https://www.docker.com | ||
[cli]: https://docs.crossplane.io/latest/cli |
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,25 @@ | ||
# Example manifests | ||
|
||
You can run your function locally and test it using `crossplane beta render` | ||
with these example manifests. | ||
|
||
```shell | ||
# Run the function locally | ||
$ hatch run python function/main.py --insecure --debug | ||
``` | ||
|
||
```shell | ||
# Then, in another terminal, call it with these example manifests | ||
$ crossplane beta render xr.yaml composition.yaml functions.yaml -r | ||
--- | ||
apiVersion: example.crossplane.io/v1 | ||
kind: XR | ||
metadata: | ||
name: example-xr | ||
--- | ||
apiVersion: render.crossplane.io/v1beta1 | ||
kind: Result | ||
message: I was run with input "Hello world"! | ||
severity: SEVERITY_NORMAL | ||
step: run-the-template | ||
``` |
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,17 @@ | ||
apiVersion: apiextensions.crossplane.io/v1 | ||
kind: Composition | ||
metadata: | ||
name: function-template-python | ||
spec: | ||
compositeTypeRef: | ||
apiVersion: example.crossplane.io/v1 | ||
kind: XR | ||
mode: Pipeline | ||
pipeline: | ||
- step: run-the-template | ||
functionRef: | ||
name: function-template-python | ||
input: | ||
apiVersion: template.fn.crossplane.io/v1beta1 | ||
kind: Input | ||
example: "Hello world" |
Oops, something went wrong.