mirror of
https://github.com/clearlinux/libnetwork.git
synced 2026-06-16 02:25:48 +00:00
Add LeaveAll support
Currently container can join one endpoint when it is started. More endpoints can be attached at a later point in time. But when that happens this attachment should only have meaning only as long as the container is alive. The attachment should lose it's meaning when the container goes away. Cuurently there is no way for the container management code to tell libnetwork to detach the container from all attached endpoints. This PR provides an additional API `LeaveAll` which adds this functionality, To facilitate this and make the sanbox lifecycle consistent some slight changes have been made to the behavior of sandbox management code. The sandbox is no longer destroyed when the last endpoint is detached from the container. Instead the sandbox ie kept alive and can only be destroyed with a `LeaveAll` call. This gives better control of sandbox lifecycle by the container management code and the sandbox doesn't get destroyed from under the carpet while the container is still using it. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
+95
-19
@@ -1009,6 +1009,8 @@ func TestEndpointJoin(t *testing.T) {
|
||||
t.Fatalf("Expected an empty sandbox key for an empty endpoint. Instead found a non-empty sandbox key: %s", info.SandboxKey())
|
||||
}
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
|
||||
err = ep1.Join(containerID,
|
||||
libnetwork.JoinOptionHostname("test"),
|
||||
libnetwork.JoinOptionDomainname("docker.io"),
|
||||
@@ -1017,7 +1019,6 @@ func TestEndpointJoin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = ep1.Leave(containerID)
|
||||
runtime.LockOSThread()
|
||||
@@ -1072,19 +1073,21 @@ func TestEndpointJoin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ep1.ContainerInfo().ID() != ep2.ContainerInfo().ID() {
|
||||
t.Fatalf("ep1 and ep2 returned different container info")
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer func() {
|
||||
err = ep2.Leave(containerID)
|
||||
runtime.LockOSThread()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if ep1.ContainerInfo().ID() != ep2.ContainerInfo().ID() {
|
||||
t.Fatalf("ep1 and ep2 returned different container info")
|
||||
}
|
||||
|
||||
checkSandbox(t, info)
|
||||
|
||||
}
|
||||
|
||||
func TestEndpointJoinInvalidContainerId(t *testing.T) {
|
||||
@@ -1151,6 +1154,14 @@ func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
err = ep.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
|
||||
err = ep.Join(containerID,
|
||||
libnetwork.JoinOptionHostname("test"),
|
||||
@@ -1166,11 +1177,6 @@ func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ep.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = ep.Delete()
|
||||
@@ -1213,6 +1219,8 @@ func TestEndpointMultipleJoins(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
|
||||
err = ep.Join(containerID,
|
||||
libnetwork.JoinOptionHostname("test"),
|
||||
libnetwork.JoinOptionDomainname("docker.io"),
|
||||
@@ -1239,6 +1247,71 @@ func TestEndpointMultipleJoins(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeaveAll(t *testing.T) {
|
||||
if !netutils.IsRunningInContainer() {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
}
|
||||
|
||||
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
|
||||
netlabel.GenericData: options.Generic{
|
||||
"BridgeName": "testnetwork",
|
||||
"AllowNonDefaultBridge": true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := n.Delete(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
ep1, err := n.CreateEndpoint("ep1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := ep1.Delete(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
ep2, err := n.CreateEndpoint("ep2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := ep2.Delete(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = ep1.Join("leaveall")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to join ep1: %v", err)
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
|
||||
err = ep2.Join("leaveall")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to join ep2: %v", err)
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
|
||||
err = ep1.Leave("leaveall")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to leave ep1: %v", err)
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
|
||||
err = controller.LeaveAll("leaveall")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func TestEndpointInvalidLeave(t *testing.T) {
|
||||
if !netutils.IsRunningInContainer() {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
@@ -1280,6 +1353,8 @@ func TestEndpointInvalidLeave(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
|
||||
err = ep.Join(containerID,
|
||||
libnetwork.JoinOptionHostname("test"),
|
||||
libnetwork.JoinOptionDomainname("docker.io"),
|
||||
@@ -1313,7 +1388,6 @@ func TestEndpointInvalidLeave(t *testing.T) {
|
||||
if _, ok := err.(libnetwork.InvalidContainerIDError); !ok {
|
||||
t.Fatalf("Failed for unexpected reason: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEndpointUpdateParent(t *testing.T) {
|
||||
@@ -1346,6 +1420,7 @@ func TestEndpointUpdateParent(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
err = ep1.Join(containerID,
|
||||
libnetwork.JoinOptionHostname("test1"),
|
||||
libnetwork.JoinOptionDomainname("docker.io"),
|
||||
@@ -1372,6 +1447,7 @@ func TestEndpointUpdateParent(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
defer controller.LeaveAll("container2")
|
||||
err = ep2.Join("container2",
|
||||
libnetwork.JoinOptionHostname("test2"),
|
||||
libnetwork.JoinOptionDomainname("docker.io"),
|
||||
@@ -1382,13 +1458,11 @@ func TestEndpointUpdateParent(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = ep2.Leave("container2")
|
||||
runtime.LockOSThread()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
err = ep2.Leave("container2")
|
||||
runtime.LockOSThread()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1452,6 +1526,7 @@ func TestEnableIPv6(t *testing.T) {
|
||||
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
|
||||
defer os.Remove(resolvConfPath)
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
err = ep1.Join(containerID,
|
||||
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
||||
runtime.LockOSThread()
|
||||
@@ -1536,6 +1611,7 @@ func TestResolvConf(t *testing.T) {
|
||||
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
|
||||
defer os.Remove(resolvConfPath)
|
||||
|
||||
defer controller.LeaveAll(containerID)
|
||||
err = ep1.Join(containerID,
|
||||
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
||||
runtime.LockOSThread()
|
||||
|
||||
Reference in New Issue
Block a user