Skip to content

Commit 6931839

Browse files
authored
Fix build_repositories for layered images (#1663)
I neglected to call the method that overwrites etc/apk/repositories with RuntimeRepositories instead of BuildRepositories + RuntimeRepositories. Added a test to make that not happen again. Also moved tarfs around to make it accessible from that test. Signed-off-by: Jon Johnson <[email protected]>
1 parent 10e022f commit 6931839

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

internal/cli/publish_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@ package cli_test
1717
import (
1818
"context"
1919
"fmt"
20+
"io"
21+
"io/fs"
2022
"net/http"
2123
"net/http/httptest"
2224
"net/url"
2325
"os"
2426
"path/filepath"
27+
"strings"
2528
"testing"
2629

2730
"github.com/google/go-containerregistry/pkg/name"
2831
"github.com/google/go-containerregistry/pkg/registry"
32+
"github.com/google/go-containerregistry/pkg/v1/mutate"
2933
"github.com/google/go-containerregistry/pkg/v1/remote"
3034
"github.com/google/go-containerregistry/pkg/v1/validate"
3135
"github.com/stretchr/testify/require"
3236

3337
"chainguard.dev/apko/internal/cli"
38+
"chainguard.dev/apko/internal/tarfs"
3439
"chainguard.dev/apko/pkg/build"
3540
"chainguard.dev/apko/pkg/build/types"
3641
"chainguard.dev/apko/pkg/sbom"
@@ -162,7 +167,7 @@ func TestPublishLayering(t *testing.T) {
162167

163168
// This test will fail if we ever make a change in apko that changes the image.
164169
// Sometimes, this is intentional, and we need to change this and bump the version.
165-
want := "sha256:316b607c2d30e686d9170edcefa9bc07aae922e4c20c06cc85bd6e4c0ed85f25"
170+
want := "sha256:d5fe88a41005bc378fc42d3066d4762b2c082e528cd2856e27f4e005031bfd35"
166171
require.Equal(t, want, digest.String())
167172

168173
im, err := idx.IndexManifest()
@@ -176,5 +181,20 @@ func TestPublishLayering(t *testing.T) {
176181
require.NoError(t, err)
177182

178183
require.Equal(t, 2, len(cm.Layers))
184+
185+
tr := mutate.Extract(child)
186+
tmp, err := os.CreateTemp(t.TempDir(), "")
187+
require.NoError(t, err)
188+
size, err := io.Copy(tmp, tr)
189+
require.NoError(t, err)
190+
fsys, err := tarfs.New(tmp, size)
191+
require.NoError(t, err)
192+
193+
b, err := fs.ReadFile(fsys, "etc/apk/repositories")
194+
require.NoError(t, err)
195+
196+
if strings.Contains(string(b), "./packages") {
197+
t.Errorf("etc/apk/repositories contains build_repositories entry %q", "./packages")
198+
}
179199
}
180200
}

internal/cli/testdata/layering.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
contents:
22
keyring:
33
- ./testdata/melange.rsa.pub
4+
build_repositories:
5+
- ./packages
46
repositories:
57
- ./testdata/packages
68
packages:
File renamed without changes.

pkg/apk/apk/implementation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ import (
5151
"golang.org/x/sys/unix"
5252
"gopkg.in/ini.v1"
5353

54+
"chainguard.dev/apko/internal/tarfs"
5455
"chainguard.dev/apko/pkg/apk/auth"
5556
"chainguard.dev/apko/pkg/apk/expandapk"
5657
apkfs "chainguard.dev/apko/pkg/apk/fs"
57-
"chainguard.dev/apko/pkg/apk/internal/tarfs"
5858
"chainguard.dev/apko/pkg/paths"
5959

6060
"github.com/chainguard-dev/clog"

pkg/apk/apk/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
"go.opentelemetry.io/otel"
3131

32-
"chainguard.dev/apko/pkg/apk/internal/tarfs"
32+
"chainguard.dev/apko/internal/tarfs"
3333
)
3434

3535
// writeOneFile writes one file from the APK given the tar header and tar reader.

pkg/apk/expandapk/expandapk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
"sync"
2424

25-
"chainguard.dev/apko/pkg/apk/internal/tarfs"
25+
"chainguard.dev/apko/internal/tarfs"
2626
"github.com/klauspost/compress/gzip"
2727

2828
"go.opentelemetry.io/otel"

pkg/build/layers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ func (bc *Context) buildLayers(ctx context.Context) ([]v1.Layer, error) {
4949
return nil, fmt.Errorf("building filesystem: %w", err)
5050
}
5151

52+
// We don't pass around repositories cleanly between apko and the library
53+
// formerly known as go-apk. Instead, we write stuff to bc.fs directly
54+
// and the library formerly known as go-apk reads from bc.fs to know
55+
// which repositories it can fetch packages from. We need to call this
56+
// to overwrite etc/apk/repositories with _only_ runtime repositories
57+
// and not runtime + build repositories.
58+
//
59+
// TODO: Clean this up when time permits.
60+
if err := bc.postBuildSetApk(ctx); err != nil {
61+
return nil, err
62+
}
63+
5264
// Use our layering strategy to partition packages into a set of Budget groups.
5365
groups, err := groupByOriginAndSize(pkgs, bc.ic.Layering.Budget)
5466
if err != nil {

0 commit comments

Comments
 (0)