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

feature: Allow multi snapshotters #2631

Merged
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
55 changes: 55 additions & 0 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/containerd/containerd"
eventstypes "github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -305,3 +307,56 @@ func (c *Client) collectContainerdEvents() {
}
}
}

// CheckSnapshotterValid checks whether the given snapshotter is valid
func (c *Client) CheckSnapshotterValid(snapshotter string, allowMultiSnapshotter bool) error {
var (
driverFound = false
)

plugins, err := c.Plugins(context.Background(), []string{fmt.Sprintf("type==%s", plugin.SnapshotPlugin)})
if err != nil {
logrus.Errorf("failed to get containerd plugins: %v", err)
return err
}

for _, p := range plugins {
if p.Status != PluginStatusOk {
continue
}

if p.ID == snapshotter {
driverFound = true
continue
}

// if allowMultiSnapshotter, ignore check snapshots exist
if !allowMultiSnapshotter {
// check if other snapshotter exists snapshots
exist, err := c.checkSnapshotsExist(p.ID)
if err != nil {
return fmt.Errorf("failed to check snapshotter driver %s: %v", p.ID, err)
}

if exist {
return fmt.Errorf("current snapshotter driver is %s, cannot change to %s", p.ID, snapshotter)
}
}
}

if !driverFound {
return fmt.Errorf("containerd not support snapshotter driver %s", snapshotter)
}

return nil
}

func (c *Client) checkSnapshotsExist(snapshotter string) (existSnapshot bool, err error) {
fn := func(c context.Context, s snapshots.Info) error {
existSnapshot = true
return nil
}

err = c.WalkSnapshot(context.Background(), snapshotter, fn)
return
}
1 change: 1 addition & 0 deletions ctrd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type APIClient interface {
Version(ctx context.Context) (containerd.Version, error)
Cleanup() error
Plugins(ctx context.Context, filters []string) ([]Plugin, error)
CheckSnapshotterValid(snapshotter string, allowMultiSnapshotter bool) error
}

// ContainerAPIClient provides access to containerd container features.
Expand Down
3 changes: 3 additions & 0 deletions daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type Config struct {

// Snapshotter is passed to containerd, default to overlayfs
Snapshotter string `json:"snapshotter,omitempty"`

// AllowMultiSnapshotter allows multi snapshotter, default false
AllowMultiSnapshotter bool `json:"allow-multi-snapshotter,omitempty"`
}

// GetCgroupDriver gets cgroup driver used in runc.
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewDaemon(cfg *config.Config) *Daemon {
ctrd.SetSnapshotterName(cfg.Snapshotter)
}

if err = checkSnapshotterValid(ctrd.CurrentSnapshotterName(), ctrdClient); err != nil {
if err = ctrdClient.CheckSnapshotterValid(ctrd.CurrentSnapshotterName(), cfg.AllowMultiSnapshotter); err != nil {
logrus.Errorf("failed to check snapshotter driver: %v", err)
return nil
}
Expand Down
56 changes: 0 additions & 56 deletions daemon/daemon_utils.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package daemon

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/ctrd"

"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshots"
"github.com/sirupsen/logrus"
)

var (
Expand Down Expand Up @@ -58,53 +52,3 @@ func initialRuntime(baseDir string, runtimes map[string]types.Runtime) error {

return nil
}

// checkSnapshotterValid checks whether the given snapshotter is valid
func checkSnapshotterValid(snapshotter string, ctrdClient ctrd.APIClient) error {
var (
driverFound = false
)

plugins, err := ctrdClient.Plugins(context.Background(), []string{fmt.Sprintf("type==%s", plugin.SnapshotPlugin)})
if err != nil {
logrus.Errorf("failed to get containerd plugins: %v", err)
return err
}

for _, p := range plugins {
if p.Status != ctrd.PluginStatusOk {
continue
}

if p.ID == snapshotter {
driverFound = true
continue
}

// check if other snapshotter exists snapshots
exist, err := checkSnapshotsExist(p.ID, ctrdClient)
if exist {
return fmt.Errorf("current snapshotter driver is %s, cannot change to %s", p.ID, snapshotter)
}

if err != nil {
return fmt.Errorf("failed to check snapshotter driver %s: %v", p.ID, err)
}
}

if !driverFound {
return fmt.Errorf("containerd not support snapshotter driver %s", snapshotter)
}

return nil
}

func checkSnapshotsExist(snapshotter string, ctrdClient ctrd.APIClient) (existSnapshot bool, err error) {
fn := func(c context.Context, s snapshots.Info) error {
existSnapshot = true
return nil
}

err = ctrdClient.WalkSnapshot(context.Background(), snapshotter, fn)
return
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func setupFlags(cmd *cobra.Command) {
flagSet.StringVar(&cfg.QuotaDriver, "quota-driver", "", "Set quota driver(grpquota/prjquota), if not set, it will set by kernel version")
flagSet.StringVar(&cfg.ConfigFile, "config-file", "/etc/pouch/config.json", "Configuration file of pouchd")
flagSet.StringVar(&cfg.Snapshotter, "snapshotter", "overlayfs", "Snapshotter driver of pouchd, it will be passed to containerd")
flagSet.BoolVar(&cfg.AllowMultiSnapshotter, "allow-multi-snapshotter", false, "If set true, pouchd will allow multi snapshotter")

// volume config
flagSet.StringVar(&cfg.VolumeConfig.DriverAlias, "volume-driver-alias", "", "Set volume driver alias, <name=alias>[;name1=alias1]")
Expand Down
33 changes: 33 additions & 0 deletions test/cli_snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,39 @@ func (suite *PouchSnapshotterSuite) TestOldSnapshotterNotClean(c *check.C) {
dcfg.KillDaemon()
}

// TestAllowMultiSnapshotter tests pouchd with two snapshotter
func (suite *PouchSnapshotterSuite) TestAllowMultiSnapshotter(c *check.C) {
dcfg, err := StartDefaultDaemon("--snapshotter", "overlayfs", "--allow-multi-snapshotter")
c.Assert(err, check.IsNil)

fileSystemInfo, err := mount.Lookup(dcfg.HomeDir)
c.Assert(err, check.IsNil)

if fileSystemInfo.FSType != "btrfs" {
dcfg.KillDaemon()
c.Skip("btrfs is not supported! Ignore test suite TestOldSnapshotterNotClean.")
}

result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage)
c.Assert(result.ExitCode, check.Equals, 0)
dcfg.KillDaemon()
time.Sleep(10 * time.Second)

dcfg, err = StartDefaultDaemon("--snapshotter", "btrfs", "--allow-multi-snapshotter")
c.Assert(err, check.IsNil)

dcfg.KillDaemon()
time.Sleep(10 * time.Second)

// clean image
dcfg, err = StartDefaultDaemon("--snapshotter", "overlayfs")
c.Assert(err, check.IsNil)

result = RunWithSpecifiedDaemon(dcfg, "rmi", busyboxImage)
c.Assert(result.ExitCode, check.Equals, 0)
dcfg.KillDaemon()
}

// checkSnapshotsDir returns snapshots directory names by given snapshotter name
func checkSnapshotsDir(homeDir string, snapshotter string) ([]string, error) {
const snapshotterPrefix = "io.containerd.snapshotter.v1."
Expand Down