2015-04-30 01:25:01 +00:00
package libnetwork
import (
2015-05-31 18:49:11 +00:00
"encoding/json"
2015-06-05 20:31:12 +00:00
"fmt"
2015-07-02 05:00:48 +00:00
"net"
2015-05-09 04:50:03 +00:00
"sync"
2015-04-30 05:58:12 +00:00
2021-04-06 00:24:47 +00: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 21:18:31 +00:00
"github.com/sirupsen/logrus"
2015-04-30 01:25:01 +00:00
)
2016-01-11 10:15:20 +00:00
// EndpointOption is an option setter function type used to pass various options to Network
2015-05-02 00:11:13 +00:00
// and Endpoint interfaces methods. The various setter functions of type EndpointOption are
// provided by libnetwork, they look like <Create|Join|Leave>Option[...](...)
2023-01-12 01:42:24 +00:00
type EndpointOption func ( ep * Endpoint )
2015-05-02 00:11:13 +00:00
2023-01-12 01:42:24 +00:00
// Endpoint represents a logical connection between a network and a sandbox.
type Endpoint struct {
2015-12-24 09:51:32 +00: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 22:02:03 +00:00
aliases map [ string ] string
2016-01-08 03:19:31 +00:00
myAliases [ ] string
2016-04-14 00:53:41 +00:00
svcID string
svcName string
2016-05-25 05:46:18 +00:00
virtualIP net . IP
2016-06-14 23:40:54 +00:00
svcAliases [ ] string
2016-05-31 06:55:51 +00:00
ingressPorts [ ] * PortConfig
2015-12-24 09:51:32 +00:00
dbIndex uint64
dbExists bool
2016-08-21 05:55:00 +00:00
serviceEnabled bool
2017-08-29 06:35:31 +00:00
loadBalancer bool
2023-01-11 22:51:59 +00:00
mu sync . Mutex
2015-04-30 01:25:01 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) MarshalJSON ( ) ( [ ] byte , error ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-06-05 20:31:12 +00:00
2015-05-31 18:49:11 +00:00
epMap := make ( map [ string ] interface { } )
epMap [ "name" ] = ep . name
2015-07-02 05:00:48 +00:00
epMap [ "id" ] = ep . id
2015-09-09 23:06:35 +00:00
epMap [ "ep_iface" ] = ep . iface
2016-06-11 00:32:19 +00:00
epMap [ "joinInfo" ] = ep . joinInfo
2015-05-31 18:49:11 +00:00
epMap [ "exposed_ports" ] = ep . exposedPorts
2015-10-03 23:11:50 +00:00
if ep . generic != nil {
epMap [ "generic" ] = ep . generic
}
2015-07-02 05:00:48 +00:00
epMap [ "sandbox" ] = ep . sandboxID
2015-10-20 19:12:16 +00:00
epMap [ "anonymous" ] = ep . anonymous
2015-12-24 09:51:32 +00:00
epMap [ "disableResolution" ] = ep . disableResolution
2016-01-08 03:19:31 +00:00
epMap [ "myAliases" ] = ep . myAliases
2016-04-14 00:53:41 +00:00
epMap [ "svcName" ] = ep . svcName
epMap [ "svcID" ] = ep . svcID
2016-05-25 05:46:18 +00:00
epMap [ "virtualIP" ] = ep . virtualIP . String ( )
2016-05-31 06:55:51 +00:00
epMap [ "ingressPorts" ] = ep . ingressPorts
2016-06-14 23:40:54 +00:00
epMap [ "svcAliases" ] = ep . svcAliases
2017-08-29 06:35:31 +00:00
epMap [ "loadBalancer" ] = ep . loadBalancer
2016-04-14 00:53:41 +00:00
2015-05-31 18:49:11 +00:00
return json . Marshal ( epMap )
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-06-05 20:31:12 +00:00
2015-05-31 18:49:11 +00:00
var epMap map [ string ] interface { }
if err := json . Unmarshal ( b , & epMap ) ; err != nil {
return err
}
ep . name = epMap [ "name" ] . ( string )
2015-07-02 05:00:48 +00:00
ep . id = epMap [ "id" ] . ( string )
2015-05-31 18:49:11 +00:00
2021-05-28 00:15:56 +00: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 18:49:11 +00:00
ib , _ := json . Marshal ( epMap [ "ep_iface" ] )
2022-07-13 20:30:47 +00:00
json . Unmarshal ( ib , & ep . iface ) //nolint:errcheck
2015-05-31 18:49:11 +00:00
2016-06-11 00:32:19 +00:00
jb , _ := json . Marshal ( epMap [ "joinInfo" ] )
2022-07-13 20:30:47 +00:00
json . Unmarshal ( jb , & ep . joinInfo ) //nolint:errcheck
2016-06-11 00:32:19 +00:00
2015-05-31 18:49:11 +00:00
tb , _ := json . Marshal ( epMap [ "exposed_ports" ] )
var tPorts [ ] types . TransportPort
2022-07-13 20:30:47 +00:00
json . Unmarshal ( tb , & tPorts ) //nolint:errcheck
2015-05-31 18:49:11 +00:00
ep . exposedPorts = tPorts
2015-07-02 05:00:48 +00:00
cb , _ := json . Marshal ( epMap [ "sandbox" ] )
2022-07-13 20:30:47 +00:00
json . Unmarshal ( cb , & ep . sandboxID ) //nolint:errcheck
2015-06-01 16:43:24 +00:00
2015-10-03 23:11:50 +00:00
if v , ok := epMap [ "generic" ] ; ok {
ep . generic = v . ( map [ string ] interface { } )
2015-10-22 15:41:52 +00: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 04:26:14 +00:00
logrus . Error ( err )
2015-10-22 15:41:52 +00:00
break
}
err = json . Unmarshal ( bytes , & pb )
if err != nil {
2016-11-01 04:26:14 +00:00
logrus . Error ( err )
2015-10-22 15:41:52 +00: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 04:26:14 +00:00
logrus . Error ( err )
2015-10-22 15:41:52 +00:00
break
}
err = json . Unmarshal ( bytes , & tp )
if err != nil {
2016-11-01 04:26:14 +00:00
logrus . Error ( err )
2015-10-22 15:41:52 +00:00
break
}
tplist = append ( tplist , tp )
}
ep . generic [ netlabel . ExposedPorts ] = tplist
}
2015-05-31 18:49:11 +00:00
}
2015-10-20 19:12:16 +00:00
if v , ok := epMap [ "anonymous" ] ; ok {
ep . anonymous = v . ( bool )
}
2015-12-24 09:51:32 +00:00
if v , ok := epMap [ "disableResolution" ] ; ok {
ep . disableResolution = v . ( bool )
}
2016-04-14 00:53:41 +00:00
if sn , ok := epMap [ "svcName" ] ; ok {
ep . svcName = sn . ( string )
}
if si , ok := epMap [ "svcID" ] ; ok {
ep . svcID = si . ( string )
}
2016-05-25 05:46:18 +00:00
if vip , ok := epMap [ "virtualIP" ] ; ok {
ep . virtualIP = net . ParseIP ( vip . ( string ) )
}
2017-08-29 06:35:31 +00:00
if v , ok := epMap [ "loadBalancer" ] ; ok {
ep . loadBalancer = v . ( bool )
}
2016-06-14 23:40:54 +00:00
sal , _ := json . Marshal ( epMap [ "svcAliases" ] )
var svcAliases [ ] string
2022-07-13 20:30:47 +00:00
json . Unmarshal ( sal , & svcAliases ) //nolint:errcheck
2016-06-14 23:40:54 +00:00
ep . svcAliases = svcAliases
2016-05-31 06:55:51 +00:00
pc , _ := json . Marshal ( epMap [ "ingressPorts" ] )
var ingressPorts [ ] * PortConfig
2022-07-13 20:30:47 +00:00
json . Unmarshal ( pc , & ingressPorts ) //nolint:errcheck
2016-05-31 06:55:51 +00:00
ep . ingressPorts = ingressPorts
2016-01-08 03:19:31 +00:00
ma , _ := json . Marshal ( epMap [ "myAliases" ] )
var myAliases [ ] string
2022-07-13 20:30:47 +00:00
json . Unmarshal ( ma , & myAliases ) //nolint:errcheck
2016-01-08 03:19:31 +00:00
ep . myAliases = myAliases
2015-05-31 18:49:11 +00:00
return nil
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) New ( ) datastore . KVObject {
return & Endpoint { network : ep . getNetwork ( ) }
2015-10-05 11:21:15 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) CopyTo ( o datastore . KVObject ) error {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-10-05 11:21:15 +00:00
2023-01-12 01:42:24 +00:00
dstEp := o . ( * Endpoint )
2015-10-05 11:21:15 +00:00
dstEp . name = ep . name
dstEp . id = ep . id
dstEp . sandboxID = ep . sandboxID
dstEp . dbIndex = ep . dbIndex
dstEp . dbExists = ep . dbExists
2015-10-20 19:12:16 +00:00
dstEp . anonymous = ep . anonymous
2015-12-24 09:51:32 +00:00
dstEp . disableResolution = ep . disableResolution
2016-04-14 00:53:41 +00:00
dstEp . svcName = ep . svcName
dstEp . svcID = ep . svcID
2016-05-25 05:46:18 +00:00
dstEp . virtualIP = ep . virtualIP
2017-08-29 06:35:31 +00:00
dstEp . loadBalancer = ep . loadBalancer
2015-10-05 11:21:15 +00:00
2016-06-14 23:40:54 +00:00
dstEp . svcAliases = make ( [ ] string , len ( ep . svcAliases ) )
copy ( dstEp . svcAliases , ep . svcAliases )
2016-05-31 06:55:51 +00:00
dstEp . ingressPorts = make ( [ ] * PortConfig , len ( ep . ingressPorts ) )
copy ( dstEp . ingressPorts , ep . ingressPorts )
2015-10-05 11:21:15 +00:00
if ep . iface != nil {
dstEp . iface = & endpointInterface { }
2021-05-28 00:15:56 +00:00
if err := ep . iface . CopyTo ( dstEp . iface ) ; err != nil {
return err
}
2015-10-05 11:21:15 +00:00
}
2016-06-11 00:32:19 +00:00
if ep . joinInfo != nil {
dstEp . joinInfo = & endpointJoinInfo { }
2021-05-28 00:15:56 +00:00
if err := ep . joinInfo . CopyTo ( dstEp . joinInfo ) ; err != nil {
return err
}
2016-06-11 00:32:19 +00:00
}
2015-10-05 11:21:15 +00:00
dstEp . exposedPorts = make ( [ ] types . TransportPort , len ( ep . exposedPorts ) )
copy ( dstEp . exposedPorts , ep . exposedPorts )
2016-01-08 03:19:31 +00:00
dstEp . myAliases = make ( [ ] string , len ( ep . myAliases ) )
copy ( dstEp . myAliases , ep . myAliases )
2015-10-05 11:21:15 +00:00
dstEp . generic = options . Generic { }
for k , v := range ep . generic {
dstEp . generic [ k ] = v
}
return nil
}
2023-01-12 01:42:24 +00:00
// ID returns the system-generated id for this endpoint.
func ( ep * Endpoint ) ID ( ) string {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2015-07-02 05:00:48 +00:00
return ep . id
2015-04-30 01:25:01 +00:00
}
2023-01-12 01:42:24 +00:00
// Name returns the name of this endpoint.
func ( ep * Endpoint ) Name ( ) string {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2015-04-30 01:25:01 +00:00
return ep . name
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) MyAliases ( ) [ ] string {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2016-01-08 03:19:31 +00:00
return ep . myAliases
}
2023-01-12 01:42:24 +00:00
// Network returns the name of the network to which this endpoint is attached.
func ( ep * Endpoint ) Network ( ) string {
2015-10-05 11:21:15 +00:00
if ep . network == nil {
return ""
}
return ep . network . name
2015-04-30 01:25:01 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) isAnonymous ( ) bool {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-10-20 19:12:16 +00:00
return ep . anonymous
}
2018-01-08 22:32:49 +00:00
// isServiceEnabled check if service is enabled on the endpoint
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) isServiceEnabled ( ) bool {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2018-01-08 22:32:49 +00:00
return ep . serviceEnabled
}
// enableService sets service enabled on the endpoint
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) enableService ( ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2018-01-08 22:32:49 +00:00
ep . serviceEnabled = true
}
// disableService disables service on the endpoint
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) disableService ( ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2018-01-08 22:32:49 +00:00
ep . serviceEnabled = false
2016-08-21 05:55:00 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) needResolver ( ) bool {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-12-24 09:51:32 +00:00
return ! ep . disableResolution
}
2015-06-05 20:31:12 +00:00
// endpoint Key structure : endpoint/network-id/endpoint-id
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) Key ( ) [ ] string {
2015-10-05 11:21:15 +00:00
if ep . network == nil {
return nil
}
return [ ] string { datastore . EndpointKeyPrefix , ep . network . id , ep . id }
2015-05-31 18:49:11 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) KeyPrefix ( ) [ ] string {
2015-10-05 11:21:15 +00:00
if ep . network == nil {
return nil
}
return [ ] string { datastore . EndpointKeyPrefix , ep . network . id }
2015-06-05 20:31:12 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) Value ( ) [ ] byte {
2015-05-31 18:49:11 +00:00
b , err := json . Marshal ( ep )
if err != nil {
return nil
}
return b
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) SetValue ( value [ ] byte ) error {
2015-06-18 22:13:38 +00:00
return json . Unmarshal ( value , ep )
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) Index ( ) uint64 {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-31 18:49:11 +00:00
return ep . dbIndex
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) SetIndex ( index uint64 ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-31 18:49:11 +00:00
ep . dbIndex = index
2015-06-18 22:13:38 +00:00
ep . dbExists = true
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) Exists ( ) bool {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-06-18 22:13:38 +00:00
return ep . dbExists
2015-05-31 18:49:11 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) Skip ( ) bool {
2015-10-08 03:01:38 +00:00
return ep . getNetwork ( ) . Skip ( )
2015-09-22 14:09:39 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) processOptions ( options ... EndpointOption ) {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2015-05-01 00:57:06 +00:00
for _ , opt := range options {
if opt != nil {
opt ( ep )
}
}
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) getNetwork ( ) * network {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-10-05 11:21:15 +00:00
return ep . network
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) getNetworkFromStore ( ) ( * network , error ) {
2015-10-05 11:21:15 +00:00
if ep . network == nil {
return nil , fmt . Errorf ( "invalid network object in endpoint %s" , ep . Name ( ) )
}
2016-01-29 18:29:18 +00:00
return ep . network . getController ( ) . getNetworkFromStore ( ep . network . id )
2015-10-05 11:21:15 +00:00
}
2015-05-09 04:50:03 +00:00
2023-01-12 01:42:24 +00:00
// Join joins the sandbox to the endpoint and populates into the sandbox
// the network resources allocated for the endpoint.
func ( ep * Endpoint ) Join ( sb * Sandbox , options ... EndpointOption ) error {
2023-01-12 01:10:09 +00:00
if sb == nil || sb . ID ( ) == "" || sb . Key ( ) == "" {
return types . BadRequestErrorf ( "invalid Sandbox passed to endpoint join: %v" , sb )
2015-09-19 00:33:55 +00:00
}
2015-05-09 04:50:03 +00:00
2015-09-19 00:33:55 +00:00
sb . joinLeaveStart ( )
defer sb . joinLeaveEnd ( )
2015-05-09 04:50:03 +00:00
2015-12-07 22:45:51 +00:00
return ep . sbJoin ( sb , options ... )
2015-05-09 04:50:03 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) sbJoin ( sb * Sandbox , options ... EndpointOption ) ( err error ) {
2015-12-07 22:45:51 +00:00
n , err := ep . getNetworkFromStore ( )
2015-10-05 11:21:15 +00:00
if err != nil {
return fmt . Errorf ( "failed to get network from store during join: %v" , err )
}
2015-12-07 22:45:51 +00:00
ep , err = n . getEndpointFromStore ( ep . ID ( ) )
2015-10-05 11:21:15 +00:00
if err != nil {
return fmt . Errorf ( "failed to get endpoint from store during join: %v" , err )
}
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-07-02 05:00:48 +00:00
if ep . sandboxID != "" {
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-10-09 08:45:24 +00:00
return types . ForbiddenErrorf ( "another container is attached to the same network endpoint" )
2015-04-30 05:58:12 +00:00
}
2015-12-07 22:45:51 +00:00
ep . network = n
ep . sandboxID = sb . ID ( )
2015-05-14 06:23:45 +00:00
ep . joinInfo = & endpointJoinInfo { }
2015-05-09 04:50:03 +00:00
epid := ep . id
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-04-30 05:58:12 +00:00
defer func ( ) {
if err != nil {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-07-02 05:00:48 +00:00
ep . sandboxID = ""
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-04-30 05:58:12 +00:00
}
} ( )
2015-12-07 22:45:51 +00:00
nid := n . ID ( )
2015-05-03 20:29:43 +00:00
2015-05-09 04:50:03 +00:00
ep . processOptions ( options ... )
2015-05-04 05:18:49 +00:00
2015-12-07 22:45:51 +00:00
d , err := n . driver ( true )
2015-10-05 11:21:15 +00:00
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get driver during join: %v" , err )
2015-10-05 11:21:15 +00:00
}
2015-12-07 22:45:51 +00:00
err = d . Join ( nid , epid , sb . Key ( ) , ep , sb . Labels ( ) )
2015-04-30 05:58:12 +00:00
if err != nil {
2015-05-24 09:41:03 +00:00
return err
2015-04-30 05:58:12 +00:00
}
2015-06-20 01:41:31 +00:00
defer func ( ) {
if err != nil {
2017-04-17 17:44:14 +00:00
if e := d . Leave ( nid , epid ) ; e != nil {
logrus . Warnf ( "driver leave failed while rolling back join: %v" , e )
2015-06-20 01:41:31 +00:00
}
}
} ( )
2015-05-09 04:50:03 +00:00
2015-11-25 23:25:56 +00:00
// Watch for service records
2016-05-24 23:17:19 +00:00
if ! n . getController ( ) . isAgent ( ) {
2016-04-14 00:53:41 +00:00
n . getController ( ) . watchSvcRecord ( ep )
}
2015-11-25 23:25:56 +00:00
2020-10-31 20:18:03 +00:00
// Do not update hosts file with internal networks endpoint IP
if ! n . ingress && n . Name ( ) != libnGWNetwork {
2019-08-30 21:22:46 +00:00
var addresses [ ] string
2019-08-30 21:24:43 +00:00
if ip := ep . getFirstInterfaceIPv4Address ( ) ; ip != nil {
addresses = append ( addresses , ip . String ( ) )
}
if ip := ep . getFirstInterfaceIPv6Address ( ) ; ip != nil {
2019-08-30 21:22:46 +00:00
addresses = append ( addresses , ip . String ( ) )
2016-10-04 21:04:48 +00:00
}
2019-08-30 21:22:46 +00:00
if err = sb . updateHostsFile ( addresses ) ; err != nil {
2016-10-04 21:04:48 +00:00
return err
}
2015-04-30 01:25:01 +00:00
}
2015-12-07 22:45:51 +00:00
if err = sb . updateDNS ( n . enableIPv6 ) ; err != nil {
2015-05-24 09:41:03 +00:00
return err
2015-04-30 01:25:01 +00:00
}
2015-05-03 20:29:43 +00:00
2015-12-07 22:45:51 +00:00
// Current endpoint providing external connectivity for the sandbox
extEp := sb . getGatewayEndpoint ( )
2018-02-27 16:15:31 +00:00
sb . addEndpoint ( ep )
2015-09-09 23:20:54 +00:00
defer func ( ) {
if err != nil {
2015-12-07 22:45:51 +00:00
sb . removeEndpoint ( ep )
2015-09-09 23:20:54 +00:00
}
} ( )
2015-07-02 05:00:48 +00:00
if err = sb . populateNetworkResources ( ep ) ; err != nil {
2015-06-05 20:31:12 +00:00
return err
}
2015-09-07 01:34:50 +00:00
2016-10-13 18:14:39 +00:00
if err = n . getController ( ) . updateToStore ( ep ) ; err != nil {
return err
}
2016-11-11 08:42:34 +00:00
if err = ep . addDriverInfoToCluster ( ) ; err != nil {
return err
}
2017-03-15 23:44:47 +00: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 16:34:41 +00: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 16:11:45 +00:00
if sb . needDefaultGW ( ) && sb . getEndpointInGWNetwork ( ) == nil {
2017-04-17 17:44:14 +00:00
return sb . setupDefaultGW ( )
2015-09-07 01:34:50 +00:00
}
2015-12-07 22:45:51 +00:00
moveExtConn := sb . getGatewayEndpoint ( ) != extEp
if moveExtConn {
if extEp != nil {
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Revoking external connectivity on endpoint %s (%s)" , extEp . Name ( ) , extEp . ID ( ) )
2016-09-29 02:41:14 +00:00
extN , err := extEp . getNetworkFromStore ( )
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get network from store for revoking external connectivity during join: %v" , err )
2016-09-29 02:41:14 +00:00
}
extD , err := extN . driver ( true )
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get driver for revoking external connectivity during join: %v" , err )
2016-09-29 02:41:14 +00:00
}
if err = extD . RevokeExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) ) ; err != nil {
2016-03-08 06:21:17 +00:00
return types . InternalErrorf (
"driver failed revoking external connectivity on endpoint %s (%s): %v" ,
2015-12-07 22:45:51 +00:00
extEp . Name ( ) , extEp . ID ( ) , err )
}
2016-03-08 06:21:17 +00:00
defer func ( ) {
if err != nil {
2016-09-29 02:41:14 +00:00
if e := extD . ProgramExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) , sb . Labels ( ) ) ; e != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Failed to roll-back external connectivity on endpoint %s (%s): %v" ,
2016-03-08 06:21:17 +00:00
extEp . Name ( ) , extEp . ID ( ) , e )
}
}
} ( )
2015-12-07 22:45:51 +00:00
}
if ! n . internal {
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Programming external connectivity on endpoint %s (%s)" , ep . Name ( ) , ep . ID ( ) )
2016-03-08 06:21:17 +00: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 22:45:51 +00:00
}
}
}
2016-04-06 16:11:45 +00:00
if ! sb . needDefaultGW ( ) {
2017-04-17 17:44:14 +00:00
if e := sb . clearDefaultGW ( ) ; e != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Failure while disconnecting sandbox %s (%s) from gateway network: %v" ,
2017-04-17 17:44:14 +00:00
sb . ID ( ) , sb . ContainerID ( ) , e )
2016-04-06 16:11:45 +00:00
}
}
return nil
2015-04-30 01:25:01 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) rename ( name string ) error {
2017-03-16 04:13:36 +00:00
var (
err error
netWatch * netWatch
ok bool
)
2015-10-23 03:18:25 +00:00
n := ep . getNetwork ( )
if n == nil {
return fmt . Errorf ( "network not connected for ep %q" , ep . name )
}
2017-03-16 04:13:36 +00:00
c := n . getController ( )
2015-10-23 03:18:25 +00:00
2017-06-06 23:04:50 +00: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 04:13:36 +00: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 22:04:23 +00:00
if err = ep . deleteServiceInfoFromCluster ( sb , true , "rename" ) ; err != nil {
2017-03-16 04:13:36 +00:00
return types . InternalErrorf ( "Could not delete service state for endpoint %s from cluster on rename: %v" , ep . Name ( ) , err )
}
} else {
2023-01-11 20:56:50 +00:00
c . mu . Lock ( )
2017-03-16 04:13:36 +00:00
netWatch , ok = c . nmap [ n . ID ( ) ]
2023-01-11 20:56:50 +00:00
c . mu . Unlock ( )
2017-03-16 04:13:36 +00:00
if ! ok {
return fmt . Errorf ( "watch null for network %q" , n . Name ( ) )
}
n . updateSvcRecord ( ep , c . getLocalEps ( netWatch ) , false )
2015-10-23 03:18:25 +00:00
}
oldName := ep . name
2016-05-05 05:17:24 +00:00
oldAnonymous := ep . anonymous
2015-10-23 03:18:25 +00:00
ep . name = name
2016-05-05 05:17:24 +00:00
ep . anonymous = false
2015-10-23 03:18:25 +00:00
2017-03-16 04:13:36 +00:00
if c . isAgent ( ) {
2017-06-06 23:04:50 +00:00
if err = ep . addServiceInfoToCluster ( sb ) ; err != nil {
2017-03-16 04:13:36 +00:00
return types . InternalErrorf ( "Could not add service state for endpoint %s to cluster on rename: %v" , ep . Name ( ) , err )
2015-10-23 03:18:25 +00:00
}
2017-03-16 04:13:36 +00:00
defer func ( ) {
if err != nil {
2021-05-28 00:15:56 +00: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 04:13:36 +00:00
ep . name = oldName
ep . anonymous = oldAnonymous
2021-05-28 00:15:56 +00: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 04:13:36 +00: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-23 03:18:25 +00:00
// Update the store with the updated name
2017-03-16 04:13:36 +00:00
if err = c . updateToStore ( ep ) ; err != nil {
2015-10-23 03:18:25 +00: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 20:18:03 +00:00
_ = n . getEpCnt ( ) . updateStore ( )
2015-10-23 03:18:25 +00:00
return err
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) hasInterface ( iName string ) bool {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-25 06:24:23 +00:00
2015-09-09 23:06:35 +00:00
return ep . iface != nil && ep . iface . srcName == iName
2015-05-25 06:24:23 +00:00
}
2023-01-12 01:42:24 +00:00
// Leave detaches the network resources populated in the sandbox.
func ( ep * Endpoint ) Leave ( sb * Sandbox , options ... EndpointOption ) error {
2023-01-12 01:10:09 +00:00
if sb == nil || sb . ID ( ) == "" || sb . Key ( ) == "" {
return types . BadRequestErrorf ( "invalid Sandbox passed to endpoint leave: %v" , sb )
2015-07-02 05:00:48 +00:00
}
2015-05-09 04:50:03 +00:00
2015-09-19 00:33:55 +00:00
sb . joinLeaveStart ( )
defer sb . joinLeaveEnd ( )
2015-12-07 22:45:51 +00:00
return ep . sbLeave ( sb , false , options ... )
2015-09-19 00:33:55 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) sbLeave ( sb * Sandbox , force bool , options ... EndpointOption ) error {
2015-10-05 11:21:15 +00: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 )
}
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-07-02 05:00:48 +00:00
sid := ep . sandboxID
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2015-07-02 05:00:48 +00:00
if sid == "" {
return types . ForbiddenErrorf ( "cannot leave endpoint with no attached sandbox" )
2015-05-09 04:50:03 +00:00
}
2015-12-07 22:45:51 +00: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 05:00:48 +00:00
}
ep . processOptions ( options ... )
2016-01-16 22:24:44 +00:00
d , err := n . driver ( ! force )
2015-10-05 11:21:15 +00:00
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get driver during endpoint leave: %v" , err )
2015-06-05 20:31:12 +00:00
}
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-10-16 23:11:55 +00:00
ep . sandboxID = ""
ep . network = n
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-10-16 23:11:55 +00:00
2015-12-07 22:45:51 +00:00
// Current endpoint providing external connectivity to the sandbox
extEp := sb . getGatewayEndpoint ( )
moveExtConn := extEp != nil && ( extEp . ID ( ) == ep . ID ( ) )
2016-01-16 22:24:44 +00:00
if d != nil {
2015-12-07 22:45:51 +00:00
if moveExtConn {
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Revoking external connectivity on endpoint %s (%s)" , ep . Name ( ) , ep . ID ( ) )
2015-12-07 22:45:51 +00:00
if err := d . RevokeExternalConnectivity ( n . id , ep . id ) ; err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "driver failed revoking external connectivity on endpoint %s (%s): %v" ,
2015-12-07 22:45:51 +00:00
ep . Name ( ) , ep . ID ( ) , err )
}
}
2016-01-16 22:24:44 +00:00
if err := d . Leave ( n . id , ep . id ) ; err != nil {
if _ , ok := err . ( types . MaskableError ) ; ! ok {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "driver error disconnecting container %s : %v" , ep . name , err )
2016-01-16 22:24:44 +00:00
}
2015-10-16 23:11:55 +00:00
}
2015-07-02 05:00:48 +00:00
}
2015-05-09 04:50:03 +00:00
2018-05-31 18:21:55 +00: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 22:04:23 +00:00
}
2015-09-07 01:34:50 +00: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 22:04:23 +00:00
logrus . Warnf ( "Failed to clean up network resources on container %s disconnect: %v" , ep . name , err )
2015-10-16 23:11:55 +00: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-07 01:34:50 +00:00
return err
}
2016-11-11 08:42:34 +00: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 22:04:23 +00:00
logrus . Errorf ( "Failed to delete endpoint state for endpoint %s from cluster: %v" , ep . Name ( ) , e )
2016-03-30 21:42:58 +00:00
}
2015-10-24 20:31:01 +00:00
sb . deleteHostsEntries ( n . getSvcRecords ( ep ) )
2016-04-06 16:11:45 +00:00
if ! sb . inDelete && sb . needDefaultGW ( ) && sb . getEndpointInGWNetwork ( ) == nil {
2015-12-07 22:45:51 +00:00
return sb . setupDefaultGW ( )
}
// New endpoint providing external connectivity for the sandbox
extEp = sb . getGatewayEndpoint ( )
if moveExtConn && extEp != nil {
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Programming external connectivity on endpoint %s (%s)" , extEp . Name ( ) , extEp . ID ( ) )
2016-09-29 02:41:14 +00:00
extN , err := extEp . getNetworkFromStore ( )
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get network from store for programming external connectivity during leave: %v" , err )
2016-09-29 02:41:14 +00:00
}
extD , err := extN . driver ( true )
if err != nil {
2017-04-17 17:44:14 +00:00
return fmt . Errorf ( "failed to get driver for programming external connectivity during leave: %v" , err )
2016-09-29 02:41:14 +00:00
}
if err := extD . ProgramExternalConnectivity ( extEp . network . ID ( ) , extEp . ID ( ) , sb . Labels ( ) ) ; err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "driver failed programming external connectivity on endpoint %s: (%s) %v" ,
2015-12-07 22:45:51 +00:00
extEp . Name ( ) , extEp . ID ( ) , err )
}
}
2016-04-06 16:11:45 +00:00
if ! sb . needDefaultGW ( ) {
if err := sb . clearDefaultGW ( ) ; err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Failure while disconnecting sandbox %s (%s) from gateway network: %v" ,
2016-04-06 16:11:45 +00:00
sb . ID ( ) , sb . ContainerID ( ) , err )
}
}
return nil
2015-04-30 01:25:01 +00:00
}
2023-01-12 01:42:24 +00:00
// Delete deletes and detaches this endpoint from the network.
func ( ep * Endpoint ) Delete ( force bool ) error {
2015-06-05 20:31:12 +00:00
var err error
2015-10-05 11:21:15 +00: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 )
}
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-06-05 20:31:12 +00:00
epid := ep . id
name := ep . name
2016-01-08 19:24:14 +00:00
sbid := ep . sandboxID
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2016-01-08 19:24:14 +00:00
sb , _ := n . getController ( ) . SandboxByID ( sbid )
if sb != nil && ! force {
2015-07-02 05:00:48 +00:00
return & ActiveContainerError { name : name , id : epid }
2015-06-01 16:43:24 +00:00
}
2016-01-08 19:24:14 +00:00
if sb != nil {
2023-01-12 01:10:09 +00:00
if e := ep . sbLeave ( sb , force ) ; e != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "failed to leave sandbox for endpoint %s : %v" , name , e )
2016-01-08 19:24:14 +00:00
}
}
2015-06-01 16:43:24 +00:00
2015-10-23 17:09:00 +00:00
if err = n . getController ( ) . deleteFromStore ( ep ) ; err != nil {
2015-10-05 11:21:15 +00:00
return err
2015-06-05 20:31:12 +00:00
}
2016-01-08 19:24:14 +00:00
2015-06-05 20:31:12 +00:00
defer func ( ) {
2016-01-08 19:24:14 +00:00
if err != nil && ! force {
2015-10-23 17:09:00 +00:00
ep . dbExists = false
if e := n . getController ( ) . updateToStore ( ep ) ; e != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "failed to recreate endpoint in store %s : %v" , name , e )
2015-06-05 20:31:12 +00:00
}
}
} ( )
2015-10-23 17:09:00 +00:00
// unwatch for service records
2016-07-22 21:51:36 +00:00
n . getController ( ) . unWatchSvcRecord ( ep )
2015-10-23 17:09:00 +00:00
2016-01-16 22:24:44 +00:00
if err = ep . deleteEndpoint ( force ) ; err != nil && ! force {
2015-06-01 16:43:24 +00:00
return err
2015-05-06 20:02:40 +00:00
}
2015-06-05 20:31:12 +00:00
2015-10-03 23:11:50 +00:00
ep . releaseAddress ( )
2016-09-02 02:09:34 +00:00
if err := n . getEpCnt ( ) . DecEndpointCnt ( ) ; err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "failed to decrement endpoint count for ep %s: %v" , ep . ID ( ) , err )
2016-09-02 02:09:34 +00:00
}
2015-06-01 16:43:24 +00:00
return nil
}
2015-04-30 01:25:01 +00:00
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) deleteEndpoint ( force bool ) error {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-04-30 01:25:01 +00:00
n := ep . network
2015-06-01 16:43:24 +00:00
name := ep . name
epid := ep . id
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2016-01-16 22:24:44 +00:00
driver , err := n . driver ( ! force )
2015-10-05 11:21:15 +00:00
if err != nil {
return fmt . Errorf ( "failed to delete endpoint: %v" , err )
2015-04-30 01:25:01 +00:00
}
2016-01-16 22:24:44 +00:00
if driver == nil {
return nil
}
2015-10-05 11:21:15 +00:00
if err := driver . DeleteEndpoint ( n . id , epid ) ; err != nil {
2015-06-10 20:27:23 +00:00
if _ , ok := err . ( types . ForbiddenError ) ; ok {
return err
2015-04-30 01:25:01 +00:00
}
2015-10-19 22:56:25 +00:00
if _ , ok := err . ( types . MaskableError ) ; ! ok {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "driver error deleting endpoint %s : %v" , name , err )
2015-10-19 22:56:25 +00:00
}
2015-06-10 20:27:23 +00:00
}
2015-06-19 06:40:17 +00:00
2015-06-10 20:27:23 +00:00
return nil
2015-04-30 01:25:01 +00:00
}
2015-04-30 05:58:12 +00:00
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) getSandbox ( ) ( * Sandbox , bool ) {
2015-07-02 05:00:48 +00:00
c := ep . network . getController ( )
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-07-02 05:00:48 +00:00
sid := ep . sandboxID
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2023-01-11 20:56:50 +00:00
c . mu . Lock ( )
2015-07-02 05:00:48 +00:00
ps , ok := c . sandboxes [ sid ]
2023-01-11 20:56:50 +00:00
c . mu . Unlock ( )
2015-04-30 05:58:12 +00:00
2015-07-02 05:00:48 +00:00
return ps , ok
2015-04-30 05:58:12 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) getFirstInterfaceIPv4Address ( ) net . IP {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2015-05-09 04:50:03 +00:00
2015-10-03 23:11:50 +00:00
if ep . iface . addr != nil {
2015-09-09 23:06:35 +00:00
return ep . iface . addr . IP
2015-05-03 20:29:43 +00:00
}
return nil
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) getFirstInterfaceIPv6Address ( ) net . IP {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
defer ep . mu . Unlock ( )
2019-08-30 21:24:43 +00:00
if ep . iface . addrv6 != nil {
return ep . iface . addrv6 . IP
}
return nil
}
2015-05-04 05:18:49 +00: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 {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-05-04 05:18:49 +00:00
for k , v := range generic {
ep . generic [ k ] = v
}
}
}
2016-05-25 03:04:49 +00:00
var (
linkLocalMask = net . CIDRMask ( 16 , 32 )
linkLocalMaskIPv6 = net . CIDRMask ( 64 , 128 )
)
2015-12-04 03:20:42 +00:00
// CreateOptionIpam function returns an option setter for the ipam configuration for this endpoint
2016-05-25 03:04:49 +00:00
func CreateOptionIpam ( ipV4 , ipV6 net . IP , llIPs [ ] net . IP , ipamOptions map [ string ] string ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-12-09 02:03:38 +00:00
ep . prefAddress = ipV4
ep . prefAddressV6 = ipV6
2016-05-25 03:04:49 +00: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-04 03:20:42 +00:00
ep . ipamOptions = ipamOptions
}
}
2015-05-05 20:46:12 +00:00
// CreateOptionExposedPorts function returns an option setter for the container exposed
// ports option to be passed to network.CreateEndpoint() method.
2015-05-20 20:28:46 +00:00
func CreateOptionExposedPorts ( exposedPorts [ ] types . TransportPort ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-05-05 20:46:12 +00:00
// Defensive copy
2015-05-20 20:28:46 +00:00
eps := make ( [ ] types . TransportPort , len ( exposedPorts ) )
2015-05-05 20:46:12 +00:00
copy ( eps , exposedPorts )
// Store endpoint label and in generic because driver needs it
ep . exposedPorts = eps
2015-05-06 04:19:57 +00:00
ep . generic [ netlabel . ExposedPorts ] = eps
2015-05-05 20:46:12 +00:00
}
}
// CreateOptionPortMapping function returns an option setter for the mapping
2015-05-02 00:11:13 +00:00
// ports option to be passed to network.CreateEndpoint() method.
2015-05-20 20:28:46 +00:00
func CreateOptionPortMapping ( portBindings [ ] types . PortBinding ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-05-05 06:45:07 +00:00
// Store a copy of the bindings as generic data to pass to the driver
2015-05-20 20:28:46 +00:00
pbs := make ( [ ] types . PortBinding , len ( portBindings ) )
2015-05-05 20:46:12 +00:00
copy ( pbs , portBindings )
2015-05-06 04:19:57 +00:00
ep . generic [ netlabel . PortMap ] = pbs
2015-05-02 00:01:21 +00:00
}
}
2016-09-19 22:48:06 +00:00
// CreateOptionDNS function returns an option setter for dns entry option to
// be passed to container Create method.
func CreateOptionDNS ( dns [ ] string ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2016-09-19 22:48:06 +00:00
ep . generic [ netlabel . DNSServers ] = dns
}
}
2015-10-20 19:12:16 +00:00
// CreateOptionAnonymous function returns an option setter for setting
// this endpoint as anonymous
func CreateOptionAnonymous ( ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-10-20 19:12:16 +00:00
ep . anonymous = true
}
}
2015-12-24 09:51:32 +00:00
// CreateOptionDisableResolution function returns an option setter to indicate
// this endpoint doesn't want embedded DNS server functionality
func CreateOptionDisableResolution ( ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-12-24 09:51:32 +00:00
ep . disableResolution = true
}
}
2017-09-07 17:36:11 +00:00
// CreateOptionAlias function returns an option setter for setting endpoint alias
2016-01-04 22:02:03 +00:00
func CreateOptionAlias ( name string , alias string ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2016-01-04 22:02:03 +00:00
if ep . aliases == nil {
ep . aliases = make ( map [ string ] string )
}
ep . aliases [ alias ] = name
}
}
2016-04-14 00:53:41 +00:00
// CreateOptionService function returns an option setter for setting service binding configuration
2016-06-14 23:40:54 +00:00
func CreateOptionService ( name , id string , vip net . IP , ingressPorts [ ] * PortConfig , aliases [ ] string ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2016-04-14 00:53:41 +00:00
ep . svcName = name
ep . svcID = id
2016-05-25 05:46:18 +00:00
ep . virtualIP = vip
2016-05-31 06:55:51 +00:00
ep . ingressPorts = ingressPorts
2016-06-14 23:40:54 +00:00
ep . svcAliases = aliases
2016-04-14 00:53:41 +00:00
}
}
2017-09-07 17:36:11 +00:00
// CreateOptionMyAlias function returns an option setter for setting endpoint's self alias
2016-01-08 03:19:31 +00:00
func CreateOptionMyAlias ( alias string ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2016-01-08 03:19:31 +00:00
ep . myAliases = append ( ep . myAliases , alias )
}
}
2017-09-07 17:36:11 +00:00
// CreateOptionLoadBalancer function returns an option setter for denoting the endpoint is a load balancer for a network
2017-08-29 06:35:31 +00:00
func CreateOptionLoadBalancer ( ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2017-08-29 06:35:31 +00:00
ep . loadBalancer = true
}
}
2015-07-02 05:00:48 +00:00
// JoinOptionPriority function returns an option setter for priority option to
// be passed to the endpoint.Join() method.
2020-05-15 03:02:30 +00:00
func JoinOptionPriority ( prio int ) EndpointOption {
2023-01-12 01:42:24 +00:00
return func ( ep * Endpoint ) {
2015-07-02 05:00:48 +00:00
// ep lock already acquired
c := ep . network . getController ( )
2023-01-11 20:56:50 +00:00
c . mu . Lock ( )
2015-07-02 05:00:48 +00:00
sb , ok := c . sandboxes [ ep . sandboxID ]
2023-01-11 20:56:50 +00:00
c . mu . Unlock ( )
2015-07-02 05:00:48 +00:00
if ! ok {
2016-11-01 04:26:14 +00: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 05:00:48 +00:00
return
}
sb . epPriority [ ep . id ] = prio
2015-04-30 21:52:46 +00:00
}
}
2015-09-16 11:39:46 +00:00
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) DataScope ( ) string {
2015-10-05 11:21:15 +00:00
return ep . getNetwork ( ) . DataScope ( )
2015-09-16 11:42:35 +00:00
}
2015-10-03 23:11:50 +00:00
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) assignAddress ( ipam ipamapi . Ipam , assignIPv4 , assignIPv6 bool ) error {
2015-12-10 02:09:58 +00:00
var err error
2015-10-05 21:53:25 +00:00
2015-10-03 23:11:50 +00:00
n := ep . getNetwork ( )
2016-06-15 04:34:44 +00:00
if n . hasSpecialDriver ( ) {
2015-10-03 23:11:50 +00:00
return nil
}
2015-10-05 21:53:25 +00:00
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Assigning addresses for endpoint %s's interface on network %s" , ep . Name ( ) , n . Name ( ) )
2015-10-05 21:53:25 +00:00
2015-11-06 07:50:10 +00:00
if assignIPv4 {
if err = ep . assignAddressVersion ( 4 , ipam ) ; err != nil {
return err
}
2015-10-04 04:25:57 +00:00
}
2015-11-06 07:50:10 +00:00
if assignIPv6 {
err = ep . assignAddressVersion ( 6 , ipam )
}
return err
2015-10-04 04:25:57 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) assignAddressVersion ( ipVer int , ipam ipamapi . Ipam ) error {
2015-10-04 04:25:57 +00:00
var (
poolID * string
address * * net . IPNet
2015-12-09 02:03:38 +00:00
prefAdd net . IP
progAdd net . IP
2015-10-04 04:25:57 +00:00
)
n := ep . getNetwork ( )
switch ipVer {
case 4 :
poolID = & ep . iface . v4PoolID
address = & ep . iface . addr
2015-12-09 02:03:38 +00:00
prefAdd = ep . prefAddress
2015-10-04 04:25:57 +00:00
case 6 :
poolID = & ep . iface . v6PoolID
address = & ep . iface . addrv6
2015-12-09 02:03:38 +00:00
prefAdd = ep . prefAddressV6
2015-10-04 04:25:57 +00: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-09 02:03:38 +00: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 04:25:57 +00:00
for _ , d := range ipInfo {
2015-12-09 02:03:38 +00:00
if progAdd != nil && ! d . Pool . Contains ( progAdd ) {
continue
2015-11-06 07:50:10 +00:00
}
2015-12-09 02:03:38 +00:00
addr , _ , err := ipam . RequestAddress ( d . PoolID , progAdd , ep . ipamOptions )
2015-10-03 23:11:50 +00:00
if err == nil {
2023-01-11 22:51:59 +00:00
ep . mu . Lock ( )
2015-10-04 04:25:57 +00:00
* address = addr
* poolID = d . PoolID
2023-01-11 22:51:59 +00:00
ep . mu . Unlock ( )
2015-10-03 23:11:50 +00:00
return nil
}
2015-12-09 02:03:38 +00:00
if err != ipamapi . ErrNoAvailableIPs || progAdd != nil {
2015-10-03 23:11:50 +00:00
return err
}
}
2015-12-09 02:03:38 +00:00
if progAdd != nil {
2016-01-26 15:09:29 +00:00
return types . BadRequestErrorf ( "Invalid address %s: It does not belong to any of this network's subnets" , prefAdd )
2015-12-09 02:03:38 +00:00
}
2015-10-04 04:25:57 +00: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 23:11:50 +00:00
}
2023-01-12 01:42:24 +00:00
func ( ep * Endpoint ) releaseAddress ( ) {
2015-10-03 23:11:50 +00:00
n := ep . getNetwork ( )
2016-06-15 04:34:44 +00:00
if n . hasSpecialDriver ( ) {
2015-10-03 23:11:50 +00:00
return
}
2015-10-05 21:53:25 +00:00
2016-11-01 04:26:14 +00:00
logrus . Debugf ( "Releasing addresses for endpoint %s's interface on network %s" , ep . Name ( ) , n . Name ( ) )
2015-10-05 21:53:25 +00:00
2016-03-01 02:17:04 +00:00
ipam , _ , err := n . getController ( ) . getIPAMDriver ( n . ipamType )
2015-10-03 23:11:50 +00:00
if err != nil {
2016-11-01 04:26:14 +00: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 23:11:50 +00:00
return
}
2016-03-04 19:35:11 +00:00
if ep . iface . addr != nil {
if err := ipam . ReleaseAddress ( ep . iface . v4PoolID , ep . iface . addr . IP ) ; err != nil {
2016-11-01 04:26:14 +00: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 19:35:11 +00:00
}
2015-10-03 23:11:50 +00:00
}
2016-03-04 19:35:11 +00:00
2015-10-04 04:25:57 +00: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 04:26:14 +00: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 04:25:57 +00:00
}
}
2015-10-03 23:11:50 +00:00
}
2015-10-19 20:20:23 +00:00
2023-01-11 22:43:32 +00:00
func ( c * Controller ) cleanupLocalEndpoints ( ) {
2016-06-11 00:32:19 +00:00
// Get used endpoints
eps := make ( map [ string ] interface { } )
for _ , sb := range c . sandboxes {
for _ , ep := range sb . endpoints {
eps [ ep . id ] = true
}
}
2023-01-14 00:04:43 +00:00
nl , err := c . getNetworks ( )
2015-10-19 20:20:23 +00:00
if err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Could not get list of networks during endpoint cleanup: %v" , err )
2015-10-19 20:20:23 +00:00
return
}
for _ , n := range nl {
2017-04-07 17:51:39 +00:00
if n . ConfigOnly ( ) {
continue
}
2015-10-19 20:20:23 +00:00
epl , err := n . getEndpointsFromStore ( )
if err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Could not get list of endpoints in network %s during endpoint cleanup: %v" , n . name , err )
2015-10-19 20:20:23 +00:00
continue
}
for _ , ep := range epl {
2016-06-11 00:32:19 +00:00
if _ , ok := eps [ ep . id ] ; ok {
continue
}
2016-11-01 04:26:14 +00:00
logrus . Infof ( "Removing stale endpoint %s (%s)" , ep . name , ep . id )
2016-01-17 20:43:41 +00:00
if err := ep . Delete ( true ) ; err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Could not delete local endpoint %s during endpoint cleanup: %v" , ep . name , err )
2015-10-19 20:20:23 +00:00
}
}
2016-03-16 04:07:42 +00:00
epl , err = n . getEndpointsFromStore ( )
if err != nil {
2016-11-01 04:26:14 +00:00
logrus . Warnf ( "Could not get list of endpoints in network %s for count update: %v" , n . name , err )
2016-03-16 04:07:42 +00:00
continue
}
epCnt := n . getEpCnt ( ) . EndpointCnt ( )
if epCnt != uint64 ( len ( epl ) ) {
2016-11-01 04:26:14 +00:00
logrus . Infof ( "Fixing inconsistent endpoint_cnt for network %s. Expected=%d, Actual=%d" , n . name , len ( epl ) , epCnt )
2021-05-28 00:15:56 +00: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 04:07:42 +00:00
}
2015-10-19 20:20:23 +00:00
}
}