rootless: improve automatic range split

sort.Search returns the smallest index, so provide the available IDs
in decreasing order.

It fixes an issue when splitting the current mappings over multiple
available IDs.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2021-05-05 17:49:52 +02:00
parent 4d2ba323f2
commit c2c0d50582
2 changed files with 59 additions and 2 deletions

View File

@@ -137,7 +137,7 @@ func GetAvailableGids() (int64, error) {
// It assumes availableMappings is sorted by ID.
func findIDInMappings(id int64, availableMappings []user.IDMap) *user.IDMap {
i := sort.Search(len(availableMappings), func(i int) bool {
return availableMappings[i].ID >= id
return availableMappings[i].ID <= id
})
if i < 0 || i >= len(availableMappings) {
return nil
@@ -157,7 +157,7 @@ func MaybeSplitMappings(mappings []spec.LinuxIDMapping, availableMappings []user
overflow.Size = 0
consumed := 0
sort.Slice(availableMappings, func(i, j int) bool {
return availableMappings[i].ID < availableMappings[j].ID
return availableMappings[i].ID > availableMappings[j].ID
})
for {
cur := overflow

View File

@@ -98,4 +98,61 @@ func TestMaybeSplitMappings(t *testing.T) {
if !reflect.DeepEqual(newMappings, desiredMappings) {
t.Fatal("wrong mappings generated")
}
mappings = []spec.LinuxIDMapping{
{
ContainerID: 0,
HostID: 0,
Size: 4,
},
}
desiredMappings = []spec.LinuxIDMapping{
{
ContainerID: 0,
HostID: 0,
Size: 1,
},
{
ContainerID: 1,
HostID: 1,
Size: 1,
},
{
ContainerID: 2,
HostID: 2,
Size: 1,
},
{
ContainerID: 3,
HostID: 3,
Size: 1,
},
}
availableMappings = []user.IDMap{
{
ID: 0,
ParentID: 0,
Count: 1,
},
{
ID: 1,
ParentID: 1,
Count: 1,
},
{
ID: 2,
ParentID: 2,
Count: 1,
},
{
ID: 3,
ParentID: 3,
Count: 1,
},
}
newMappings = MaybeSplitMappings(mappings, availableMappings)
if !reflect.DeepEqual(newMappings, desiredMappings) {
t.Fatal("wrong mappings generated")
}
}