daemon: use slices.Clone, maps.Collect in some places

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-12 12:04:39 +02:00
parent 16f2e81369
commit 423980614e
2 changed files with 8 additions and 16 deletions

View File

@@ -4,10 +4,12 @@ import (
"context"
"errors"
"fmt"
"maps"
"net"
"net/netip"
"os"
"runtime"
"slices"
"strings"
"time"
@@ -115,16 +117,10 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet
// slice (PortMap is a map[Port][]PortBinding).
bindings := make(containertypes.PortMap)
for p, b := range ctr.HostConfig.PortBindings {
copied := make([]containertypes.PortBinding, len(b))
copy(copied, b)
bindings[p] = copied
bindings[p] = slices.Clone(b)
}
// TODO(thaJeztah): Move this code to a method on nat.PortSet.
ports := make([]containertypes.PortRangeProto, 0, len(ctr.Config.ExposedPorts))
for p := range ctr.Config.ExposedPorts {
ports = append(ports, p)
}
ports := slices.Collect(maps.Keys(ctr.Config.ExposedPorts))
nat.SortPortMap(ports, bindings)
var (

View File

@@ -4,8 +4,10 @@ import (
"context"
"errors"
"fmt"
"maps"
"net"
"net/netip"
"slices"
"sort"
"strconv"
"strings"
@@ -954,16 +956,10 @@ func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwor
// slice (PortMap is a map[Port][]PortBinding).
bindings := make(containertypes.PortMap)
for p, b := range c.HostConfig.PortBindings {
copied := make([]containertypes.PortBinding, len(b))
copy(copied, b)
bindings[p] = copied
bindings[p] = slices.Clone(b)
}
// TODO(thaJeztah): Move this code to a method on nat.PortSet.
ports := make([]containertypes.PortRangeProto, 0, len(c.Config.ExposedPorts))
for p := range c.Config.ExposedPorts {
ports = append(ports, p)
}
ports := slices.Collect(maps.Keys(bindings))
nat.SortPortMap(ports, bindings)
var (