daemon: buildCreateEndpointOptions: don't use PortBinding.GetCopy()

This code was initializing a new PortBinding, and creating a deep copy
for each binding. It's unclear what the intent was here, but at least
PortBinding.GetCopy() wasn't adding much value, as it created a new
PortBinding, [copying all values from the original][1], which includes
a [copy of IPAddresses in it][2]. Our original "template" did not have any
of that, so let's forego that, and just create new PortBindings as we go.

[1]: 454b6a7cf5/libnetwork/types/types.go (L110-L120)
[2]: 454b6a7cf5/libnetwork/types/types.go (L236-L244)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-23 15:52:56 +02:00
parent cc79024761
commit 1c6dae1291
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -920,10 +920,8 @@ func buildCreateEndpointOptions(c *container.Container, n *libnetwork.Network, e
Port: portNum,
})
pb := networktypes.PortBinding{Port: portNum, Proto: portProto}
binding := bindings[port]
for i := 0; i < len(binding); i++ {
pbCopy := pb.GetCopy()
newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort))
var portStart, portEnd int
if err == nil {
@ -932,14 +930,20 @@ func buildCreateEndpointOptions(c *container.Container, n *libnetwork.Network, e
if err != nil {
return nil, errors.Wrapf(err, "Error parsing HostPort value (%s)", binding[i].HostPort)
}
pbCopy.HostPort = uint16(portStart)
pbCopy.HostPortEnd = uint16(portEnd)
pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
publishedPorts = append(publishedPorts, pbCopy)
publishedPorts = append(publishedPorts, networktypes.PortBinding{
Proto: portProto,
Port: portNum,
HostIP: net.ParseIP(binding[i].HostIP),
HostPort: uint16(portStart),
HostPortEnd: uint16(portEnd),
})
}
if c.HostConfig.PublishAllPorts && len(binding) == 0 {
publishedPorts = append(publishedPorts, pb)
publishedPorts = append(publishedPorts, networktypes.PortBinding{
Proto: portProto,
Port: portNum,
})
}
}