Skip to content

Commit

Permalink
CRI: "Fix" imageFSPath behavior
Browse files Browse the repository at this point in the history
Currently it didn't take into account that certain snapshots can explicitly
have their root directories placed at a different location. This changes
it to use the RootPath method of the snapshotter if it implements it.

Without this change, cadvisor is not able to get filesystem usage
information, which prevents the kubelet from doing image garbage
collection and enforcing ephemeral storage limits.

Signed-off-by: Danny Canter <danny@dcantah.dev>
(cherry picked from commit 6aeec45)
Signed-off-by: Kern Walster <walster@amazon.com>
  • Loading branch information
dcantah authored and Kern-- committed Apr 18, 2024
1 parent bd423bf commit a8ebceb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/cri/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
plugin.ServicePlugin,
plugin.NRIApiPlugin,
plugin.WarningPlugin,
plugin.SnapshotPlugin,
},
InitFn: initCRIService,
})
Expand Down
49 changes: 46 additions & 3 deletions pkg/cri/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package server

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -126,6 +128,11 @@ type criService struct {
func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.API, 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 @@ -149,7 +156,11 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
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 @@ -341,8 +352,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

0 comments on commit a8ebceb

Please sign in to comment.