diff --git a/pkg/cdi/container-edits.go b/pkg/cdi/container-edits.go index a7ac70d..4744eff 100644 --- a/pkg/cdi/container-edits.go +++ b/pkg/cdi/container-edits.go @@ -355,7 +355,7 @@ func ensureOCIHooks(spec *oci.Spec) { func sortMounts(specgen *ocigen.Generator) { mounts := specgen.Mounts() specgen.ClearMounts() - sort.Sort(orderedMounts(mounts)) + sort.Stable(orderedMounts(mounts)) specgen.Config.Mounts = mounts } @@ -375,14 +375,7 @@ func (m orderedMounts) Len() int { // mount indexed by parameter 1 is less than that of the mount indexed by // parameter 2. Used in sorting. func (m orderedMounts) Less(i, j int) bool { - ip, jp := m.parts(i), m.parts(j) - if ip < jp { - return true - } - if jp < ip { - return false - } - return m[i].Destination < m[j].Destination + return m.parts(i) < m.parts(j) } // Swap swaps two items in an array of mounts. Used in sorting diff --git a/pkg/cdi/container-edits_test.go b/pkg/cdi/container-edits_test.go index 28f3378..aaa0620 100644 --- a/pkg/cdi/container-edits_test.go +++ b/pkg/cdi/container-edits_test.go @@ -599,6 +599,84 @@ func TestApplyContainerEdits(t *testing.T) { }, result: &oci.Spec{}, }, + { + name: "apply mount edits do not change the order of original mounts", + spec: &oci.Spec{ + Mounts: []oci.Mount{ + { + Source: "/some/host/path1", + Destination: "/dest/path/b", + }, + { + Source: "/some/host/path2", + Destination: "/dest/path/a", + }, + }, + }, + edits: &cdi.ContainerEdits{ + Mounts: []*cdi.Mount{ + { + HostPath: "/some/host/path3", + ContainerPath: "/dest/edit", + }, + }, + }, + result: &oci.Spec{ + Mounts: []oci.Mount{ + { + Source: "/some/host/path3", + Destination: "/dest/edit", + }, + { + Source: "/some/host/path1", + Destination: "/dest/path/b", + }, + { + Source: "/some/host/path2", + Destination: "/dest/path/a", + }, + }, + }, + }, + { + name: "mount added by edit comes after existing ones of same number of path parts", + spec: &oci.Spec{ + Mounts: []oci.Mount{ + { + Source: "/some/host/path1", + Destination: "/dest/path/c", + }, + { + Source: "/some/host/path2", + Destination: "/dest/path/b", + }, + }, + }, + edits: &cdi.ContainerEdits{ + Mounts: []*cdi.Mount{ + { + HostPath: "/some/host/path3", + ContainerPath: "/dest/path/a", + }, + }, + }, + result: &oci.Spec{ + Mounts: []oci.Mount{ + { + Source: "/some/host/path1", + Destination: "/dest/path/c", + }, + { + Source: "/some/host/path2", + Destination: "/dest/path/b", + }, + { + Source: "/some/host/path3", + Destination: "/dest/path/a", + }, + }, + }, + }, } { t.Run(tc.name, func(t *testing.T) { edits := ContainerEdits{tc.edits}