Skip to content

Commit

Permalink
Fix invalid layer index error
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad A. Ionescu <[email protected]>
  • Loading branch information
vladaionescu committed Feb 17, 2022
1 parent c14bf69 commit d67243e
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 30 deletions.
9 changes: 0 additions & 9 deletions cache/remotecache/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import (
"fmt"
"sort"

"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/solver"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// EmptyLayerRemovalSupported defines if implementation supports removal of empty layers. Buildkit image exporter
// removes empty layers, but moby layerstore based implementation does not.
var EmptyLayerRemovalSupported = true

// sortConfig sorts the config structure to make sure it is deterministic
func sortConfig(cc *CacheConfig) {
type indexedLayer struct {
Expand Down Expand Up @@ -303,10 +298,6 @@ func marshalRemote(ctx context.Context, r *solver.Remote, state *marshalState) s
}
desc := r.Descriptors[len(r.Descriptors)-1]

if desc.Digest == exptypes.EmptyGZLayer && EmptyLayerRemovalSupported {
return parentID
}

state.descriptors[desc.Digest] = DescriptorProviderPair{
Descriptor: desc,
Provider: r.Provider,
Expand Down
8 changes: 4 additions & 4 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2798,7 +2798,7 @@ func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
require.NotEqual(t, "", ociimg.Architecture)
require.NotEqual(t, "", ociimg.Config.WorkingDir)
require.Equal(t, "layers", ociimg.RootFS.Type)
require.Equal(t, 2, len(ociimg.RootFS.DiffIDs))
require.Equal(t, 3, len(ociimg.RootFS.DiffIDs))
require.NotNil(t, ociimg.Created)
require.True(t, time.Since(*ociimg.Created) < 2*time.Minute)
require.Condition(t, func() bool {
Expand All @@ -2815,7 +2815,7 @@ func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
require.Contains(t, ociimg.History[1].CreatedBy, "true")
require.Contains(t, ociimg.History[2].CreatedBy, "foo/sub/baz")
require.False(t, ociimg.History[0].EmptyLayer)
require.True(t, ociimg.History[1].EmptyLayer)
require.False(t, ociimg.History[1].EmptyLayer)
require.False(t, ociimg.History[2].EmptyLayer)

dt, err = content.ReadBlob(ctx, img.ContentStore(), img.Target())
Expand All @@ -2830,7 +2830,7 @@ func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)

require.Equal(t, images.MediaTypeDockerSchema2Manifest, mfst.MediaType)
require.Equal(t, 2, len(mfst.Layers))
require.Equal(t, 3, len(mfst.Layers))
require.Equal(t, images.MediaTypeDockerSchema2LayerGzip, mfst.Layers[0].MediaType)
require.Equal(t, images.MediaTypeDockerSchema2LayerGzip, mfst.Layers[1].MediaType)

Expand All @@ -2857,7 +2857,7 @@ func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
_, ok = m["foo/sub/baz"]
require.False(t, ok)

dt, err = content.ReadBlob(ctx, img.ContentStore(), ocispecs.Descriptor{Digest: mfst.Layers[1].Digest})
dt, err = content.ReadBlob(ctx, img.ContentStore(), ocispecs.Descriptor{Digest: mfst.Layers[2].Digest})
require.NoError(t, err)

m, err = testutil.ReadTarToMap(dt, true)
Expand Down
3 changes: 0 additions & 3 deletions exporter/containerimage/exptypes/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package exptypes

import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand All @@ -16,8 +15,6 @@ const (
ExporterPlatformsKey = "refs.platforms"
)

const EmptyGZLayer = digest.Digest("sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1")

type Platforms struct {
Platforms []Platform
}
Expand Down
11 changes: 3 additions & 8 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,10 @@ func normalizeLayersAndHistory(ctx context.Context, remote *solver.Remote, histo
var layerIndex int
for i, h := range history {
if !h.EmptyLayer {
if remote.Descriptors[layerIndex].Digest == exptypes.EmptyGZLayer {
h.EmptyLayer = true
remote.Descriptors = append(remote.Descriptors[:layerIndex], remote.Descriptors[layerIndex+1:]...)
} else {
if h.Created == nil {
h.Created = refMeta[layerIndex].createdAt
}
layerIndex++
if h.Created == nil {
h.Created = refMeta[layerIndex].createdAt
}
layerIndex++
}
history[i] = h
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2946,7 +2946,7 @@ RUN ["ls"]

require.Equal(t, "layers", ociimg.RootFS.Type)
// this depends on busybox. should be ok after freezing images
require.Equal(t, 3, len(ociimg.RootFS.DiffIDs))
require.Equal(t, 4, len(ociimg.RootFS.DiffIDs))

require.Equal(t, 7, len(ociimg.History))
require.Contains(t, ociimg.History[2].CreatedBy, "lbl=val")
Expand All @@ -2962,7 +2962,7 @@ RUN ["ls"]
require.Equal(t, false, ociimg.History[5].EmptyLayer)
require.NotNil(t, ociimg.History[5].Created)
require.Contains(t, ociimg.History[6].CreatedBy, "RUN ls")
require.Equal(t, true, ociimg.History[6].EmptyLayer)
require.Equal(t, false, ociimg.History[6].EmptyLayer)
require.NotNil(t, ociimg.History[6].Created)
}

Expand Down
5 changes: 1 addition & 4 deletions snapshot/imagerefchecker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -90,9 +89,7 @@ func toDigests(layers []ocispecs.Descriptor) []digest.Digest {
func layerKey(layers []digest.Digest) string {
b := &strings.Builder{}
for _, l := range layers {
if l != exptypes.EmptyGZLayer {
b.Write([]byte(l))
}
b.Write([]byte(l))
}
return b.String()
}
Expand Down

0 comments on commit d67243e

Please sign in to comment.