Skip to content

Commit 3b4918f

Browse files
committed
add support syncfs
Signed-off-by: ningmingxiao <[email protected]>
1 parent a1a2ec2 commit 3b4918f

File tree

8 files changed

+25
-7
lines changed

8 files changed

+25
-7
lines changed

cmd/nerdctl/image/image_load.go

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func NewLoadCommand() *cobra.Command {
4545
loadCommand.Flags().StringSlice("platform", []string{}, "Import content for a specific platform")
4646
loadCommand.RegisterFlagCompletionFunc("platform", completion.Platforms)
4747
loadCommand.Flags().Bool("all-platforms", false, "Import content for all platforms")
48+
loadCommand.Flags().Bool("sync-fs", false, "Synchronize the underlying filesystem containing files when unpack images")
4849
// #endregion
4950

5051
return loadCommand
@@ -71,6 +72,10 @@ func processLoadCommandFlags(cmd *cobra.Command) (types.ImageLoadOptions, error)
7172
if err != nil {
7273
return types.ImageLoadOptions{}, err
7374
}
75+
syncfs, err := cmd.Flags().GetBool("sync-fs")
76+
if err != nil {
77+
return types.ImageLoadOptions{}, err
78+
}
7479
return types.ImageLoadOptions{
7580
GOptions: globalOptions,
7681
Input: input,
@@ -79,6 +84,7 @@ func processLoadCommandFlags(cmd *cobra.Command) (types.ImageLoadOptions, error)
7984
Stdout: cmd.OutOrStdout(),
8085
Stdin: cmd.InOrStdin(),
8186
Quiet: quiet,
87+
SyncFs: syncfs,
8288
}, nil
8389
}
8490

cmd/nerdctl/image/image_pull.go

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func NewPullCommand() *cobra.Command {
6666
// #endregion
6767

6868
pullCommand.Flags().BoolP("quiet", "q", false, "Suppress verbose output")
69+
pullCommand.Flags().BoolP("sync-fs", "s", false, "Synchronize the underlying filesystem containing files when unpack images")
6970

7071
pullCommand.Flags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")
7172

@@ -114,6 +115,11 @@ func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error)
114115
return types.ImagePullOptions{}, err
115116
}
116117

118+
syncFs, err := cmd.Flags().GetBool("sync-fs")
119+
if err != nil {
120+
return types.ImagePullOptions{}, err
121+
}
122+
117123
verifyOptions, err := helpers.ProcessImageVerifyOptions(cmd)
118124
if err != nil {
119125
return types.ImagePullOptions{}, err
@@ -123,6 +129,7 @@ func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error)
123129
VerifyOptions: verifyOptions,
124130
OCISpecPlatform: ociSpecPlatform,
125131
Unpack: unpack,
132+
SyncFs: syncFs,
126133
Mode: "always",
127134
Quiet: quiet,
128135
IPFSAddress: ipfsAddressStr,

pkg/api/types/image_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ type ImagePullOptions struct {
200200
// If nil, it will unpack automatically if only 1 platform is specified.
201201
Unpack *bool
202202
// Content for specific platforms. Empty if `--all-platforms` is true
203+
SyncFs bool
203204
OCISpecPlatform []v1.Platform
204205
// Pull mode
205206
Mode string

pkg/api/types/load_types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ type ImageLoadOptions struct {
3030
// AllPlatforms import content for all platforms
3131
AllPlatforms bool
3232
// Quiet suppresses the load output.
33-
Quiet bool
33+
Quiet bool
34+
SyncFs bool
3435
}

pkg/imgutil/imgutil.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
containerd "github.com/containerd/containerd/v2/client"
3131
"github.com/containerd/containerd/v2/core/content"
32+
"github.com/containerd/containerd/v2/core/diff"
3233
"github.com/containerd/containerd/v2/core/images"
3334
"github.com/containerd/containerd/v2/core/remotes"
3435
"github.com/containerd/containerd/v2/core/snapshots"
@@ -59,7 +60,7 @@ type EnsuredImage struct {
5960
type PullMode = string
6061

6162
// GetExistingImage returns the specified image if exists in containerd. Return errdefs.NotFound() if not exists.
62-
func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotter, rawRef string, platform ocispec.Platform) (*EnsuredImage, error) {
63+
func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotter string, syncfs bool, rawRef string, platform ocispec.Platform) (*EnsuredImage, error) {
6364
var res *EnsuredImage
6465
imgwalker := &imagewalker.ImageWalker{
6566
Client: client,
@@ -82,7 +83,7 @@ func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotte
8283
Remote: getSnapshotterOpts(snapshotter).isRemote(),
8384
}
8485
if unpacked, err := image.IsUnpacked(ctx, snapshotter); err == nil && !unpacked {
85-
if err := image.Unpack(ctx, snapshotter); err != nil {
86+
if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(syncfs))); err != nil {
8687
return err
8788
}
8889
}
@@ -115,7 +116,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, rawRef string,
115116

116117
// if not `always` pull and given one platform and image found locally, return existing image directly.
117118
if options.Mode != "always" && len(options.OCISpecPlatform) == 1 {
118-
if res, err := GetExistingImage(ctx, client, options.GOptions.Snapshotter, rawRef, options.OCISpecPlatform[0]); err == nil {
119+
if res, err := GetExistingImage(ctx, client, options.GOptions.Snapshotter, options.SyncFs, rawRef, options.OCISpecPlatform[0]); err == nil {
119120
return res, nil
120121
} else if !errdefs.IsNotFound(err) {
121122
return nil, err
@@ -204,6 +205,7 @@ func PullImage(ctx context.Context, client *containerd.Client, resolver remotes.
204205
Resolver: resolver,
205206
RemoteOpts: []containerd.RemoteOpt{},
206207
Platforms: options.OCISpecPlatform, // empty for all-platforms
208+
SyncFs: options.SyncFs,
207209
}
208210
if !options.Quiet {
209211
config.ProgressOutput = options.Stderr

pkg/imgutil/load/load.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
containerd "github.com/containerd/containerd/v2/client"
28+
"github.com/containerd/containerd/v2/core/diff"
2829
"github.com/containerd/containerd/v2/core/images"
2930
"github.com/containerd/containerd/v2/core/images/archive"
3031
"github.com/containerd/containerd/v2/pkg/archive/compression"
@@ -137,8 +138,7 @@ func unpackImage(ctx context.Context, client *containerd.Client, model images.Im
137138
if !options.Quiet {
138139
fmt.Fprintf(options.Stdout, "unpacking %s (%s)...\n", model.Name, model.Target.Digest)
139140
}
140-
141-
err := image.Unpack(ctx, options.GOptions.Snapshotter)
141+
err := image.Unpack(ctx, options.GOptions.Snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(options.SyncFs)))
142142
if err != nil {
143143
return err
144144
}

pkg/imgutil/pull/pull.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Config struct {
4646
// RemoteOpts related to unpacking can be set only when len(Platforms) is 1.
4747
RemoteOpts []containerd.RemoteOpt
4848
Platforms []ocispec.Platform // empty for all-platforms
49+
SyncFs bool
4950
}
5051

5152
// Pull loads all resources into the content store and returns the image

pkg/ipfs/image.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, scheme, ref, ip
5858

5959
// if not `always` pull and given one platform and image found locally, return existing image directly.
6060
if options.Mode != "always" && len(options.OCISpecPlatform) == 1 {
61-
if res, err := imgutil.GetExistingImage(ctx, client, options.GOptions.Snapshotter, ref, options.OCISpecPlatform[0]); err == nil {
61+
if res, err := imgutil.GetExistingImage(ctx, client, options.GOptions.Snapshotter, options.SyncFs, ref, options.OCISpecPlatform[0]); err == nil {
6262
return res, nil
6363
} else if !errdefs.IsNotFound(err) {
6464
return nil, err

0 commit comments

Comments
 (0)