Quellcode durchsuchen

Endpoint to provide ContainerInfo

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch vor 10 Jahren
Ursprung
Commit
8aaf82c5b3
3 geänderte Dateien mit 40 neuen und 0 gelöschten Zeilen
  1. 11 0
      libnetwork/endpoint.go
  2. 20 0
      libnetwork/endpoint_info.go
  3. 9 0
      libnetwork/libnetwork_test.go

+ 11 - 0
libnetwork/endpoint.go

@@ -46,6 +46,9 @@ type Endpoint interface {
 	// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
 	DriverInfo() (map[string]interface{}, error)
 
+	// ContainerInfo returns the info available at the endpoint about the attached container
+	ContainerInfo() ContainerInfo
+
 	// Delete and detaches this endpoint from the network.
 	Delete() error
 }
@@ -102,6 +105,14 @@ type containerInfo struct {
 	sync.Mutex
 }
 
+func (ci *containerInfo) ID() string {
+	return ci.id
+}
+
+func (ci *containerInfo) Labels() map[string]interface{} {
+	return ci.config.generic
+}
+
 type endpoint struct {
 	name          string
 	id            types.UUID

+ 20 - 0
libnetwork/endpoint_info.go

@@ -40,6 +40,14 @@ type InterfaceInfo interface {
 	AddressIPv6() net.IPNet
 }
 
+// ContainerInfo provides an interface to retrieve the info about the container attached to the endpoint
+type ContainerInfo interface {
+	// ID returns the ID of the container
+	ID() string
+	// Labels returns the container's labels
+	Labels() map[string]interface{}
+}
+
 type endpointInterface struct {
 	id        int
 	mac       net.HardwareAddr
@@ -111,6 +119,18 @@ type endpointJoinInfo struct {
 	StaticRoutes   []*types.StaticRoute
 }
 
+func (ep *endpoint) ContainerInfo() ContainerInfo {
+	ep.Lock()
+	ci := ep.container
+	defer ep.Unlock()
+
+	// Need this since we return the interface
+	if ci == nil {
+		return nil
+	}
+	return ci
+}
+
 func (ep *endpoint) Info() EndpointInfo {
 	return ep
 }

+ 9 - 0
libnetwork/libnetwork_test.go

@@ -1045,6 +1045,11 @@ func TestEndpointJoin(t *testing.T) {
 		t.Fatalf("Expected an non-empty sandbox key for a joined endpoint. Instead found a empty sandbox key")
 	}
 
+	// Check endpoint provided container information
+	if ep1.ContainerInfo().ID() != containerID {
+		t.Fatalf("Endpoint ContainerInfo returned unexpected id: %s", ep1.ContainerInfo().ID())
+	}
+
 	// Now test the container joining another network
 	n2, err := createTestNetwork(bridgeNetType, "testnetwork2",
 		options.Generic{
@@ -1077,6 +1082,10 @@ func TestEndpointJoin(t *testing.T) {
 		t.Fatal(err)
 	}
 
+	if ep1.ContainerInfo().ID() != ep2.ContainerInfo().ID() {
+		t.Fatalf("ep1 and ep2 returned different container info")
+	}
+
 	defer func() {
 		err = ep2.Leave(containerID)
 		if err != nil {