Skip to content

Commit 0f08be4

Browse files
committed
chore: migrate to standard build
Adapts the team's standard Makefile to use on GitHub, and migrates the controller's Docker base image library/golang and distroless.
1 parent de44ccc commit 0f08be4

File tree

12 files changed

+147
-125
lines changed

12 files changed

+147
-125
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/

.github/workflows/docker.yaml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
name: Docker
22
on:
3-
push:
4-
tags:
5-
- 'v*'
3+
- pull_request
4+
- push
65
jobs:
7-
publish-docker:
6+
docker:
87
runs-on: ubuntu-latest
98
steps:
10-
- uses: actions/checkout@v2
11-
- uses: nixbuild/nix-quick-install-action@v4
12-
- run: nix-build ./hack/docker.nix -o docker-amd64
13-
- run: nix-build ./hack/docker.nix --arg pkgs '(import ./hack/nixpkgs.nix {}).pkgsCross.aarch64-multiplatform' -o docker-arm64
14-
- run: |
15-
nix run -f ./hack/nixpkgs.nix pkgs.buildah<<EOF
16-
buildah manifest create origin-ca-issuer
17-
buildah manifest add origin-ca-issuer docker-archive:./docker-amd64
18-
buildah manifest add origin-ca-issuer docker-archive:./docker-arm64
19-
buildah manifest inspect origin-ca-issuer
20-
buildah manifest push --all --creds ${DOCKER_HUB_USERNAME}:${DOCKER_HUB_TOKEN} -f v2s2 origin-ca-issuer docker://cloudflare/origin-ca-issuer:${GITHUB_REF#refs/tags/}
21-
EOF
22-
env:
23-
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
24-
DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }}
9+
- uses: actions/checkout@v4
10+
- uses: docker/setup-qemu-action@v3
11+
- uses: docker/metadata-action@v5
12+
id: docker-meta
13+
with:
14+
images: cloudflare/origin-ca-issuer
15+
- uses: docker/setup-buildx-action@v3
16+
- uses: docker/login-action@v3
17+
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
18+
with:
19+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
20+
password: ${{ secrets.DOCKER_HUB_TOKEN }}
21+
- uses: docker/build-push-action@v5
22+
with:
23+
file: ./cmd/controller/Dockerfile
24+
platforms: linux/amd64, linux/arm64
25+
tags: ${{ steps.docker-meta.outputs.tags }}
26+
push: ${{ startsWith(github.ref, 'refs/tags/v') }}

.github/workflows/tests.yaml

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,38 @@ on:
55
jobs:
66
unit:
77
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
go: [ 'stable', 'oldstable' ]
11+
name: 'Go ${{ matrix.go }} Test'
812
steps:
9-
- uses: actions/checkout@v2
10-
- uses: nixbuild/nix-quick-install-action@v4
11-
- run: nix-shell --pure --run "go test -v -race ./..."
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-go@v4
15+
with:
16+
go-version: ${{ matrix.go }}
17+
- run: make test
1218
lint:
1319
runs-on: ubuntu-latest
1420
steps:
15-
- uses: actions/checkout@v2
16-
- uses: nixbuild/nix-quick-install-action@v4
17-
- run: nix-shell --pure --run "golangci-lint run --timeout 15m -e 'please use pkg/envtest for testing'"
21+
- uses: actions/checkout@v4
22+
- uses: actions/setup-go@v4
23+
with:
24+
go-version: 'stable'
25+
- uses: dominikh/staticcheck-action@v1
26+
with:
27+
build-tags: suite
28+
install-go: false
1829
integration:
1930
needs:
2031
- unit
2132
- lint
2233
runs-on: ubuntu-latest
2334
steps:
24-
- uses: actions/checkout@v2
25-
- uses: nixbuild/nix-quick-install-action@v4
26-
- run: nix-shell --pure --run "go test ./... -tags suite"
35+
- uses: actions/checkout@v4
36+
- uses: actions/setup-go@v4
37+
with:
38+
go-version: 'stable'
39+
- run: |
40+
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
41+
source <(setup-envtest use -p env)
42+
go test ./... -tags suite

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
result
22
result-*
33
.envrc
4+
/bin/

Makefile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.DEFAULT_GOAL := binaries
2+
3+
KERNEL := $(shell uname -s)
4+
GOTESTSUM := $(shell command -v gotestsum 2> /dev/null)
5+
6+
DIB ?= docker
7+
IMAGE_ROOT ?= localhost/origin-ca-issuer
8+
IMAGE_VERSION ?= $(shell git log -1 --pretty=format:%cd-%h --date short HEAD)
9+
VERSION := $(shell git describe --tags --always --dirty=-dev)
10+
# Build docker images for the native arch, but allow overriding in the environment for local development
11+
PLATFORM ?= local
12+
13+
# Bind mount $SSL_CERT_FILE (or default) to build container if the file exists.
14+
SSL_CERT_FILE ?= /etc/ssl/certs/ca-certificates.crt
15+
ifneq (,$(wildcard ${SSL_CERT_FILE}))
16+
SECRETS = --secret id=certificates,src=${SSL_CERT_FILE}
17+
endif
18+
19+
# When compiling for Linux enable Security's recommend hardening to satisfy `checksec' checks.
20+
# Unfortunately, most of these flags aren't portable to other operating systems.
21+
ifeq (${KERNEL},Linux)
22+
CGO_ENABLED ?= 1
23+
CPPFLAGS ?= -D_FORTIFY_SOURCE=2 -fstack-protector-all
24+
CFLAGS ?= -O2 -pipe -fno-plt
25+
CXXFLAGS ?= -O2 -pipe -fno-plt
26+
LDFLAGS ?= -Wl,-O1,-sort-common,-as-needed,-z,relro,-z,now
27+
GO_LDFLAGS ?= -linkmode=external
28+
GOFLAGS ?= -buildmode=pie
29+
endif
30+
31+
GO_LDFLAGS += -w -s -X main.version=${VERSION}
32+
GOFLAGS += -v
33+
34+
export CGO_ENABLED
35+
export CGO_CPPFLAGS ?= ${CPPFLAGS}
36+
export CGO_CFLAGS ?= ${CFLAGS}
37+
export CGO_CXXFLAGS ?= ${CXXFLAGS}
38+
export CGO_LDFLAGS ?= ${LDFLAGS}
39+
40+
CMDS := $(shell find cmd -mindepth 1 -maxdepth 1 -type d | awk -F '/' '{ print $$NF }' )
41+
IMAGES := $(shell find cmd -mindepth 1 -type f -name Dockerfile | awk -F '/' '{ print $$2 }')
42+
43+
define make-go-target
44+
.PHONY: bin/$1
45+
bin/$1:
46+
go build ${GOFLAGS} -o $$@ -ldflags "${GO_LDFLAGS}" ./cmd/$1
47+
endef
48+
49+
define make-dib-targets
50+
.PHONY: images/$1
51+
images/$1:
52+
${DIB} buildx build --platform "$(PLATFORM)" ${SECRETS} -f cmd/$1/Dockerfile -t "${IMAGE_ROOT}/$1:${IMAGE_VERSION}" .
53+
54+
.PHONY: push/images/$1
55+
push/images/$1:
56+
${DIB} push "${IMAGE_ROOT}/$1:${IMAGE_VERSION}"
57+
endef
58+
59+
$(foreach element,$(CMDS), $(eval $(call make-go-target,$(element))))
60+
$(foreach element,$(IMAGES), $(eval $(call make-dib-targets,$(element))))
61+
62+
.PHONY: binaries
63+
binaries: $(CMDS:%=bin/%)
64+
65+
.PHONY: images
66+
images: $(IMAGES:%=images/%)
67+
68+
.PHONY: push-images
69+
push-images: $(IMAGES:%=push/images/%)
70+
71+
.PHONY: clean
72+
clean:
73+
rm -rf bin
74+
75+
.PHONY: test
76+
test:
77+
ifdef GOTESTSUM
78+
"${GOTESTSUM}" -- -count 1 ./...
79+
else
80+
go test -cover -count 1 ./...
81+
endif
82+
83+
.PHONY: lint
84+
lint:
85+
staticcheck -tags suite ./...

cmd/controller/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM docker.io/library/golang:1.21.5-bookworm AS builder
2+
WORKDIR /go/src/app
3+
ADD . /go/src/app
4+
5+
RUN --mount=type=cache,target=/go/pkg/mod \
6+
--mount=type=cache,target=/root/.cache/go-build \
7+
--mount=type=secret,id=certificates,target=/etc/ssl/certs/ca-certificates.crt \
8+
make bin/controller
9+
10+
11+
FROM gcr.io/distroless/base-nossl-debian12:nonroot
12+
COPY --from=builder /go/src/app/bin/controller /bin
13+
ENTRYPOINT ["/bin/controller"]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/cloudflare/origin-ca-issuer
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/cert-manager/cert-manager v1.9.2

hack/boilerplate.go.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

hack/derivation.nix

Lines changed: 0 additions & 14 deletions
This file was deleted.

hack/docker.nix

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)