Skip to content

Commit

Permalink
snapshot: fix error on proxy driver when switching different snapshotter
Browse files Browse the repository at this point in the history
Fixes: containerd#592

Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
  • Loading branch information
ChengyuZhu6 committed May 10, 2024
1 parent ce4848e commit 1ff7eb0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
14 changes: 14 additions & 0 deletions snapshot/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func chooseProcessor(ctx context.Context, logger *logrus.Entry,
}
}

proxyhandler := func() (bool, []mount.Mount, error) {
logger.Infof("Nydus proxy handler")
mounts, err := sn.mountProxy(ctx, labels, s)
return false, mounts, err
}

// OCI image is also marked with "containerd.io/snapshot.ref" by Containerd
target, isRoLayer := labels[label.TargetSnapshotRef]

Expand Down Expand Up @@ -118,6 +124,14 @@ func chooseProcessor(ctx context.Context, logger *logrus.Entry,
// It should not be committed during this Prepare() operation.

pID, pInfo, _, pErr := snapshot.GetSnapshotInfo(ctx, sn.ms, parent)
if checkErr := checkLabelsWithDriver(pInfo.Labels); checkErr != nil {
logger.Infof("not found proxy label: %v, err = %v", pInfo.Labels, checkErr)
// if pInfo.Labels == nil {
// pInfo.Labels = make(map[string]string)
// }
// pInfo.Labels[label.NydusProxyMode] = "true"
handler = proxyhandler
}
if pErr == nil && label.IsNydusProxyMode(pInfo.Labels) {
logger.Infof("Prepare active snapshot %s in proxy mode", key)
handler = remoteHandler(pID, pInfo.Labels)
Expand Down
71 changes: 70 additions & 1 deletion snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/continuity/fs"

"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/config/daemonconfig"
rafs "github.com/containerd/nydus-snapshotter/pkg/rafs"

"github.com/containerd/nydus-snapshotter/pkg/cache"
"github.com/containerd/nydus-snapshotter/pkg/cgroup"
Expand Down Expand Up @@ -318,6 +318,7 @@ func (o *snapshotter) Cleanup(ctx context.Context) error {
}

func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) {
log.L.Debugf("[Stat] snapshots")
_, info, _, err := snapshot.GetSnapshotInfo(ctx, o.ms, key)
return info, err
}
Expand Down Expand Up @@ -432,6 +433,11 @@ func (o *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, er
return nil, errors.Wrapf(err, "get snapshot %s", key)
}

if checkErr := checkLabelsWithDriver(info.Labels); checkErr != nil {
log.L.Infof("[Mounts] not found proxy label")
return o.mountProxy(ctx, info.Labels, *snap)
}

if needRemoteMounts {
return o.mountRemote(ctx, info.Labels, *snap, metaSnapshotID, key)
}
Expand Down Expand Up @@ -838,6 +844,53 @@ func overlayMount(options []string) []mount.Mount {
}
}

func (o *snapshotter) mountProxy(ctx context.Context, labels map[string]string, s storage.Snapshot) ([]mount.Mount, error) {

Check warning on line 847 in snapshot/snapshot.go

View workflow job for this annotation

GitHub Actions / Build and Lint

unused-parameter: parameter 'labels' seems to be unused, consider removing or renaming it as _ (revive)
var overlayOptions []string
var parentPaths []string
if s.Kind == snapshots.KindActive {
overlayOptions = append(overlayOptions,
fmt.Sprintf("workdir=%s", o.workPath(s.ID)),
fmt.Sprintf("upperdir=%s", o.upperPath(s.ID)),
)
}
log.G(ctx).Infof("len(s.ParentIDs) = %v", len(s.ParentIDs))
if len(s.ParentIDs) == 0 {
parentPaths = make([]string, 0, 8)
parentPaths = append(parentPaths, config.GetSnapshotsRootDir())
} else {
parentPaths = make([]string, len(s.ParentIDs))
for i := range s.ParentIDs {
parentPaths[i] = o.upperPath(s.ParentIDs[i])
}
}

lowerDirOption := fmt.Sprintf("lowerdir=%s", strings.Join(parentPaths, ":"))
overlayOptions = append(overlayOptions, lowerDirOption)
log.G(ctx).Infof("proxy mount options %v", overlayOptions)
options, err := o.mountWithProxyVolume(rafs.Rafs{
FsDriver: config.GetFsDriver(),
ImageID: "",
SnapshotID: "",
SnapshotDir: "",
Annotations: make(map[string]string),
})
if err != nil {
return []mount.Mount{}, errors.Wrapf(err, "create kata volume for proxy")
}
if len(options) > 0 {
overlayOptions = append(overlayOptions, options...)
}
log.G(ctx).Debugf("fuse.nydus-overlayfs mount options %v", overlayOptions)
mounts := []mount.Mount{
{
Type: "fuse.nydus-overlayfs",
Source: "overlay",
Options: overlayOptions,
},
}
return mounts, nil
}

// `s` is the upmost snapshot and `id` refers to the nydus meta snapshot
// `s` and `id` can represent a different layer, it's useful when View an image
func (o *snapshotter) mountRemote(ctx context.Context, labels map[string]string, s storage.Snapshot, id, key string) ([]mount.Mount, error) {
Expand Down Expand Up @@ -1011,3 +1064,19 @@ func (o *snapshotter) snapshotRoot() string {
func (o *snapshotter) snapshotDir(id string) string {
return filepath.Join(o.snapshotRoot(), id)
}

func checkLabelsWithDriver(labels map[string]string) error {
isProxyDriver := config.GetFsDriver() == config.FsDriverProxy
isProxyLabel := label.IsNydusProxyMode(labels)
_, isProxyImage := labels[label.CRIImageRef]
log.G(context.Background()).Infof("isProxyDriver = %b", isProxyDriver)

Check failure on line 1072 in snapshot/snapshot.go

View workflow job for this annotation

GitHub Actions / Build and Lint

printf: (*github.com/sirupsen/logrus.Entry).Infof format %b has arg isProxyDriver of wrong type bool (govet)
log.G(context.Background()).Infof("isProxyLabel = %b", isProxyLabel)

Check failure on line 1073 in snapshot/snapshot.go

View workflow job for this annotation

GitHub Actions / Build and Lint

printf: (*github.com/sirupsen/logrus.Entry).Infof format %b has arg isProxyLabel of wrong type bool (govet)
log.G(context.Background()).Infof("isProxyImage = %b", isProxyImage)

Check failure on line 1074 in snapshot/snapshot.go

View workflow job for this annotation

GitHub Actions / Build and Lint

printf: (*github.com/sirupsen/logrus.Entry).Infof format %b has arg isProxyImage of wrong type bool (govet)
if isProxyDriver && isProxyImage {
return nil
}
if (isProxyDriver && !isProxyLabel) || (!isProxyDriver && isProxyLabel) {
return errors.Errorf("check Labels With Driver failed, driver = %q, labels = %q", config.GetFsDriver(), labels)
}
return nil
}

0 comments on commit 1ff7eb0

Please sign in to comment.