From 6577ffee58119e2498bf3dc16fe4d54246313fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Tue, 19 Nov 2024 20:40:34 +0100 Subject: [PATCH 1/3] Improve build caching for s2i local build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use 'RUN --mount=type=cache...' to cache build artifacts, e.g. the local maven repostory. Signed-off-by: Matej Vašek --- pkg/builders/s2i/builder.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/builders/s2i/builder.go b/pkg/builders/s2i/builder.go index c7fb5950f..d2cd1f0bf 100644 --- a/pkg/builders/s2i/builder.go +++ b/pkg/builders/s2i/builder.go @@ -3,6 +3,8 @@ package s2i import ( "archive/tar" "context" + "crypto/sha1" + "encoding/hex" "errors" "fmt" "io" @@ -256,6 +258,14 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf // s2i apparently is not excluding the files in --as-dockerfile mode exclude := regexp.MustCompile(cfg.ExcludeRegExp) + // if exists, patch dockerfile to using cache mount + if _, e := os.Stat(cfg.AsDockerfile); e == nil { + err = patchDockerfile(cfg.AsDockerfile, f) + if err != nil { + return err + } + } + const up = ".." + string(os.PathSeparator) go func() { tw := tar.NewWriter(pw) @@ -356,6 +366,20 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf return jsonmessage.DisplayJSONMessagesStream(resp.Body, out, fd, isTerminal, nil) } +func patchDockerfile(path string, f fn.Function) error { + data, err := os.ReadFile(path) + if err != nil { + return err + } + re := regexp.MustCompile(`RUN (.*assemble)`) + s := sha1.Sum([]byte(f.Root)) + mountCmd := "--mount=type=cache,target=/tmp/artifacts/,uid=1001,id=" + hex.EncodeToString(s[:8]) + replacement := fmt.Sprintf("RUN %s \\\n $1", mountCmd) + newDockerFileStr := re.ReplaceAllString(string(data), replacement) + + return os.WriteFile(path, []byte(newDockerFileStr), 0644) +} + func s2iScriptURL(ctx context.Context, cli DockerClient, image string) (string, error) { img, _, err := cli.ImageInspectWithRaw(ctx, image) if err != nil { From 1bb32ae96ec9d6234a6f5eb923d66b97fd7d16b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Thu, 21 Nov 2024 14:08:56 +0100 Subject: [PATCH 2/3] Update podman version in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Vašek --- .github/workflows/test-podman.yaml | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test-podman.yaml b/.github/workflows/test-podman.yaml index 4ab94dbab..200c770d9 100644 --- a/.github/workflows/test-podman.yaml +++ b/.github/workflows/test-podman.yaml @@ -18,17 +18,16 @@ jobs: - uses: ./.github/composite/go-setup - name: Install Podman run: | - # TODO uncomment following once https:.github.com/containers/podman/pull/16781 is in the kubic repository - #. /etc/os-release - #sudo mkdir -p /etc/apt/keyrings - #curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/Release.key \ - # | gpg --dearmor \ - # | sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null - #echo \ - # "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\ - # https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/ /" \ - # | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null - #sudo apt-get update -qq + . /etc/os-release + sudo mkdir -p /etc/apt/keyrings + curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/Release.key \ + | gpg --dearmor \ + | sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\ + https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/ /" \ + | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null + sudo apt-get update -qq sudo apt-get -qq -y install podman podman info - name: Install Binaries From de99b457a10d0c1187537a9d0fe92eeacd8093a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Thu, 21 Nov 2024 15:24:38 +0100 Subject: [PATCH 3/3] fixup: set buildopt version to buildkit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Vašek --- pkg/builders/s2i/builder.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/builders/s2i/builder.go b/pkg/builders/s2i/builder.go index d2cd1f0bf..f7df3c129 100644 --- a/pkg/builders/s2i/builder.go +++ b/pkg/builders/s2i/builder.go @@ -343,6 +343,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf opts := types.ImageBuildOptions{ Tags: []string{f.Build.Image}, PullParent: true, + Version: types.BuilderBuildKit, } resp, err := client.ImageBuild(ctx, pr, opts)