Skip to content

Commit eea1d7e

Browse files
authored
Merge pull request #22 from timebertt/ko
Use ko to build images everywhere
2 parents 818bf72 + 7035f24 commit eea1d7e

File tree

5 files changed

+56
-113
lines changed

5 files changed

+56
-113
lines changed

.github/workflows/verify-build.yaml

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,73 +30,60 @@ jobs:
3030
- uses: actions/checkout@v3
3131
with:
3232
path: go/src/github.com/timebertt/kubernetes-controller-sharding
33-
- name: Setup Go
34-
uses: actions/setup-go@v4
33+
- uses: actions/setup-go@v4
3534
with:
3635
go-version-file: go/src/github.com/timebertt/kubernetes-controller-sharding/webhosting-operator/go.mod
3736
cache-dependency-path: go/src/github.com/timebertt/kubernetes-controller-sharding/webhosting-operator/go.sum
3837
- name: Verify
3938
run: make -C webhosting-operator verify
4039

41-
buildAndPush:
40+
images:
4241
runs-on: ubuntu-latest
43-
strategy:
44-
matrix:
45-
include:
46-
- name: webhosting-operator
47-
context: webhosting-operator
4842
steps:
4943
- uses: actions/checkout@v3
50-
- name: Cache Docker layers
51-
uses: actions/cache@v3
44+
- uses: actions/setup-go@v4
5245
with:
53-
path: /tmp/.buildx-cache
54-
key: ${{ runner.os }}-buildx-${{ github.sha }}
55-
restore-keys: |
56-
${{ runner.os }}-buildx-
57-
- name: Setup Docker buildx
58-
uses: docker/setup-buildx-action@v2
46+
go-version-file: webhosting-operator/go.mod
47+
cache-dependency-path: webhosting-operator/go.sum
48+
- uses: ko-build/[email protected]
5949
with:
60-
install: true
61-
- name: Login to GHCR
62-
if: github.event_name != 'pull_request'
63-
uses: docker/login-action@v2
64-
with:
65-
registry: ghcr.io
66-
username: ${{ github.repository_owner }}
67-
password: ${{ secrets.GITHUB_TOKEN }}
68-
69-
- uses: docker/metadata-action@v4
50+
version: v0.14.1
51+
- name: Prepare image metadata
7052
id: meta
71-
with:
72-
images: |
73-
ghcr.io/${{ github.repository }}/${{ matrix.name }}
74-
tags: |
75-
type=ref,event=branch
76-
type=ref,event=tag
77-
type=ref,event=pr
78-
type=sha
79-
flavor: |
80-
latest=${{ endsWith(github.ref, github.event.repository.default_branch) }}
53+
# generate comma-separated tags and labels for image build (similar to docker/metadata-action)
54+
run: |
55+
short_ref=${{ github.ref_name }}
56+
if ${{ github.event_name == 'pull_request' }} ; then
57+
# for PR events, ref_name is '<pr-number>/merge' but tags may not contain /
58+
short_ref=pr-${{ github.event.pull_request.number }}
59+
fi
8160
82-
- name: Build and push
83-
uses: docker/build-push-action@v4
84-
with:
85-
context: ${{ matrix.context }}
86-
target: ${{ matrix.name }}
87-
platforms: linux/amd64
88-
push: ${{ github.event_name != 'pull_request' }}
89-
tags: ${{ steps.meta.outputs.tags }}
90-
labels: ${{ steps.meta.outputs.labels }}
91-
cache-from: type=local,src=/tmp/.buildx-cache
92-
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
93-
secrets: |
94-
GIT_AUTH_TOKEN=${{ secrets.GITHUB_TOKEN }}
61+
tags=(
62+
$short_ref
63+
sha-$( echo ${{ github.sha }} | head -c7 )
64+
)
9565
96-
- name: Move cache
97-
# Temp fix
98-
# https://github.com/docker/build-push-action/issues/252
99-
# https://github.com/moby/buildkit/issues/1896
66+
if ${{ github.ref_name == 'master' }} ; then
67+
tags+=( latest )
68+
fi
69+
70+
echo "tags=$(IFS=, ; echo "${tags[*]}")" >> $GITHUB_OUTPUT
71+
72+
labels=(
73+
org.opencontainers.image.created=$( date -u +%Y-%m-%dT%H:%M:%SZ )
74+
org.opencontainers.image.licenses=Apache-2.0
75+
org.opencontainers.image.revision=${{ github.sha }}
76+
org.opencontainers.image.source=https://github.com/${{ github.repository }}
77+
org.opencontainers.image.url=https://github.com/${{ github.repository }}
78+
org.opencontainers.image.version=$short_ref
79+
)
80+
81+
echo "labels=$(IFS=, ; echo "${labels[*]}")" >> $GITHUB_OUTPUT
82+
- name: ko build
10083
run: |
101-
rm -rf /tmp/.buildx-cache
102-
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
84+
set -ex
85+
86+
cd ./webhosting-operator
87+
ko build --push=${{ github.event_name != 'pull_request' }} --sbom none --base-import-paths --platform linux/amd64,linux/arm64 \
88+
--tags "${{ steps.meta.outputs.tags }}" --image-label "${{ steps.meta.outputs.labels }}" \
89+
./cmd/webhosting-operator

webhosting-operator/.dockerignore

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

webhosting-operator/Dockerfile

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

webhosting-operator/Makefile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
22

33
# Image URL to use all building/pushing image targets
44
TAG ?= latest
5-
CONTROLLER_IMG ?= ghcr.io/timebertt/kubernetes-controller-sharding/webhosting-operator:$(TAG)
5+
GHCR_REPO ?= ghcr.io/timebertt/kubernetes-controller-sharding
6+
OPERATOR_IMG ?= $(GHCR_REPO)/webhosting-operator:$(TAG)
7+
68
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
79
ENVTEST_K8S_VERSION = 1.24
810
# set OVERLAY to shoot to configure ingress-nginx with public dns and a TLS certificate
@@ -101,13 +103,12 @@ build: generate fmt vet ## Build manager binary.
101103
run: manifests generate fmt vet ## Run the webhosting-operator from your host.
102104
go run ./cmd/webhosting-operator
103105

104-
.PHONY: docker-build
105-
docker-build: test ## Build docker image with the manager.
106-
docker build -t $(CONTROLLER_IMG) --target webhosting-operator .
106+
PUSH ?= false
107+
images: export KO_DOCKER_REPO = $(GHCR_REPO)
107108

108-
.PHONY: docker-push
109-
docker-push: ## Push docker image with the manager.
110-
docker push $(CONTROLLER_IMG)
109+
.PHONY: images
110+
images: $(KO) ## Build and push container images using ko.
111+
$(KO) build --push=$(PUSH) --sbom none --base-import-paths -t $(TAG) --platform linux/amd64,linux/arm64 ./cmd/webhosting-operator
111112

112113
##@ Deployment
113114

@@ -136,7 +137,7 @@ up dev down: export SKAFFOLD_LABEL = skaffold.dev/run-id=webhosting-operator
136137

137138
.PHONY: deploy
138139
deploy: $(SKAFFOLD) ## Build all images and deploy everything to K8s cluster specified in $KUBECONFIG.
139-
$(SKAFFOLD) deploy --port-forward=user --tail -i $(CONTROLLER_IMG)
140+
$(SKAFFOLD) deploy --port-forward=user --tail -i $(OPERATOR_IMG)
140141

141142
.PHONY: up
142143
up: $(SKAFFOLD) ## Build all images, deploy everything to K8s cluster specified in $KUBECONFIG, start port-forward and tail logs.

webhosting-operator/tools.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ $(KIND): $(call tool_version_file,$(KIND),$(KIND_VERSION))
2828
curl -L -o $(KIND) https://kind.sigs.k8s.io/dl/$(KIND_VERSION)/kind-$(shell uname -s | tr '[:upper:]' '[:lower:]')-$(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
2929
chmod +x $(KIND)
3030

31+
KO := $(TOOLS_BIN_DIR)/ko
32+
KO_VERSION ?= v0.14.1
33+
$(KO): $(call tool_version_file,$(KO),$(KO_VERSION))
34+
GOBIN=$(abspath $(TOOLS_BIN_DIR)) go install github.com/google/ko@$(KO_VERSION)
35+
3136
KUBECTL := $(TOOLS_BIN_DIR)/kubectl
3237
KUBECTL_VERSION ?= v1.27.3
3338
$(KUBECTL): $(call tool_version_file,$(KUBECTL),$(KUBECTL_VERSION))

0 commit comments

Comments
 (0)