Skip to content

Commit 1b2ab83

Browse files
committed
feat: donot unpack image when upload
Signed-off-by: zwtop <[email protected]>
1 parent 7f417b5 commit 1b2ab83

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

client/clienttest/runtime.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ func NewRuntime(followWaitTime time.Duration) client.Runtime {
4848
}
4949
}
5050

51-
func (r *runtime) Platform() platforms.MatchComparer { return platforms.All }
52-
func (r *runtime) ContainerdClient() *containerd.Client { return &containerd.Client{} }
53-
func (r *runtime) Namespace() string { return "unknown" }
54-
func (r *runtime) ConfigRuntime(context.Context) error { return nil }
55-
func (r *runtime) RemoveNamespace(context.Context) error { return nil }
51+
func (r *runtime) Platform() platforms.MatchComparer { return platforms.All }
52+
func (r *runtime) ContainerdClient() *containerd.Client { return &containerd.Client{} }
53+
func (r *runtime) Namespace() string { return "unknown" }
54+
func (r *runtime) ConfigRuntime(context.Context) error { return nil }
55+
func (r *runtime) RemoveNamespace(context.Context) error { return nil }
56+
func (r *runtime) UnpackImage(context.Context, string) error { return nil }
5657

5758
func (r *runtime) RecommendedRuntimeInfo(context.Context, *model.Container) *containers.RuntimeInfo {
5859
return &containers.RuntimeInfo{}

client/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type ImageManager interface {
6666
// RemoveImage remove an image from containerd
6767
RemoveImage(ctx context.Context, ref string) error
6868

69+
// UnpackImage unpack an image in containerd
70+
UnpackImage(ctx context.Context, ref string) error
71+
6972
// GetImage return image details
7073
GetImage(ctx context.Context, ref string) (*images.Image, bool, error)
7174
}

client/runtime.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,10 @@ func (r *runtime) ImportImages(ctx context.Context, refs ...string) error {
181181

182182
for _, ref := range refs {
183183
// fix: pull with unpack do not fetch missing contents
184-
img, err := r.client.Fetch(ctx, ref, containerd.WithPlatformMatcher(r.platform), containerd.WithResolver(r.resolver))
184+
_, err := r.client.Fetch(ctx, ref, containerd.WithPlatformMatcher(r.platform), containerd.WithResolver(r.resolver))
185185
if err != nil {
186186
return fmt.Errorf("import %s: %w", ref, err)
187187
}
188-
189-
err = containerd.NewImageWithPlatform(r.client, img, r.platform).Unpack(ctx, containerd.DefaultSnapshotter)
190-
if err != nil {
191-
return fmt.Errorf("unpack %s: %w", ref, err)
192-
}
193188
}
194189
return nil
195190
}
@@ -205,6 +200,15 @@ func (r *runtime) RemoveImage(ctx context.Context, ref string) error {
205200
return ignoreNotFoundError(err)
206201
}
207202

203+
func (r *runtime) UnpackImage(ctx context.Context, ref string) error {
204+
ctx = namespaces.WithNamespace(ctx, r.namespace)
205+
img, err := r.getImage(ctx, ref)
206+
if err != nil {
207+
return err
208+
}
209+
return unpackImage(ctx, img, containerd.DefaultSnapshotter)
210+
}
211+
208212
func (r *runtime) GetImage(ctx context.Context, ref string) (*images.Image, bool, error) {
209213
ctx = namespaces.WithNamespace(ctx, r.namespace)
210214

@@ -549,22 +553,27 @@ func withoutAnyMounts() oci.SpecOpts {
549553
func withNewSnapshotAndConfig(img containerd.Image, configContent []model.ConfigFile) containerd.NewContainerOpts {
550554
return func(ctx context.Context, client *containerd.Client, c *containers.Container) error {
551555
var (
552-
snapshotID = rand.String(10)
553-
data = toRawConfig(configContent)
554-
descriptor = v1.Descriptor{
556+
snapshotID = rand.String(10)
557+
snapshotterName = containerd.DefaultSnapshotter
558+
data = toRawConfig(configContent)
559+
descriptor = v1.Descriptor{
555560
MediaType: v1.MediaTypeImageLayer,
556561
Digest: digest.SHA256.FromBytes(data),
557562
Size: int64(len(data)),
558563
}
559564
ref = fmt.Sprintf("ingest-%s", descriptor.Digest)
560565
)
561566

562-
diffIDs, err := img.RootFS(ctx)
567+
err := unpackImage(ctx, img, snapshotterName)
563568
if err != nil {
564569
return err
565570
}
566571

567-
mounts, err := client.SnapshotService(containerd.DefaultSnapshotter).Prepare(ctx, snapshotID, identity.ChainID(diffIDs).String())
572+
diffIDs, err := img.RootFS(ctx)
573+
if err != nil {
574+
return err
575+
}
576+
mounts, err := client.SnapshotService(snapshotterName).Prepare(ctx, snapshotID, identity.ChainID(diffIDs).String())
568577
if err != nil {
569578
return err
570579
}
@@ -734,3 +743,16 @@ func withAllowAllDevices(_ context.Context, _ oci.Client, _ *containers.Containe
734743
}
735744
return nil
736745
}
746+
747+
func unpackImage(ctx context.Context, img containerd.Image, snapshotterName string) error {
748+
unpacked, err := img.IsUnpacked(ctx, snapshotterName)
749+
if err != nil {
750+
return err
751+
}
752+
if !unpacked {
753+
if err := img.Unpack(ctx, snapshotterName); err != nil {
754+
return err
755+
}
756+
}
757+
return nil
758+
}

0 commit comments

Comments
 (0)