Browse code

Fix up example code

- runs to completion without error
- demonstrates info available when using bridge network driver

Closes #837

Signed-off-by: Gabe Rosenhouse <grosenhouse@pivotal.io>

David Bellotti authored on 2015/12/24 04:47:14
Showing 2 changed files
... ...
@@ -15,6 +15,11 @@ There are many networking solutions available to suit a broad range of use-cases
15 15
 
16 16
 
17 17
 ```go
18
+func main() {
19
+	if reexec.Init() {
20
+		return
21
+	}
22
+
18 23
 	// Select and configure the network driver
19 24
 	networkType := "bridge"
20 25
 
... ...
@@ -24,14 +29,14 @@ There are many networking solutions available to suit a broad range of use-cases
24 24
 	genericOption[netlabel.GenericData] = driverOptions
25 25
 	controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
26 26
 	if err != nil {
27
-		return
27
+		log.Fatalf("libnetwork.New: %s", err)
28 28
 	}
29 29
 
30 30
 	// Create a network for containers to join.
31 31
 	// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
32 32
 	network, err := controller.NewNetwork(networkType, "network1")
33 33
 	if err != nil {
34
-		return
34
+		log.Fatalf("controller.NewNetwork: %s", err)
35 35
 	}
36 36
 
37 37
 	// For each new container: allocate IP and interfaces. The returned network
... ...
@@ -40,7 +45,7 @@ There are many networking solutions available to suit a broad range of use-cases
40 40
 	// from the returned endpoint.
41 41
 	ep, err := network.CreateEndpoint("Endpoint1")
42 42
 	if err != nil {
43
-		return
43
+		log.Fatalf("network.CreateEndpoint: %s", err)
44 44
 	}
45 45
 
46 46
 	// Create the sandbox for the container.
... ...
@@ -48,22 +53,29 @@ There are many networking solutions available to suit a broad range of use-cases
48 48
 	sbx, err := controller.NewSandbox("container1",
49 49
 		libnetwork.OptionHostname("test"),
50 50
 		libnetwork.OptionDomainname("docker.io"))
51
+	if err != nil {
52
+		log.Fatalf("controller.NewSandbox: %s", err)
53
+	}
51 54
 
52 55
 	// A sandbox can join the endpoint via the join api.
53 56
 	err = ep.Join(sbx)
54 57
 	if err != nil {
55
-		return
58
+		log.Fatalf("ep.Join: %s", err)
56 59
 	}
57 60
 
58 61
 	// libnetwork client can check the endpoint's operational data via the Info() API
59 62
 	epInfo, err := ep.DriverInfo()
60
-	mapData, ok := epInfo[netlabel.PortMap]
61
-	if ok {
62
-		portMapping, ok := mapData.([]types.PortBinding)
63
-		if ok {
64
-			fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
65
-		}
63
+	if err != nil {
64
+		log.Fatalf("ep.DriverInfo: %s", err)
66 65
 	}
66
+
67
+	macAddress, ok := epInfo[netlabel.MacAddress]
68
+	if !ok {
69
+		log.Fatalf("failed to get mac address from endpoint info")
70
+	}
71
+
72
+	fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
73
+}
67 74
 ```
68 75
 
69 76
 ## Future
... ...
@@ -2,15 +2,20 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"log"
5 6
 
7
+	"github.com/docker/docker/pkg/reexec"
6 8
 	"github.com/docker/libnetwork"
7 9
 	"github.com/docker/libnetwork/config"
8 10
 	"github.com/docker/libnetwork/netlabel"
9 11
 	"github.com/docker/libnetwork/options"
10
-	"github.com/docker/libnetwork/types"
11 12
 )
12 13
 
13 14
 func main() {
15
+	if reexec.Init() {
16
+		return
17
+	}
18
+
14 19
 	// Select and configure the network driver
15 20
 	networkType := "bridge"
16 21
 
... ...
@@ -20,14 +25,14 @@ func main() {
20 20
 	genericOption[netlabel.GenericData] = driverOptions
21 21
 	controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
22 22
 	if err != nil {
23
-		return
23
+		log.Fatalf("libnetwork.New: %s", err)
24 24
 	}
25 25
 
26 26
 	// Create a network for containers to join.
27 27
 	// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
28 28
 	network, err := controller.NewNetwork(networkType, "network1")
29 29
 	if err != nil {
30
-		return
30
+		log.Fatalf("controller.NewNetwork: %s", err)
31 31
 	}
32 32
 
33 33
 	// For each new container: allocate IP and interfaces. The returned network
... ...
@@ -36,28 +41,34 @@ func main() {
36 36
 	// from the returned endpoint.
37 37
 	ep, err := network.CreateEndpoint("Endpoint1")
38 38
 	if err != nil {
39
-		return
39
+		log.Fatalf("network.CreateEndpoint: %s", err)
40 40
 	}
41 41
 
42
-	// Create the sandbox for the containr.
42
+	// Create the sandbox for the container.
43
+	// NewSandbox accepts Variadic optional arguments which libnetwork can use.
43 44
 	sbx, err := controller.NewSandbox("container1",
44 45
 		libnetwork.OptionHostname("test"),
45 46
 		libnetwork.OptionDomainname("docker.io"))
47
+	if err != nil {
48
+		log.Fatalf("controller.NewSandbox: %s", err)
49
+	}
46 50
 
47 51
 	// A sandbox can join the endpoint via the join api.
48
-	// Join accepts Variadic arguments which libnetwork and Drivers can use.
49 52
 	err = ep.Join(sbx)
50 53
 	if err != nil {
51
-		return
54
+		log.Fatalf("ep.Join: %s", err)
52 55
 	}
53 56
 
54 57
 	// libnetwork client can check the endpoint's operational data via the Info() API
55 58
 	epInfo, err := ep.DriverInfo()
56
-	mapData, ok := epInfo[netlabel.PortMap]
57
-	if ok {
58
-		portMapping, ok := mapData.([]types.PortBinding)
59
-		if ok {
60
-			fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
61
-		}
59
+	if err != nil {
60
+		log.Fatalf("ep.DriverInfo: %s", err)
62 61
 	}
62
+
63
+	macAddress, ok := epInfo[netlabel.MacAddress]
64
+	if !ok {
65
+		log.Fatalf("failed to get mac address from endpoint info")
66
+	}
67
+
68
+	fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
63 69
 }