2015-04-29 21:25:01 -04:00
package libnetwork
import (
2015-05-31 14:49:11 -04:00
"encoding/json"
2015-06-05 16:31:12 -04:00
"fmt"
2015-07-02 01:00:48 -04:00
"net"
2015-05-09 00:50:03 -04:00
"sync"
2015-04-30 01:58:12 -04:00
2021-04-05 20:24:47 -04:00
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/options"
"github.com/docker/docker/libnetwork/types"
2017-07-26 17:18:31 -04:00
"github.com/sirupsen/logrus"
2015-04-29 21:25:01 -04:00
)
// Endpoint represents a logical connection between a network and a sandbox.
type Endpoint interface {
// A system generated id for this endpoint.
ID ( ) string
// Name returns the name of this endpoint.
Name ( ) string
// Network returns the name of the network to which this endpoint is attached.
Network ( ) string
2015-07-02 01:00:48 -04:00
// Join joins the sandbox to the endpoint and populates into the sandbox
// the network resources allocated for the endpoint.
Join ( sandbox Sandbox , options ... EndpointOption ) error
2015-04-29 21:25:01 -04:00
2015-07-02 01:00:48 -04:00
// Leave detaches the network resources populated in the sandbox.
Leave ( sandbox Sandbox , options ... EndpointOption ) error
2015-04-29 21:25:01 -04:00
2015-05-14 02:23:45 -04:00
// Return certain operational data belonging to this endpoint
Info ( ) EndpointInfo
2015-04-29 21:25:01 -04:00
2015-05-21 04:08:10 -04:00
// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
2015-05-14 02:23:45 -04:00
DriverInfo ( ) ( map [ string ] interface { } , error )
2015-05-04 14:49:53 -04:00
2015-04-29 21:25:01 -04:00
// Delete and detaches this endpoint from the network.
2016-01-08 14:24:14 -05:00
Delete ( force bool ) error
2015-04-29 21:25:01 -04:00
}
2016-01-11 05:15:20 -05:00
// EndpointOption is an option setter function type used to pass various options to Network
2015-05-01 20:11:13 -04:00
// and Endpoint interfaces methods. The various setter functions of type EndpointOption are
// provided by libnetwork, they look like <Create|Join|Leave>Option[...](...)
type EndpointOption func ( ep * endpoint )
2015-04-29 21:25:01 -04:00
type endpoint struct {
2015-12-24 04:51:32 -05:00
name string
id string
network * network
iface * endpointInterface
joinInfo * endpointJoinInfo
sandboxID string
exposedPorts [ ] types . TransportPort
anonymous bool
disableResolution bool
generic map [ string ] interface { }
prefAddress net . IP
prefAddressV6 net . IP
ipamOptions map [ string ] string
2016-01-04 17:02:03 -05:00
aliases map [ string ] string
2016-01-07 22:19:31 -05:00
myAliases [ ] string
2016-04-13 20:53:41 -04:00
svcID string
svcName string
2016-05-25 01:46:18 -04:00
virtualIP net . IP
2016-06-14 19:40:54 -04:00
svcAliases [ ] string
2016-05-31 02:55:51 -04:00
ingressPorts [ ] * PortConfig
2015-12-24 04:51:32 -05:00
dbIndex uint64
dbExists bool
2016-08-21 01:55:00 -04:00
serviceEnabled bool
2017-08-29 02:35:31 -04:00
loadBalancer bool
2015-05-09 00:50:03 -04:00
sync . Mutex
2015-04-29 21:25:01 -04:00
}
2015-05-31 14:49:11 -04:00
func ( ep * endpoint ) MarshalJSON ( ) ( [ ] byte , error ) {
2015-06-05 16:31:12 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-05-31 14:49:11 -04:00
epMap := make ( map [ string ] interface { } )
epMap [ "name" ] = ep . name
2015-07-02 01:00:48 -04:00
epMap [ "id" ] = ep . id
2015-09-09 19:06:35 -04:00
epMap [ "ep_iface" ] = ep . iface
2016-06-10 20:32:19 -04:00
epMap [ "joinInfo" ] = ep . joinInfo
2015-05-31 14:49:11 -04:00
epMap [ "exposed_ports" ] = ep . exposedPorts
2015-10-03 19:11:50 -04:00
if ep . generic != nil {
epMap [ "generic" ] = ep . generic
}
2015-07-02 01:00:48 -04:00
epMap [ "sandbox" ] = ep . sandboxID
2015-10-20 15:12:16 -04:00
epMap [ "anonymous" ] = ep . anonymous
2015-12-24 04:51:32 -05:00
epMap [ "disableResolution" ] = ep . disableResolution
2016-01-07 22:19:31 -05:00
epMap [ "myAliases" ] = ep . myAliases
2016-04-13 20:53:41 -04:00
epMap [ "svcName" ] = ep . svcName
epMap [ "svcID" ] = ep . svcID
2016-05-25 01:46:18 -04:00
epMap [ "virtualIP" ] = ep . virtualIP . String ( )
2016-05-31 02:55:51 -04:00
epMap [ "ingressPorts" ] = ep . ingressPorts
2016-06-14 19:40:54 -04:00
epMap [ "svcAliases" ] = ep . svcAliases
2017-08-29 02:35:31 -04:00
epMap [ "loadBalancer" ] = ep . loadBalancer
2016-04-13 20:53:41 -04:00
2015-05-31 14:49:11 -04:00
return json . Marshal ( epMap )
}
func ( ep * endpoint ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
2015-06-05 16:31:12 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-05-31 14:49:11 -04:00
var epMap map [ string ] interface { }
if err := json . Unmarshal ( b , & epMap ) ; err != nil {
return err
}
ep . name = epMap [ "name" ] . ( string )
2015-07-02 01:00:48 -04:00
ep . id = epMap [ "id" ] . ( string )
2015-05-31 14:49:11 -04:00
2021-05-27 20:15:56 -04:00
// TODO(cpuguy83): So yeah, this isn't checking any errors anywhere.
// Seems like we should be checking errors even because of memory related issues that can arise.
// Alas it seems like given the nature of this data we could introduce problems if we start checking these errors.
//
// If anyone ever comes here and figures out one way or another if we can/should be checking these errors and it turns out we can't... then please document *why*
2015-05-31 14:49:11 -04:00
ib , _ := json . Marshal ( epMap [ "ep_iface" ] )
2022-07-13 16:30:47 -04:00
json . Unmarshal ( ib , & ep . iface ) //nolint:errcheck
2015-05-31 14:49:11 -04:00
2016-06-10 20:32:19 -04:00
jb , _ := json . Marshal ( epMap [ "joinInfo" ] )
2022-07-13 16:30:47 -04:00
json . Unmarshal ( jb , & ep . joinInfo ) //nolint:errcheck
2016-06-10 20:32:19 -04:00
2015-05-31 14:49:11 -04:00
tb , _ := json . Marshal ( epMap [ "exposed_ports" ] )
var tPorts [ ] types . TransportPort
2022-07-13 16:30:47 -04:00
json . Unmarshal ( tb , & tPorts ) //nolint:errcheck
2015-05-31 14:49:11 -04:00
ep . exposedPorts = tPorts
2015-07-02 01:00:48 -04:00
cb , _ := json . Marshal ( epMap [ "sandbox" ] )
2022-07-13 16:30:47 -04:00
json . Unmarshal ( cb , & ep . sandboxID ) //nolint:errcheck
2015-06-01 12:43:24 -04:00
2015-10-03 19:11:50 -04:00
if v , ok := epMap [ "generic" ] ; ok {
ep . generic = v . ( map [ string ] interface { } )
2015-10-22 11:41:52 -04:00
if opt , ok := ep . generic [ netlabel . PortMap ] ; ok {
pblist := [ ] types . PortBinding { }
for i := 0 ; i < len ( opt . ( [ ] interface { } ) ) ; i ++ {
pb := types . PortBinding { }
tmp := opt . ( [ ] interface { } ) [ i ] . ( map [ string ] interface { } )
bytes , err := json . Marshal ( tmp )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Error ( err )
2015-10-22 11:41:52 -04:00
break
}
err = json . Unmarshal ( bytes , & pb )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Error ( err )
2015-10-22 11:41:52 -04:00
break
}
pblist = append ( pblist , pb )
}
ep . generic [ netlabel . PortMap ] = pblist
}
if opt , ok := ep . generic [ netlabel . ExposedPorts ] ; ok {
tplist := [ ] types . TransportPort { }
for i := 0 ; i < len ( opt . ( [ ] interface { } ) ) ; i ++ {
tp := types . TransportPort { }
tmp := opt . ( [ ] interface { } ) [ i ] . ( map [ string ] interface { } )
bytes , err := json . Marshal ( tmp )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Error ( err )
2015-10-22 11:41:52 -04:00
break
}
err = json . Unmarshal ( bytes , & tp )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Error ( err )
2015-10-22 11:41:52 -04:00
break
}
tplist = append ( tplist , tp )
}
ep . generic [ netlabel . ExposedPorts ] = tplist
}
2015-05-31 14:49:11 -04:00
}
2015-10-20 15:12:16 -04:00
if v , ok := epMap [ "anonymous" ] ; ok {
ep . anonymous = v . ( bool )
}
2015-12-24 04:51:32 -05:00
if v , ok := epMap [ "disableResolution" ] ; ok {
ep . disableResolution = v . ( bool )
}
2016-04-13 20:53:41 -04:00
if sn , ok := epMap [ "svcName" ] ; ok {
ep . svcName = sn . ( string )
}
if si , ok := epMap [ "svcID" ] ; ok {
ep . svcID = si . ( string )
}
2016-05-25 01:46:18 -04:00
if vip , ok := epMap [ "virtualIP" ] ; ok {
ep . virtualIP = net . ParseIP ( vip . ( string ) )
}
2017-08-29 02:35:31 -04:00
if v , ok := epMap [ "loadBalancer" ] ; ok {
ep . loadBalancer = v . ( bool )
}
2016-06-14 19:40:54 -04:00
sal , _ := json . Marshal ( epMap [ "svcAliases" ] )
var svcAliases [ ] string
2022-07-13 16:30:47 -04:00
json . Unmarshal ( sal , & svcAliases ) //nolint:errcheck
2016-06-14 19:40:54 -04:00
ep . svcAliases = svcAliases
2016-05-31 02:55:51 -04:00
pc , _ := json . Marshal ( epMap [ "ingressPorts" ] )
var ingressPorts [ ] * PortConfig
2022-07-13 16:30:47 -04:00
json . Unmarshal ( pc , & ingressPorts ) //nolint:errcheck
2016-05-31 02:55:51 -04:00
ep . ingressPorts = ingressPorts
2016-01-07 22:19:31 -05:00
ma , _ := json . Marshal ( epMap [ "myAliases" ] )
var myAliases [ ] string
2022-07-13 16:30:47 -04:00
json . Unmarshal ( ma , & myAliases ) //nolint:errcheck
2016-01-07 22:19:31 -05:00
ep . myAliases = myAliases
2015-05-31 14:49:11 -04:00
return nil
}
2015-10-05 07:21:15 -04:00
func ( ep * endpoint ) New ( ) datastore . KVObject {
return & endpoint { network : ep . getNetwork ( ) }
}
func ( ep * endpoint ) CopyTo ( o datastore . KVObject ) error {
ep . Lock ( )
defer ep . Unlock ( )
dstEp := o . ( * endpoint )
dstEp . name = ep . name
dstEp . id = ep . id
dstEp . sandboxID = ep . sandboxID
dstEp . dbIndex = ep . dbIndex
dstEp . dbExists = ep . dbExists
2015-10-20 15:12:16 -04:00
dstEp . anonymous = ep . anonymous
2015-12-24 04:51:32 -05:00
dstEp . disableResolution = ep . disableResolution
2016-04-13 20:53:41 -04:00
dstEp . svcName = ep . svcName
dstEp . svcID = ep . svcID
2016-05-25 01:46:18 -04:00
dstEp . virtualIP = ep . virtualIP
2017-08-29 02:35:31 -04:00
dstEp . loadBalancer = ep . loadBalancer
2015-10-05 07:21:15 -04:00
2016-06-14 19:40:54 -04:00
dstEp . svcAliases = make ( [ ] string , len ( ep . svcAliases ) )
copy ( dstEp . svcAliases , ep . svcAliases )
2016-05-31 02:55:51 -04:00
dstEp . ingressPorts = make ( [ ] * PortConfig , len ( ep . ingressPorts ) )
copy ( dstEp . ingressPorts , ep . ingressPorts )
2015-10-05 07:21:15 -04:00
if ep . iface != nil {
dstEp . iface = & endpointInterface { }
2021-05-27 20:15:56 -04:00
if err := ep . iface . CopyTo ( dstEp . iface ) ; err != nil {
return err
}
2015-10-05 07:21:15 -04:00
}
2016-06-10 20:32:19 -04:00
if ep . joinInfo != nil {
dstEp . joinInfo = & endpointJoinInfo { }
2021-05-27 20:15:56 -04:00
if err := ep . joinInfo . CopyTo ( dstEp . joinInfo ) ; err != nil {
return err
}
2016-06-10 20:32:19 -04:00
}
2015-10-05 07:21:15 -04:00
dstEp . exposedPorts = make ( [ ] types . TransportPort , len ( ep . exposedPorts ) )
copy ( dstEp . exposedPorts , ep . exposedPorts )
2016-01-07 22:19:31 -05:00
dstEp . myAliases = make ( [ ] string , len ( ep . myAliases ) )
copy ( dstEp . myAliases , ep . myAliases )
2015-10-05 07:21:15 -04:00
dstEp . generic = options . Generic { }
for k , v := range ep . generic {
dstEp . generic [ k ] = v
}
return nil
}
2015-04-29 21:25:01 -04:00
func ( ep * endpoint ) ID ( ) string {
2015-05-09 00:50:03 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-07-02 01:00:48 -04:00
return ep . id
2015-04-29 21:25:01 -04:00
}
func ( ep * endpoint ) Name ( ) string {
2015-05-09 00:50:03 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-04-29 21:25:01 -04:00
return ep . name
}
2016-01-07 22:19:31 -05:00
func ( ep * endpoint ) MyAliases ( ) [ ] string {
ep . Lock ( )
defer ep . Unlock ( )
return ep . myAliases
}
2015-04-29 21:25:01 -04:00
func ( ep * endpoint ) Network ( ) string {
2015-10-05 07:21:15 -04:00
if ep . network == nil {
return ""
}
return ep . network . name
2015-04-29 21:25:01 -04:00
}
2015-10-20 15:12:16 -04:00
func ( ep * endpoint ) isAnonymous ( ) bool {
ep . Lock ( )
defer ep . Unlock ( )
return ep . anonymous
}
2018-01-08 17:32:49 -05:00
// isServiceEnabled check if service is enabled on the endpoint
func ( ep * endpoint ) isServiceEnabled ( ) bool {
2016-08-21 01:55:00 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2018-01-08 17:32:49 -05:00
return ep . serviceEnabled
}
// enableService sets service enabled on the endpoint
func ( ep * endpoint ) enableService ( ) {
ep . Lock ( )
defer ep . Unlock ( )
ep . serviceEnabled = true
}
// disableService disables service on the endpoint
func ( ep * endpoint ) disableService ( ) {
ep . Lock ( )
defer ep . Unlock ( )
ep . serviceEnabled = false
2016-08-21 01:55:00 -04:00
}
2015-12-24 04:51:32 -05:00
func ( ep * endpoint ) needResolver ( ) bool {
ep . Lock ( )
defer ep . Unlock ( )
return ! ep . disableResolution
}
2015-06-05 16:31:12 -04:00
// endpoint Key structure : endpoint/network-id/endpoint-id
2015-05-31 14:49:11 -04:00
func ( ep * endpoint ) Key ( ) [ ] string {
2015-10-05 07:21:15 -04:00
if ep . network == nil {
return nil
}
return [ ] string { datastore . EndpointKeyPrefix , ep . network . id , ep . id }
2015-05-31 14:49:11 -04:00
}
2015-06-01 12:43:24 -04:00
func ( ep * endpoint ) KeyPrefix ( ) [ ] string {
2015-10-05 07:21:15 -04:00
if ep . network == nil {
return nil
}
return [ ] string { datastore . EndpointKeyPrefix , ep . network . id }
2015-06-05 16:31:12 -04:00
}
2015-05-31 14:49:11 -04:00
func ( ep * endpoint ) Value ( ) [ ] byte {
b , err := json . Marshal ( ep )
if err != nil {
return nil
}
return b
}
2015-06-18 18:13:38 -04:00
func ( ep * endpoint ) SetValue ( value [ ] byte ) error {
return json . Unmarshal ( value , ep )
}
2015-05-31 14:49:11 -04:00
func ( ep * endpoint ) Index ( ) uint64 {
2015-06-05 16:31:12 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-05-31 14:49:11 -04:00
return ep . dbIndex
}
func ( ep * endpoint ) SetIndex ( index uint64 ) {
2015-06-05 16:31:12 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-05-31 14:49:11 -04:00
ep . dbIndex = index
2015-06-18 18:13:38 -04:00
ep . dbExists = true
}
func ( ep * endpoint ) Exists ( ) bool {
ep . Lock ( )
defer ep . Unlock ( )
return ep . dbExists
2015-05-31 14:49:11 -04:00
}
2015-09-22 10:09:39 -04:00
func ( ep * endpoint ) Skip ( ) bool {
2015-10-07 23:01:38 -04:00
return ep . getNetwork ( ) . Skip ( )
2015-09-22 10:09:39 -04:00
}
2015-04-30 20:57:06 -04:00
func ( ep * endpoint ) processOptions ( options ... EndpointOption ) {
2015-05-09 00:50:03 -04:00
ep . Lock ( )
defer ep . Unlock ( )
2015-04-30 20:57:06 -04:00
for _ , opt := range options {
if opt != nil {
opt ( ep )
}
}
}
2015-10-05 07:21:15 -04:00
func ( ep * endpoint ) getNetwork ( ) * network {
ep . Lock ( )
defer ep . Unlock ( )
return ep . network
}
func ( ep * endpoint ) getNetworkFromStore ( ) ( * network , error ) {
if ep . network == nil {
return nil , fmt . Errorf ( "invalid network object in endpoint %s" , ep . Name ( ) )
}
2016-01-29 13:29:18 -05:00
return ep . network . getController ( ) . getNetworkFromStore ( ep . network . id )
2015-10-05 07:21:15 -04:00
}
2015-05-09 00:50:03 -04:00
2015-10-05 07:21:15 -04:00
func ( ep * endpoint ) Join ( sbox Sandbox , options ... EndpointOption ) error {
2015-09-18 20:33:55 -04:00
if sbox == nil {
return types . BadRequestErrorf ( "endpoint cannot be joined by nil container" )
2015-05-09 00:50:03 -04:00
}
2015-09-18 20:33:55 -04:00
sb , ok := sbox . ( * sandbox )
if ! ok {
return types . BadRequestErrorf ( "not a valid Sandbox interface" )
}
2015-05-09 00:50:03 -04:00
2015-09-18 20:33:55 -04:00
sb . joinLeaveStart ( )
defer sb . joinLeaveEnd ( )
2015-05-09 00:50:03 -04:00
2015-12-07 17:45:51 -05:00
return ep . sbJoin ( sb , options ... )
2015-05-09 00:50:03 -04:00
}
2017-04-17 13:44:14 -04:00
func ( ep * endpoint ) sbJoin ( sb * sandbox , options ... EndpointOption ) ( err error ) {
2015-12-07 17:45:51 -05:00
n , err := ep . getNetworkFromStore ( )
2015-10-05 07:21:15 -04:00
if err != nil {
return fmt . Errorf ( "failed to get network from store during join: %v" , err )
}
2015-12-07 17:45:51 -05:00
ep , err = n . getEndpointFromStore ( ep . ID ( ) )
2015-10-05 07:21:15 -04:00
if err != nil {
return fmt . Errorf ( "failed to get endpoint from store during join: %v" , err )
}
2015-05-09 00:50:03 -04:00
ep . Lock ( )
2015-07-02 01:00:48 -04:00
if ep . sandboxID != "" {
2015-05-09 00:50:03 -04:00
ep . Unlock ( )
2015-10-09 04:45:24 -04:00
return types . ForbiddenErrorf ( "another container is attached to the same network endpoint" )
2015-04-30 01:58:12 -04:00
}
2015-12-07 17:45:51 -05:00
ep . network = n
ep . sandboxID = sb . ID ( )
2015-05-14 02:23:45 -04:00
ep . joinInfo = & endpointJoinInfo { }
2015-05-09 00:50:03 -04:00
epid := ep . id
ep . Unlock ( )
2015-04-30 01:58:12 -04:00
defer func ( ) {
if err != nil {
2015-06-01 12:43:24 -04:00
ep . Lock ( )
2015-07-02 01:00:48 -04:00
ep . sandboxID = ""
2015-06-01 12:43:24 -04:00
ep . Unlock ( )
2015-04-30 01:58:12 -04:00
}
} ( )
2015-12-07 17:45:51 -05:00
nid := n . ID ( )
2015-05-03 16:29:43 -04:00
2015-05-09 00:50:03 -04:00
ep . processOptions ( options ... )
2015-05-04 01:18:49 -04:00
2015-12-07 17:45:51 -05:00
d , err := n . driver ( true )
2015-10-05 07:21:15 -04:00
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get driver during join: %v" , err )
2015-10-05 07:21:15 -04:00
}
2015-12-07 17:45:51 -05:00
err = d . Join ( nid , epid , sb . Key ( ) , ep , sb . Labels ( ) )
2015-04-30 01:58:12 -04:00
if err != nil {
2015-05-24 05:41:03 -04:00
return err
2015-04-30 01:58:12 -04:00
}
2015-06-19 21:41:31 -04:00
defer func ( ) {
if err != nil {
2017-04-17 13:44:14 -04:00
if e := d . Leave ( nid , epid ) ; e != nil {
logrus . Warnf ( "driver leave failed while rolling back join: %v" , e )
2015-06-19 21:41:31 -04:00
}
}
} ( )
2015-05-09 00:50:03 -04:00
2015-11-25 18:25:56 -05:00
// Watch for service records
2016-05-24 19:17:19 -04:00
if ! n . getController ( ) . isAgent ( ) {
2016-04-13 20:53:41 -04:00
n . getController ( ) . watchSvcRecord ( ep )
}
2015-11-25 18:25:56 -05:00
2020-10-31 16:18:03 -04:00
// Do not update hosts file with internal networks endpoint IP
if ! n . ingress && n . Name ( ) != libnGWNetwork {
2019-08-30 17:22:46 -04:00
var addresses [ ] string
2019-08-30 17:24:43 -04:00
if ip := ep . getFirstInterfaceIPv4Address ( ) ; ip != nil {
addresses = append ( addresses , ip . String ( ) )
}
if ip := ep . getFirstInterfaceIPv6Address ( ) ; ip != nil {
2019-08-30 17:22:46 -04:00
addresses = append ( addresses , ip . String ( ) )
2016-10-04 17:04:48 -04:00
}
2019-08-30 17:22:46 -04:00
if err = sb . updateHostsFile ( addresses ) ; err != nil {
2016-10-04 17:04:48 -04:00
return err
}
2015-04-29 21:25:01 -04:00
}
2015-12-07 17:45:51 -05:00
if err = sb . updateDNS ( n . enableIPv6 ) ; err != nil {
2015-05-24 05:41:03 -04:00
return err
2015-04-29 21:25:01 -04:00
}
2015-05-03 16:29:43 -04:00
2015-12-07 17:45:51 -05:00
// Current endpoint providing external connectivity for the sandbox
extEp := sb . getGatewayEndpoint ( )
2018-02-27 11:15:31 -05:00
sb . addEndpoint ( ep )
2015-09-09 19:20:54 -04:00
defer func ( ) {
if err != nil {
2015-12-07 17:45:51 -05:00
sb . removeEndpoint ( ep )
2015-09-09 19:20:54 -04:00
}
} ( )
2015-07-02 01:00:48 -04:00
if err = sb . populateNetworkResources ( ep ) ; err != nil {
2015-06-05 16:31:12 -04:00
return err
}
2015-09-06 21:34:50 -04:00
2016-10-13 14:14:39 -04:00
if err = n . getController ( ) . updateToStore ( ep ) ; err != nil {
return err
}
2016-11-11 03:42:34 -05:00
if err = ep . addDriverInfoToCluster ( ) ; err != nil {
return err
}
2017-03-15 19:44:47 -04:00
defer func ( ) {
if err != nil {
if e := ep . deleteDriverInfoFromCluster ( ) ; e != nil {
logrus . Errorf ( "Could not delete endpoint state for endpoint %s from cluster on join failure: %v" , ep . Name ( ) , e )
}
}
} ( )
2018-04-10 12:34:41 -04:00
// Load balancing endpoints should never have a default gateway nor
// should they alter the status of a network's default gateway
if ep . loadBalancer && ! sb . ingress {
return nil
}
2016-04-06 12:11:45 -04:00
if sb . needDefaultGW ( ) && sb . getEndpointInGWNetwork ( ) == nil {
2017-04-17 13:44:14 -04:00
return sb . setupDefaultGW ( )
2015-09-06 21:34:50 -04:00
}
2015-12-07 17:45:51 -05:00
moveExtConn := sb . getGatewayEndpoint ( ) != extEp
if moveExtConn {
if extEp != nil {
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Revoking external connectivity on endpoint %s (%s)" , extEp . Name ( ) , extEp . ID ( ) )
2016-09-28 22:41:14 -04:00
extN , err := extEp . getNetworkFromStore ( )
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get network from store for revoking external connectivity during join: %v" , err )
2016-09-28 22:41:14 -04:00
}
extD , err := extN . driver ( true )
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get driver for revoking external connectivity during join: %v" , err )
2016-09-28 22:41:14 -04:00
}
if err = extD . RevokeExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) ) ; err != nil {
2016-03-08 01:21:17 -05:00
return types . InternalErrorf (
"driver failed revoking external connectivity on endpoint %s (%s): %v" ,
2015-12-07 17:45:51 -05:00
extEp . Name ( ) , extEp . ID ( ) , err )
}
2016-03-08 01:21:17 -05:00
defer func ( ) {
if err != nil {
2016-09-28 22:41:14 -04:00
if e := extD . ProgramExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) , sb . Labels ( ) ) ; e != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failed to roll-back external connectivity on endpoint %s (%s): %v" ,
2016-03-08 01:21:17 -05:00
extEp . Name ( ) , extEp . ID ( ) , e )
}
}
} ( )
2015-12-07 17:45:51 -05:00
}
if ! n . internal {
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Programming external connectivity on endpoint %s (%s)" , ep . Name ( ) , ep . ID ( ) )
2016-03-08 01:21:17 -05:00
if err = d . ProgramExternalConnectivity ( n . ID ( ) , ep . ID ( ) , sb . Labels ( ) ) ; err != nil {
return types . InternalErrorf (
"driver failed programming external connectivity on endpoint %s (%s): %v" ,
ep . Name ( ) , ep . ID ( ) , err )
2015-12-07 17:45:51 -05:00
}
}
2016-04-10 04:26:00 -04:00
2015-12-07 17:45:51 -05:00
}
2016-04-06 12:11:45 -04:00
if ! sb . needDefaultGW ( ) {
2017-04-17 13:44:14 -04:00
if e := sb . clearDefaultGW ( ) ; e != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failure while disconnecting sandbox %s (%s) from gateway network: %v" ,
2017-04-17 13:44:14 -04:00
sb . ID ( ) , sb . ContainerID ( ) , e )
2016-04-06 12:11:45 -04:00
}
}
return nil
2015-04-29 21:25:01 -04:00
}
2015-10-22 23:18:25 -04:00
func ( ep * endpoint ) rename ( name string ) error {
2017-03-16 00:13:36 -04:00
var (
err error
netWatch * netWatch
ok bool
)
2015-10-22 23:18:25 -04:00
n := ep . getNetwork ( )
if n == nil {
return fmt . Errorf ( "network not connected for ep %q" , ep . name )
}
2017-03-16 00:13:36 -04:00
c := n . getController ( )
2015-10-22 23:18:25 -04:00
2017-06-06 19:04:50 -04:00
sb , ok := ep . getSandbox ( )
if ! ok {
logrus . Warnf ( "rename for %s aborted, sandbox %s is not anymore present" , ep . ID ( ) , ep . sandboxID )
return nil
}
2017-03-16 00:13:36 -04:00
if c . isAgent ( ) {
Gracefully remove LB endpoints from services
This patch attempts to allow endpoints to complete servicing connections
while being removed from a service. The change adds a flag to the
endpoint.deleteServiceInfoFromCluster() method to indicate whether this
removal should fully remove connectivity through the load balancer
to the endpoint or should just disable directing further connections to
the endpoint. If the flag is 'false', then the load balancer assigns
a weight of 0 to the endpoint but does not remove it as a linux load
balancing destination. It does remove the endpoint as a docker load
balancing endpoint but tracks it in a special map of "disabled-but-not-
destroyed" load balancing endpoints. This allows traffic to continue
flowing, at least under Linux. If the flag is 'true', then the code
removes the endpoint entirely as a load balancing destination.
The sandbox.DisableService() method invokes deleteServiceInfoFromCluster()
with the flag sent to 'false', while the endpoint.sbLeave() method invokes
it with the flag set to 'true' to complete the removal on endpoint
finalization. Renaming the endpoint invokes deleteServiceInfoFromCluster()
with the flag set to 'true' because renaming attempts to completely
remove and then re-add each endpoint service entry.
The controller.rmServiceBinding() method, which carries out the operation,
similarly gets a new flag for whether to fully remove the endpoint. If
the flag is false, it does the job of moving the endpoint from the
load balancing set to the 'disabled' set. It then removes or
de-weights the entry in the OS load balancing table via
network.rmLBBackend(). It removes the service entirely via said method
ONLY IF there are no more live or disabled load balancing endpoints.
Similarly network.addLBBackend() requires slight tweaking to properly
manage the disabled set.
Finally, this change requires propagating the status of disabled
service endpoints via the networkDB. Accordingly, the patch includes
both code to generate and handle service update messages. It also
augments the service structure with a ServiceDisabled boolean to convey
whether an endpoint should ultimately be removed or just disabled.
This, naturally, required a rebuild of the protocol buffer code as well.
Signed-off-by: Chris Telfer <ctelfer@docker.com>
2018-02-14 17:04:23 -05:00
if err = ep . deleteServiceInfoFromCluster ( sb , true , "rename" ) ; err != nil {
2017-03-16 00:13:36 -04:00
return types . InternalErrorf ( "Could not delete service state for endpoint %s from cluster on rename: %v" , ep . Name ( ) , err )
}
} else {
c . Lock ( )
netWatch , ok = c . nmap [ n . ID ( ) ]
c . Unlock ( )
if ! ok {
return fmt . Errorf ( "watch null for network %q" , n . Name ( ) )
}
n . updateSvcRecord ( ep , c . getLocalEps ( netWatch ) , false )
2015-10-22 23:18:25 -04:00
}
oldName := ep . name
2016-05-05 01:17:24 -04:00
oldAnonymous := ep . anonymous
2015-10-22 23:18:25 -04:00
ep . name = name
2016-05-05 01:17:24 -04:00
ep . anonymous = false
2015-10-22 23:18:25 -04:00
2017-03-16 00:13:36 -04:00
if c . isAgent ( ) {
2017-06-06 19:04:50 -04:00
if err = ep . addServiceInfoToCluster ( sb ) ; err != nil {
2017-03-16 00:13:36 -04:00
return types . InternalErrorf ( "Could not add service state for endpoint %s to cluster on rename: %v" , ep . Name ( ) , err )
2015-10-22 23:18:25 -04:00
}
2017-03-16 00:13:36 -04:00
defer func ( ) {
if err != nil {
2021-05-27 20:15:56 -04:00
if err2 := ep . deleteServiceInfoFromCluster ( sb , true , "rename" ) ; err2 != nil {
logrus . WithField ( "main error" , err ) . WithError ( err2 ) . Debug ( "Error during cleanup due deleting service info from cluster while cleaning up due to other error" )
}
2017-03-16 00:13:36 -04:00
ep . name = oldName
ep . anonymous = oldAnonymous
2021-05-27 20:15:56 -04:00
if err2 := ep . addServiceInfoToCluster ( sb ) ; err2 != nil {
logrus . WithField ( "main error" , err ) . WithError ( err2 ) . Debug ( "Error during cleanup due adding service to from cluster while cleaning up due to other error" )
}
2017-03-16 00:13:36 -04:00
}
} ( )
} else {
n . updateSvcRecord ( ep , c . getLocalEps ( netWatch ) , true )
defer func ( ) {
if err != nil {
n . updateSvcRecord ( ep , c . getLocalEps ( netWatch ) , false )
ep . name = oldName
ep . anonymous = oldAnonymous
n . updateSvcRecord ( ep , c . getLocalEps ( netWatch ) , true )
}
} ( )
}
2015-10-22 23:18:25 -04:00
// Update the store with the updated name
2017-03-16 00:13:36 -04:00
if err = c . updateToStore ( ep ) ; err != nil {
2015-10-22 23:18:25 -04:00
return err
}
// After the name change do a dummy endpoint count update to
// trigger the service record update in the peer nodes
// Ignore the error because updateStore fail for EpCnt is a
// benign error. Besides there is no meaningful recovery that
// we can do. When the cluster recovers subsequent EpCnt update
// will force the peers to get the correct EP name.
2020-10-31 16:18:03 -04:00
_ = n . getEpCnt ( ) . updateStore ( )
2015-10-22 23:18:25 -04:00
return err
}
2015-05-25 02:24:23 -04:00
func ( ep * endpoint ) hasInterface ( iName string ) bool {
ep . Lock ( )
defer ep . Unlock ( )
2015-09-09 19:06:35 -04:00
return ep . iface != nil && ep . iface . srcName == iName
2015-05-25 02:24:23 -04:00
}
2015-07-02 01:00:48 -04:00
func ( ep * endpoint ) Leave ( sbox Sandbox , options ... EndpointOption ) error {
if sbox == nil || sbox . ID ( ) == "" || sbox . Key ( ) == "" {
2017-03-02 03:36:49 -05:00
return types . BadRequestErrorf ( "invalid Sandbox passed to endpoint leave: %v" , sbox )
2015-07-02 01:00:48 -04:00
}
2015-04-30 17:52:46 -04:00
2015-07-02 01:00:48 -04:00
sb , ok := sbox . ( * sandbox )
if ! ok {
return types . BadRequestErrorf ( "not a valid Sandbox interface" )
}
2015-05-09 00:50:03 -04:00
2015-09-18 20:33:55 -04:00
sb . joinLeaveStart ( )
defer sb . joinLeaveEnd ( )
2015-12-07 17:45:51 -05:00
return ep . sbLeave ( sb , false , options ... )
2015-09-18 20:33:55 -04:00
}
2015-12-07 17:45:51 -05:00
func ( ep * endpoint ) sbLeave ( sb * sandbox , force bool , options ... EndpointOption ) error {
2015-10-05 07:21:15 -04:00
n , err := ep . getNetworkFromStore ( )
if err != nil {
return fmt . Errorf ( "failed to get network from store during leave: %v" , err )
}
ep , err = n . getEndpointFromStore ( ep . ID ( ) )
if err != nil {
return fmt . Errorf ( "failed to get endpoint from store during leave: %v" , err )
}
2015-07-02 01:00:48 -04:00
ep . Lock ( )
sid := ep . sandboxID
ep . Unlock ( )
2015-05-09 00:50:03 -04:00
2015-07-02 01:00:48 -04:00
if sid == "" {
return types . ForbiddenErrorf ( "cannot leave endpoint with no attached sandbox" )
2015-05-09 00:50:03 -04:00
}
2015-12-07 17:45:51 -05:00
if sid != sb . ID ( ) {
return types . ForbiddenErrorf ( "unexpected sandbox ID in leave request. Expected %s. Got %s" , ep . sandboxID , sb . ID ( ) )
2015-07-02 01:00:48 -04:00
}
ep . processOptions ( options ... )
2016-01-16 17:24:44 -05:00
d , err := n . driver ( ! force )
2015-10-05 07:21:15 -04:00
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get driver during endpoint leave: %v" , err )
2015-06-05 16:31:12 -04:00
}
2015-10-16 19:11:55 -04:00
ep . Lock ( )
ep . sandboxID = ""
ep . network = n
ep . Unlock ( )
2015-12-07 17:45:51 -05:00
// Current endpoint providing external connectivity to the sandbox
extEp := sb . getGatewayEndpoint ( )
moveExtConn := extEp != nil && ( extEp . ID ( ) == ep . ID ( ) )
2016-01-16 17:24:44 -05:00
if d != nil {
2015-12-07 17:45:51 -05:00
if moveExtConn {
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Revoking external connectivity on endpoint %s (%s)" , ep . Name ( ) , ep . ID ( ) )
2015-12-07 17:45:51 -05:00
if err := d . RevokeExternalConnectivity ( n . id , ep . id ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "driver failed revoking external connectivity on endpoint %s (%s): %v" ,
2015-12-07 17:45:51 -05:00
ep . Name ( ) , ep . ID ( ) , err )
}
}
2016-01-16 17:24:44 -05:00
if err := d . Leave ( n . id , ep . id ) ; err != nil {
if _ , ok := err . ( types . MaskableError ) ; ! ok {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "driver error disconnecting container %s : %v" , ep . name , err )
2016-01-16 17:24:44 -05:00
}
2015-10-16 19:11:55 -04:00
}
2015-07-02 01:00:48 -04:00
}
2015-05-09 00:50:03 -04:00
2018-05-31 14:21:55 -04:00
if err := ep . deleteServiceInfoFromCluster ( sb , true , "sbLeave" ) ; err != nil {
logrus . Warnf ( "Failed to clean up service info on container %s disconnect: %v" , ep . name , err )
Gracefully remove LB endpoints from services
This patch attempts to allow endpoints to complete servicing connections
while being removed from a service. The change adds a flag to the
endpoint.deleteServiceInfoFromCluster() method to indicate whether this
removal should fully remove connectivity through the load balancer
to the endpoint or should just disable directing further connections to
the endpoint. If the flag is 'false', then the load balancer assigns
a weight of 0 to the endpoint but does not remove it as a linux load
balancing destination. It does remove the endpoint as a docker load
balancing endpoint but tracks it in a special map of "disabled-but-not-
destroyed" load balancing endpoints. This allows traffic to continue
flowing, at least under Linux. If the flag is 'true', then the code
removes the endpoint entirely as a load balancing destination.
The sandbox.DisableService() method invokes deleteServiceInfoFromCluster()
with the flag sent to 'false', while the endpoint.sbLeave() method invokes
it with the flag set to 'true' to complete the removal on endpoint
finalization. Renaming the endpoint invokes deleteServiceInfoFromCluster()
with the flag set to 'true' because renaming attempts to completely
remove and then re-add each endpoint service entry.
The controller.rmServiceBinding() method, which carries out the operation,
similarly gets a new flag for whether to fully remove the endpoint. If
the flag is false, it does the job of moving the endpoint from the
load balancing set to the 'disabled' set. It then removes or
de-weights the entry in the OS load balancing table via
network.rmLBBackend(). It removes the service entirely via said method
ONLY IF there are no more live or disabled load balancing endpoints.
Similarly network.addLBBackend() requires slight tweaking to properly
manage the disabled set.
Finally, this change requires propagating the status of disabled
service endpoints via the networkDB. Accordingly, the patch includes
both code to generate and handle service update messages. It also
augments the service structure with a ServiceDisabled boolean to convey
whether an endpoint should ultimately be removed or just disabled.
This, naturally, required a rebuild of the protocol buffer code as well.
Signed-off-by: Chris Telfer <ctelfer@docker.com>
2018-02-14 17:04:23 -05:00
}
2015-09-06 21:34:50 -04:00
if err := sb . clearNetworkResources ( ep ) ; err != nil {
Gracefully remove LB endpoints from services
This patch attempts to allow endpoints to complete servicing connections
while being removed from a service. The change adds a flag to the
endpoint.deleteServiceInfoFromCluster() method to indicate whether this
removal should fully remove connectivity through the load balancer
to the endpoint or should just disable directing further connections to
the endpoint. If the flag is 'false', then the load balancer assigns
a weight of 0 to the endpoint but does not remove it as a linux load
balancing destination. It does remove the endpoint as a docker load
balancing endpoint but tracks it in a special map of "disabled-but-not-
destroyed" load balancing endpoints. This allows traffic to continue
flowing, at least under Linux. If the flag is 'true', then the code
removes the endpoint entirely as a load balancing destination.
The sandbox.DisableService() method invokes deleteServiceInfoFromCluster()
with the flag sent to 'false', while the endpoint.sbLeave() method invokes
it with the flag set to 'true' to complete the removal on endpoint
finalization. Renaming the endpoint invokes deleteServiceInfoFromCluster()
with the flag set to 'true' because renaming attempts to completely
remove and then re-add each endpoint service entry.
The controller.rmServiceBinding() method, which carries out the operation,
similarly gets a new flag for whether to fully remove the endpoint. If
the flag is false, it does the job of moving the endpoint from the
load balancing set to the 'disabled' set. It then removes or
de-weights the entry in the OS load balancing table via
network.rmLBBackend(). It removes the service entirely via said method
ONLY IF there are no more live or disabled load balancing endpoints.
Similarly network.addLBBackend() requires slight tweaking to properly
manage the disabled set.
Finally, this change requires propagating the status of disabled
service endpoints via the networkDB. Accordingly, the patch includes
both code to generate and handle service update messages. It also
augments the service structure with a ServiceDisabled boolean to convey
whether an endpoint should ultimately be removed or just disabled.
This, naturally, required a rebuild of the protocol buffer code as well.
Signed-off-by: Chris Telfer <ctelfer@docker.com>
2018-02-14 17:04:23 -05:00
logrus . Warnf ( "Failed to clean up network resources on container %s disconnect: %v" , ep . name , err )
2015-10-16 19:11:55 -04:00
}
// Update the store about the sandbox detach only after we
// have completed sb.clearNetworkresources above to avoid
// spurious logs when cleaning up the sandbox when the daemon
// ungracefully exits and restarts before completing sandbox
// detach but after store has been updated.
if err := n . getController ( ) . updateToStore ( ep ) ; err != nil {
2015-09-06 21:34:50 -04:00
return err
}
2016-11-11 03:42:34 -05:00
if e := ep . deleteDriverInfoFromCluster ( ) ; e != nil {
Gracefully remove LB endpoints from services
This patch attempts to allow endpoints to complete servicing connections
while being removed from a service. The change adds a flag to the
endpoint.deleteServiceInfoFromCluster() method to indicate whether this
removal should fully remove connectivity through the load balancer
to the endpoint or should just disable directing further connections to
the endpoint. If the flag is 'false', then the load balancer assigns
a weight of 0 to the endpoint but does not remove it as a linux load
balancing destination. It does remove the endpoint as a docker load
balancing endpoint but tracks it in a special map of "disabled-but-not-
destroyed" load balancing endpoints. This allows traffic to continue
flowing, at least under Linux. If the flag is 'true', then the code
removes the endpoint entirely as a load balancing destination.
The sandbox.DisableService() method invokes deleteServiceInfoFromCluster()
with the flag sent to 'false', while the endpoint.sbLeave() method invokes
it with the flag set to 'true' to complete the removal on endpoint
finalization. Renaming the endpoint invokes deleteServiceInfoFromCluster()
with the flag set to 'true' because renaming attempts to completely
remove and then re-add each endpoint service entry.
The controller.rmServiceBinding() method, which carries out the operation,
similarly gets a new flag for whether to fully remove the endpoint. If
the flag is false, it does the job of moving the endpoint from the
load balancing set to the 'disabled' set. It then removes or
de-weights the entry in the OS load balancing table via
network.rmLBBackend(). It removes the service entirely via said method
ONLY IF there are no more live or disabled load balancing endpoints.
Similarly network.addLBBackend() requires slight tweaking to properly
manage the disabled set.
Finally, this change requires propagating the status of disabled
service endpoints via the networkDB. Accordingly, the patch includes
both code to generate and handle service update messages. It also
augments the service structure with a ServiceDisabled boolean to convey
whether an endpoint should ultimately be removed or just disabled.
This, naturally, required a rebuild of the protocol buffer code as well.
Signed-off-by: Chris Telfer <ctelfer@docker.com>
2018-02-14 17:04:23 -05:00
logrus . Errorf ( "Failed to delete endpoint state for endpoint %s from cluster: %v" , ep . Name ( ) , e )
2016-03-30 17:42:58 -04:00
}
2015-10-24 16:31:01 -04:00
sb . deleteHostsEntries ( n . getSvcRecords ( ep ) )
2016-04-06 12:11:45 -04:00
if ! sb . inDelete && sb . needDefaultGW ( ) && sb . getEndpointInGWNetwork ( ) == nil {
2015-12-07 17:45:51 -05:00
return sb . setupDefaultGW ( )
}
// New endpoint providing external connectivity for the sandbox
extEp = sb . getGatewayEndpoint ( )
if moveExtConn && extEp != nil {
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Programming external connectivity on endpoint %s (%s)" , extEp . Name ( ) , extEp . ID ( ) )
2016-09-28 22:41:14 -04:00
extN , err := extEp . getNetworkFromStore ( )
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get network from store for programming external connectivity during leave: %v" , err )
2016-09-28 22:41:14 -04:00
}
extD , err := extN . driver ( true )
if err != nil {
2017-04-17 13:44:14 -04:00
return fmt . Errorf ( "failed to get driver for programming external connectivity during leave: %v" , err )
2016-09-28 22:41:14 -04:00
}
if err := extD . ProgramExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) , sb . Labels ( ) ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "driver failed programming external connectivity on endpoint %s: (%s) %v" ,
2015-12-07 17:45:51 -05:00
extEp . Name ( ) , extEp . ID ( ) , err )
}
}
2016-04-06 12:11:45 -04:00
if ! sb . needDefaultGW ( ) {
if err := sb . clearDefaultGW ( ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failure while disconnecting sandbox %s (%s) from gateway network: %v" ,
2016-04-06 12:11:45 -04:00
sb . ID ( ) , sb . ContainerID ( ) , err )
}
}
return nil
2015-04-29 21:25:01 -04:00
}
2016-01-08 14:24:14 -05:00
func ( ep * endpoint ) Delete ( force bool ) error {
2015-06-05 16:31:12 -04:00
var err error
2015-10-05 07:21:15 -04:00
n , err := ep . getNetworkFromStore ( )
if err != nil {
return fmt . Errorf ( "failed to get network during Delete: %v" , err )
}
ep , err = n . getEndpointFromStore ( ep . ID ( ) )
if err != nil {
return fmt . Errorf ( "failed to get endpoint from store during Delete: %v" , err )
}
2015-05-09 00:50:03 -04:00
ep . Lock ( )
2015-06-05 16:31:12 -04:00
epid := ep . id
name := ep . name
2016-01-08 14:24:14 -05:00
sbid := ep . sandboxID
ep . Unlock ( )
sb , _ := n . getController ( ) . SandboxByID ( sbid )
if sb != nil && ! force {
2015-07-02 01:00:48 -04:00
return & ActiveContainerError { name : name , id : epid }
2015-06-01 12:43:24 -04:00
}
2016-01-08 14:24:14 -05:00
if sb != nil {
2015-12-07 17:45:51 -05:00
if e := ep . sbLeave ( sb . ( * sandbox ) , force ) ; e != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "failed to leave sandbox for endpoint %s : %v" , name , e )
2016-01-08 14:24:14 -05:00
}
}
2015-06-01 12:43:24 -04:00
2015-10-23 13:09:00 -04:00
if err = n . getController ( ) . deleteFromStore ( ep ) ; err != nil {
2015-10-05 07:21:15 -04:00
return err
2015-06-05 16:31:12 -04:00
}
2016-01-08 14:24:14 -05:00
2015-06-05 16:31:12 -04:00
defer func ( ) {
2016-01-08 14:24:14 -05:00
if err != nil && ! force {
2015-10-23 13:09:00 -04:00
ep . dbExists = false
if e := n . getController ( ) . updateToStore ( ep ) ; e != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "failed to recreate endpoint in store %s : %v" , name , e )
2015-06-05 16:31:12 -04:00
}
}
} ( )
2015-10-23 13:09:00 -04:00
// unwatch for service records
2016-07-22 17:51:36 -04:00
n . getController ( ) . unWatchSvcRecord ( ep )
2015-10-23 13:09:00 -04:00
2016-01-16 17:24:44 -05:00
if err = ep . deleteEndpoint ( force ) ; err != nil && ! force {
2015-06-01 12:43:24 -04:00
return err
2015-05-06 16:02:40 -04:00
}
2015-06-05 16:31:12 -04:00
2015-10-03 19:11:50 -04:00
ep . releaseAddress ( )
2016-09-01 22:09:34 -04:00
if err := n . getEpCnt ( ) . DecEndpointCnt ( ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "failed to decrement endpoint count for ep %s: %v" , ep . ID ( ) , err )
2016-09-01 22:09:34 -04:00
}
2015-06-01 12:43:24 -04:00
return nil
}
2015-04-29 21:25:01 -04:00
2016-01-16 17:24:44 -05:00
func ( ep * endpoint ) deleteEndpoint ( force bool ) error {
2015-06-01 12:43:24 -04:00
ep . Lock ( )
2015-04-29 21:25:01 -04:00
n := ep . network
2015-06-01 12:43:24 -04:00
name := ep . name
epid := ep . id
2015-05-09 00:50:03 -04:00
ep . Unlock ( )
2016-01-16 17:24:44 -05:00
driver , err := n . driver ( ! force )
2015-10-05 07:21:15 -04:00
if err != nil {
return fmt . Errorf ( "failed to delete endpoint: %v" , err )
2015-04-29 21:25:01 -04:00
}
2016-01-16 17:24:44 -05:00
if driver == nil {
return nil
}
2015-10-05 07:21:15 -04:00
if err := driver . DeleteEndpoint ( n . id , epid ) ; err != nil {
2015-06-10 16:27:23 -04:00
if _ , ok := err . ( types . ForbiddenError ) ; ok {
return err
2015-04-29 21:25:01 -04:00
}
2015-10-19 18:56:25 -04:00
if _ , ok := err . ( types . MaskableError ) ; ! ok {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "driver error deleting endpoint %s : %v" , name , err )
2015-10-19 18:56:25 -04:00
}
2015-06-10 16:27:23 -04:00
}
2015-06-19 02:40:17 -04:00
2015-06-10 16:27:23 -04:00
return nil
2015-04-29 21:25:01 -04:00
}
2015-04-30 01:58:12 -04:00
2015-07-02 01:00:48 -04:00
func ( ep * endpoint ) getSandbox ( ) ( * sandbox , bool ) {
c := ep . network . getController ( )
2016-01-08 22:25:53 -05:00
ep . Lock ( )
2015-07-02 01:00:48 -04:00
sid := ep . sandboxID
2015-05-09 00:50:03 -04:00
ep . Unlock ( )
2015-07-02 01:00:48 -04:00
c . Lock ( )
ps , ok := c . sandboxes [ sid ]
c . Unlock ( )
2015-04-30 01:58:12 -04:00
2015-07-02 01:00:48 -04:00
return ps , ok
2015-04-30 01:58:12 -04:00
}
2019-08-30 17:24:43 -04:00
func ( ep * endpoint ) getFirstInterfaceIPv4Address ( ) net . IP {
2015-05-09 00:50:03 -04:00
ep . Lock ( )
2015-07-02 01:00:48 -04:00
defer ep . Unlock ( )
2015-05-09 00:50:03 -04:00
2015-10-03 19:11:50 -04:00
if ep . iface . addr != nil {
2015-09-09 19:06:35 -04:00
return ep . iface . addr . IP
2015-05-03 16:29:43 -04:00
}
return nil
}
2019-08-30 17:24:43 -04:00
func ( ep * endpoint ) getFirstInterfaceIPv6Address ( ) net . IP {
ep . Lock ( )
defer ep . Unlock ( )
if ep . iface . addrv6 != nil {
return ep . iface . addrv6 . IP
}
return nil
}
2015-05-04 01:18:49 -04:00
// EndpointOptionGeneric function returns an option setter for a Generic option defined
// in a Dictionary of Key-Value pair
func EndpointOptionGeneric ( generic map [ string ] interface { } ) EndpointOption {
return func ( ep * endpoint ) {
for k , v := range generic {
ep . generic [ k ] = v
}
}
}
2016-05-24 23:04:49 -04:00
var (
linkLocalMask = net . CIDRMask ( 16 , 32 )
linkLocalMaskIPv6 = net . CIDRMask ( 64 , 128 )
)
2015-12-03 22:20:42 -05:00
// CreateOptionIpam function returns an option setter for the ipam configuration for this endpoint
2016-05-24 23:04:49 -04:00
func CreateOptionIpam ( ipV4 , ipV6 net . IP , llIPs [ ] net . IP , ipamOptions map [ string ] string ) EndpointOption {
2015-12-03 22:20:42 -05:00
return func ( ep * endpoint ) {
2015-12-08 21:03:38 -05:00
ep . prefAddress = ipV4
ep . prefAddressV6 = ipV6
2016-05-24 23:04:49 -04:00
if len ( llIPs ) != 0 {
for _ , ip := range llIPs {
nw := & net . IPNet { IP : ip , Mask : linkLocalMask }
if ip . To4 ( ) == nil {
nw . Mask = linkLocalMaskIPv6
}
ep . iface . llAddrs = append ( ep . iface . llAddrs , nw )
}
}
2015-12-03 22:20:42 -05:00
ep . ipamOptions = ipamOptions
}
}
2015-05-05 16:46:12 -04:00
// CreateOptionExposedPorts function returns an option setter for the container exposed
// ports option to be passed to network.CreateEndpoint() method.
2015-05-20 16:28:46 -04:00
func CreateOptionExposedPorts ( exposedPorts [ ] types . TransportPort ) EndpointOption {
2015-05-05 16:46:12 -04:00
return func ( ep * endpoint ) {
// Defensive copy
2015-05-20 16:28:46 -04:00
eps := make ( [ ] types . TransportPort , len ( exposedPorts ) )
2015-05-05 16:46:12 -04:00
copy ( eps , exposedPorts )
// Store endpoint label and in generic because driver needs it
ep . exposedPorts = eps
2015-05-06 00:19:57 -04:00
ep . generic [ netlabel . ExposedPorts ] = eps
2015-05-05 16:46:12 -04:00
}
}
// CreateOptionPortMapping function returns an option setter for the mapping
2015-05-01 20:11:13 -04:00
// ports option to be passed to network.CreateEndpoint() method.
2015-05-20 16:28:46 -04:00
func CreateOptionPortMapping ( portBindings [ ] types . PortBinding ) EndpointOption {
2015-05-01 20:01:21 -04:00
return func ( ep * endpoint ) {
2015-05-05 02:45:07 -04:00
// Store a copy of the bindings as generic data to pass to the driver
2015-05-20 16:28:46 -04:00
pbs := make ( [ ] types . PortBinding , len ( portBindings ) )
2015-05-05 16:46:12 -04:00
copy ( pbs , portBindings )
2015-05-06 00:19:57 -04:00
ep . generic [ netlabel . PortMap ] = pbs
2015-05-01 20:01:21 -04:00
}
}
2016-09-19 18:48:06 -04:00
// CreateOptionDNS function returns an option setter for dns entry option to
// be passed to container Create method.
func CreateOptionDNS ( dns [ ] string ) EndpointOption {
return func ( ep * endpoint ) {
ep . generic [ netlabel . DNSServers ] = dns
}
}
2015-10-20 15:12:16 -04:00
// CreateOptionAnonymous function returns an option setter for setting
// this endpoint as anonymous
func CreateOptionAnonymous ( ) EndpointOption {
return func ( ep * endpoint ) {
ep . anonymous = true
}
}
2015-12-24 04:51:32 -05:00
// CreateOptionDisableResolution function returns an option setter to indicate
// this endpoint doesn't want embedded DNS server functionality
func CreateOptionDisableResolution ( ) EndpointOption {
return func ( ep * endpoint ) {
ep . disableResolution = true
}
}
2017-09-07 13:36:11 -04:00
// CreateOptionAlias function returns an option setter for setting endpoint alias
2016-01-04 17:02:03 -05:00
func CreateOptionAlias ( name string , alias string ) EndpointOption {
return func ( ep * endpoint ) {
if ep . aliases == nil {
ep . aliases = make ( map [ string ] string )
}
ep . aliases [ alias ] = name
}
}
2016-04-13 20:53:41 -04:00
// CreateOptionService function returns an option setter for setting service binding configuration
2016-06-14 19:40:54 -04:00
func CreateOptionService ( name , id string , vip net . IP , ingressPorts [ ] * PortConfig , aliases [ ] string ) EndpointOption {
2016-04-13 20:53:41 -04:00
return func ( ep * endpoint ) {
ep . svcName = name
ep . svcID = id
2016-05-25 01:46:18 -04:00
ep . virtualIP = vip
2016-05-31 02:55:51 -04:00
ep . ingressPorts = ingressPorts
2016-06-14 19:40:54 -04:00
ep . svcAliases = aliases
2016-04-13 20:53:41 -04:00
}
}
2017-09-07 13:36:11 -04:00
// CreateOptionMyAlias function returns an option setter for setting endpoint's self alias
2016-01-07 22:19:31 -05:00
func CreateOptionMyAlias ( alias string ) EndpointOption {
return func ( ep * endpoint ) {
ep . myAliases = append ( ep . myAliases , alias )
}
}
2017-09-07 13:36:11 -04:00
// CreateOptionLoadBalancer function returns an option setter for denoting the endpoint is a load balancer for a network
2017-08-29 02:35:31 -04:00
func CreateOptionLoadBalancer ( ) EndpointOption {
return func ( ep * endpoint ) {
ep . loadBalancer = true
}
}
2015-07-02 01:00:48 -04:00
// JoinOptionPriority function returns an option setter for priority option to
// be passed to the endpoint.Join() method.
2020-05-14 23:02:30 -04:00
func JoinOptionPriority ( prio int ) EndpointOption {
2015-04-30 17:52:46 -04:00
return func ( ep * endpoint ) {
2015-07-02 01:00:48 -04:00
// ep lock already acquired
c := ep . network . getController ( )
c . Lock ( )
sb , ok := c . sandboxes [ ep . sandboxID ]
c . Unlock ( )
if ! ok {
2016-11-01 00:26:14 -04:00
logrus . Errorf ( "Could not set endpoint priority value during Join to endpoint %s: No sandbox id present in endpoint" , ep . id )
2015-07-02 01:00:48 -04:00
return
}
sb . epPriority [ ep . id ] = prio
2015-04-30 17:52:46 -04:00
}
}
2015-09-16 07:39:46 -04:00
2015-10-05 07:21:15 -04:00
func ( ep * endpoint ) DataScope ( ) string {
return ep . getNetwork ( ) . DataScope ( )
2015-09-16 07:42:35 -04:00
}
2015-10-03 19:11:50 -04:00
2015-12-09 21:09:58 -05:00
func ( ep * endpoint ) assignAddress ( ipam ipamapi . Ipam , assignIPv4 , assignIPv6 bool ) error {
var err error
2015-10-05 17:53:25 -04:00
2015-10-03 19:11:50 -04:00
n := ep . getNetwork ( )
2016-06-15 00:34:44 -04:00
if n . hasSpecialDriver ( ) {
2015-10-03 19:11:50 -04:00
return nil
}
2015-10-05 17:53:25 -04:00
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Assigning addresses for endpoint %s's interface on network %s" , ep . Name ( ) , n . Name ( ) )
2015-10-05 17:53:25 -04:00
2015-11-06 02:50:10 -05:00
if assignIPv4 {
if err = ep . assignAddressVersion ( 4 , ipam ) ; err != nil {
return err
}
2015-10-04 00:25:57 -04:00
}
2015-11-06 02:50:10 -05:00
if assignIPv6 {
err = ep . assignAddressVersion ( 6 , ipam )
}
return err
2015-10-04 00:25:57 -04:00
}
func ( ep * endpoint ) assignAddressVersion ( ipVer int , ipam ipamapi . Ipam ) error {
var (
poolID * string
address * * net . IPNet
2015-12-08 21:03:38 -05:00
prefAdd net . IP
progAdd net . IP
2015-10-04 00:25:57 -04:00
)
n := ep . getNetwork ( )
switch ipVer {
case 4 :
poolID = & ep . iface . v4PoolID
address = & ep . iface . addr
2015-12-08 21:03:38 -05:00
prefAdd = ep . prefAddress
2015-10-04 00:25:57 -04:00
case 6 :
poolID = & ep . iface . v6PoolID
address = & ep . iface . addrv6
2015-12-08 21:03:38 -05:00
prefAdd = ep . prefAddressV6
2015-10-04 00:25:57 -04:00
default :
return types . InternalErrorf ( "incorrect ip version number passed: %d" , ipVer )
}
ipInfo := n . getIPInfo ( ipVer )
// ipv6 address is not mandatory
if len ( ipInfo ) == 0 && ipVer == 6 {
return nil
}
2015-12-08 21:03:38 -05:00
// The address to program may be chosen by the user or by the network driver in one specific
// case to support backward compatibility with `docker daemon --fixed-cidrv6` use case
if prefAdd != nil {
progAdd = prefAdd
} else if * address != nil {
progAdd = ( * address ) . IP
}
2015-10-04 00:25:57 -04:00
for _ , d := range ipInfo {
2015-12-08 21:03:38 -05:00
if progAdd != nil && ! d . Pool . Contains ( progAdd ) {
continue
2015-11-06 02:50:10 -05:00
}
2015-12-08 21:03:38 -05:00
addr , _ , err := ipam . RequestAddress ( d . PoolID , progAdd , ep . ipamOptions )
2015-10-03 19:11:50 -04:00
if err == nil {
ep . Lock ( )
2015-10-04 00:25:57 -04:00
* address = addr
* poolID = d . PoolID
2015-10-03 19:11:50 -04:00
ep . Unlock ( )
return nil
}
2015-12-08 21:03:38 -05:00
if err != ipamapi . ErrNoAvailableIPs || progAdd != nil {
2015-10-03 19:11:50 -04:00
return err
}
}
2015-12-08 21:03:38 -05:00
if progAdd != nil {
2016-01-26 10:09:29 -05:00
return types . BadRequestErrorf ( "Invalid address %s: It does not belong to any of this network's subnets" , prefAdd )
2015-12-08 21:03:38 -05:00
}
2015-10-04 00:25:57 -04:00
return fmt . Errorf ( "no available IPv%d addresses on this network's address pools: %s (%s)" , ipVer , n . Name ( ) , n . ID ( ) )
2015-10-03 19:11:50 -04:00
}
func ( ep * endpoint ) releaseAddress ( ) {
n := ep . getNetwork ( )
2016-06-15 00:34:44 -04:00
if n . hasSpecialDriver ( ) {
2015-10-03 19:11:50 -04:00
return
}
2015-10-05 17:53:25 -04:00
2016-11-01 00:26:14 -04:00
logrus . Debugf ( "Releasing addresses for endpoint %s's interface on network %s" , ep . Name ( ) , n . Name ( ) )
2015-10-05 17:53:25 -04:00
2016-02-29 21:17:04 -05:00
ipam , _ , err := n . getController ( ) . getIPAMDriver ( n . ipamType )
2015-10-03 19:11:50 -04:00
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failed to retrieve ipam driver to release interface address on delete of endpoint %s (%s): %v" , ep . Name ( ) , ep . ID ( ) , err )
2015-10-03 19:11:50 -04:00
return
}
2016-03-04 14:35:11 -05:00
if ep . iface . addr != nil {
if err := ipam . ReleaseAddress ( ep . iface . v4PoolID , ep . iface . addr . IP ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failed to release ip address %s on delete of endpoint %s (%s): %v" , ep . iface . addr . IP , ep . Name ( ) , ep . ID ( ) , err )
2016-03-04 14:35:11 -05:00
}
2015-10-03 19:11:50 -04:00
}
2016-03-04 14:35:11 -05:00
2015-10-04 00:25:57 -04:00
if ep . iface . addrv6 != nil && ep . iface . addrv6 . IP . IsGlobalUnicast ( ) {
if err := ipam . ReleaseAddress ( ep . iface . v6PoolID , ep . iface . addrv6 . IP ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Failed to release ip address %s on delete of endpoint %s (%s): %v" , ep . iface . addrv6 . IP , ep . Name ( ) , ep . ID ( ) , err )
2015-10-04 00:25:57 -04:00
}
}
2015-10-03 19:11:50 -04:00
}
2015-10-19 16:20:23 -04:00
func ( c * controller ) cleanupLocalEndpoints ( ) {
2016-06-10 20:32:19 -04:00
// Get used endpoints
eps := make ( map [ string ] interface { } )
for _ , sb := range c . sandboxes {
for _ , ep := range sb . endpoints {
eps [ ep . id ] = true
}
}
2015-10-19 16:20:23 -04:00
nl , err := c . getNetworksForScope ( datastore . LocalScope )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Could not get list of networks during endpoint cleanup: %v" , err )
2015-10-19 16:20:23 -04:00
return
}
for _ , n := range nl {
2017-04-07 13:51:39 -04:00
if n . ConfigOnly ( ) {
continue
}
2015-10-19 16:20:23 -04:00
epl , err := n . getEndpointsFromStore ( )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Could not get list of endpoints in network %s during endpoint cleanup: %v" , n . name , err )
2015-10-19 16:20:23 -04:00
continue
}
for _ , ep := range epl {
2016-06-10 20:32:19 -04:00
if _ , ok := eps [ ep . id ] ; ok {
continue
}
2016-11-01 00:26:14 -04:00
logrus . Infof ( "Removing stale endpoint %s (%s)" , ep . name , ep . id )
2016-01-17 15:43:41 -05:00
if err := ep . Delete ( true ) ; err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Could not delete local endpoint %s during endpoint cleanup: %v" , ep . name , err )
2015-10-19 16:20:23 -04:00
}
}
2016-03-16 00:07:42 -04:00
epl , err = n . getEndpointsFromStore ( )
if err != nil {
2016-11-01 00:26:14 -04:00
logrus . Warnf ( "Could not get list of endpoints in network %s for count update: %v" , n . name , err )
2016-03-16 00:07:42 -04:00
continue
}
epCnt := n . getEpCnt ( ) . EndpointCnt ( )
if epCnt != uint64 ( len ( epl ) ) {
2016-11-01 00:26:14 -04:00
logrus . Infof ( "Fixing inconsistent endpoint_cnt for network %s. Expected=%d, Actual=%d" , n . name , len ( epl ) , epCnt )
2021-05-27 20:15:56 -04:00
if err := n . getEpCnt ( ) . setCnt ( uint64 ( len ( epl ) ) ) ; err != nil {
logrus . WithField ( "network" , n . name ) . WithError ( err ) . Warn ( "Error while fixing inconsistent endpoint_cnt for network" )
}
2016-03-16 00:07:42 -04:00
}
2015-10-19 16:20:23 -04:00
}
}