Browse code

Vendoring libnetwork

Vendoring libnetwork @ 05a5a1510f85977f374a9b9804a116391bab5089

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
(cherry picked from commit 10e1b9f02ee633349bc45631dac18cb4cc5baa61)

Jana Radhakrishnan authored on 2015/11/02 03:32:37
Showing 5 changed files
... ...
@@ -909,17 +909,16 @@ func createNetwork(controller libnetwork.NetworkController, dnet string, driver
909 909
 }
910 910
 
911 911
 func (container *Container) allocateNetwork() error {
912
-	sb := container.getNetworkSandbox()
913
-	if sb != nil {
914
-		// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
915
-		if err := sb.Delete(); err != nil {
916
-			logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
917
-		}
912
+	controller := container.daemon.netController
913
+
914
+	// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
915
+	if err := controller.SandboxDestroy(container.ID); err != nil {
916
+		logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
918 917
 	}
918
+
919 919
 	updateSettings := false
920 920
 	if len(container.NetworkSettings.Networks) == 0 {
921 921
 		mode := container.hostConfig.NetworkMode
922
-		controller := container.daemon.netController
923 922
 		if container.Config.NetworkDisabled || mode.IsContainer() {
924 923
 			return nil
925 924
 		}
... ...
@@ -20,7 +20,7 @@ clone git github.com/tchap/go-patricia v2.1.0
20 20
 clone git golang.org/x/net 3cffabab72adf04f8e3b01c5baf775361837b5fe https://github.com/golang/net.git
21 21
 
22 22
 #get libnetwork packages
23
-clone git github.com/docker/libnetwork 47edb73dd3e64cfcc04234b073872205cd79694a
23
+clone git github.com/docker/libnetwork e7719596c01a83f9ef24d33e9d609a64acacd7b8
24 24
 clone git github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec
25 25
 clone git github.com/hashicorp/go-msgpack 71c2886f5a673a35f909803f38ece5810165097b
26 26
 clone git github.com/hashicorp/memberlist 9a1e242e454d2443df330bdd51a436d5a9058fc4
... ...
@@ -100,6 +100,9 @@ type NetworkController interface {
100 100
 	// SandboxByID returns the Sandbox which has the passed id. If not found, a types.NotFoundError is returned.
101 101
 	SandboxByID(id string) (Sandbox, error)
102 102
 
103
+	// SandboxDestroy destroys a sandbox given a container ID
104
+	SandboxDestroy(id string) error
105
+
103 106
 	// Stop network controller
104 107
 	Stop()
105 108
 }
... ...
@@ -144,6 +147,8 @@ type controller struct {
144 144
 	unWatchCh      chan *endpoint
145 145
 	svcDb          map[string]svcMap
146 146
 	nmap           map[string]*netWatch
147
+	defOsSbox      osl.Sandbox
148
+	sboxOnce       sync.Once
147 149
 	sync.Mutex
148 150
 }
149 151
 
... ...
@@ -476,27 +481,37 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
476 476
 		return nil, types.BadRequestErrorf("invalid container ID")
477 477
 	}
478 478
 
479
-	var existing Sandbox
480
-	look := SandboxContainerWalker(&existing, containerID)
481
-	c.WalkSandboxes(look)
482
-	if existing != nil {
483
-		return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, existing)
479
+	var sb *sandbox
480
+	c.Lock()
481
+	for _, s := range c.sandboxes {
482
+		if s.containerID == containerID {
483
+			// If not a stub, then we already have a complete sandbox.
484
+			if !s.isStub {
485
+				c.Unlock()
486
+				return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, s)
487
+			}
488
+
489
+			// We already have a stub sandbox from the
490
+			// store. Make use of it so that we don't lose
491
+			// the endpoints from store but reset the
492
+			// isStub flag.
493
+			sb = s
494
+			sb.isStub = false
495
+			break
496
+		}
484 497
 	}
498
+	c.Unlock()
485 499
 
486 500
 	// Create sandbox and process options first. Key generation depends on an option
487
-	sb := &sandbox{
488
-		id:          stringid.GenerateRandomID(),
489
-		containerID: containerID,
490
-		endpoints:   epHeap{},
491
-		epPriority:  map[string]int{},
492
-		config:      containerConfig{},
493
-		controller:  c,
494
-	}
495
-	// This sandbox may be using an existing osl sandbox, sharing it with another sandbox
496
-	var peerSb Sandbox
497
-	c.WalkSandboxes(SandboxKeyWalker(&peerSb, sb.Key()))
498
-	if peerSb != nil {
499
-		sb.osSbox = peerSb.(*sandbox).osSbox
501
+	if sb == nil {
502
+		sb = &sandbox{
503
+			id:          stringid.GenerateRandomID(),
504
+			containerID: containerID,
505
+			endpoints:   epHeap{},
506
+			epPriority:  map[string]int{},
507
+			config:      containerConfig{},
508
+			controller:  c,
509
+		}
500 510
 	}
501 511
 
502 512
 	heap.Init(&sb.endpoints)
... ...
@@ -507,14 +522,26 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
507 507
 		return nil, err
508 508
 	}
509 509
 
510
-	c.Lock()
510
+	if sb.config.useDefaultSandBox {
511
+		c.sboxOnce.Do(func() {
512
+			c.defOsSbox, err = osl.NewSandbox(sb.Key(), false)
513
+		})
514
+
515
+		if err != nil {
516
+			c.sboxOnce = sync.Once{}
517
+			return nil, fmt.Errorf("failed to create default sandbox: %v", err)
518
+		}
519
+
520
+		sb.osSbox = c.defOsSbox
521
+	}
522
+
511 523
 	if sb.osSbox == nil && !sb.config.useExternalKey {
512 524
 		if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox); err != nil {
513
-			c.Unlock()
514 525
 			return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
515 526
 		}
516 527
 	}
517 528
 
529
+	c.Lock()
518 530
 	c.sandboxes[sb.id] = sb
519 531
 	c.Unlock()
520 532
 	defer func() {
... ...
@@ -539,6 +566,11 @@ func (c *controller) Sandboxes() []Sandbox {
539 539
 
540 540
 	list := make([]Sandbox, 0, len(c.sandboxes))
541 541
 	for _, s := range c.sandboxes {
542
+		// Hide stub sandboxes from libnetwork users
543
+		if s.isStub {
544
+			continue
545
+		}
546
+
542 547
 		list = append(list, s)
543 548
 	}
544 549
 
... ...
@@ -566,6 +598,26 @@ func (c *controller) SandboxByID(id string) (Sandbox, error) {
566 566
 	return s, nil
567 567
 }
568 568
 
569
+// SandboxDestroy destroys a sandbox given a container ID
570
+func (c *controller) SandboxDestroy(id string) error {
571
+	var sb *sandbox
572
+	c.Lock()
573
+	for _, s := range c.sandboxes {
574
+		if s.containerID == id {
575
+			sb = s
576
+			break
577
+		}
578
+	}
579
+	c.Unlock()
580
+
581
+	// It is not an error if sandbox is not available
582
+	if sb == nil {
583
+		return nil
584
+	}
585
+
586
+	return sb.Delete()
587
+}
588
+
569 589
 // SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
570 590
 func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker {
571 591
 	return func(sb Sandbox) bool {
... ...
@@ -68,6 +68,7 @@ type sandbox struct {
68 68
 	joinLeaveDone chan struct{}
69 69
 	dbIndex       uint64
70 70
 	dbExists      bool
71
+	isStub        bool
71 72
 	inDelete      bool
72 73
 	sync.Mutex
73 74
 }
... ...
@@ -197,7 +198,7 @@ func (sb *sandbox) Delete() error {
197 197
 	// likely not required any more. Drop it.
198 198
 	etchosts.Drop(sb.config.hostsPath)
199 199
 
200
-	if sb.osSbox != nil {
200
+	if sb.osSbox != nil && !sb.config.useDefaultSandBox {
201 201
 		sb.osSbox.Destroy()
202 202
 	}
203 203
 
... ...
@@ -128,6 +128,12 @@ func (sb *sandbox) storeUpdate() error {
128 128
 retry:
129 129
 	sbs.Eps = nil
130 130
 	for _, ep := range sb.getConnectedEndpoints() {
131
+		// If the endpoint is not persisted then do not add it to
132
+		// the sandbox checkpoint
133
+		if ep.Skip() {
134
+			continue
135
+		}
136
+
131 137
 		eps := epState{
132 138
 			Nid: ep.getNetwork().ID(),
133 139
 			Eid: ep.ID(),
... ...
@@ -188,6 +194,7 @@ func (c *controller) sandboxCleanup() {
188 188
 			endpoints:   epHeap{},
189 189
 			epPriority:  map[string]int{},
190 190
 			dbIndex:     sbs.dbIndex,
191
+			isStub:      true,
191 192
 			dbExists:    true,
192 193
 		}
193 194