ipvlan_endpoint.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //go:build linux
  2. // +build linux
  3. package ipvlan
  4. import (
  5. "fmt"
  6. "github.com/docker/docker/libnetwork/driverapi"
  7. "github.com/docker/docker/libnetwork/netlabel"
  8. "github.com/docker/docker/libnetwork/ns"
  9. "github.com/docker/docker/libnetwork/osl"
  10. "github.com/docker/docker/libnetwork/types"
  11. "github.com/sirupsen/logrus"
  12. )
  13. // CreateEndpoint assigns the mac, ip and endpoint id for the new container
  14. func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
  15. epOptions map[string]interface{}) error {
  16. defer osl.InitOSContext()()
  17. if err := validateID(nid, eid); err != nil {
  18. return err
  19. }
  20. n, err := d.getNetwork(nid)
  21. if err != nil {
  22. return fmt.Errorf("network id %q not found", nid)
  23. }
  24. if ifInfo.MacAddress() != nil {
  25. return fmt.Errorf("ipvlan interfaces do not support custom mac address assignment")
  26. }
  27. ep := &endpoint{
  28. id: eid,
  29. nid: nid,
  30. addr: ifInfo.Address(),
  31. addrv6: ifInfo.AddressIPv6(),
  32. }
  33. if ep.addr == nil {
  34. return fmt.Errorf("create endpoint was not passed an IP address")
  35. }
  36. // disallow port mapping -p
  37. if opt, ok := epOptions[netlabel.PortMap]; ok {
  38. if _, ok := opt.([]types.PortBinding); ok {
  39. if len(opt.([]types.PortBinding)) > 0 {
  40. logrus.Warnf("ipvlan driver does not support port mappings")
  41. }
  42. }
  43. }
  44. // disallow port exposure --expose
  45. if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
  46. if _, ok := opt.([]types.TransportPort); ok {
  47. if len(opt.([]types.TransportPort)) > 0 {
  48. logrus.Warnf("ipvlan driver does not support port exposures")
  49. }
  50. }
  51. }
  52. if err := d.storeUpdate(ep); err != nil {
  53. return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err)
  54. }
  55. n.addEndpoint(ep)
  56. return nil
  57. }
  58. // DeleteEndpoint remove the endpoint and associated netlink interface
  59. func (d *driver) DeleteEndpoint(nid, eid string) error {
  60. defer osl.InitOSContext()()
  61. if err := validateID(nid, eid); err != nil {
  62. return err
  63. }
  64. n := d.network(nid)
  65. if n == nil {
  66. return fmt.Errorf("network id %q not found", nid)
  67. }
  68. ep := n.endpoint(eid)
  69. if ep == nil {
  70. return fmt.Errorf("endpoint id %q not found", eid)
  71. }
  72. if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
  73. if err := ns.NlHandle().LinkDel(link); err != nil {
  74. logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
  75. }
  76. }
  77. if err := d.storeDelete(ep); err != nil {
  78. logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
  79. }
  80. n.deleteEndpoint(ep.id)
  81. return nil
  82. }