Skip to content

Commit

Permalink
Skip export of caches with no layers to OCI structures
Browse files Browse the repository at this point in the history
These happen when a container image is built using only metadata
actions, and hence does not generate any filesystem changes.

Such a degenerate cache is not interesting, and some OCI registries
reject OCI images with no layers.

Specifically, this only affects the `registry` and `local` exporters.

The `inline` exporter already early-outs in this case with the same
warning, and the `gha`, `s3`, and `azblob` exporters are unchanged.

Signed-off-by: Paul "TBBle" Hampson <[email protected]>
  • Loading branch information
TBBle committed Oct 17, 2023
1 parent 6fd6e03 commit bf211cf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cache/remotecache/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/progress"
Expand Down Expand Up @@ -185,6 +186,11 @@ func (ce *contentCacheExporter) Finalize(ctx context.Context) (map[string]string
return nil, err
}

if len(config.Layers) == 0 {
bklog.G(ctx).Warn("failed to match any cache with layers")
return nil, progress.OneOff(ctx, "skipping cache export for empty result")(nil)
}

cache, err := NewExportableCache(ce.oci, ce.imageManifest)
if err != nil {
return nil, err
Expand Down
65 changes: 65 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func TestIntegration(t *testing.T) {
testLLBMountPerformance,
testClientCustomGRPCOpts,
testMultipleRecordsWithSameLayersCacheImportExport,
testRegistryEmptyCacheExport,
testSnapshotWithMultipleBlobs,
testExportLocalNoPlatformSplit,
testExportLocalNoPlatformSplitOverwrite,
Expand Down Expand Up @@ -5437,6 +5438,70 @@ func testBasicGhaCacheImportExport(t *testing.T, sb integration.Sandbox) {
testBasicCacheImportExport(t, sb, []CacheOptionsEntry{im}, []CacheOptionsEntry{ex})
}

func testRegistryEmptyCacheExport(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)

workers.CheckFeatureCompat(t, sb,
workers.FeatureCacheExport,
workers.FeatureCacheBackendRegistry,
)

for _, ociMediaTypes := range []bool{true, false} {
ociMediaTypes := ociMediaTypes
for _, imageManifest := range []bool{true, false} {
imageManifest := imageManifest
if imageManifest && !ociMediaTypes {
// invalid configuration for remote cache
continue
}

t.Run(t.Name()+fmt.Sprintf("/ociMediaTypes=%t/imageManifest=%t", ociMediaTypes, imageManifest), func(t *testing.T) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

st := llb.Scratch()
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

cacheTarget := registry + "/buildkit/testregistryemptycache:latest"

cacheOptionsEntry := CacheOptionsEntry{
Type: "registry",
Attrs: map[string]string{
"ref": cacheTarget,
"image-manifest": strconv.FormatBool(imageManifest),
"oci-mediatypes": strconv.FormatBool(ociMediaTypes),
},
}

_, err = c.Solve(sb.Context(), def, SolveOpt{
CacheExports: []CacheOptionsEntry{cacheOptionsEntry},
}, nil)
require.NoError(t, err)

ctx := namespaces.WithNamespace(sb.Context(), "buildkit")
cdAddress := sb.ContainerdAddress()
var client *containerd.Client
if cdAddress != "" {
client, err = newContainerd(cdAddress)
require.NoError(t, err)
defer client.Close()

_, err := client.Fetch(ctx, cacheTarget)
require.ErrorIs(t, err, ctderrdefs.ErrNotFound, "unexpected error %v", err)
}
})
}
}
}

func testMultipleRecordsWithSameLayersCacheImportExport(t *testing.T, sb integration.Sandbox) {
workers.CheckFeatureCompat(t, sb,
workers.FeatureCacheExport,
Expand Down

0 comments on commit bf211cf

Please sign in to comment.