From 8aaf82c5b3eb78d68a119bf5b432665e2c7a96ce Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Wed, 10 Jun 2015 18:50:19 -0700 Subject: [PATCH] Endpoint to provide ContainerInfo Signed-off-by: Alessandro Boch --- libnetwork/endpoint.go | 11 +++++++++++ libnetwork/endpoint_info.go | 20 ++++++++++++++++++++ libnetwork/libnetwork_test.go | 9 +++++++++ 3 files changed, 40 insertions(+) diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index 77c9adcbe2..65dd91cfbc 100644 --- a/libnetwork/endpoint.go +++ b/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 diff --git a/libnetwork/endpoint_info.go b/libnetwork/endpoint_info.go index 38d5bdc365..e45ab1cd01 100644 --- a/libnetwork/endpoint_info.go +++ b/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 } diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 27e3bdd6f2..6c4b25992f 100644 --- a/libnetwork/libnetwork_test.go +++ b/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 {