Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved build caching for s2i local build #2581

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading