Skip to content

Commit c9ee33e

Browse files
kolyshkinPaweł Szulik
authored andcommitted
container: remove Mounts from CgroupSubsystems
This field is only used - to check if there are any mounts, i.e. len() == 0; - to iterate over the list of mounts in watcher. In both cases, we can reuse MountPoints without any degradation. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 60eba7b commit c9ee33e

File tree

4 files changed

+7
-40
lines changed

4 files changed

+7
-40
lines changed

container/libcontainer/helpers.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import (
3030
)
3131

3232
type CgroupSubsystems struct {
33-
// Cgroup subsystem mounts.
34-
// e.g.: "/sys/fs/cgroup/cpu" -> ["cpu", "cpuacct"]
35-
Mounts []cgroups.Mount
36-
3733
// Cgroup subsystem to their mount location.
3834
// e.g.: "cpu" -> "/sys/fs/cgroup/cpu"
3935
MountPoints map[string]string
@@ -99,8 +95,6 @@ func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[st
9995
}
10096

10197
// Trim the mounts to only the subsystems we care about.
102-
supportedCgroups := make([]cgroups.Mount, 0, len(allCgroups))
103-
recordedMountpoints := make(map[string]struct{}, len(allCgroups))
10498
mountPoints := make(map[string]string, len(allCgroups))
10599
for _, mount := range allCgroups {
106100
for _, subsystem := range mount.Subsystems {
@@ -116,17 +110,11 @@ func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[st
116110
klog.V(5).Infof("skipping %s, already using mount at %s", mount.Mountpoint, mountPoints[subsystem])
117111
continue
118112
}
119-
if _, ok := recordedMountpoints[mount.Mountpoint]; !ok {
120-
// avoid appending the same mount twice in e.g. `cpu,cpuacct` case
121-
supportedCgroups = append(supportedCgroups, mount)
122-
recordedMountpoints[mount.Mountpoint] = struct{}{}
123-
}
124113
mountPoints[subsystem] = mount.Mountpoint
125114
}
126115
}
127116

128117
return CgroupSubsystems{
129-
Mounts: supportedCgroups,
130118
MountPoints: mountPoints,
131119
}, nil
132120
}

container/libcontainer/helpers_test.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
package libcontainer
1616

1717
import (
18-
"fmt"
1918
"io/ioutil"
2019
"path/filepath"
2120
"reflect"
22-
"sort"
2321
"strings"
2422
"testing"
2523

@@ -44,8 +42,6 @@ func cgroupMountsAt(path string, subsystems []string) []cgroups.Mount {
4442
}
4543

4644
func TestGetCgroupSubsystems(t *testing.T) {
47-
ourSubsystems := []string{"cpu,cpuacct", "devices", "memory", "hugetlb", "cpuset", "blkio", "pids"}
48-
4945
testCases := []struct {
5046
mounts []cgroups.Mount
5147
expected CgroupSubsystems
@@ -69,7 +65,6 @@ func TestGetCgroupSubsystems(t *testing.T) {
6965
"hugetlb": "/sys/fs/cgroup/hugetlb",
7066
"pids": "/sys/fs/cgroup/pids",
7167
},
72-
Mounts: cgroupMountsAt("/sys/fs/cgroup", ourSubsystems),
7368
},
7469
},
7570
{
@@ -87,7 +82,6 @@ func TestGetCgroupSubsystems(t *testing.T) {
8782
"hugetlb": "/sys/fs/cgroup/hugetlb",
8883
"pids": "/sys/fs/cgroup/pids",
8984
},
90-
Mounts: cgroupMountsAt("/sys/fs/cgroup", ourSubsystems),
9185
},
9286
},
9387
{
@@ -97,7 +91,6 @@ func TestGetCgroupSubsystems(t *testing.T) {
9791
MountPoints: map[string]string{
9892
"cpu": "/sys/fs/cgroup/cpu",
9993
},
100-
Mounts: cgroupMountsAt("/sys/fs/cgroup", []string{"cpu"}),
10194
},
10295
},
10396
}
@@ -113,23 +106,9 @@ func TestGetCgroupSubsystems(t *testing.T) {
113106
if err != nil {
114107
t.Fatalf("[case %d] Expected no error, but got %v", i, err)
115108
}
116-
assertCgroupSubsystemsEqual(t, testCase.expected, subSystems, fmt.Sprintf("[case %d]", i))
117-
}
118-
}
119-
120-
func assertCgroupSubsystemsEqual(t *testing.T, expected, actual CgroupSubsystems, message string) {
121-
if !reflect.DeepEqual(expected.MountPoints, actual.MountPoints) {
122-
t.Fatalf("%s Expected %v == %v", message, expected.MountPoints, actual.MountPoints)
123-
}
124-
125-
sort.Slice(expected.Mounts, func(i, j int) bool {
126-
return expected.Mounts[i].Mountpoint < expected.Mounts[j].Mountpoint
127-
})
128-
sort.Slice(actual.Mounts, func(i, j int) bool {
129-
return actual.Mounts[i].Mountpoint < actual.Mounts[j].Mountpoint
130-
})
131-
if !reflect.DeepEqual(expected.Mounts, actual.Mounts) {
132-
t.Fatalf("%s Expected %v == %v", message, expected.Mounts, actual.Mounts)
109+
if !reflect.DeepEqual(testCase.expected.MountPoints, subSystems.MountPoints) {
110+
t.Fatalf("[case %d] Expected %v == %v", i, testCase.expected.MountPoints, subSystems.MountPoints)
111+
}
133112
}
134113
}
135114

container/raw/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func Register(machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, incl
8989
if err != nil {
9090
return fmt.Errorf("failed to get cgroup subsystems: %v", err)
9191
}
92-
if len(cgroupSubsystems.Mounts) == 0 {
92+
if len(cgroupSubsystems.MountPoints) == 0 {
9393
return fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
9494
}
9595

container/raw/watcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewRawContainerWatcher() (watcher.ContainerWatcher, error) {
5050
if err != nil {
5151
return nil, fmt.Errorf("failed to get cgroup subsystems: %v", err)
5252
}
53-
if len(cgroupSubsystems.Mounts) == 0 {
53+
if len(cgroupSubsystems.MountPoints) == 0 {
5454
return nil, fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
5555
}
5656

@@ -196,8 +196,8 @@ func (w *rawContainerWatcher) processEvent(event *inotify.Event, events chan wat
196196

197197
// Derive the container name from the path name.
198198
var containerName string
199-
for _, mount := range w.cgroupSubsystems.Mounts {
200-
mountLocation := path.Clean(mount.Mountpoint) + "/"
199+
for _, mount := range w.cgroupSubsystems.MountPoints {
200+
mountLocation := path.Clean(mount) + "/"
201201
if strings.HasPrefix(event.Name, mountLocation) {
202202
containerName = event.Name[len(mountLocation)-1:]
203203
break

0 commit comments

Comments
 (0)