Skip to content

Commit cd8e84a

Browse files
Merge pull request moby#50705 from thaJeztah/more_modernize
daemon: use slices.Clone, maps.Collect in some places
2 parents f17f923 + 4239806 commit cd8e84a

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

daemon/container_operations.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"maps"
78
"net"
89
"net/netip"
910
"os"
1011
"runtime"
12+
"slices"
1113
"strings"
1214
"time"
1315

@@ -115,16 +117,10 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet
115117
// slice (PortMap is a map[Port][]PortBinding).
116118
bindings := make(containertypes.PortMap)
117119
for p, b := range ctr.HostConfig.PortBindings {
118-
copied := make([]containertypes.PortBinding, len(b))
119-
copy(copied, b)
120-
bindings[p] = copied
120+
bindings[p] = slices.Clone(b)
121121
}
122122

123-
// TODO(thaJeztah): Move this code to a method on nat.PortSet.
124-
ports := make([]containertypes.PortRangeProto, 0, len(ctr.Config.ExposedPorts))
125-
for p := range ctr.Config.ExposedPorts {
126-
ports = append(ports, p)
127-
}
123+
ports := slices.Collect(maps.Keys(ctr.Config.ExposedPorts))
128124
nat.SortPortMap(ports, bindings)
129125

130126
var (

daemon/network.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"maps"
78
"net"
89
"net/netip"
10+
"slices"
911
"sort"
1012
"strconv"
1113
"strings"
@@ -954,16 +956,10 @@ func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwor
954956
// slice (PortMap is a map[Port][]PortBinding).
955957
bindings := make(containertypes.PortMap)
956958
for p, b := range c.HostConfig.PortBindings {
957-
copied := make([]containertypes.PortBinding, len(b))
958-
copy(copied, b)
959-
bindings[p] = copied
959+
bindings[p] = slices.Clone(b)
960960
}
961961

962-
// TODO(thaJeztah): Move this code to a method on nat.PortSet.
963-
ports := make([]containertypes.PortRangeProto, 0, len(c.Config.ExposedPorts))
964-
for p := range c.Config.ExposedPorts {
965-
ports = append(ports, p)
966-
}
962+
ports := slices.Collect(maps.Keys(bindings))
967963
nat.SortPortMap(ports, bindings)
968964

969965
var (

0 commit comments

Comments
 (0)