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

fix: try fetch lost layer when unpack image #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 20 additions & 1 deletion image.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ func WithUnpackDuplicationSuppressor(suppressor kmutex.KeyedLocker) UnpackOpt {
}
}

type DiscardUnpackedLayersKey struct{}

func (i *image) Unpack(ctx context.Context, snapshotterName string, opts ...UnpackOpt) error {
ctx, done, err := i.client.WithLease(ctx)
if err != nil {
Expand Down Expand Up @@ -384,7 +386,24 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string, opts ...Unpa
for _, layer := range layers {
unpacked, err = rootfs.ApplyLayerWithOpts(ctx, layer, chain, sn, a, config.SnapshotOpts, config.ApplyOpts)
if err != nil {
return err
// layer not found in content, try to fetch layer and retry unpack
if errdefs.IsNotFound(err) {
pullOpts := []RemoteOpt{}
if discardUnpackedLayers, ok := ctx.Value(DiscardUnpackedLayersKey{}).(bool); ok && discardUnpackedLayers {
pullOpts = append(pullOpts, WithChildLabelMap(images.ChildGCLabelsFilterLayers))
}

if _, err2 := i.client.Pull(ctx, i.Name(), pullOpts...); err2 != nil {
return fmt.Errorf("fetch lost layer failed: %w : %w", err2, err)
}
// retry
unpacked, err = rootfs.ApplyLayerWithOpts(ctx, layer, chain, sn, a, config.SnapshotOpts, config.ApplyOpts)
if err != nil {
return err
}
} else {
return err
}
}

if unpacked {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cri/server/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
}()

var cntr containerd.Container
if c.config.ContainerdConfig.DiscardUnpackedLayers {
ctx = context.WithValue(ctx, containerd.DiscardUnpackedLayersKey{}, true)
}
if cntr, err = c.client.NewContainer(ctx, id, opts...); err != nil {
return nil, fmt.Errorf("failed to create containerd container: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/cri/server/sandbox_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata),
containerd.WithRuntime(ociRuntime.Type, runtimeOpts)}

if c.config.ContainerdConfig.DiscardUnpackedLayers {
ctx = context.WithValue(ctx, containerd.DiscardUnpackedLayersKey{}, true)
}
container, err := c.client.NewContainer(ctx, id, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create containerd container: %w", err)
Expand Down