Skip to content

Commit f221df1

Browse files
Merge pull request #2 from stevendborrelli/cel
Add Conditionals via CEL, change name of repo
2 parents 659dad3 + a0618e5 commit f221df1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1942
-515
lines changed

.github/workflows/ci.yml

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- release-*
8+
pull_request: {}
9+
workflow_dispatch:
10+
inputs:
11+
version:
12+
description: Package version (e.g. v0.1.0)
13+
required: false
14+
15+
env:
16+
# Common versions
17+
GO_VERSION: '1.21.3'
18+
GOLANGCI_VERSION: 'v1.54.2'
19+
DOCKER_BUILDX_VERSION: 'v0.11.2'
20+
21+
# These environment variables are important to the Crossplane CLI install.sh
22+
# script. They determine what version it installs.
23+
XP_CHANNEL: master # TODO(negz): Pin to stable once v1.14 is released.
24+
XP_VERSION: current # TODO(negz): Pin to a version once v1.14 is released.
25+
26+
# This CI job will automatically push new builds to xpkg.upbound.io if the
27+
# XPKG_ACCESS_ID and XPKG_TOKEN secrets are set in the GitHub respository (or
28+
# organization) settings. Create a token at https://accounts.upbound.io.
29+
XPKG_ACCESS_ID: ${{ secrets.XPKG_ACCESS_ID }}
30+
31+
# The package to push, without a version tag. The default matches GitHub. For
32+
# example xpkg.upbound.io/crossplane/function-template-go.
33+
XPKG: xpkg.upbound.io/${{ github.repository}}
34+
35+
# The package version to push. The default is 0.0.0-gitsha.
36+
XPKG_VERSION: ${{ inputs.version }}
37+
38+
jobs:
39+
lint:
40+
runs-on: ubuntu-22.04
41+
steps:
42+
- name: Checkout
43+
uses: actions/checkout@v4
44+
45+
- name: Setup Go
46+
uses: actions/setup-go@v4
47+
with:
48+
go-version: ${{ env.GO_VERSION }}
49+
cache: false # The golangci-lint action does its own caching.
50+
51+
- name: Lint
52+
uses: golangci/golangci-lint-action@v3
53+
with:
54+
version: ${{ env.GOLANGCI_VERSION }}
55+
56+
unit-test:
57+
runs-on: ubuntu-22.04
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v4
61+
62+
- name: Setup Go
63+
uses: actions/setup-go@v4
64+
with:
65+
go-version: ${{ env.GO_VERSION }}
66+
67+
- name: Run Unit Tests
68+
run: go test -v -cover ./...
69+
70+
# We want to build most packages for the amd64 and arm64 architectures. To
71+
# speed this up we build single-platform packages in parallel. We then upload
72+
# those packages to GitHub as a build artifact. The push job downloads those
73+
# artifacts and pushes them as a single multi-platform package.
74+
build:
75+
runs-on: ubuntu-22.04
76+
strategy:
77+
fail-fast: true
78+
matrix:
79+
arch:
80+
- amd64
81+
- arm64
82+
steps:
83+
- name: Setup QEMU
84+
uses: docker/setup-qemu-action@v3
85+
with:
86+
platforms: all
87+
88+
- name: Setup Docker Buildx
89+
uses: docker/setup-buildx-action@v3
90+
with:
91+
version: ${{ env.DOCKER_BUILDX_VERSION }}
92+
install: true
93+
94+
- name: Checkout
95+
uses: actions/checkout@v4
96+
97+
# We ask Docker to use GitHub Action's native caching support to speed up
98+
# the build, per https://docs.docker.com/build/cache/backends/gha/.
99+
- name: Build Runtime
100+
id: image
101+
uses: docker/build-push-action@v5
102+
with:
103+
context: .
104+
platforms: linux/${{ matrix.arch }}
105+
cache-from: type=gha
106+
cache-to: type=gha,mode=max
107+
target: image
108+
build-args:
109+
GO_VERSION=${{ env.GO_VERSION }}
110+
outputs: type=docker,dest=runtime-${{ matrix.arch }}.tar
111+
112+
- name: Setup the Crossplane CLI
113+
run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh"
114+
115+
- name: Build Package
116+
run: ./crossplane xpkg build --package-file=${{ matrix.arch }}.xpkg --package-root=package/ --embed-runtime-image-tarball=runtime-${{ matrix.arch }}.tar
117+
118+
- name: Upload Single-Platform Package
119+
uses: actions/upload-artifact@v3
120+
with:
121+
name: packages
122+
path: "*.xpkg"
123+
if-no-files-found: error
124+
retention-days: 1
125+
126+
# This job downloads the single-platform packages built by the build job, and
127+
# pushes them as a multi-platform package. We only push the package it the
128+
# XPKG_ACCESS_ID and XPKG_TOKEN secrets were provided.
129+
push:
130+
runs-on: ubuntu-22.04
131+
needs:
132+
- build
133+
steps:
134+
- name: Checkout
135+
uses: actions/checkout@v4
136+
137+
- name: Download Single-Platform Packages
138+
uses: actions/download-artifact@v3
139+
with:
140+
name: packages
141+
path: .
142+
143+
- name: Setup the Crossplane CLI
144+
run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh"
145+
146+
- name: Login to Upbound
147+
uses: docker/login-action@v3
148+
if: env.XPKG_ACCESS_ID != ''
149+
with:
150+
registry: xpkg.upbound.io
151+
username: ${{ secrets.XPKG_ACCESS_ID }}
152+
password: ${{ secrets.XPKG_TOKEN }}
153+
154+
# If a version wasn't explicitly passed as a workflow_dispatch input we
155+
# default to version v0.0.0-<git-commit-date>-<git-short-sha>, for example
156+
# v0.0.0-20231101115142-1091066df799. This is a simple implementation of
157+
# Go's pseudo-versions: https://go.dev/ref/mod#pseudo-versions.
158+
- name: Set Default Multi-Platform Package Version
159+
if: env.XPKG_VERSION == ''
160+
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
161+
162+
- name: Push Multi-Platform Package to Upbound
163+
if: env.XPKG_ACCESS_ID != ''
164+
run: "./crossplane --verbose xpkg push --package-files $(echo *.xpkg|tr ' ' ,) ${{ env.XPKG }}:${{ env.XPKG_VERSION }}"

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ linters-settings:
3838
- default
3939
- prefix(github.com/crossplane/crossplane-runtime)
4040
- prefix(github.com/crossplane/function-sdk-go)
41-
- prefix(github.com/crossplane-contrib/function-patch-and-transform)
41+
- prefix(github.com/stevendborrelli/function-conditional-patch-and-transform)
4242
- blank
4343
- dot
4444

Dockerfile

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
FROM golang:1.20 as build-stage
1+
# syntax=docker/dockerfile:1
22

3-
WORKDIR /fn
4-
5-
COPY go.mod go.sum ./
6-
RUN go mod download
7-
8-
COPY input/ ./input
9-
COPY *.go ./
10-
11-
RUN CGO_ENABLED=0 go build -o /function .
12-
13-
FROM debian:12.1-slim as package-stage
14-
15-
# TODO(negz): Use a proper Crossplane package building tool. We're abusing the
16-
# fact that this image won't have an io.crossplane.pkg: base annotation. This
17-
# means Crossplane package manager will pull this entire ~100MB image, which
18-
# also happens to contain a valid Function runtime.
19-
# https://github.com/crossplane/crossplane/blob/v1.13.2/contributing/specifications/xpkg.md
20-
WORKDIR /package
21-
COPY package/ ./
3+
# We use the latest Go 1.x version unless asked to use something else.
4+
ARG GO_VERSION=1
225

23-
RUN cat crossplane.yaml > /package.yaml
24-
RUN cat input/*.yaml >> /package.yaml
6+
# Setup the base environment.
7+
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS base
258

26-
FROM gcr.io/distroless/base-debian11 AS build-release-stage
9+
WORKDIR /fn
10+
ENV CGO_ENABLED=0
2711

12+
COPY go.mod go.sum ./
13+
RUN --mount=type=cache,target=/go/pkg/mod go mod download
14+
15+
# Build the Function.
16+
FROM base AS build
17+
ARG TARGETOS
18+
ARG TARGETARCH
19+
RUN --mount=target=. \
20+
--mount=type=cache,target=/go/pkg/mod \
21+
--mount=type=cache,target=/root/.cache/go-build \
22+
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /function .
23+
24+
# Produce the Function image.
25+
FROM gcr.io/distroless/base-debian11 AS image
2826
WORKDIR /
29-
30-
COPY --from=build-stage /function /function
31-
COPY --from=package-stage /package.yaml /package.yaml
32-
27+
COPY --from=build /function /function
3328
EXPOSE 9443
34-
3529
USER nonroot:nonroot
36-
37-
ENTRYPOINT ["/function"]
30+
ENTRYPOINT ["/function"]

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
DOCKER := docker
3+
GO := go
4+
5+
GOLANGCI_VERSION := 1.55.2
6+
7+
test:
8+
$(GO) test ./...
9+
10+
lint:
11+
$(DOCKER) run --rm -v $(CURDIR):/app -v ~/.cache/golangci-lint/v$(GOLANGCI_VERSION):/root/.cache -w /app golangci/golangci-lint:v$(GOLANGCI_VERSION) golangci-lint run --fix
12+
13+
reviewable: test lint

0 commit comments

Comments
 (0)