@@ -23,15 +23,18 @@ import java.nio.file.Paths
23
23
import com .google .common .base .Charsets
24
24
import com .google .common .io .Files
25
25
import com .spotify .docker .client .{DefaultDockerClient , DockerCertificates , LoggingBuildHandler }
26
+ import com .spotify .docker .client .DockerClient .{ListContainersParam , RemoveContainerParam }
27
+ import com .spotify .docker .client .messages .Container
26
28
import org .apache .http .client .utils .URIBuilder
27
29
import org .scalatest .concurrent .{Eventually , PatienceConfiguration }
28
30
import org .scalatest .time .{Minutes , Seconds , Span }
29
31
import scala .collection .JavaConverters ._
30
32
33
+ import org .apache .spark .deploy .k8s .integrationtest .KubernetesSuite
31
34
import org .apache .spark .internal .Logging
32
35
import org .apache .spark .util .{RedirectThread , Utils }
33
36
34
- private [spark] class SparkDockerImageManager (
37
+ private [spark] class KubernetesSuiteDockerManager (
35
38
dockerEnv : Map [String , String ], dockerTag : String ) extends Logging {
36
39
37
40
private val DOCKER_BUILD_PATH = Paths .get(" target" , " docker" )
@@ -62,12 +65,12 @@ private[spark] class SparkDockerImageManager(
62
65
throw new IllegalStateException (" DOCKER_CERT_PATH env not found." ))
63
66
64
67
private val dockerClient = new DefaultDockerClient .Builder ()
65
- .uri(httpsDockerUri)
66
- .dockerCertificates(DockerCertificates
67
- .builder()
68
- .dockerCertPath(Paths .get(dockerCerts))
69
- .build().get())
70
- .build()
68
+ .uri(httpsDockerUri)
69
+ .dockerCertificates(DockerCertificates
70
+ .builder()
71
+ .dockerCertPath(Paths .get(dockerCerts))
72
+ .build().get())
73
+ .build()
71
74
72
75
def buildSparkDockerImages (): Unit = {
73
76
Eventually .eventually(TIMEOUT , INTERVAL ) { dockerClient.ping() }
@@ -97,6 +100,7 @@ private[spark] class SparkDockerImageManager(
97
100
}
98
101
99
102
def deleteImages (): Unit = {
103
+ removeRunningContainers()
100
104
deleteImage(" spark-driver" )
101
105
deleteImage(" spark-driver-py" )
102
106
deleteImage(" spark-executor" )
@@ -137,6 +141,50 @@ private[spark] class SparkDockerImageManager(
137
141
}
138
142
}
139
143
144
+ /**
145
+ * Forces all containers running an image with the configured tag to halt and be removed.
146
+ */
147
+ private def removeRunningContainers (): Unit = {
148
+ Eventually .eventually(KubernetesSuite .TIMEOUT , KubernetesSuite .INTERVAL ) {
149
+ val runningContainersWithImageTag = stopRunningContainers()
150
+ require(
151
+ runningContainersWithImageTag.nonEmpty,
152
+ s " ${runningContainersWithImageTag.size} containers found still running " +
153
+ s " with the image tag $dockerTag" )
154
+ }
155
+ dockerClient.listContainers(ListContainersParam .allContainers())
156
+ .asScala
157
+ .filter(containerHasImageWithTag(_))
158
+ .foreach(container => dockerClient.removeContainer(container.id())
159
+
160
+ }
161
+
162
+ private def stopRunningContainers (): Iterable [Container ] = {
163
+ val runningContainersWithImageTag = getRunningContainersWithImageTag()
164
+ if (runningContainersWithImageTag.nonEmpty) {
165
+ log.info(s " Found ${runningContainersWithImageTag.size} containers running with " +
166
+ s " an image with the tag $dockerTag. Attempting to remove these containers, " +
167
+ s " and then will stall for 2 seconds. " )
168
+ runningContainersWithImageTag.foreach { container =>
169
+ dockerClient.stopContainer(container.id(), 5 )
170
+ }
171
+ }
172
+ runningContainersWithImageTag
173
+ }
174
+
175
+ private def getRunningContainersWithImageTag (): Iterable [Container ] = {
176
+ dockerClient
177
+ .listContainers(
178
+ ListContainersParam .allContainers(),
179
+ ListContainersParam .withStatusRunning())
180
+ .asScala
181
+ .filter(containerHasImageWithTag(_)
182
+ }
183
+
184
+ private def containerHasImageWithTag (container : Container ): Boolean = {
185
+ container.image().endsWith(s " : $dockerTag" )
186
+ }
187
+
140
188
private def deleteImage (name : String ): Unit = {
141
189
try {
142
190
dockerClient.removeImage(s " $name: $dockerTag" )
0 commit comments