mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
06922d2d81
The previous code used string slices to limit the length of certain fields like endpoint or sandbox IDs. This assumes that these strings are at least as long as the slice length. Unfortunately, some sandbox IDs can be smaller than 7 characters. This fix addresses this issue by systematically converting format string calls that were taking fixed-slice arguments to use a precision specifier in the string format itself. From the golang fmt package documentation: For strings, byte slices and byte arrays, however, precision limits the length of the input to be formatted (not the size of the output), truncating if necessary. Normally it is measured in runes, but for these types when formatted with the %x or %X format it is measured in bytes. This nicely fits the desired behavior: it will limit the number of runes considered for string interpolation to the precision value. Signed-off-by: Chris Telfer <ctelfer@docker.com>
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package ipvlan
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
"github.com/docker/libnetwork/netlabel"
|
|
"github.com/docker/libnetwork/ns"
|
|
"github.com/docker/libnetwork/osl"
|
|
"github.com/docker/libnetwork/types"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// CreateEndpoint assigns the mac, ip and endpoint id for the new container
|
|
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
epOptions map[string]interface{}) error {
|
|
defer osl.InitOSContext()()
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
return err
|
|
}
|
|
n, err := d.getNetwork(nid)
|
|
if err != nil {
|
|
return fmt.Errorf("network id %q not found", nid)
|
|
}
|
|
if ifInfo.MacAddress() != nil {
|
|
return fmt.Errorf("%s interfaces do not support custom mac address assigment", ipvlanType)
|
|
}
|
|
ep := &endpoint{
|
|
id: eid,
|
|
nid: nid,
|
|
addr: ifInfo.Address(),
|
|
addrv6: ifInfo.AddressIPv6(),
|
|
}
|
|
if ep.addr == nil {
|
|
return fmt.Errorf("create endpoint was not passed an IP address")
|
|
}
|
|
// disallow port mapping -p
|
|
if opt, ok := epOptions[netlabel.PortMap]; ok {
|
|
if _, ok := opt.([]types.PortBinding); ok {
|
|
if len(opt.([]types.PortBinding)) > 0 {
|
|
logrus.Warnf("%s driver does not support port mappings", ipvlanType)
|
|
}
|
|
}
|
|
}
|
|
// disallow port exposure --expose
|
|
if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
|
|
if _, ok := opt.([]types.TransportPort); ok {
|
|
if len(opt.([]types.TransportPort)) > 0 {
|
|
logrus.Warnf("%s driver does not support port exposures", ipvlanType)
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := d.storeUpdate(ep); err != nil {
|
|
return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err)
|
|
}
|
|
|
|
n.addEndpoint(ep)
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteEndpoint remove the endpoint and associated netlink interface
|
|
func (d *driver) DeleteEndpoint(nid, eid string) error {
|
|
defer osl.InitOSContext()()
|
|
if err := validateID(nid, eid); err != nil {
|
|
return err
|
|
}
|
|
n := d.network(nid)
|
|
if n == nil {
|
|
return fmt.Errorf("network id %q not found", nid)
|
|
}
|
|
ep := n.endpoint(eid)
|
|
if ep == nil {
|
|
return fmt.Errorf("endpoint id %q not found", eid)
|
|
}
|
|
if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
|
|
if err := ns.NlHandle().LinkDel(link); err != nil {
|
|
logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
|
|
}
|
|
}
|
|
|
|
if err := d.storeDelete(ep); err != nil {
|
|
logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
|
|
}
|
|
n.deleteEndpoint(ep.id)
|
|
return nil
|
|
}
|