2015-07-02 01:00:48 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-12-24 04:51:32 -05:00
|
|
|
"net"
|
2018-02-27 11:15:31 -05:00
|
|
|
"sort"
|
2015-12-24 04:51:32 -05:00
|
|
|
"strings"
|
2015-07-02 01:00:48 -04:00
|
|
|
"sync"
|
2016-03-28 21:02:09 -04:00
|
|
|
"time"
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
"github.com/docker/libnetwork/etchosts"
|
2015-12-07 17:45:51 -05:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-07-02 01:00:48 -04:00
|
|
|
"github.com/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-07-02 01:00:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sandbox provides the control over the network container entity. It is a one to one mapping with the container.
|
|
|
|
type Sandbox interface {
|
|
|
|
// ID returns the ID of the sandbox
|
|
|
|
ID() string
|
|
|
|
// Key returns the sandbox's key
|
|
|
|
Key() string
|
|
|
|
// ContainerID returns the container id associated to this sandbox
|
|
|
|
ContainerID() string
|
|
|
|
// Labels returns the sandbox's labels
|
|
|
|
Labels() map[string]interface{}
|
|
|
|
// Statistics retrieves the interfaces' statistics for the sandbox
|
2015-09-26 13:12:20 -04:00
|
|
|
Statistics() (map[string]*types.InterfaceStatistics, error)
|
2016-05-22 22:55:17 -04:00
|
|
|
// Refresh leaves all the endpoints, resets and re-applies the options,
|
2015-09-01 21:55:53 -04:00
|
|
|
// re-joins all the endpoints without destroying the osl sandbox
|
|
|
|
Refresh(options ...SandboxOption) error
|
2015-09-09 19:20:54 -04:00
|
|
|
// SetKey updates the Sandbox Key
|
|
|
|
SetKey(key string) error
|
2015-10-22 23:18:25 -04:00
|
|
|
// Rename changes the name of all attached Endpoints
|
|
|
|
Rename(name string) error
|
2015-07-02 01:00:48 -04:00
|
|
|
// Delete destroys this container after detaching it from all connected endpoints.
|
|
|
|
Delete() error
|
2016-01-25 19:23:00 -05:00
|
|
|
// Endpoints returns all the endpoints connected to the sandbox
|
|
|
|
Endpoints() []Endpoint
|
2016-09-19 18:48:06 -04:00
|
|
|
// ResolveService returns all the backend details about the containers or hosts
|
|
|
|
// backing a service. Its purpose is to satisfy an SRV query
|
|
|
|
ResolveService(name string) ([]*net.SRV, []net.IP)
|
2016-08-21 01:55:00 -04:00
|
|
|
// EnableService makes a managed container's service available by adding the
|
|
|
|
// endpoint to the service load balancer and service discovery
|
|
|
|
EnableService() error
|
2016-12-27 06:42:32 -05:00
|
|
|
// DisableService removes a managed container's endpoints from the load balancer
|
2016-08-21 01:55:00 -04:00
|
|
|
// and service discovery
|
|
|
|
DisableService() error
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-01-11 05:15:20 -05:00
|
|
|
// SandboxOption is an option setter function type used to pass various options to
|
2015-07-02 01:00:48 -04:00
|
|
|
// NewNetContainer method. The various setter functions of type SandboxOption are
|
|
|
|
// provided by libnetwork, they look like ContainerOptionXXXX(...)
|
|
|
|
type SandboxOption func(sb *sandbox)
|
|
|
|
|
|
|
|
func (sb *sandbox) processOptions(options ...SandboxOption) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(sb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type sandbox struct {
|
2016-06-15 17:00:48 -04:00
|
|
|
id string
|
|
|
|
containerID string
|
|
|
|
config containerConfig
|
2016-12-18 21:27:13 -05:00
|
|
|
extDNS []extDNSEntry
|
2016-06-15 17:00:48 -04:00
|
|
|
osSbox osl.Sandbox
|
|
|
|
controller *controller
|
|
|
|
resolver Resolver
|
|
|
|
resolverOnce sync.Once
|
|
|
|
refCnt int
|
2018-02-27 11:15:31 -05:00
|
|
|
endpoints []*endpoint
|
2016-06-15 17:00:48 -04:00
|
|
|
epPriority map[string]int
|
|
|
|
populatedEndpoints map[string]struct{}
|
|
|
|
joinLeaveDone chan struct{}
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
isStub bool
|
|
|
|
inDelete bool
|
|
|
|
ingress bool
|
2016-09-10 00:45:03 -04:00
|
|
|
ndotsSet bool
|
2018-05-18 17:10:14 -04:00
|
|
|
oslTypes []osl.SandboxType // slice of properties of this sandbox
|
2015-07-02 01:00:48 -04:00
|
|
|
sync.Mutex
|
2017-06-06 19:04:50 -04:00
|
|
|
// This mutex is used to serialize service related operation for an endpoint
|
|
|
|
// The lock is here because the endpoint is saved into the store so is not unique
|
|
|
|
Service sync.Mutex
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// These are the container configs used to customize container /etc/hosts file.
|
|
|
|
type hostsPathConfig struct {
|
|
|
|
hostName string
|
|
|
|
domainName string
|
|
|
|
hostsPath string
|
|
|
|
originHostsPath string
|
|
|
|
extraHosts []extraHost
|
|
|
|
parentUpdates []parentUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
type parentUpdate struct {
|
|
|
|
cid string
|
|
|
|
name string
|
|
|
|
ip string
|
|
|
|
}
|
|
|
|
|
|
|
|
type extraHost struct {
|
|
|
|
name string
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are the container configs used to customize container /etc/resolv.conf file.
|
|
|
|
type resolvConfPathConfig struct {
|
|
|
|
resolvConfPath string
|
|
|
|
originResolvConfPath string
|
|
|
|
resolvConfHashFile string
|
|
|
|
dnsList []string
|
|
|
|
dnsSearchList []string
|
2015-08-21 20:31:31 -04:00
|
|
|
dnsOptionsList []string
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type containerConfig struct {
|
|
|
|
hostsPathConfig
|
|
|
|
resolvConfPathConfig
|
|
|
|
generic map[string]interface{}
|
|
|
|
useDefaultSandBox bool
|
2015-09-09 19:20:54 -04:00
|
|
|
useExternalKey bool
|
2015-07-02 01:00:48 -04:00
|
|
|
prio int // higher the value, more the priority
|
2015-12-07 17:45:51 -05:00
|
|
|
exposedPorts []types.TransportPort
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
const (
|
|
|
|
resolverIPSandbox = "127.0.0.11"
|
|
|
|
)
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (sb *sandbox) ID() string {
|
|
|
|
return sb.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) ContainerID() string {
|
|
|
|
return sb.containerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) Key() string {
|
|
|
|
if sb.config.useDefaultSandBox {
|
|
|
|
return osl.GenerateKey("default")
|
|
|
|
}
|
|
|
|
return osl.GenerateKey(sb.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) Labels() map[string]interface{} {
|
2015-12-07 17:45:51 -05:00
|
|
|
sb.Lock()
|
2016-09-08 15:22:20 -04:00
|
|
|
defer sb.Unlock()
|
2015-12-07 17:45:51 -05:00
|
|
|
opts := make(map[string]interface{}, len(sb.config.generic))
|
|
|
|
for k, v := range sb.config.generic {
|
|
|
|
opts[k] = v
|
|
|
|
}
|
|
|
|
return opts
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 13:12:20 -04:00
|
|
|
func (sb *sandbox) Statistics() (map[string]*types.InterfaceStatistics, error) {
|
|
|
|
m := make(map[string]*types.InterfaceStatistics)
|
2015-07-02 01:00:48 -04:00
|
|
|
|
2016-02-29 12:45:15 -05:00
|
|
|
sb.Lock()
|
|
|
|
osb := sb.osSbox
|
|
|
|
sb.Unlock()
|
|
|
|
if osb == nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2016-02-29 12:45:15 -05:00
|
|
|
for _, i := range osb.Info().Interfaces() {
|
2015-07-02 01:00:48 -04:00
|
|
|
if m[i.DstName()], err = i.Statistics(); err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) Delete() error {
|
2016-01-16 17:24:44 -05:00
|
|
|
return sb.delete(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) delete(force bool) error {
|
2015-10-16 21:00:30 -04:00
|
|
|
sb.Lock()
|
|
|
|
if sb.inDelete {
|
|
|
|
sb.Unlock()
|
|
|
|
return types.ForbiddenErrorf("another sandbox delete in progress")
|
|
|
|
}
|
|
|
|
// Set the inDelete flag. This will ensure that we don't
|
|
|
|
// update the store until we have completed all the endpoint
|
|
|
|
// leaves and deletes. And when endpoint leaves and deletes
|
|
|
|
// are completed then we can finally delete the sandbox object
|
|
|
|
// altogether from the data store. If the daemon exits
|
|
|
|
// ungracefully in the middle of a sandbox delete this way we
|
|
|
|
// will have all the references to the endpoints in the
|
|
|
|
// sandbox so that we can clean them up when we restart
|
|
|
|
sb.inDelete = true
|
|
|
|
sb.Unlock()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
c := sb.controller
|
|
|
|
|
2015-09-01 21:55:53 -04:00
|
|
|
// Detach from all endpoints
|
2015-10-29 20:16:52 -04:00
|
|
|
retain := false
|
2015-09-01 21:55:53 -04:00
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
2016-04-06 12:11:45 -04:00
|
|
|
// gw network endpoint detach and removal are automatic
|
2016-09-01 17:27:36 -04:00
|
|
|
if ep.endpointInGWNetwork() && !force {
|
2016-04-06 12:11:45 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-11-02 20:54:22 -05:00
|
|
|
// Retain the sanbdox if we can't obtain the network from store.
|
|
|
|
if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil {
|
2016-09-01 17:27:36 -04:00
|
|
|
if c.isDistributedControl() {
|
|
|
|
retain = true
|
|
|
|
}
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
|
2015-11-02 20:54:22 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:24:44 -05:00
|
|
|
if !force {
|
|
|
|
if err := ep.Leave(sb); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
|
2016-01-16 17:24:44 -05:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2015-10-07 23:01:38 -04:00
|
|
|
|
2016-01-16 17:24:44 -05:00
|
|
|
if err := ep.Delete(force); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err)
|
2015-10-07 23:01:38 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2015-10-29 20:16:52 -04:00
|
|
|
if retain {
|
|
|
|
sb.Lock()
|
|
|
|
sb.inDelete = false
|
|
|
|
sb.Unlock()
|
|
|
|
return fmt.Errorf("could not cleanup all the endpoints in container %s / sandbox %s", sb.containerID, sb.id)
|
|
|
|
}
|
2015-10-21 01:50:23 -04:00
|
|
|
// Container is going away. Path cache in etchosts is most
|
|
|
|
// likely not required any more. Drop it.
|
|
|
|
etchosts.Drop(sb.config.hostsPath)
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
if sb.resolver != nil {
|
|
|
|
sb.resolver.Stop()
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:15:15 -05:00
|
|
|
if sb.osSbox != nil && !sb.config.useDefaultSandBox {
|
2015-07-02 01:00:48 -04:00
|
|
|
sb.osSbox.Destroy()
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:01:38 -04:00
|
|
|
if err := sb.storeDelete(); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
|
2015-10-07 23:01:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
c.Lock()
|
2016-06-10 11:58:19 -04:00
|
|
|
if sb.ingress {
|
|
|
|
c.ingressSandbox = nil
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
delete(c.sandboxes, sb.ID())
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-22 23:18:25 -04:00
|
|
|
func (sb *sandbox) Rename(name string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if ep.endpointInGWNetwork() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
oldName := ep.Name()
|
|
|
|
lEp := ep
|
|
|
|
if err = ep.rename(name); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:14:54 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
lEp.rename(oldName)
|
|
|
|
}
|
|
|
|
}()
|
2015-10-22 23:18:25 -04:00
|
|
|
}
|
2015-10-27 13:14:54 -04:00
|
|
|
|
2015-10-22 23:18:25 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-01 21:55:53 -04:00
|
|
|
func (sb *sandbox) Refresh(options ...SandboxOption) error {
|
|
|
|
// Store connected endpoints
|
|
|
|
epList := sb.getConnectedEndpoints()
|
|
|
|
|
|
|
|
// Detach from all endpoints
|
|
|
|
for _, ep := range epList {
|
|
|
|
if err := ep.Leave(sb); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
|
2015-09-01 21:55:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-apply options
|
|
|
|
sb.config = containerConfig{}
|
|
|
|
sb.processOptions(options...)
|
|
|
|
|
|
|
|
// Setup discovery files
|
|
|
|
if err := sb.setupResolutionFiles(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-22 22:55:17 -04:00
|
|
|
// Re-connect to all endpoints
|
2015-09-01 21:55:53 -04:00
|
|
|
for _, ep := range epList {
|
|
|
|
if err := ep.Join(sb); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed attach sandbox %s to endpoint %s: %v\n", sb.ID(), ep.ID(), err)
|
2015-09-01 21:55:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (sb *sandbox) MarshalJSON() ([]byte, error) {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
// We are just interested in the container ID. This can be expanded to include all of containerInfo if there is a need
|
|
|
|
return json.Marshal(sb.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
var id string
|
|
|
|
if err := json.Unmarshal(b, &id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb.id = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 19:23:00 -05:00
|
|
|
func (sb *sandbox) Endpoints() []Endpoint {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
endpoints := make([]Endpoint, len(sb.endpoints))
|
|
|
|
for i, ep := range sb.endpoints {
|
|
|
|
endpoints[i] = ep
|
|
|
|
}
|
|
|
|
return endpoints
|
|
|
|
}
|
|
|
|
|
2015-09-01 21:55:53 -04:00
|
|
|
func (sb *sandbox) getConnectedEndpoints() []*endpoint {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
eps := make([]*endpoint, len(sb.endpoints))
|
2018-03-08 05:45:04 -05:00
|
|
|
copy(eps, sb.endpoints)
|
2015-09-01 21:55:53 -04:00
|
|
|
|
|
|
|
return eps
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:15:31 -05:00
|
|
|
func (sb *sandbox) addEndpoint(ep *endpoint) {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
l := len(sb.endpoints)
|
|
|
|
i := sort.Search(l, func(j int) bool {
|
|
|
|
return ep.Less(sb.endpoints[j])
|
|
|
|
})
|
|
|
|
|
|
|
|
sb.endpoints = append(sb.endpoints, nil)
|
|
|
|
copy(sb.endpoints[i+1:], sb.endpoints[i:])
|
|
|
|
sb.endpoints[i] = ep
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
func (sb *sandbox) removeEndpoint(ep *endpoint) {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
2018-02-27 11:15:31 -05:00
|
|
|
sb.removeEndpointRaw(ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) removeEndpointRaw(ep *endpoint) {
|
2015-12-07 17:45:51 -05:00
|
|
|
for i, e := range sb.endpoints {
|
|
|
|
if e == ep {
|
2018-02-27 11:15:31 -05:00
|
|
|
sb.endpoints = append(sb.endpoints[:i], sb.endpoints[i+1:]...)
|
2015-12-07 17:45:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (sb *sandbox) getEndpoint(id string) *endpoint {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
for _, ep := range sb.endpoints {
|
|
|
|
if ep.id == id {
|
|
|
|
return ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (sb *sandbox) updateGateway(ep *endpoint) error {
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Lock()
|
|
|
|
osSbox := sb.osSbox
|
|
|
|
sb.Unlock()
|
|
|
|
if osSbox == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
osSbox.UnsetGateway()
|
|
|
|
osSbox.UnsetGatewayIPv6()
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
if ep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.Lock()
|
|
|
|
joinInfo := ep.joinInfo
|
|
|
|
ep.Unlock()
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
if err := osSbox.SetGateway(joinInfo.gw); err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
return fmt.Errorf("failed to set gateway while updating gateway: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:56:24 -05:00
|
|
|
func (sb *sandbox) HandleQueryResp(name string, ip net.IP) {
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
n := ep.getNetwork()
|
|
|
|
n.HandleQueryResp(name, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
func (sb *sandbox) ResolveIP(ip string) string {
|
|
|
|
var svc string
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("IP To resolve %v", ip)
|
2015-12-24 04:51:32 -05:00
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
n := ep.getNetwork()
|
2016-09-19 18:48:06 -04:00
|
|
|
svc = n.ResolveIP(ip)
|
|
|
|
if len(svc) != 0 {
|
|
|
|
return svc
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 18:48:06 -04:00
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
func (sb *sandbox) ExecFunc(f func()) error {
|
2016-11-15 12:49:00 -05:00
|
|
|
sb.Lock()
|
|
|
|
osSbox := sb.osSbox
|
|
|
|
sb.Unlock()
|
|
|
|
if osSbox != nil {
|
|
|
|
return osSbox.InvokeFunc(f)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("osl sandbox unavailable in ExecFunc for %v", sb.ContainerID())
|
2016-01-24 00:47:57 -05:00
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
func (sb *sandbox) ResolveService(name string) ([]*net.SRV, []net.IP) {
|
2016-05-08 03:48:04 -04:00
|
|
|
srv := []*net.SRV{}
|
|
|
|
ip := []net.IP{}
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Service name To resolve: %v", name)
|
2016-05-08 03:48:04 -04:00
|
|
|
|
2017-05-21 22:25:52 -04:00
|
|
|
// There are DNS implementations that allow SRV queries for names not in
|
2016-08-12 18:40:39 -04:00
|
|
|
// the format defined by RFC 2782. Hence specific validations checks are
|
|
|
|
// not done
|
2016-05-08 03:48:04 -04:00
|
|
|
parts := strings.Split(name, ".")
|
|
|
|
if len(parts) < 3 {
|
2016-09-19 18:48:06 -04:00
|
|
|
return nil, nil
|
2016-05-08 03:48:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
n := ep.getNetwork()
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
srv, ip = n.ResolveService(name)
|
2016-05-08 03:48:04 -04:00
|
|
|
if len(srv) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 18:48:06 -04:00
|
|
|
return srv, ip
|
2016-05-08 03:48:04 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 20:44:33 -04:00
|
|
|
func getDynamicNwEndpoints(epList []*endpoint) []*endpoint {
|
|
|
|
eps := []*endpoint{}
|
|
|
|
for _, ep := range epList {
|
|
|
|
n := ep.getNetwork()
|
|
|
|
if n.dynamic && !n.ingress {
|
|
|
|
eps = append(eps, ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return eps
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIngressNwEndpoint(epList []*endpoint) *endpoint {
|
|
|
|
for _, ep := range epList {
|
|
|
|
n := ep.getNetwork()
|
|
|
|
if n.ingress {
|
|
|
|
return ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLocalNwEndpoints(epList []*endpoint) []*endpoint {
|
|
|
|
eps := []*endpoint{}
|
|
|
|
for _, ep := range epList {
|
|
|
|
n := ep.getNetwork()
|
|
|
|
if !n.dynamic && !n.ingress {
|
|
|
|
eps = append(eps, ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return eps
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:07:08 -04:00
|
|
|
func (sb *sandbox) ResolveName(name string, ipType int) ([]net.IP, bool) {
|
2016-01-19 03:40:46 -05:00
|
|
|
// Embedded server owns the docker network domain. Resolution should work
|
|
|
|
// for both container_name and container_name.network_name
|
|
|
|
// We allow '.' in service name and network name. For a name a.b.c.d the
|
|
|
|
// following have to tried;
|
|
|
|
// {a.b.c.d in the networks container is connected to}
|
|
|
|
// {a.b.c in network d},
|
|
|
|
// {a.b in network c.d},
|
|
|
|
// {a in network b.c.d},
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Name To resolve: %v", name)
|
2016-01-19 03:40:46 -05:00
|
|
|
name = strings.TrimSuffix(name, ".")
|
|
|
|
reqName := []string{name}
|
|
|
|
networkName := []string{""}
|
|
|
|
|
|
|
|
if strings.Contains(name, ".") {
|
|
|
|
var i int
|
|
|
|
dup := name
|
|
|
|
for {
|
|
|
|
if i = strings.LastIndex(dup, "."); i == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
networkName = append(networkName, name[i+1:])
|
|
|
|
reqName = append(reqName, name[:i])
|
|
|
|
|
|
|
|
dup = dup[:i]
|
|
|
|
}
|
2016-01-04 17:02:03 -05:00
|
|
|
}
|
2016-01-19 03:40:46 -05:00
|
|
|
|
2016-01-04 17:02:03 -05:00
|
|
|
epList := sb.getConnectedEndpoints()
|
2016-08-10 20:44:33 -04:00
|
|
|
|
|
|
|
// In swarm mode services with exposed ports are connected to user overlay
|
|
|
|
// network, ingress network and docker_gwbridge network. Name resolution
|
|
|
|
// should prioritize returning the VIP/IPs on user overlay network.
|
|
|
|
newList := []*endpoint{}
|
|
|
|
if !sb.controller.isDistributedControl() {
|
|
|
|
newList = append(newList, getDynamicNwEndpoints(epList)...)
|
2016-08-11 20:51:13 -04:00
|
|
|
ingressEP := getIngressNwEndpoint(epList)
|
|
|
|
if ingressEP != nil {
|
|
|
|
newList = append(newList, ingressEP)
|
|
|
|
}
|
2016-08-10 20:44:33 -04:00
|
|
|
newList = append(newList, getLocalNwEndpoints(epList)...)
|
|
|
|
epList = newList
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:40:46 -05:00
|
|
|
for i := 0; i < len(reqName); i++ {
|
2016-01-04 17:02:03 -05:00
|
|
|
|
2016-01-19 03:40:46 -05:00
|
|
|
// First check for local container alias
|
2016-03-19 06:07:08 -04:00
|
|
|
ip, ipv6Miss := sb.resolveName(reqName[i], networkName[i], epList, true, ipType)
|
2016-01-19 03:40:46 -05:00
|
|
|
if ip != nil {
|
2016-03-19 06:07:08 -04:00
|
|
|
return ip, false
|
|
|
|
}
|
|
|
|
if ipv6Miss {
|
|
|
|
return ip, ipv6Miss
|
2016-01-19 03:40:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the actual container name
|
2016-03-19 06:07:08 -04:00
|
|
|
ip, ipv6Miss = sb.resolveName(reqName[i], networkName[i], epList, false, ipType)
|
2016-01-19 03:40:46 -05:00
|
|
|
if ip != nil {
|
2016-03-19 06:07:08 -04:00
|
|
|
return ip, false
|
|
|
|
}
|
|
|
|
if ipv6Miss {
|
|
|
|
return ip, ipv6Miss
|
2016-01-19 03:40:46 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:07:08 -04:00
|
|
|
return nil, false
|
2016-01-04 17:02:03 -05:00
|
|
|
}
|
|
|
|
|
2016-03-19 06:07:08 -04:00
|
|
|
func (sb *sandbox) resolveName(req string, networkName string, epList []*endpoint, alias bool, ipType int) ([]net.IP, bool) {
|
|
|
|
var ipv6Miss bool
|
|
|
|
|
2016-01-04 17:02:03 -05:00
|
|
|
for _, ep := range epList {
|
|
|
|
name := req
|
2015-12-24 04:51:32 -05:00
|
|
|
n := ep.getNetwork()
|
|
|
|
|
2016-01-04 17:02:03 -05:00
|
|
|
if networkName != "" && networkName != n.Name() {
|
2015-12-24 04:51:32 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-04 17:02:03 -05:00
|
|
|
if alias {
|
|
|
|
if ep.aliases == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
ep.Lock()
|
|
|
|
name, ok = ep.aliases[req]
|
|
|
|
ep.Unlock()
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If it is a regular lookup and if the requested name is an alias
|
2016-02-23 16:44:38 -05:00
|
|
|
// don't perform a svc lookup for this endpoint.
|
2016-01-04 17:02:03 -05:00
|
|
|
ep.Lock()
|
|
|
|
if _, ok := ep.aliases[req]; ok {
|
|
|
|
ep.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ep.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
ip, miss := n.ResolveName(name, ipType)
|
2016-07-20 10:28:32 -04:00
|
|
|
|
2016-03-19 06:07:08 -04:00
|
|
|
if ip != nil {
|
|
|
|
return ip, false
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
2016-09-19 18:48:06 -04:00
|
|
|
|
|
|
|
if miss {
|
|
|
|
ipv6Miss = miss
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
2016-03-19 06:07:08 -04:00
|
|
|
return nil, ipv6Miss
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
func (sb *sandbox) SetKey(basePath string) error {
|
2016-03-28 21:02:09 -04:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2017-07-06 12:42:38 -04:00
|
|
|
logrus.Debugf("sandbox set key processing took %s for container %s", time.Since(start), sb.ContainerID())
|
2016-03-28 21:02:09 -04:00
|
|
|
}()
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
if basePath == "" {
|
|
|
|
return types.BadRequestErrorf("invalid sandbox key")
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.Lock()
|
2017-05-25 03:17:34 -04:00
|
|
|
if sb.inDelete {
|
|
|
|
sb.Unlock()
|
|
|
|
return types.ForbiddenErrorf("failed to SetKey: sandbox %q delete in progress", sb.id)
|
|
|
|
}
|
2016-01-14 12:56:32 -05:00
|
|
|
oldosSbox := sb.osSbox
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Unlock()
|
2015-10-09 16:43:25 -04:00
|
|
|
|
2016-01-14 12:56:32 -05:00
|
|
|
if oldosSbox != nil {
|
2015-10-09 16:43:25 -04:00
|
|
|
// If we already have an OS sandbox, release the network resources from that
|
|
|
|
// and destroy the OS snab. We are moving into a new home further down. Note that none
|
|
|
|
// of the network resources gets destroyed during the move.
|
|
|
|
sb.releaseOSSbox()
|
|
|
|
}
|
|
|
|
|
2016-01-14 12:56:32 -05:00
|
|
|
osSbox, err := osl.GetSandboxForExternalKey(basePath, sb.Key())
|
2015-09-09 19:20:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-09 16:43:25 -04:00
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Lock()
|
|
|
|
sb.osSbox = osSbox
|
|
|
|
sb.Unlock()
|
|
|
|
|
2016-01-14 12:56:32 -05:00
|
|
|
// If the resolver was setup before stop it and set it up in the
|
|
|
|
// new osl sandbox.
|
|
|
|
if oldosSbox != nil && sb.resolver != nil {
|
|
|
|
sb.resolver.Stop()
|
|
|
|
|
2016-09-21 16:38:29 -04:00
|
|
|
if err := sb.osSbox.InvokeFunc(sb.resolver.SetupFunc(0)); err == nil {
|
|
|
|
if err := sb.resolver.Start(); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Resolver Start failed for container %s, %q", sb.ContainerID(), err)
|
2016-09-21 16:38:29 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Resolver Setup Function failed for container %s, %q", sb.ContainerID(), err)
|
2016-01-14 12:56:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if err = sb.populateNetworkResources(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:32:49 -05:00
|
|
|
func (sb *sandbox) EnableService() (err error) {
|
2017-06-06 19:04:50 -04:00
|
|
|
logrus.Debugf("EnableService %s START", sb.containerID)
|
2018-01-08 17:32:49 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
sb.DisableService()
|
|
|
|
}
|
|
|
|
}()
|
2016-08-21 01:55:00 -04:00
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
2018-01-08 17:32:49 -05:00
|
|
|
if !ep.isServiceEnabled() {
|
2017-06-06 19:04:50 -04:00
|
|
|
if err := ep.addServiceInfoToCluster(sb); err != nil {
|
2016-08-21 01:55:00 -04:00
|
|
|
return fmt.Errorf("could not update state for endpoint %s into cluster: %v", ep.Name(), err)
|
|
|
|
}
|
2018-01-08 17:32:49 -05:00
|
|
|
ep.enableService()
|
2016-08-21 01:55:00 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 19:04:50 -04:00
|
|
|
logrus.Debugf("EnableService %s DONE", sb.containerID)
|
2016-08-21 01:55:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:32:49 -05:00
|
|
|
func (sb *sandbox) DisableService() (err error) {
|
2017-06-06 19:04:50 -04:00
|
|
|
logrus.Debugf("DisableService %s START", sb.containerID)
|
2018-01-08 17:32:49 -05:00
|
|
|
failedEps := []string{}
|
|
|
|
defer func() {
|
|
|
|
if len(failedEps) > 0 {
|
|
|
|
err = fmt.Errorf("failed to disable service on sandbox:%s, for endpoints %s", sb.ID(), strings.Join(failedEps, ","))
|
|
|
|
}
|
|
|
|
}()
|
2016-08-21 01:55:00 -04:00
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
2018-01-08 17:32:49 -05:00
|
|
|
if ep.isServiceEnabled() {
|
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, false, "DisableService"); err != nil {
|
2018-01-08 17:32:49 -05:00
|
|
|
failedEps = append(failedEps, ep.Name())
|
|
|
|
logrus.Warnf("failed update state for endpoint %s into cluster: %v", ep.Name(), err)
|
|
|
|
}
|
|
|
|
ep.disableService()
|
|
|
|
}
|
2016-08-21 01:55:00 -04:00
|
|
|
}
|
2017-06-06 19:04:50 -04:00
|
|
|
logrus.Debugf("DisableService %s DONE", sb.containerID)
|
2016-08-21 01:55:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-09 16:43:25 -04:00
|
|
|
func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {
|
|
|
|
for _, i := range osSbox.Info().Interfaces() {
|
|
|
|
// Only remove the interfaces owned by this endpoint from the sandbox.
|
|
|
|
if ep.hasInterface(i.SrcName()) {
|
|
|
|
if err := i.Remove(); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Remove interface %s failed: %v", i.SrcName(), err)
|
2015-10-09 16:43:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.Lock()
|
|
|
|
joinInfo := ep.joinInfo
|
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-16 21:00:30 -04:00
|
|
|
if joinInfo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-09 16:43:25 -04:00
|
|
|
// Remove non-interface routes.
|
|
|
|
for _, r := range joinInfo.StaticRoutes {
|
|
|
|
if err := osSbox.RemoveStaticRoute(r); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Remove route failed: %v", err)
|
2015-10-09 16:43:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) releaseOSSbox() {
|
|
|
|
sb.Lock()
|
|
|
|
osSbox := sb.osSbox
|
|
|
|
sb.osSbox = nil
|
|
|
|
sb.Unlock()
|
|
|
|
|
|
|
|
if osSbox == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
releaseOSSboxResources(osSbox, ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
osSbox.Destroy()
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:32:19 -04:00
|
|
|
func (sb *sandbox) restoreOslSandbox() error {
|
|
|
|
var routes []*types.StaticRoute
|
|
|
|
|
|
|
|
// restore osl sandbox
|
|
|
|
Ifaces := make(map[string][]osl.IfaceOption)
|
|
|
|
for _, ep := range sb.endpoints {
|
|
|
|
var ifaceOptions []osl.IfaceOption
|
|
|
|
ep.Lock()
|
|
|
|
joinInfo := ep.joinInfo
|
|
|
|
i := ep.iface
|
|
|
|
ep.Unlock()
|
2016-07-27 03:55:46 -04:00
|
|
|
|
|
|
|
if i == nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("error restoring endpoint %s for container %s", ep.Name(), sb.ContainerID())
|
2016-07-27 03:55:46 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:32:19 -04:00
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
|
|
|
|
if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
|
|
|
|
}
|
|
|
|
if i.mac != nil {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().MacAddress(i.mac))
|
|
|
|
}
|
|
|
|
if len(i.llAddrs) != 0 {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
|
|
|
|
}
|
|
|
|
Ifaces[fmt.Sprintf("%s+%s", i.srcName, i.dstPrefix)] = ifaceOptions
|
|
|
|
if joinInfo != nil {
|
2017-07-06 12:42:38 -04:00
|
|
|
routes = append(routes, joinInfo.StaticRoutes...)
|
2016-06-10 20:32:19 -04:00
|
|
|
}
|
|
|
|
if ep.needResolver() {
|
2016-06-09 19:05:11 -04:00
|
|
|
sb.startResolver(true)
|
2016-06-10 20:32:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gwep := sb.getGatewayEndpoint()
|
|
|
|
if gwep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore osl sandbox
|
|
|
|
err := sb.osSbox.Restore(Ifaces, routes, gwep.joinInfo.gw, gwep.joinInfo.gw6)
|
2017-07-06 12:42:38 -04:00
|
|
|
return err
|
2016-06-10 20:32:19 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Lock()
|
|
|
|
if sb.osSbox == nil {
|
|
|
|
sb.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-16 21:00:30 -04:00
|
|
|
inDelete := sb.inDelete
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Unlock()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.Lock()
|
|
|
|
joinInfo := ep.joinInfo
|
2015-09-09 19:06:35 -04:00
|
|
|
i := ep.iface
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
if ep.needResolver() {
|
2016-06-09 19:05:11 -04:00
|
|
|
sb.startResolver(false)
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
2015-11-02 20:54:22 -05:00
|
|
|
if i != nil && i.srcName != "" {
|
2015-07-02 01:00:48 -04:00
|
|
|
var ifaceOptions []osl.IfaceOption
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
|
|
|
|
if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2016-05-24 23:04:49 -04:00
|
|
|
if len(i.llAddrs) != 0 {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
|
|
|
|
}
|
2015-10-29 03:04:08 -04:00
|
|
|
if i.mac != nil {
|
|
|
|
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().MacAddress(i.mac))
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
if err := sb.osSbox.AddInterface(i.srcName, i.dstPrefix, ifaceOptions...); err != nil {
|
|
|
|
return fmt.Errorf("failed to add interface %s to sandbox: %v", i.srcName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if joinInfo != nil {
|
|
|
|
// Set up non-interface routes.
|
|
|
|
for _, r := range joinInfo.StaticRoutes {
|
|
|
|
if err := sb.osSbox.AddStaticRoute(r); err != nil {
|
|
|
|
return fmt.Errorf("failed to add static route %s: %v", r.Destination.String(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
if ep == sb.getGatewayEndpoint() {
|
|
|
|
if err := sb.updateGateway(ep); err != nil {
|
|
|
|
return err
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-16 21:00:30 -04:00
|
|
|
|
2016-06-15 17:00:48 -04:00
|
|
|
// Make sure to add the endpoint to the populated endpoint set
|
|
|
|
// before populating loadbalancers.
|
|
|
|
sb.Lock()
|
|
|
|
sb.populatedEndpoints[ep.ID()] = struct{}{}
|
|
|
|
sb.Unlock()
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
// Populate load balancer only after updating all the other
|
|
|
|
// information including gateway and other routes so that
|
|
|
|
// loadbalancers are populated all the network state is in
|
|
|
|
// place in the sandbox.
|
2018-04-10 12:34:41 -04:00
|
|
|
sb.populateLoadBalancers(ep)
|
2016-05-25 01:46:18 -04:00
|
|
|
|
2015-10-16 21:00:30 -04:00
|
|
|
// Only update the store if we did not come here as part of
|
|
|
|
// sandbox delete. If we came here as part of delete then do
|
|
|
|
// not bother updating the store. The sandbox object will be
|
|
|
|
// deleted anyway
|
|
|
|
if !inDelete {
|
|
|
|
return sb.storeUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (sb *sandbox) clearNetworkResources(origEp *endpoint) error {
|
|
|
|
ep := sb.getEndpoint(origEp.id)
|
|
|
|
if ep == nil {
|
|
|
|
return fmt.Errorf("could not find the sandbox endpoint data for endpoint %s",
|
2016-03-03 11:23:10 -05:00
|
|
|
origEp.id)
|
2015-10-05 07:21:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Lock()
|
|
|
|
osSbox := sb.osSbox
|
2015-10-16 21:00:30 -04:00
|
|
|
inDelete := sb.inDelete
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Unlock()
|
|
|
|
if osSbox != nil {
|
2015-10-09 16:43:25 -04:00
|
|
|
releaseOSSboxResources(osSbox, ep)
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sb.Lock()
|
2016-10-18 13:20:05 -04:00
|
|
|
delete(sb.populatedEndpoints, ep.ID())
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if len(sb.endpoints) == 0 {
|
|
|
|
// sb.endpoints should never be empty and this is unexpected error condition
|
|
|
|
// We log an error message to note this down for debugging purposes.
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("No endpoints in sandbox while trying to remove endpoint %s", ep.Name())
|
2015-07-02 01:00:48 -04:00
|
|
|
sb.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-09-18 20:33:55 -04:00
|
|
|
gwepBefore, gwepAfter *endpoint
|
|
|
|
index = -1
|
2015-07-02 01:00:48 -04:00
|
|
|
)
|
2015-09-18 20:33:55 -04:00
|
|
|
for i, e := range sb.endpoints {
|
2015-07-02 01:00:48 -04:00
|
|
|
if e == ep {
|
2015-09-18 20:33:55 -04:00
|
|
|
index = i
|
|
|
|
}
|
|
|
|
if len(e.Gateway()) > 0 && gwepBefore == nil {
|
|
|
|
gwepBefore = e
|
|
|
|
}
|
|
|
|
if index != -1 && gwepBefore != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 17:33:27 -04:00
|
|
|
|
|
|
|
if index == -1 {
|
2017-09-07 13:36:11 -04:00
|
|
|
logrus.Warnf("Endpoint %s has already been deleted", ep.Name())
|
2017-09-05 17:33:27 -04:00
|
|
|
sb.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:15:31 -05:00
|
|
|
sb.removeEndpointRaw(ep)
|
2015-09-18 20:33:55 -04:00
|
|
|
for _, e := range sb.endpoints {
|
|
|
|
if len(e.Gateway()) > 0 {
|
|
|
|
gwepAfter = e
|
|
|
|
break
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
delete(sb.epPriority, ep.ID())
|
|
|
|
sb.Unlock()
|
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
if gwepAfter != nil && gwepBefore != gwepAfter {
|
|
|
|
sb.updateGateway(gwepAfter)
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 21:00:30 -04:00
|
|
|
// Only update the store if we did not come here as part of
|
|
|
|
// sandbox delete. If we came here as part of delete then do
|
|
|
|
// not bother updating the store. The sandbox object will be
|
|
|
|
// deleted anyway
|
|
|
|
if !inDelete {
|
|
|
|
return sb.storeUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 17:00:48 -04:00
|
|
|
func (sb *sandbox) isEndpointPopulated(ep *endpoint) bool {
|
|
|
|
sb.Lock()
|
|
|
|
_, ok := sb.populatedEndpoints[ep.ID()]
|
|
|
|
sb.Unlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
// joinLeaveStart waits to ensure there are no joins or leaves in progress and
|
|
|
|
// marks this join/leave in progress without race
|
|
|
|
func (sb *sandbox) joinLeaveStart() {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
for sb.joinLeaveDone != nil {
|
|
|
|
joinLeaveDone := sb.joinLeaveDone
|
|
|
|
sb.Unlock()
|
|
|
|
|
2017-07-06 12:42:38 -04:00
|
|
|
<-joinLeaveDone
|
2015-09-18 20:33:55 -04:00
|
|
|
|
|
|
|
sb.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.joinLeaveDone = make(chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// joinLeaveEnd marks the end of this join/leave operation and
|
|
|
|
// signals the same without race to other join and leave waiters
|
|
|
|
func (sb *sandbox) joinLeaveEnd() {
|
|
|
|
sb.Lock()
|
|
|
|
defer sb.Unlock()
|
|
|
|
|
|
|
|
if sb.joinLeaveDone != nil {
|
|
|
|
close(sb.joinLeaveDone)
|
|
|
|
sb.joinLeaveDone = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
func (sb *sandbox) hasPortConfigs() bool {
|
|
|
|
opts := sb.Labels()
|
|
|
|
_, hasExpPorts := opts[netlabel.ExposedPorts]
|
|
|
|
_, hasPortMaps := opts[netlabel.PortMap]
|
|
|
|
return hasExpPorts || hasPortMaps
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// OptionHostname function returns an option setter for hostname option to
|
|
|
|
// be passed to NewSandbox method.
|
|
|
|
func OptionHostname(name string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.hostName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionDomainname function returns an option setter for domainname option to
|
|
|
|
// be passed to NewSandbox method.
|
|
|
|
func OptionDomainname(name string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.domainName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionHostsPath function returns an option setter for hostspath option to
|
|
|
|
// be passed to NewSandbox method.
|
|
|
|
func OptionHostsPath(path string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.hostsPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionOriginHostsPath function returns an option setter for origin hosts file path
|
2016-05-22 22:55:17 -04:00
|
|
|
// to be passed to NewSandbox method.
|
2015-07-02 01:00:48 -04:00
|
|
|
func OptionOriginHostsPath(path string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.originHostsPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionExtraHost function returns an option setter for extra /etc/hosts options
|
|
|
|
// which is a name and IP as strings.
|
|
|
|
func OptionExtraHost(name string, IP string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.extraHosts = append(sb.config.extraHosts, extraHost{name: name, IP: IP})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionParentUpdate function returns an option setter for parent container
|
|
|
|
// which needs to update the IP address for the linked container.
|
|
|
|
func OptionParentUpdate(cid string, name, ip string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.parentUpdates = append(sb.config.parentUpdates, parentUpdate{cid: cid, name: name, ip: ip})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionResolvConfPath function returns an option setter for resolvconfpath option to
|
|
|
|
// be passed to net container methods.
|
|
|
|
func OptionResolvConfPath(path string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.resolvConfPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionOriginResolvConfPath function returns an option setter to set the path to the
|
|
|
|
// origin resolv.conf file to be passed to net container methods.
|
|
|
|
func OptionOriginResolvConfPath(path string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.originResolvConfPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionDNS function returns an option setter for dns entry option to
|
|
|
|
// be passed to container Create method.
|
|
|
|
func OptionDNS(dns string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.dnsList = append(sb.config.dnsList, dns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionDNSSearch function returns an option setter for dns search entry option to
|
|
|
|
// be passed to container Create method.
|
|
|
|
func OptionDNSSearch(search string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.dnsSearchList = append(sb.config.dnsSearchList, search)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 20:31:31 -04:00
|
|
|
// OptionDNSOptions function returns an option setter for dns options entry option to
|
|
|
|
// be passed to container Create method.
|
|
|
|
func OptionDNSOptions(options string) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.dnsOptionsList = append(sb.config.dnsOptionsList, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// OptionUseDefaultSandbox function returns an option setter for using default sandbox to
|
|
|
|
// be passed to container Create method.
|
|
|
|
func OptionUseDefaultSandbox() SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.useDefaultSandBox = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
// OptionUseExternalKey function returns an option setter for using provided namespace
|
|
|
|
// instead of creating one.
|
|
|
|
func OptionUseExternalKey() SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.config.useExternalKey = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// OptionGeneric function returns an option setter for Generic configuration
|
|
|
|
// that is not managed by libNetwork but can be used by the Drivers during the call to
|
|
|
|
// net container creation method. Container Labels are a good example.
|
|
|
|
func OptionGeneric(generic map[string]interface{}) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
2015-12-07 17:45:51 -05:00
|
|
|
if sb.config.generic == nil {
|
|
|
|
sb.config.generic = make(map[string]interface{}, len(generic))
|
|
|
|
}
|
|
|
|
for k, v := range generic {
|
|
|
|
sb.config.generic[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionExposedPorts function returns an option setter for the container exposed
|
|
|
|
// ports option to be passed to container Create method.
|
|
|
|
func OptionExposedPorts(exposedPorts []types.TransportPort) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
if sb.config.generic == nil {
|
|
|
|
sb.config.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
// Defensive copy
|
|
|
|
eps := make([]types.TransportPort, len(exposedPorts))
|
|
|
|
copy(eps, exposedPorts)
|
|
|
|
// Store endpoint label and in generic because driver needs it
|
|
|
|
sb.config.exposedPorts = eps
|
|
|
|
sb.config.generic[netlabel.ExposedPorts] = eps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionPortMapping function returns an option setter for the mapping
|
|
|
|
// ports option to be passed to container Create method.
|
|
|
|
func OptionPortMapping(portBindings []types.PortBinding) SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
if sb.config.generic == nil {
|
|
|
|
sb.config.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
// Store a copy of the bindings as generic data to pass to the driver
|
|
|
|
pbs := make([]types.PortBinding, len(portBindings))
|
|
|
|
copy(pbs, portBindings)
|
|
|
|
sb.config.generic[netlabel.PortMap] = pbs
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
// OptionIngress function returns an option setter for marking a
|
|
|
|
// sandbox as the controller's ingress sandbox.
|
|
|
|
func OptionIngress() SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.ingress = true
|
2018-05-18 17:10:14 -04:00
|
|
|
sb.oslTypes = append(sb.oslTypes, osl.SandboxTypeIngress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionLoadBalancer function returns an option setter for marking a
|
|
|
|
// sandbox as a load balancer sandbox.
|
|
|
|
func OptionLoadBalancer() SandboxOption {
|
|
|
|
return func(sb *sandbox) {
|
|
|
|
sb.oslTypes = append(sb.oslTypes, osl.SandboxTypeLoadBalancer)
|
2016-05-31 02:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
// <=> Returns true if a < b, false if a > b and advances to next level if a == b
|
|
|
|
// epi.prio <=> epj.prio # 2 < 1
|
|
|
|
// epi.gw <=> epj.gw # non-gw < gw
|
|
|
|
// epi.internal <=> epj.internal # non-internal < internal
|
|
|
|
// epi.joininfo <=> epj.joininfo # ipv6 < ipv4
|
|
|
|
// epi.name <=> epj.name # bar < foo
|
2018-02-27 11:15:31 -05:00
|
|
|
func (epi *endpoint) Less(epj *endpoint) bool {
|
2015-11-02 20:54:22 -05:00
|
|
|
var (
|
2018-03-28 10:15:55 -04:00
|
|
|
prioi, prioj int
|
2015-11-02 20:54:22 -05:00
|
|
|
)
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
sbi, _ := epi.getSandbox()
|
|
|
|
sbj, _ := epj.getSandbox()
|
2015-09-06 21:34:50 -04:00
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
// Prio defaults to 0
|
|
|
|
if sbi != nil {
|
|
|
|
prioi = sbi.epPriority[epi.ID()]
|
|
|
|
}
|
|
|
|
if sbj != nil {
|
|
|
|
prioj = sbj.epPriority[epj.ID()]
|
2015-09-06 21:34:50 -04:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
if prioi != prioj {
|
|
|
|
return prioi > prioj
|
2015-09-06 21:34:50 -04:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
gwi := epi.endpointInGWNetwork()
|
|
|
|
gwj := epj.endpointInGWNetwork()
|
|
|
|
if gwi != gwj {
|
|
|
|
return gwj
|
2016-04-28 15:04:22 -04:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
inti := epi.getNetwork().Internal()
|
|
|
|
intj := epj.getNetwork().Internal()
|
|
|
|
if inti != intj {
|
|
|
|
return intj
|
2016-04-28 15:04:22 -04:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
jii := 0
|
|
|
|
if epi.joinInfo != nil {
|
|
|
|
if epi.joinInfo.gw != nil {
|
|
|
|
jii = jii + 1
|
2017-01-03 14:39:04 -05:00
|
|
|
}
|
2018-03-28 10:15:55 -04:00
|
|
|
if epi.joinInfo.gw6 != nil {
|
|
|
|
jii = jii + 2
|
2017-01-03 14:39:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
jij := 0
|
|
|
|
if epj.joinInfo != nil {
|
|
|
|
if epj.joinInfo.gw != nil {
|
|
|
|
jij = jij + 1
|
2015-11-02 20:54:22 -05:00
|
|
|
}
|
2018-03-28 10:15:55 -04:00
|
|
|
if epj.joinInfo.gw6 != nil {
|
|
|
|
jij = jij + 2
|
2015-11-02 20:54:22 -05:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2015-11-02 20:54:22 -05:00
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
if jii != jij {
|
|
|
|
return jii > jij
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:15:55 -04:00
|
|
|
return epi.network.Name() < epj.network.Name()
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2016-09-19 18:48:06 -04:00
|
|
|
|
|
|
|
func (sb *sandbox) NdotsSet() bool {
|
|
|
|
return sb.ndotsSet
|
|
|
|
}
|