Skip to content

Commit

Permalink
Improved build caching for s2i local build (#2581)
Browse files Browse the repository at this point in the history
* Improve build caching for s2i local build

Use 'RUN --mount=type=cache...' to cache build artifacts,
e.g. the local maven repostory.

Signed-off-by: Matej Vašek <[email protected]>

* Update podman version in tests

Signed-off-by: Matej Vašek <[email protected]>

* fixup: set buildopt version to buildkit

Signed-off-by: Matej Vašek <[email protected]>

---------

Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek authored Nov 22, 2024
1 parent 27110e1 commit 1dd2e43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/test-podman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/builders/s2i/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package s2i
import (
"archive/tar"
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -333,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)
Expand All @@ -356,6 +367,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 {
Expand Down

0 comments on commit 1dd2e43

Please sign in to comment.