Skip to content

Commit c1111b5

Browse files
fix: skip processing ztocs/indexes that are found in the artifacts store but not in the content store
Signed-off-by: Swapnanil-Gupta <swpnlg@amazon.com>
1 parent 4e2e6c0 commit c1111b5

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

cmd/soci/commands/ztoc/list.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ package ztoc
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"io"
2324
"os"
2425
"text/tabwriter"
2526

2627
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal"
2728
"github.com/awslabs/soci-snapshotter/soci"
29+
"github.com/containerd/containerd/errdefs"
2830
"github.com/containerd/containerd/images"
31+
"github.com/containerd/log"
2932
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3033
"github.com/urfave/cli/v3"
3134
)
@@ -96,7 +99,11 @@ var listCommand = &cli.Command{
9699
for _, indexInfo := range indexInfos {
97100
readerAt, err := cs.ReaderAt(ctx, indexInfo.Descriptor)
98101
if err != nil {
99-
return fmt.Errorf("failed to read a soci index with digest %s from the content store (try running \"soci rebuild-db\" first): %w", indexInfo.Descriptor.Digest, err)
102+
if errors.Is(err, errdefs.ErrNotFound) {
103+
log.G(ctx).Debug("a soci index was found in the artifact store but not in the content store, skipping")
104+
continue
105+
}
106+
return fmt.Errorf("failed to read a soci index with digest %s from the content store: %w", indexInfo.Descriptor.Digest, err)
100107
}
101108
var sociIndex soci.Index
102109
err = soci.DecodeIndex(io.NewSectionReader(readerAt, 0, indexInfo.Descriptor.Size), &sociIndex)

integration/index_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func prepareCustomSociIndices(t *testing.T, sh *shell.Shell, images []testImageI
7979
}
8080
}
8181
img.imgInfo = dockerhub(imgName, withPlatform(platform))
82-
img.sociIndexDigest = buildIndex(sh, img.imgInfo, withIndexBuildConfig(indexBuildConfig), withMinLayerSize(0), withRunRebuildDbBeforeCreate())
82+
img.sociIndexDigest = buildIndex(sh, img.imgInfo, withIndexBuildConfig(indexBuildConfig), withMinLayerSize(0))
8383
ztocDigests, err := getZtocDigestsForImage(sh, img.imgInfo)
8484
if err != nil {
8585
t.Fatalf("could not get ztoc digests: %v", err)
@@ -345,7 +345,7 @@ func TestSociIndexRemoveAndRebuildWithSharedLayers(t *testing.T) {
345345
// the following 2 images share layers
346346
img1 := dockerhub(nginxAlpineImage)
347347
img2 := dockerhub(nginxAlpineImage2)
348-
imgDigest := buildIndex(sh, img1, withMinLayerSize(0), withRunRebuildDbBeforeCreate())
348+
imgDigest := buildIndex(sh, img1, withMinLayerSize(0))
349349
if imgDigest == "" {
350350
t.Fatal("failed to get soci index for nginx alpine test image")
351351
}

integration/run_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ func TestRunMultipleContainers(t *testing.T) {
128128
buildIndexOpts := []indexBuildOption{withMinLayerSize(0)}
129129
if tt.forceRecreateZtocs {
130130
buildIndexOpts = append(buildIndexOpts, withForceRecreateZtocs(true))
131-
} else {
132-
buildIndexOpts = append(buildIndexOpts, withRunRebuildDbBeforeCreate())
133131
}
134132
indexDigest := buildIndex(sh, regConfig.mirror(container.containerImage), buildIndexOpts...)
135133
sh.X("soci", "push", "--user", regConfig.mirror(container.containerImage).creds, regConfig.mirror(container.containerImage).ref)

integration/util_soci_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,6 @@ func withForceRecreateZtocs(forceRecreateZtocs bool) indexBuildOption {
9292
}
9393
}
9494

95-
// withRebuildDbBeforeCreate syncs the artifact store with the content store before calling "soci create"
96-
// We do this because the artifact store could be out of sync with the content store when 'soci create' is called.
97-
// This is problematic in cases where we create soci indexes for some images, delete those indexes and immediately recreate
98-
// them (like in TestSociIndexRemove) - as there could be ztoc entries in the artifact store which are not present in the
99-
// content store, causing 'soci create/convert' without --force flag to throw an error.
100-
//
101-
// We can run this by default and probably remove this option in the future when the race condition with rebuild-db is solved.
102-
func withRunRebuildDbBeforeCreate() indexBuildOption {
103-
return func(ibc *indexBuildConfig) {
104-
ibc.runRebuildDbBeforeCreate = true
105-
}
106-
}
107-
10895
// withSpanSize overrides the default span size to use when creating an index with `buildIndex`
10996
func withSpanSize(spanSize int64) indexBuildOption {
11097
return func(ibc *indexBuildConfig) {

soci/soci_index.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,21 +632,26 @@ func (b *IndexBuilder) buildSociLayer(ctx context.Context, desc ocispec.Descript
632632

633633
existingZtoc := b.getExistingZtocForLayer(desc)
634634
if existingZtoc != nil {
635+
var shouldOverwrite bool
635636
reader, err := b.blobStore.Fetch(ctx, *existingZtoc)
636637
if err != nil {
637638
if errors.Is(err, errdefs.ErrNotFound) {
638-
return nil, fmt.Errorf("a ztoc entry was found in the artifact store but not in the content store (try running \"soci rebuild-db\" first or try again with \"--force\" flag): %w", err)
639+
log.G(ctx).Debug("a ztoc entry was found in the artifact store but not in the content store, overwriting existing ztoc")
640+
shouldOverwrite = true
641+
} else {
642+
return nil, fmt.Errorf("failed to fetch existing ztoc: %w", err)
639643
}
640-
return nil, fmt.Errorf("failed to fetch existing ztoc: %w", err)
641-
}
642-
toc, err := ztoc.Unmarshal(reader)
643-
if err != nil {
644-
return nil, fmt.Errorf("cannot unmarshal existing ztoc: %w", err)
645644
}
645+
if !shouldOverwrite {
646+
toc, err := ztoc.Unmarshal(reader)
647+
if err != nil {
648+
return nil, fmt.Errorf("cannot unmarshal existing ztoc: %w", err)
649+
}
646650

647-
fmt.Printf("layer %s -> ztoc %s (already exists)\n", desc.Digest, existingZtoc.Digest)
648-
b.addSociLayerAnnotations(&desc, existingZtoc, toc)
649-
return existingZtoc, err
651+
fmt.Printf("layer %s -> ztoc %s (already exists)\n", desc.Digest, existingZtoc.Digest)
652+
b.addSociLayerAnnotations(&desc, existingZtoc, toc)
653+
return existingZtoc, err
654+
}
650655
}
651656

652657
ra, err := b.contentStore.ReaderAt(ctx, desc)

0 commit comments

Comments
 (0)