Skip to content

Commit 86a144a

Browse files
committed
automate building k0s images with chainguard
1 parent 1f73846 commit 86a144a

File tree

5 files changed

+181
-115
lines changed

5 files changed

+181
-115
lines changed

.github/actions/build-dep-image-with-apko/action.yml

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

.github/workflows/image-deps-updater.yaml

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,41 @@ on:
88
k0s-version:
99
description: 'K0s version for discovering image versions'
1010
required: false
11-
overwrite:
12-
description: 'Overwrite the existing image tags'
13-
required: false
14-
default: 'true'
1511
push:
1612
branches:
17-
- emosbaugh/sc-108755/use-chainguard-images-for-embedded-cluster
13+
- sgalsaleh/sc-108755/use-chainguard-images-for-embedded-cluster
1814

1915
jobs:
20-
define-matrix:
21-
runs-on: ubuntu-20.04
22-
outputs:
23-
matrix: ${{ steps.set-matrix.outputs.matrix }}
24-
25-
steps:
26-
- name: Checkout
27-
uses: actions/checkout@v4
28-
29-
- name: Set matrix
30-
id: set-matrix
31-
run: |
32-
set -euo pipefail
33-
34-
# We're only using the APKINDEX files to get the versions, so it doesn't matter which arch we use
35-
36-
curl -LO --fail --show-error https://packages.wolfi.dev/os/x86_64/APKINDEX.tar.gz
37-
tar -xzvf APKINDEX.tar.gz
38-
39-
calico_node_version=$(< APKINDEX grep -A1 "^P:calico-node" | tail -n 1 | sed -n -e 's/V://p' | tr -d '\n')
40-
41-
if [ -n "${{ github.event.inputs.k0s-version }}" ]; then
42-
make pkg/goods/bins/k0s K0S_VERSION="${{ github.event.inputs.k0s-version }}" K0S_BINARY_SOURCE_OVERRIDE=
43-
else
44-
make pkg/goods/bins/k0s
45-
fi
46-
47-
coredns_version=$(pkg/goods/bins/k0s airgap list-images --all | grep '/coredns:' | awk -F':' '{ print $2 }')
48-
kube_proxy_version=$(pkg/goods/bins/k0s airgap list-images --all | grep '/kube-proxy:' | awk -F':v' '{ print $2 }')
49-
metrics_server_version=$(pkg/goods/bins/k0s airgap list-images --all | grep '/metrics-server:' | awk -F':v' '{ print $2 }')
50-
51-
{
52-
printf "matrix={\"include\":["
53-
printf "{\"component\": \"calico-node\", \"version\": \"$calico_node_version\"},"
54-
printf "{\"component\": \"coredns\", \"version\": \"$coredns_version\"},"
55-
printf "{\"component\": \"metrics-server\", \"version\": \"$metrics_server_version\"}"
56-
printf "]}"
57-
} >> "$GITHUB_OUTPUT"
58-
59-
build-images:
16+
update-k0s-images:
6017
runs-on: ubuntu-20.04
61-
needs: define-matrix
62-
strategy:
63-
fail-fast: false
64-
matrix: ${{ fromJSON(needs.define-matrix.outputs.matrix) }}
65-
6618
steps:
6719
- name: Checkout
6820
uses: actions/checkout@v4
6921

70-
- name: Generate apko config
71-
run: |
72-
set -euo pipefail
73-
sed "s/__VERSION__/${{ matrix.version }}/g" deploy/images/${{ matrix.component }}/apko.tmpl.yaml > apko.yaml
22+
- name: Update k0s images
23+
run: deploy/scripts/k0s-images.sh
7424

75-
- name: Build and push image
76-
uses: ./.github/actions/build-dep-image-with-apko
25+
- name: Create Pull Request # creates a PR if there are differences
26+
uses: peter-evans/create-pull-request@v6
27+
id: cpr
7728
with:
78-
apko-config: apko.yaml
79-
image-name: ttl.sh/ec/${{ matrix.component }}:${{ matrix.version }}
80-
# registry-username: ${{ secrets.REGISTRY_USERNAME_STAGING }}
81-
# registry-password: ${{ secrets.REGISTRY_PASSWORD_STAGING }}
82-
overwrite: true # ${{ github.event.inputs.overwrite }}
29+
token: ${{ secrets.AUTOMATED_PR_GH_PAT }}
30+
commit-message: 'Update image versions'
31+
title: 'Automated image updates'
32+
branch: automation/image-dependencies
33+
delete-branch: true
34+
labels: |
35+
automated-pr
36+
images
37+
type::security
38+
# draft: false
39+
draft: true
40+
# base: "main"
41+
base: "sgalsaleh/sc-108755/use-chainguard-images-for-embedded-cluster"
42+
body: "Automated changes by the [image-deps-updater](https://github.com/replicatedhq/embedded-cluster/blob/main/.github/workflows/image-deps-updater.yaml) GitHub action"
43+
44+
- name: Check outputs
45+
if: ${{ steps.cpr.outputs.pull-request-number }}
46+
run: |
47+
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
48+
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ pkg/goods/images
77
.pre-commit-config.yaml
88
vendor
99
e2e/kots-release-install/license.yaml
10+
/build/
11+
/bin/

Makefile

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ LD_FLAGS = -X github.com/replicatedhq/embedded-cluster/pkg/defaults.K0sVersion=$
6464
-X github.com/replicatedhq/embedded-cluster/pkg/addons/velero.VeleroTag=$(VELERO_IMAGE_VERSION) \
6565
-X github.com/replicatedhq/embedded-cluster/pkg/addons/velero.AwsPluginTag=$(VELERO_AWS_PLUGIN_IMAGE_VERSION)
6666

67+
export PATH := $(shell pwd)/bin:$(PATH)
68+
6769
.DEFAULT_GOAL := default
6870
default: embedded-cluster-linux-amd64
6971

@@ -170,6 +172,8 @@ clean:
170172
rm -rf output
171173
rm -rf pkg/goods/bins
172174
rm -rf pkg/goods/internal/bins
175+
rm -rf build
176+
rm -rf bin
173177

174178
.PHONY: lint
175179
lint:
@@ -188,9 +192,6 @@ scan:
188192
--ignore-unfixed \
189193
./
190194

191-
print-%:
192-
@echo -n $($*)
193-
194195
.PHONY: build-local-artifact-mirror-image
195196
build-local-artifact-mirror-image:
196197
docker build -t $(LOCAL_ARTIFACT_MIRROR_IMAGE_LOCATION) -f Dockerfile .
@@ -202,6 +203,97 @@ push-local-artifact-mirror-image:
202203
.PHONY: build-and-push-local-artifact-mirror-image
203204
build-and-push-local-artifact-mirror-image: build-local-artifact-mirror-image push-local-artifact-mirror-image
204205

206+
CHAINGUARD_TOOLS_USE_DOCKER = 0
207+
ifeq ($(CHAINGUARD_TOOLS_USE_DOCKER),"1")
208+
MELANGE_CACHE_DIR = /go/pkg/mod
209+
APKO_CMD = docker run -v "${PWD}":/work -w /work -v "${PWD}"/build/.docker:/root/.docker cgr.dev/chainguard/apko
210+
MELANGE_CMD = docker run --privileged --rm -v "${PWD}":/work -w /work -v "$(shell go env GOMODCACHE)":${MELANGE_CACHE_DIR} cgr.dev/chainguard/melange
211+
else
212+
MELANGE_CACHE_DIR = $(shell go env GOMODCACHE)
213+
APKO_CMD = apko
214+
MELANGE_CMD = melange
215+
endif
216+
217+
.PHONY: apko-build
218+
apko-build: export ARCHS ?= amd64
219+
apko-build: check-env-IMAGE apko-template
220+
cd build && ${APKO_CMD} \
221+
build apko.yaml ${IMAGE} apko.tar \
222+
--arch ${ARCHS}
223+
224+
.PHONY: apko-build-and-publish
225+
apko-build-and-publish: export ARCHS ?= amd64
226+
apko-build-and-publish: check-env-IMAGE apko-template
227+
cd build && ${APKO_CMD} \
228+
publish apko.yaml ${IMAGE} \
229+
--arch ${ARCHS} | tee digest
230+
231+
.PHONY: apko-login
232+
apko-login:
233+
rm -f build/.docker/config.json
234+
@ { [ "${PASSWORD}" = "" ] || [ "${USERNAME}" = "" ] ; } || \
235+
${APKO_CMD} \
236+
login -u "${USERNAME}" \
237+
--password "${PASSWORD}" "${REGISTRY}"
238+
239+
.PHONY: melange-build
240+
melange-build: export ARCHS ?= amd64
241+
melange-build: melange-template
242+
mkdir -p build
243+
for f in pkg cmd go.mod go.sum Makefile ; do \
244+
rm -rf "build/$$f" && cp -r $$f build/ ; \
245+
done
246+
${MELANGE_CMD} \
247+
keygen build/melange.rsa
248+
${MELANGE_CMD} \
249+
build build/melange.yaml \
250+
--arch ${ARCHS} \
251+
--signing-key build/melange.rsa \
252+
--cache-dir=$(MELANGE_CACHE_DIR) \
253+
--out-dir build/packages/
254+
255+
.PHONY: melange-template
256+
melange-template: check-env-MELANGE_CONFIG check-env-VERSION
257+
mkdir -p build
258+
envsubst '$${VERSION}' < ${MELANGE_CONFIG} > build/melange.yaml
259+
260+
.PHONY: apko-template
261+
apko-template: check-env-APKO_CONFIG check-env-VERSION
262+
mkdir -p build
263+
envsubst '$${VERSION}' < ${APKO_CONFIG} > build/apko.yaml
264+
205265
.PHONY: buildtools
206266
buildtools:
207267
go build -o ./output/bin/buildtools ./cmd/buildtools
268+
269+
.PHONY: cache-files
270+
cache-files: export EMBEDDED_OPERATOR_BINARY_URL_OVERRIDE
271+
cache-files:
272+
./scripts/cache-files.sh
273+
274+
ifeq (,$(shell go env GOBIN))
275+
GOBIN=$(shell go env GOPATH)/bin
276+
else
277+
GOBIN=$(shell go env GOBIN)
278+
endif
279+
280+
bin/apko:
281+
mkdir -p bin
282+
go install chainguard.dev/apko@latest && \
283+
test -s $(GOBIN)/apko && \
284+
ln -sf $(GOBIN)/apko bin/apko
285+
286+
bin/melange:
287+
mkdir -p bin
288+
go install chainguard.dev/melange@latest && \
289+
test -s $(GOBIN)/melange && \
290+
ln -sf $(GOBIN)/melange bin/melange
291+
292+
print-%:
293+
@echo -n $($*)
294+
295+
check-env-%:
296+
@ if [ "${${*}}" = "" ]; then \
297+
echo "Environment variable $* not set"; \
298+
exit 1; \
299+
fi

deploy/scripts/k0s-images.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
set -euox pipefail
4+
5+
# we're only using the APKINDEX files to get the versions, so it doesn't matter which arch we use
6+
7+
mkdir -p output/tmp
8+
curl -L -o output/tmp/APKINDEX.tar.gz https://packages.wolfi.dev/os/x86_64/APKINDEX.tar.gz
9+
tar -xzvf output/tmp/APKINDEX.tar.gz -C output/tmp
10+
11+
k0s_version=${1-}
12+
if [ -n "$k0s_version" ]; then
13+
make pkg/goods/bins/k0s K0S_VERSION="$k0s_version" K0S_BINARY_SOURCE_OVERRIDE=
14+
else
15+
make pkg/goods/bins/k0s
16+
fi
17+
18+
function get_package_version() {
19+
pinned_version=$(pkg/goods/bins/k0s airgap list-images --all | grep "/$1:" | awk -F':' '{ print $2 }' | sed 's/^v//' | sed 's/-[0-9]*$//')
20+
< output/tmp/APKINDEX grep -A1 "^P:$1" | grep "V:$pinned_version" | awk -F '-r' '{print $1, $2}' | sort -k2,2n | tail -1 | awk '{print $1 "-r" $2}' | sed -n -e 's/V://p' | tr -d '\n'
21+
}
22+
23+
components='[
24+
{
25+
"name": "coredns",
26+
"version": "'$(get_package_version coredns)'",
27+
"makefile_var": "COREDNS_VERSION"
28+
},
29+
{
30+
"name": "calico-node",
31+
"version": "'$(get_package_version calico-node)'",
32+
"makefile_var": "CALICO_NODE_VERSION"
33+
},
34+
{
35+
"name": "metrics-server",
36+
"version": "'$(get_package_version metrics-server)'",
37+
"makefile_var": "METRICS_SERVER_VERSION"
38+
}
39+
]'
40+
41+
make bin/apko
42+
43+
for component in $(echo "${components}" | jq -c '.[]'); do
44+
name=$(echo "$component" | jq -r '.name')
45+
version=$(echo "$component" | jq -r '.version')
46+
makefile_var=$(echo "$component" | jq -r '.makefile_var')
47+
48+
sed "s/__VERSION__/$version/g" deploy/images/"$name"/apko.tmpl.yaml > output/tmp/apko.yaml
49+
50+
make apko-build-and-publish \
51+
IMAGE=ttl.sh/ec/"$name":"$version" \
52+
APKO_CONFIG=output/tmp/apko.yaml \
53+
VERSION="$version"
54+
55+
digest=$(awk -F'@' '{print $2}' build/digest)
56+
sed -i "s/^$makefile_var.*/$makefile_var = $version@$digest/" Makefile
57+
done

0 commit comments

Comments
 (0)