Browse code

Decouple daemon and container to cleanup containers.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/11/04 02:43:36
Showing 5 changed files
... ...
@@ -236,24 +236,6 @@ func (streamConfig *streamConfig) StderrPipe() io.ReadCloser {
236 236
 	return ioutils.NewBufReader(reader)
237 237
 }
238 238
 
239
-// cleanup releases any network resources allocated to the container along with any rules
240
-// around how containers are linked together.  It also unmounts the container's root filesystem.
241
-func (container *Container) cleanup() {
242
-	container.releaseNetwork()
243
-
244
-	container.unmountIpcMounts(detachMounted)
245
-
246
-	container.daemon.conditionalUnmountOnCleanup(container)
247
-
248
-	for _, eConfig := range container.execCommands.s {
249
-		container.daemon.unregisterExecCommand(eConfig)
250
-	}
251
-
252
-	if err := container.unmountVolumes(false); err != nil {
253
-		logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
254
-	}
255
-}
256
-
257 239
 // ExitOnNext signals to the monitor that it should not restart the container
258 240
 // after we send the kill signal.
259 241
 func (container *Container) ExitOnNext() {
... ...
@@ -1153,7 +1153,7 @@ func (container *Container) getNetworkedContainer() (*Container, error) {
1153 1153
 	}
1154 1154
 }
1155 1155
 
1156
-func (container *Container) releaseNetwork() {
1156
+func (daemon *Daemon) releaseNetwork(container *Container) {
1157 1157
 	if container.hostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
1158 1158
 		return
1159 1159
 	}
... ...
@@ -1170,7 +1170,7 @@ func (container *Container) releaseNetwork() {
1170 1170
 		return
1171 1171
 	}
1172 1172
 
1173
-	sb, err := container.daemon.netController.SandboxByID(sid)
1173
+	sb, err := daemon.netController.SandboxByID(sid)
1174 1174
 	if err != nil {
1175 1175
 		logrus.Errorf("error locating sandbox id %s: %v", sid, err)
1176 1176
 		return
... ...
@@ -162,7 +162,7 @@ func (container *Container) updateNetwork() error {
162 162
 	return nil
163 163
 }
164 164
 
165
-func (container *Container) releaseNetwork() {
165
+func (daemon *Daemon) releaseNetwork(container *Container) {
166 166
 }
167 167
 
168 168
 // appendNetworkMounts appends any network mounts to the array of mount points passed in.
... ...
@@ -21,6 +21,8 @@ const (
21 21
 type containerSupervisor interface {
22 22
 	// LogContainerEvent generates events related to a given container
23 23
 	LogContainerEvent(*Container, string)
24
+	// Cleanup ensures that the container is properly unmounted
25
+	Cleanup(*Container)
24 26
 }
25 27
 
26 28
 // containerMonitor monitors the execution of a container's main process.
... ...
@@ -96,7 +98,7 @@ func (m *containerMonitor) ExitOnNext() {
96 96
 // unmounts the contatiner's root filesystem
97 97
 func (m *containerMonitor) Close() error {
98 98
 	// Cleanup networking and mounts
99
-	m.container.cleanup()
99
+	m.supervisor.Cleanup(m.container)
100 100
 
101 101
 	// FIXME: here is race condition between two RUN instructions in Dockerfile
102 102
 	// because they share same runconfig and change image. Must be fixed
... ...
@@ -3,6 +3,7 @@ package daemon
3 3
 import (
4 4
 	"runtime"
5 5
 
6
+	"github.com/Sirupsen/logrus"
6 7
 	derr "github.com/docker/docker/errors"
7 8
 	"github.com/docker/docker/pkg/promise"
8 9
 	"github.com/docker/docker/runconfig"
... ...
@@ -83,7 +84,7 @@ func (daemon *Daemon) containerStart(container *Container) (err error) {
83 83
 				container.ExitCode = 128
84 84
 			}
85 85
 			container.toDisk()
86
-			container.cleanup()
86
+			daemon.Cleanup(container)
87 87
 			daemon.LogContainerEvent(container, "die")
88 88
 		}
89 89
 	}()
... ...
@@ -140,3 +141,21 @@ func (daemon *Daemon) waitForStart(container *Container) error {
140 140
 
141 141
 	return nil
142 142
 }
143
+
144
+// Cleanup releases any network resources allocated to the container along with any rules
145
+// around how containers are linked together.  It also unmounts the container's root filesystem.
146
+func (daemon *Daemon) Cleanup(container *Container) {
147
+	daemon.releaseNetwork(container)
148
+
149
+	container.unmountIpcMounts(detachMounted)
150
+
151
+	daemon.conditionalUnmountOnCleanup(container)
152
+
153
+	for _, eConfig := range container.execCommands.s {
154
+		daemon.unregisterExecCommand(eConfig)
155
+	}
156
+
157
+	if err := container.unmountVolumes(false); err != nil {
158
+		logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
159
+	}
160
+}