Skip to content

Commit 12cda18

Browse files
authored
Merge pull request #274 from Kuadrant/images-speed-up
refactor: cross compile for faster image building
2 parents adc0e5c + a42ec7b commit 12cda18

3 files changed

Lines changed: 94 additions & 97 deletions

File tree

.github/workflows/build-images.yaml

Lines changed: 80 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ on:
1717
default: stable
1818

1919
env:
20-
IMG_TAGS: ${{ github.sha }}
2120
IMG_REGISTRY_HOST: quay.io
2221
IMG_REGISTRY_ORG: kuadrant
2322
MAIN_BRANCH_NAME: main
@@ -32,16 +31,8 @@ jobs:
3231
steps:
3332
- name: Check out code
3433
uses: actions/checkout@v4
35-
- name: Add latest tag
36-
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
37-
id: add-latest-tag
38-
run: |
39-
echo "IMG_TAGS=latest ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
40-
- name: Add branch tag
41-
if: ${{ github.ref_name != env.MAIN_BRANCH_NAME }}
42-
id: add-branch-tag
43-
run: |
44-
echo "IMG_TAGS=${GITHUB_REF_NAME/\//-} ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3
4536
- name: Set Operator version
4637
id: operator-version
4738
run: |
@@ -50,39 +41,45 @@ jobs:
5041
else
5142
echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV
5243
fi
53-
- name: Set up QEMU
54-
uses: docker/setup-qemu-action@v3
44+
- name: Extract metadata
45+
id: meta
46+
uses: docker/metadata-action@v5
47+
with:
48+
images: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}
49+
tags: |
50+
type=raw,value=${{ github.sha }}
51+
type=raw,value=latest,enable={{is_default_branch}}
52+
type=ref,event=branch,enable={{is_not_default_branch}}
5553
- name: Install yq dependency
5654
run: make yq
5755
- name: Set default authorino image
5856
run: |
5957
echo "DEFAULT_AUTHORINO_IMAGE=$(./bin/yq e -e '.config.authorinoImage' ${{ env.BUILD_CONFIG_FILE }} || echo ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/authorino:${{ env.LATEST_AUTHORINO_GITREF }})" >> $GITHUB_ENV
58+
- name: Login to Container Registry
59+
if: ${{ !env.ACT }}
60+
uses: docker/login-action@v3
61+
with:
62+
registry: ${{ env.IMG_REGISTRY_HOST }}
63+
username: ${{ secrets.IMG_REGISTRY_USERNAME }}
64+
password: ${{ secrets.IMG_REGISTRY_TOKEN }}
6065
- name: Build Image
6166
id: build-image
62-
uses: redhat-actions/buildah-build@v2
67+
uses: docker/build-push-action@v5
6368
with:
64-
image: ${{ env.OPERATOR_NAME }}
65-
tags: ${{ env.IMG_TAGS }}
69+
context: .
6670
platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
71+
push: ${{ !env.ACT }}
72+
tags: ${{ steps.meta.outputs.tags }}
6773
build-args: |
6874
OPERATOR_VERSION=${{ env.VERSION }}
6975
GIT_SHA=${{ github.sha }}
7076
DIRTY=false
7177
DEFAULT_AUTHORINO_IMAGE=${{ env.DEFAULT_AUTHORINO_IMAGE }}
72-
containerfiles: |
73-
./Dockerfile
74-
- name: Push Image
75-
if: ${{ !env.ACT }}
76-
id: push-to-quay
77-
uses: redhat-actions/push-to-registry@v2
78-
with:
79-
image: ${{ steps.build-image.outputs.image }}
80-
tags: ${{ steps.build-image.outputs.tags }}
81-
registry: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}
82-
username: ${{ secrets.IMG_REGISTRY_USERNAME }}
83-
password: ${{ secrets.IMG_REGISTRY_TOKEN }}
78+
provenance: false
8479
- name: Print Image URL
85-
run: echo "Image pushed to ${{ steps.push-to-quay.outputs.registry-paths }}"
80+
run: |
81+
echo "Image(s) pushed:"
82+
echo "${{ steps.meta.outputs.tags }}"
8683
8784
build-bundle:
8885
needs: build
@@ -97,18 +94,8 @@ jobs:
9794
with:
9895
go-version-file: go.mod
9996
id: go
100-
- name: Add latest tag
101-
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
102-
id: add-latest-tag
103-
run: |
104-
echo "IMG_TAGS=latest ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
105-
- name: Add release tag
106-
if: ${{ github.ref_name != env.MAIN_BRANCH_NAME }}
107-
id: add-branch-tag
108-
run: |
109-
TAG_NAME=${GITHUB_REF_NAME/\//-}
110-
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV
111-
echo "IMG_TAGS=${TAG_NAME} ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
97+
- name: Set up Docker Buildx
98+
uses: docker/setup-buildx-action@v3
11299
- name: Set Operator version
113100
id: operator-version
114101
run: |
@@ -118,8 +105,15 @@ jobs:
118105
else
119106
echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV
120107
fi
121-
- name: Set up QEMU
122-
uses: docker/setup-qemu-action@v3
108+
- name: Extract metadata
109+
id: meta
110+
uses: docker/metadata-action@v5
111+
with:
112+
images: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-bundle
113+
tags: |
114+
type=raw,value=${{ github.sha }}
115+
type=raw,value=latest,enable={{is_default_branch}}
116+
type=ref,event=branch,enable={{is_not_default_branch}}
123117
- name: Run make bundle (main)
124118
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
125119
run: make bundle REGISTRY=${{ env.IMG_REGISTRY_HOST }} ORG=${{ env.IMG_REGISTRY_ORG }} IMAGE_TAG=${{ github.sha }} AUTHORINO_VERSION=${{ env.LATEST_AUTHORINO_GITREF }}
@@ -134,29 +128,29 @@ jobs:
134128
- name: Verify manifests and bundle (release)
135129
if: ${{ github.ref_name != env.MAIN_BRANCH_NAME }}
136130
run: make verify-manifests verify-bundle REGISTRY=${{ env.IMG_REGISTRY_HOST }} ORG=${{ env.IMG_REGISTRY_ORG }} VERSION=${{env.VERSION}} AUTHORINO_VERSION=${{ github.event.inputs.authorinoVersion }} CHANNELS=${{ github.event.inputs.channels }} DEFAULT_CHANNEL=stable
131+
- name: Login to Container Registry
132+
if: ${{ !env.ACT }}
133+
uses: docker/login-action@v3
134+
with:
135+
registry: ${{ env.IMG_REGISTRY_HOST }}
136+
username: ${{ secrets.IMG_REGISTRY_USERNAME }}
137+
password: ${{ secrets.IMG_REGISTRY_TOKEN }}
137138
- name: Build Image
138139
id: build-image
139-
uses: redhat-actions/buildah-build@v2
140+
uses: docker/build-push-action@v5
140141
with:
141-
image: ${{ env.OPERATOR_NAME }}-bundle
142-
tags: ${{ env.IMG_TAGS }}
142+
context: .
143+
file: ./bundle.Dockerfile
143144
platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
145+
push: ${{ !env.ACT }}
146+
tags: ${{ steps.meta.outputs.tags }}
144147
build-args: |
145148
version=${{ env.VERSION }}
146-
containerfiles: |
147-
./bundle.Dockerfile
148-
- name: Push Image
149-
if: ${{ !env.ACT }}
150-
id: push-to-quay
151-
uses: redhat-actions/push-to-registry@v2
152-
with:
153-
image: ${{ steps.build-image.outputs.image }}
154-
tags: ${{ steps.build-image.outputs.tags }}
155-
registry: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}
156-
username: ${{ secrets.IMG_REGISTRY_USERNAME }}
157-
password: ${{ secrets.IMG_REGISTRY_TOKEN }}
149+
provenance: false
158150
- name: Print Image URL
159-
run: echo "Image pushed to ${{ steps.push-to-quay.outputs.registry-paths }}"
151+
run: |
152+
echo "Image(s) pushed:"
153+
echo "${{ steps.meta.outputs.tags }}"
160154
161155
build-catalog:
162156
name: Build and push catalog image
@@ -171,18 +165,8 @@ jobs:
171165
with:
172166
go-version-file: go.mod
173167
id: go
174-
- name: Add latest tag
175-
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
176-
id: add-latest-tag
177-
run: |
178-
echo "IMG_TAGS=latest ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
179-
- name: Add release tag
180-
if: ${{ github.ref_name != env.MAIN_BRANCH_NAME }}
181-
id: add-branch-tag
182-
run: |
183-
TAG_NAME=${GITHUB_REF_NAME/\//-}
184-
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV
185-
echo "IMG_TAGS=${TAG_NAME} ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
168+
- name: Set up Docker Buildx
169+
uses: docker/setup-buildx-action@v3
186170
- name: Set Operator version
187171
id: operator-version
188172
run: |
@@ -192,10 +176,15 @@ jobs:
192176
else
193177
echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV
194178
fi
195-
- name: Install qemu dependency
196-
run: |
197-
sudo apt-get update
198-
sudo apt-get install -y qemu-user-static
179+
- name: Extract metadata
180+
id: meta
181+
uses: docker/metadata-action@v5
182+
with:
183+
images: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-catalog
184+
tags: |
185+
type=raw,value=${{ github.sha }}
186+
type=raw,value=latest,enable={{is_default_branch}}
187+
type=ref,event=branch,enable={{is_not_default_branch}}
199188
- name: Run make catalog (main)
200189
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
201190
run: |
@@ -215,25 +204,24 @@ jobs:
215204
CHANNELS=${{ inputs.channels }}
216205
- name: Git diff
217206
run: git diff
218-
- name: Build Image
219-
id: build-image
220-
uses: redhat-actions/buildah-build@v2
221-
with:
222-
image: ${{ env.OPERATOR_NAME }}-catalog
223-
tags: ${{ env.IMG_TAGS }}
224-
platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
225-
context: ./catalog
226-
dockerfiles: |
227-
./catalog/${{ env.OPERATOR_NAME }}-catalog.Dockerfile
228-
- name: Push Image
207+
- name: Login to Container Registry
229208
if: ${{ !env.ACT }}
230-
id: push-to-quay
231-
uses: redhat-actions/push-to-registry@v2
209+
uses: docker/login-action@v3
232210
with:
233-
image: ${{ steps.build-image.outputs.image }}
234-
tags: ${{ steps.build-image.outputs.tags }}
235-
registry: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}
211+
registry: ${{ env.IMG_REGISTRY_HOST }}
236212
username: ${{ secrets.IMG_REGISTRY_USERNAME }}
237213
password: ${{ secrets.IMG_REGISTRY_TOKEN }}
214+
- name: Build Image
215+
id: build-image
216+
uses: docker/build-push-action@v5
217+
with:
218+
context: ./catalog
219+
file: ./catalog/${{ env.OPERATOR_NAME }}-catalog.Dockerfile
220+
platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
221+
push: ${{ !env.ACT }}
222+
tags: ${{ steps.meta.outputs.tags }}
223+
provenance: false
238224
- name: Print Image URL
239-
run: echo "Image pushed to ${{ steps.push-to-quay.outputs.registry-paths }}"
225+
run: |
226+
echo "Image(s) pushed:"
227+
echo "${{ steps.meta.outputs.tags }}"

Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Build the authorino binary
22
# https://catalog.redhat.com/software/containers/ubi9/go-toolset
3-
FROM registry.access.redhat.com/ubi9/go-toolset:1.24 AS builder
3+
FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi9/go-toolset:1.24 AS builder
44
USER root
55
WORKDIR /workspace
66

@@ -20,8 +20,13 @@ ARG OPERATOR_VERSION=latest
2020
ARG DEFAULT_AUTHORINO_IMAGE=quay.io/kuadrant/authorino:latest
2121
ARG GIT_SHA=unknown
2222
ARG DIRTY=unknown
23+
ARG TARGETOS
24+
ARG TARGETARCH
25+
ARG TARGETVARIANT
2326

24-
RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${OPERATOR_VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY} -X github.com/kuadrant/authorino-operator/pkg/reconcilers.DefaultAuthorinoImage=${DEFAULT_AUTHORINO_IMAGE}" -o manager main.go
27+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=${TARGETVARIANT} \
28+
go build -a -ldflags "-X main.version=${OPERATOR_VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY} -X github.com/kuadrant/authorino-operator/pkg/reconcilers.DefaultAuthorinoImage=${DEFAULT_AUTHORINO_IMAGE}" \
29+
-o manager main.go
2530

2631
# Use Red Hat minimal base image to package the binary
2732
# https://catalog.redhat.com/software/containers/ubi9-minimal

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"os"
23+
"runtime"
2224

2325
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2426
// to ensure that exec-entrypoint and run can make use of them.
2527
_ "k8s.io/client-go/plugin/pkg/client/auth"
2628
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2729
"sigs.k8s.io/controller-runtime/pkg/webhook"
2830

29-
"k8s.io/apimachinery/pkg/runtime"
31+
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
3032
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3133
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3234
ctrl "sigs.k8s.io/controller-runtime"
@@ -42,7 +44,7 @@ import (
4244
)
4345

4446
var (
45-
scheme = runtime.NewScheme()
47+
scheme = apimachineryruntime.NewScheme()
4648
setupLog = ctrl.Log.WithName("setup")
4749
logger log.Logger
4850
version string // value injected in compilation-time
@@ -96,7 +98,9 @@ func main() {
9698
"version", version,
9799
"commit", gitSHA,
98100
"dirty", dirty,
99-
"default authorino image", reconcilers.DefaultAuthorinoImage)
101+
"default authorino image", reconcilers.DefaultAuthorinoImage,
102+
"go version", runtime.Version(),
103+
"go os/arch", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
100104

101105
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
102106
Scheme: scheme,

0 commit comments

Comments
 (0)