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

[release/1.6] Fix snapshotter root path when not under containerd root #10127

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cri/cri.go
Expand Up @@ -55,6 +55,7 @@ func init() {
plugin.EventPlugin,
plugin.ServicePlugin,
plugin.WarningPlugin,
plugin.SnapshotPlugin,
},
InitFn: initCRIService,
})
Expand Down
49 changes: 46 additions & 3 deletions pkg/cri/server/service.go
Expand Up @@ -17,7 +17,9 @@
package server

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -127,6 +129,11 @@ type criService struct {
func NewCRIService(config criconfig.Config, client *containerd.Client, warn warning.Service) (CRIService, error) {
var err error
labels := label.NewStore()

if client.SnapshotService(config.ContainerdConfig.Snapshotter) == nil {
return nil, fmt.Errorf("failed to find snapshotter %q", config.ContainerdConfig.Snapshotter)
}

c := &criService{
config: config,
client: client,
Expand All @@ -147,7 +154,11 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, warn warn
return nil, fmt.Errorf("failed to find snapshotter %q", c.config.ContainerdConfig.Snapshotter)
}

c.imageFSPath = imageFSPath(config.ContainerdRootDir, config.ContainerdConfig.Snapshotter)
c.imageFSPath = imageFSPath(
config.ContainerdRootDir,
config.ContainerdConfig.Snapshotter,
client,
)
logrus.Infof("Get image filesystem path %q", c.imageFSPath)

if err := c.initPlatform(); err != nil {
Expand Down Expand Up @@ -331,8 +342,40 @@ func (c *criService) register(s *grpc.Server) error {

// imageFSPath returns containerd image filesystem path.
// Note that if containerd changes directory layout, we also needs to change this.
func imageFSPath(rootDir, snapshotter string) string {
return filepath.Join(rootDir, fmt.Sprintf("%s.%s", plugin.SnapshotPlugin, snapshotter))
func imageFSPath(rootDir, snapshotter string, client *containerd.Client) string {
introspection := func() (string, error) {
filters := []string{fmt.Sprintf("type==%s, id==%s", plugin.SnapshotPlugin, snapshotter)}
in := client.IntrospectionService()

resp, err := in.Plugins(context.Background(), filters)
if err != nil {
return "", err
}

if len(resp.Plugins) <= 0 {
return "", fmt.Errorf("inspection service could not find snapshotter %s plugin", snapshotter)
}

sn := resp.Plugins[0]
if root, ok := sn.Exports[plugin.SnapshotterRootDir]; ok {
return root, nil
}
return "", errors.New("snapshotter does not export root path")
}

var imageFSPath string
path, err := introspection()
if err != nil {
logrus.WithError(err).WithField("snapshotter", snapshotter).Warn("snapshotter doesn't export root path")
imageFSPath = filepath.Join(
rootDir,
plugin.SnapshotPlugin.String()+"."+snapshotter,
)
} else {
imageFSPath = path
}

return imageFSPath
}

func loadOCISpec(filename string) (*oci.Spec, error) {
Expand Down
4 changes: 4 additions & 0 deletions plugin/plugin.go
Expand Up @@ -92,6 +92,10 @@ const (
DeprecationsPlugin = "deprecations"
)

const (
SnapshotterRootDir = "root"
)

// Registration contains information for registering a plugin
type Registration struct {
// Type of the plugin
Expand Down
6 changes: 4 additions & 2 deletions services/server/config/config.go
Expand Up @@ -164,8 +164,10 @@ type CgroupConfig struct {

// ProxyPlugin provides a proxy plugin configuration
type ProxyPlugin struct {
Type string `toml:"type"`
Address string `toml:"address"`
Type string `toml:"type"`
Address string `toml:"address"`
Platform string `toml:"platform"`
Exports map[string]string `toml:"exports"`
}

// BoltConfig defines the configuration values for the bolt plugin, which is
Expand Down
21 changes: 20 additions & 1 deletion services/server/server.go
Expand Up @@ -39,6 +39,7 @@ import (
metrics "github.com/docker/go-metrics"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
bolt "go.etcd.io/bbolt"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
Expand All @@ -59,6 +60,7 @@ import (
"github.com/containerd/containerd/pkg/deprecation"
"github.com/containerd/containerd/pkg/dialer"
"github.com/containerd/containerd/pkg/timeout"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/plugin"
srvconfig "github.com/containerd/containerd/services/server/config"
"github.com/containerd/containerd/services/warning"
Expand Down Expand Up @@ -549,6 +551,8 @@ func LoadPlugins(ctx context.Context, config *srvconfig.Config) ([]*plugin.Regis
f func(*grpc.ClientConn) interface{}

address = pp.Address
p v1.Platform
err error
)

switch pp.Type {
Expand All @@ -567,12 +571,27 @@ func LoadPlugins(ctx context.Context, config *srvconfig.Config) ([]*plugin.Regis
default:
log.G(ctx).WithField("type", pp.Type).Warn("unknown proxy plugin type")
}
if pp.Platform != "" {
p, err = platforms.Parse(pp.Platform)
if err != nil {
log.G(ctx).WithError(err).WithField("plugin", name).Warn("skipping proxy platform with bad platform")
}
} else {
p = platforms.DefaultSpec()
}

exports := pp.Exports
if exports == nil {
exports = map[string]string{}
}
exports["address"] = address

plugin.Register(&plugin.Registration{
Type: t,
ID: name,
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
ic.Meta.Exports["address"] = address
ic.Meta.Exports = exports
ic.Meta.Platforms = append(ic.Meta.Platforms, p)
conn, err := clients.getClient(address)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion snapshots/btrfs/plugin/plugin.go
Expand Up @@ -53,7 +53,7 @@ func init() {
root = config.RootPath
}

ic.Meta.Exports = map[string]string{"root": root}
ic.Meta.Exports[plugin.SnapshotterRootDir] = root
return btrfs.NewSnapshotter(root)
},
})
Expand Down
1 change: 1 addition & 0 deletions snapshots/devmapper/plugin/plugin.go
Expand Up @@ -48,6 +48,7 @@ func init() {
config.RootPath = ic.Root
}

ic.Meta.Exports[plugin.SnapshotterRootDir] = config.RootPath
return devmapper.NewSnapshotter(ic.Context, config)
},
})
Expand Down
1 change: 1 addition & 0 deletions snapshots/native/plugin/plugin.go
Expand Up @@ -48,6 +48,7 @@ func init() {
root = config.RootPath
}

ic.Meta.Exports[plugin.SnapshotterRootDir] = root
return native.NewSnapshotter(root)
},
})
Expand Down
2 changes: 1 addition & 1 deletion snapshots/overlay/plugin/plugin.go
Expand Up @@ -68,7 +68,7 @@ func init() {
oOpts = append(oOpts, overlay.WithMountOptions(config.MountOptions))
}

ic.Meta.Exports["root"] = root
ic.Meta.Exports[plugin.SnapshotterRootDir] = root
return overlay.NewSnapshotter(root, oOpts...)
},
})
Expand Down