From db33cc7562d90b4ae46d844f6b7c478a460d557d Mon Sep 17 00:00:00 2001 From: Kevin Hannon Date: Wed, 13 Sep 2023 16:07:14 -0400 Subject: [PATCH] add cadvisor and crio upstream changes --- container/crio/client.go | 1 + container/crio/plugin.go | 2 +- fs/fs.go | 28 +++++++++- fs/fs_test.go | 110 +++++++++++++++++++++++++++++++++++++++ fs/types.go | 4 +- 5 files changed, 141 insertions(+), 4 deletions(-) diff --git a/container/crio/client.go b/container/crio/client.go index 0f51b3d1ce..fad9a5152c 100644 --- a/container/crio/client.go +++ b/container/crio/client.go @@ -44,6 +44,7 @@ var ( type Info struct { StorageDriver string `json:"storage_driver"` StorageRoot string `json:"storage_root"` + StorageImage string `json:"storage_image"` } // ContainerInfo represents a given container information diff --git a/container/crio/plugin.go b/container/crio/plugin.go index 8937d0680a..9384de3f43 100644 --- a/container/crio/plugin.go +++ b/container/crio/plugin.go @@ -40,7 +40,7 @@ func (p *plugin) InitializeFSContext(context *fs.Context) error { if err != nil { klog.V(5).Infof("CRI-O not connected: %v", err) } else { - context.Crio = fs.CrioContext{Root: crioInfo.StorageRoot} + context.Crio = fs.CrioContext{Root: crioInfo.StorageRoot, ImageStore: crioInfo.StorageImage, Driver: crioInfo.StorageDriver} } return nil } diff --git a/fs/fs.go b/fs/fs.go index b8eb676f77..d41e543e2b 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -43,6 +43,7 @@ const ( LabelSystemRoot = "root" LabelDockerImages = "docker-images" LabelCrioImages = "crio-images" + LabelCrioContainers = "crio-containers" DriverStatusPoolName = "Pool Name" DriverStatusDataLoopFile = "Data loop file" ) @@ -295,14 +296,37 @@ func (i *RealFsInfo) addDockerImagesLabel(context Context, mounts []*mount.Info) } func (i *RealFsInfo) addCrioImagesLabel(context Context, mounts []*mount.Info) { + labelCrioImageOrContainers := LabelCrioContainers + // If imagestore is not specified, let's fall back to the original case. + // Everything will be stored in crio-images + if context.Crio.ImageStore == "" { + labelCrioImageOrContainers = LabelCrioImages + } if context.Crio.Root != "" { crioPath := context.Crio.Root crioImagePaths := map[string]struct{}{ "/": {}, } - for _, dir := range []string{"devicemapper", "btrfs", "aufs", "overlay", "zfs"} { - crioImagePaths[path.Join(crioPath, dir+"-images")] = struct{}{} + imageOrContainerPath := context.Crio.Driver + "-containers" + if context.Crio.ImageStore == "" { + // If ImageStore is not specified then we will assume ImageFs is complete separate. + // No need to split the image store. + imageOrContainerPath = context.Crio.Driver + "-images" + + } + crioImagePaths[path.Join(crioPath, imageOrContainerPath)] = struct{}{} + for crioPath != "/" && crioPath != "." { + crioImagePaths[crioPath] = struct{}{} + crioPath = filepath.Dir(crioPath) + } + i.updateContainerImagesPath(labelCrioImageOrContainers, mounts, crioImagePaths) + } + if context.Crio.ImageStore != "" { + crioPath := context.Crio.ImageStore + crioImagePaths := map[string]struct{}{ + "/": {}, } + crioImagePaths[path.Join(crioPath, context.Crio.Driver+"-images")] = struct{}{} for crioPath != "/" && crioPath != "." { crioImagePaths[crioPath] = struct{}{} crioPath = filepath.Dir(crioPath) diff --git a/fs/fs_test.go b/fs/fs_test.go index 51a1246e33..38ec03b2e2 100644 --- a/fs/fs_test.go +++ b/fs/fs_test.go @@ -520,6 +520,116 @@ func TestAddDockerImagesLabel(t *testing.T) { } } +func TestAddCrioImagesLabel(t *testing.T) { + tests := []struct { + name string + driver string + driverStatus map[string]string + dmsetupTable string + mounts []*mount.Info + imageStore string + expectedCrioImages string + expectedCrioContainers string + expectedPartition *partition + }{ + { + name: "single partition, no dedicated image fs", + driver: "overlay2", + mounts: []*mount.Info{ + { + Source: "/dev/root", + Mountpoint: "/", + FSType: "ext4", + }, + { + Source: "/sys/fs/cgroup", + Mountpoint: "/sys/fs/cgroup", + FSType: "tmpfs", + }, + }, + expectedCrioImages: "/dev/root", + expectedCrioContainers: "", + }, + { + name: "root fs inside container, docker-images bindmount", + driver: "overlay2", + mounts: []*mount.Info{ + { + Source: "overlay", + Mountpoint: "/", + FSType: "overlay", + }, + { + Source: "/dev/sda1", + Mountpoint: "/var/lib/container", + FSType: "ext4", + }, + }, + expectedCrioImages: "/dev/sda1", + }, + { + name: "[overlay2] image and container separate", + driver: "overlay2", + mounts: []*mount.Info{ + { + Source: "/dev/sdb1", + Mountpoint: "/imagestore", + FSType: "ext4", + }, + { + Source: "/dev/sdb2", + Mountpoint: "/var/lib/container", + FSType: "ext4", + }, + }, + expectedCrioImages: "/dev/sdb1", + expectedCrioContainers: "/dev/sdb2", + imageStore: "/imagestore", + }, + } + + for _, tt := range tests { + fsInfo := &RealFsInfo{ + labels: map[string]string{}, + partitions: map[string]partition{}, + dmsetup: &testDmsetup{ + data: []byte(tt.dmsetupTable), + }, + } + + context := Context{ + Crio: CrioContext{ + Root: "/var/lib/container", + ImageStore: tt.imageStore, + Driver: "overlay", + }, + } + + fsInfo.addCrioImagesLabel(context, tt.mounts) + + if tt.imageStore != "" { + if e, a := tt.expectedCrioImages, fsInfo.labels[LabelCrioImages]; e != a { + t.Errorf("%s: docker device: expected %q, got %q", tt.name, e, a) + } + + if e, a := tt.expectedCrioContainers, fsInfo.labels[LabelCrioContainers]; e != a { + t.Errorf("%s: docker device: expected %q, got %q", tt.name, e, a) + } + + } + if tt.imageStore == "" { + if e, a := tt.expectedCrioImages, fsInfo.labels[LabelCrioImages]; e != a { + t.Errorf("%s: docker device: expected %q, got %q", tt.name, e, a) + } + + } + + if tt.expectedPartition == nil { + continue + } + } +} + func TestProcessMounts(t *testing.T) { tests := []struct { name string diff --git a/fs/types.go b/fs/types.go index 2fed326517..6bc91985cb 100644 --- a/fs/types.go +++ b/fs/types.go @@ -38,7 +38,9 @@ type PodmanContext struct { } type CrioContext struct { - Root string + Root string + ImageStore string + Driver string } type DeviceInfo struct {