Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wangforthinker committed Dec 28, 2018
1 parent 3fde0e9 commit add569a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
56 changes: 56 additions & 0 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/containerd/typeurl"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshots"

)

const (
Expand Down Expand Up @@ -305,3 +308,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
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, cfg.AllowMultiSnapshotter); 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
61 changes: 1 addition & 60 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 @@ -57,57 +51,4 @@ 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, allowMultiSnapshotter bool) 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
}

// if allowMultiSnapshotter, ignore check snapshots exist
if !allowMultiSnapshotter {
// 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
}
}

0 comments on commit add569a

Please sign in to comment.