2015-10-08 03:01:38 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-10-30 00:16:52 +00:00
|
|
|
"sync"
|
2015-10-08 03:01:38 +00:00
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
2017-07-26 21:18:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-10-08 03:01:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sandboxPrefix = "sandbox"
|
|
|
|
)
|
|
|
|
|
|
|
|
type epState struct {
|
|
|
|
Eid string
|
|
|
|
Nid string
|
|
|
|
}
|
|
|
|
|
|
|
|
type sbState struct {
|
2016-06-11 00:32:19 +00:00
|
|
|
ID string
|
|
|
|
Cid string
|
2023-01-11 22:43:32 +00:00
|
|
|
c *Controller
|
2016-06-11 00:32:19 +00:00
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
Eps []epState
|
|
|
|
EpPriority map[string]int
|
2017-01-19 22:25:26 +00:00
|
|
|
// external servers have to be persisted so that on restart of a live-restore
|
|
|
|
// enabled daemon we get the external servers for the running containers.
|
|
|
|
// We have two versions of ExtDNS to support upgrade & downgrade of the daemon
|
|
|
|
// between >=1.14 and <1.14 versions.
|
|
|
|
ExtDNS []string
|
|
|
|
ExtDNS2 []extDNSEntry
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) Key() []string {
|
|
|
|
return []string{sandboxPrefix, sbs.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) KeyPrefix() []string {
|
|
|
|
return []string{sandboxPrefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) Value() []byte {
|
|
|
|
b, err := json.Marshal(sbs)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, sbs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) Index() uint64 {
|
2023-01-12 01:10:09 +00:00
|
|
|
sb, err := sbs.c.SandboxByID(sbs.ID)
|
2015-10-08 03:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return sbs.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
maxIndex := sb.dbIndex
|
|
|
|
if sbs.dbIndex > maxIndex {
|
|
|
|
maxIndex = sbs.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) SetIndex(index uint64) {
|
|
|
|
sbs.dbIndex = index
|
|
|
|
sbs.dbExists = true
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb, err := sbs.c.SandboxByID(sbs.ID)
|
2015-10-08 03:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.dbIndex = index
|
|
|
|
sb.dbExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) Exists() bool {
|
|
|
|
if sbs.dbExists {
|
|
|
|
return sbs.dbExists
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb, err := sbs.c.SandboxByID(sbs.ID)
|
2015-10-08 03:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.dbExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) Skip() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) New() datastore.KVObject {
|
|
|
|
return &sbState{c: sbs.c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) CopyTo(o datastore.KVObject) error {
|
|
|
|
dstSbs := o.(*sbState)
|
|
|
|
dstSbs.c = sbs.c
|
|
|
|
dstSbs.ID = sbs.ID
|
|
|
|
dstSbs.Cid = sbs.Cid
|
|
|
|
dstSbs.dbIndex = sbs.dbIndex
|
|
|
|
dstSbs.dbExists = sbs.dbExists
|
2016-06-11 00:32:19 +00:00
|
|
|
dstSbs.EpPriority = sbs.EpPriority
|
2015-10-08 03:01:38 +00:00
|
|
|
|
2017-07-06 16:42:38 +00:00
|
|
|
dstSbs.Eps = append(dstSbs.Eps, sbs.Eps...)
|
2015-10-08 03:01:38 +00:00
|
|
|
|
2017-01-19 22:25:26 +00:00
|
|
|
if len(sbs.ExtDNS2) > 0 {
|
|
|
|
for _, dns := range sbs.ExtDNS2 {
|
|
|
|
dstSbs.ExtDNS2 = append(dstSbs.ExtDNS2, dns)
|
|
|
|
dstSbs.ExtDNS = append(dstSbs.ExtDNS, dns.IPStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-09 23:05:11 +00:00
|
|
|
for _, dns := range sbs.ExtDNS {
|
|
|
|
dstSbs.ExtDNS = append(dstSbs.ExtDNS, dns)
|
2017-01-19 22:25:26 +00:00
|
|
|
dstSbs.ExtDNS2 = append(dstSbs.ExtDNS2, extDNSEntry{IPStr: dns})
|
2016-06-09 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 03:01:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbs *sbState) DataScope() string {
|
|
|
|
return datastore.LocalScope
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) storeUpdate() error {
|
2015-10-08 03:01:38 +00:00
|
|
|
sbs := &sbState{
|
2016-06-11 00:32:19 +00:00
|
|
|
c: sb.controller,
|
|
|
|
ID: sb.id,
|
|
|
|
Cid: sb.containerID,
|
|
|
|
EpPriority: sb.epPriority,
|
2017-01-19 22:25:26 +00:00
|
|
|
ExtDNS2: sb.extDNS,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ext := range sb.extDNS {
|
|
|
|
sbs.ExtDNS = append(sbs.ExtDNS, ext.IPStr)
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 01:00:30 +00:00
|
|
|
retry:
|
|
|
|
sbs.Eps = nil
|
2023-01-12 01:42:24 +00:00
|
|
|
for _, ep := range sb.Endpoints() {
|
2015-11-02 16:09:49 +00:00
|
|
|
// If the endpoint is not persisted then do not add it to
|
|
|
|
// the sandbox checkpoint
|
|
|
|
if ep.Skip() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-08 03:01:38 +00:00
|
|
|
eps := epState{
|
|
|
|
Nid: ep.getNetwork().ID(),
|
|
|
|
Eid: ep.ID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
sbs.Eps = append(sbs.Eps, eps)
|
|
|
|
}
|
|
|
|
|
2015-10-17 01:00:30 +00:00
|
|
|
err := sb.controller.updateToStore(sbs)
|
|
|
|
if err == datastore.ErrKeyModified {
|
|
|
|
// When we get ErrKeyModified it is sufficient to just
|
|
|
|
// go back and retry. No need to get the object from
|
|
|
|
// the store because we always regenerate the store
|
|
|
|
// state from in memory sandbox state
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) storeDelete() error {
|
2015-10-08 03:01:38 +00:00
|
|
|
sbs := &sbState{
|
|
|
|
c: sb.controller,
|
|
|
|
ID: sb.id,
|
|
|
|
Cid: sb.containerID,
|
|
|
|
dbIndex: sb.dbIndex,
|
|
|
|
dbExists: sb.dbExists,
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.controller.deleteFromStore(sbs)
|
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
|
2023-01-14 00:04:43 +00:00
|
|
|
store := c.getStore()
|
2015-10-08 03:01:38 +00:00
|
|
|
if store == nil {
|
2016-11-01 04:26:14 +00:00
|
|
|
logrus.Error("Could not find local scope store while trying to cleanup sandboxes")
|
2015-10-08 03:01:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kvol, err := store.List(datastore.Key(sandboxPrefix), &sbState{c: c})
|
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
logrus.Errorf("failed to get sandboxes for scope %s: %v", store.Scope(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's normal for no sandboxes to be found. Just bail out.
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kvo := range kvol {
|
|
|
|
sbs := kvo.(*sbState)
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb := &Sandbox{
|
2016-06-15 21:00:48 +00:00
|
|
|
id: sbs.ID,
|
|
|
|
controller: sbs.c,
|
|
|
|
containerID: sbs.Cid,
|
2023-01-12 01:42:24 +00:00
|
|
|
endpoints: []*Endpoint{},
|
2016-06-15 21:00:48 +00:00
|
|
|
populatedEndpoints: map[string]struct{}{},
|
|
|
|
dbIndex: sbs.dbIndex,
|
|
|
|
isStub: true,
|
|
|
|
dbExists: true,
|
2017-01-19 22:25:26 +00:00
|
|
|
}
|
|
|
|
// If we are restoring from a older version extDNSEntry won't have the
|
|
|
|
// HostLoopback field
|
|
|
|
if len(sbs.ExtDNS2) > 0 {
|
|
|
|
sb.extDNS = sbs.ExtDNS2
|
|
|
|
} else {
|
|
|
|
for _, dns := range sbs.ExtDNS {
|
|
|
|
sb.extDNS = append(sb.extDNS, extDNSEntry{IPStr: dns})
|
|
|
|
}
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 00:32:19 +00:00
|
|
|
msg := " for cleanup"
|
|
|
|
create := true
|
|
|
|
isRestore := false
|
|
|
|
if val, ok := activeSandboxes[sb.ID()]; ok {
|
|
|
|
msg = ""
|
|
|
|
sb.isStub = false
|
|
|
|
isRestore = true
|
|
|
|
opts := val.([]SandboxOption)
|
|
|
|
sb.processOptions(opts...)
|
|
|
|
sb.restorePath()
|
|
|
|
create = !sb.config.useDefaultSandBox
|
|
|
|
}
|
|
|
|
sb.osSbox, err = osl.NewSandbox(sb.Key(), create, isRestore)
|
2015-10-08 03:01:38 +00:00
|
|
|
if err != nil {
|
2018-07-05 20:33:01 +00:00
|
|
|
logrus.Errorf("failed to create osl sandbox while trying to restore sandbox %.7s%s: %v", sb.ID(), msg, err)
|
2015-10-08 03:01:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
2015-10-30 19:17:54 +00:00
|
|
|
c.sandboxes[sb.id] = sb
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Unlock()
|
2015-10-30 19:17:54 +00:00
|
|
|
|
2015-10-08 03:01:38 +00:00
|
|
|
for _, eps := range sbs.Eps {
|
|
|
|
n, err := c.getNetworkFromStore(eps.Nid)
|
2023-01-12 01:42:24 +00:00
|
|
|
var ep *Endpoint
|
2015-10-08 03:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("getNetworkFromStore for nid %s failed while trying to build sandbox for cleanup: %v", eps.Nid, err)
|
2016-05-07 01:38:56 +00:00
|
|
|
n = &network{id: eps.Nid, ctrlr: c, drvOnce: &sync.Once{}, persist: true}
|
2023-01-12 01:42:24 +00:00
|
|
|
ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
|
2015-10-30 00:16:52 +00:00
|
|
|
} else {
|
|
|
|
ep, err = n.getEndpointFromStore(eps.Eid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("getEndpointFromStore for eid %s failed while trying to build sandbox for cleanup: %v", eps.Eid, err)
|
2023-01-12 01:42:24 +00:00
|
|
|
ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
|
2015-10-30 00:16:52 +00:00
|
|
|
}
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
2016-07-27 07:55:46 +00:00
|
|
|
if _, ok := activeSandboxes[sb.ID()]; ok && err != nil {
|
|
|
|
logrus.Errorf("failed to restore endpoint %s in %s for container %s due to %v", eps.Eid, eps.Nid, sb.ContainerID(), err)
|
|
|
|
continue
|
|
|
|
}
|
2018-02-27 16:15:31 +00:00
|
|
|
sb.addEndpoint(ep)
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 00:32:19 +00:00
|
|
|
if _, ok := activeSandboxes[sb.ID()]; !ok {
|
|
|
|
logrus.Infof("Removing stale sandbox %s (%s)", sb.id, sb.containerID)
|
|
|
|
if err := sb.delete(true); err != nil {
|
|
|
|
logrus.Errorf("Failed to delete sandbox %s while trying to cleanup: %v", sb.id, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct osl sandbox field
|
|
|
|
if !sb.config.useDefaultSandBox {
|
|
|
|
if err := sb.restoreOslSandbox(); err != nil {
|
|
|
|
logrus.Errorf("failed to populate fields for osl sandbox %s", sb.ID())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.sboxOnce.Do(func() {
|
|
|
|
c.defOsSbox = sb.osSbox
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range sb.endpoints {
|
|
|
|
// Watch for service records
|
|
|
|
if !c.isAgent() {
|
|
|
|
c.watchSvcRecord(ep)
|
|
|
|
}
|
2015-10-08 03:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|