/
KubernetesSuite.scala
416 lines (379 loc) · 16.1 KB
/
KubernetesSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.deploy.k8s.integrationtest
import java.io.File
import java.nio.file.{Path, Paths}
import java.util.UUID
import scala.collection.JavaConverters._
import com.google.common.base.Charsets
import com.google.common.io.Files
import io.fabric8.kubernetes.api.model.Pod
import io.fabric8.kubernetes.client.{KubernetesClientException, Watcher}
import io.fabric8.kubernetes.client.Watcher.Action
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, Tag}
import org.scalatest.Matchers
import org.scalatest.concurrent.{Eventually, PatienceConfiguration}
import org.scalatest.time.{Minutes, Seconds, Span}
import org.apache.spark.{SPARK_VERSION, SparkFunSuite}
import org.apache.spark.deploy.k8s.integrationtest.TestConstants._
import org.apache.spark.deploy.k8s.integrationtest.backend.{IntegrationTestBackend, IntegrationTestBackendFactory}
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config._
class KubernetesSuite extends SparkFunSuite
with BeforeAndAfterAll with BeforeAndAfter
//with BasicTestsSuite with SecretsTestsSuite
// with PythonTestsSuite with ClientModeTestsSuite with PodTemplateSuite
with DecommissionSuite
with Logging with Eventually with Matchers {
import KubernetesSuite._
protected var sparkHomeDir: Path = _
protected var pyImage: String = _
protected var rImage: String = _
protected var image: String = _
protected var testBackend: IntegrationTestBackend = _
protected var driverPodName: String = _
protected var kubernetesTestComponents: KubernetesTestComponents = _
protected var sparkAppConf: SparkAppConf = _
protected var containerLocalSparkDistroExamplesJar: String = _
protected var appLocator: String = _
// Default memory limit is 1024M + 384M (minimum overhead constant)
private val baseMemory = s"${1024 + 384}Mi"
protected val memOverheadConstant = 0.8
private val standardNonJVMMemory = s"${(1024 + 0.4*1024).toInt}Mi"
protected val additionalMemory = 200
// 209715200 is 200Mi
protected val additionalMemoryInBytes = 209715200
private val extraDriverTotalMemory = s"${(1024 + memOverheadConstant*1024).toInt}Mi"
private val extraExecTotalMemory =
s"${(1024 + memOverheadConstant*1024 + additionalMemory).toInt}Mi"
/**
* Build the image ref for the given image name, taking the repo and tag from the
* test configuration.
*/
private def testImageRef(name: String): String = {
val tag = sys.props.get(CONFIG_KEY_IMAGE_TAG_FILE)
.map { path =>
val tagFile = new File(path)
require(tagFile.isFile,
s"No file found for image tag at ${tagFile.getAbsolutePath}.")
Files.toString(tagFile, Charsets.UTF_8).trim
}
.orElse(sys.props.get(CONFIG_KEY_IMAGE_TAG))
.getOrElse {
throw new IllegalArgumentException(
s"One of $CONFIG_KEY_IMAGE_TAG_FILE or $CONFIG_KEY_IMAGE_TAG is required.")
}
val repo = sys.props.get(CONFIG_KEY_IMAGE_REPO)
.map { _ + "/" }
.getOrElse("")
s"$repo$name:$tag"
}
override def beforeAll(): Unit = {
super.beforeAll()
// The scalatest-maven-plugin gives system properties that are referenced but not set null
// values. We need to remove the null-value properties before initializing the test backend.
val nullValueProperties = System.getProperties.asScala
.filter(entry => entry._2.equals("null"))
.map(entry => entry._1.toString)
nullValueProperties.foreach { key =>
System.clearProperty(key)
}
val possible_spark_dirs = List(
// If someone specified the tgz for the tests look at the extraction dir
System.getProperty(CONFIG_KEY_UNPACK_DIR),
// If otherwise use my working dir + 3 up
new File(Paths.get(System.getProperty("user.dir")).toFile, ("../" * 3)).getAbsolutePath()
)
val sparkDirProp = possible_spark_dirs.filter(x =>
new File(Paths.get(x).toFile, "bin/spark-submit").exists).headOption.getOrElse(null)
require(sparkDirProp != null,
s"Spark home directory must be provided in system properties tested $possible_spark_dirs")
sparkHomeDir = Paths.get(sparkDirProp)
require(sparkHomeDir.toFile.isDirectory,
s"No directory found for spark home specified at $sparkHomeDir.")
image = testImageRef("spark")
pyImage = testImageRef("spark-py")
rImage = testImageRef("spark-r")
val scalaVersion = scala.util.Properties.versionNumberString
.split("\\.")
.take(2)
.mkString(".")
containerLocalSparkDistroExamplesJar =
s"local:///opt/spark/examples/jars/spark-examples_$scalaVersion-${SPARK_VERSION}.jar"
testBackend = IntegrationTestBackendFactory.getTestBackend
testBackend.initialize()
kubernetesTestComponents = new KubernetesTestComponents(testBackend.getKubernetesClient)
}
override def afterAll(): Unit = {
try {
testBackend.cleanUp()
} finally {
super.afterAll()
}
}
before {
appLocator = UUID.randomUUID().toString.replaceAll("-", "")
driverPodName = "spark-test-app-" + UUID.randomUUID().toString.replaceAll("-", "")
sparkAppConf = kubernetesTestComponents.newSparkAppConf()
.set("spark.kubernetes.container.image", image)
.set("spark.kubernetes.driver.pod.name", driverPodName)
.set("spark.kubernetes.driver.label.spark-app-locator", appLocator)
.set("spark.kubernetes.executor.label.spark-app-locator", appLocator)
.set(NETWORK_AUTH_ENABLED.key, "true")
if (!kubernetesTestComponents.hasUserSpecifiedNamespace) {
kubernetesTestComponents.createNamespace()
}
}
after {
if (!kubernetesTestComponents.hasUserSpecifiedNamespace) {
kubernetesTestComponents.deleteNamespace()
}
deleteDriverPod()
}
protected def runSparkPiAndVerifyCompletion(
appResource: String = containerLocalSparkDistroExamplesJar,
driverPodChecker: Pod => Unit = doBasicDriverPodCheck,
executorPodChecker: Pod => Unit = doBasicExecutorPodCheck,
appArgs: Array[String] = Array.empty[String],
appLocator: String = appLocator,
isJVM: Boolean = true ): Unit = {
runSparkApplicationAndVerifyCompletion(
appResource,
SPARK_PI_MAIN_CLASS,
Seq("Pi is roughly 3"),
appArgs,
driverPodChecker,
executorPodChecker,
appLocator,
isJVM)
}
protected def runSparkRemoteCheckAndVerifyCompletion(
appResource: String = containerLocalSparkDistroExamplesJar,
driverPodChecker: Pod => Unit = doBasicDriverPodCheck,
executorPodChecker: Pod => Unit = doBasicExecutorPodCheck,
appArgs: Array[String],
appLocator: String = appLocator): Unit = {
runSparkApplicationAndVerifyCompletion(
appResource,
SPARK_REMOTE_MAIN_CLASS,
Seq(s"Mounting of ${appArgs.head} was true"),
appArgs,
driverPodChecker,
executorPodChecker,
appLocator,
true)
}
protected def runSparkJVMCheckAndVerifyCompletion(
appResource: String = containerLocalSparkDistroExamplesJar,
mainClass: String = SPARK_DRIVER_MAIN_CLASS,
driverPodChecker: Pod => Unit = doBasicDriverPodCheck,
appArgs: Array[String] = Array("5"),
expectedJVMValue: Seq[String]): Unit = {
val appArguments = SparkAppArguments(
mainAppResource = appResource,
mainClass = mainClass,
appArgs = appArgs)
SparkAppLauncher.launch(
appArguments,
sparkAppConf,
TIMEOUT.value.toSeconds.toInt,
sparkHomeDir,
true)
val driverPod = kubernetesTestComponents.kubernetesClient
.pods()
.withLabel("spark-app-locator", appLocator)
.withLabel("spark-role", "driver")
.list()
.getItems
.get(0)
doBasicDriverPodCheck(driverPod)
Eventually.eventually(TIMEOUT, INTERVAL) {
expectedJVMValue.foreach { e =>
assert(kubernetesTestComponents.kubernetesClient
.pods()
.withName(driverPod.getMetadata.getName)
.getLog
.contains(e), "The application did not complete.")
}
}
}
protected def runSparkApplicationAndVerifyCompletion(
appResource: String,
mainClass: String,
expectedLogOnCompletion: Seq[String],
appArgs: Array[String],
driverPodChecker: Pod => Unit,
executorPodChecker: Pod => Unit,
appLocator: String,
isJVM: Boolean,
pyFiles: Option[String] = None,
decomissioningTest: Boolean = false): Unit = {
val appArguments = SparkAppArguments(
mainAppResource = appResource,
mainClass = mainClass,
appArgs = appArgs)
SparkAppLauncher.launch(
appArguments,
sparkAppConf,
TIMEOUT.value.toSeconds.toInt,
sparkHomeDir,
isJVM,
pyFiles)
println("Running spark job.")
val driverPod = kubernetesTestComponents.kubernetesClient
.pods()
.withLabel("spark-app-locator", appLocator)
.withLabel("spark-role", "driver")
.list()
.getItems
.get(0)
driverPodChecker(driverPod)
val execPods = scala.collection.mutable.Map[String, Pod]()
val execWatcher = kubernetesTestComponents.kubernetesClient
.pods()
.withLabel("spark-app-locator", appLocator)
.withLabel("spark-role", "executor")
.watch(new Watcher[Pod] {
logInfo("Beginning watch of executors")
override def onClose(cause: KubernetesClientException): Unit =
logInfo("Ending watch of executors")
override def eventReceived(action: Watcher.Action, resource: Pod): Unit = {
println("Event received.")
val name = resource.getMetadata.getName
action match {
case Action.ADDED | Action.MODIFIED =>
println("Add or modification event received.")
execPods(name) = resource
// If testing decomissioning delete the node 5 seconds after it starts running
if (decomissioningTest) {
// Wait for all the containers in the pod to be running
println("Waiting for pod to become OK then delete.")
Eventually.eventually(TIMEOUT, INTERVAL) {
resource.getStatus.getConditions().asScala
.map(cond => cond.getStatus() == "True" && cond.getType() == "Ready")
.headOption.getOrElse(false)
}
// Sleep a small interval to ensure everything is registered.
Thread.sleep(500)
// Delete the pod to simulate cluster scale down/migration.
val pod = kubernetesTestComponents.kubernetesClient.pods().withName(name)
pod.delete()
println(s"Pod: $name deleted")
}
case Action.DELETED | Action.ERROR =>
println("Deleted or error event received.")
execPods.remove(name)
}
}
})
// If we're testing decomissioning we delete all the executors
Eventually.eventually(TIMEOUT, INTERVAL) {
execPods.values.nonEmpty || decomissioningTest should be (true) }
execWatcher.close()
execPods.values.foreach(executorPodChecker(_))
println(s"Exec pods are $execPods")
Eventually.eventually(TIMEOUT, INTERVAL) {
expectedLogOnCompletion.foreach { e =>
assert(kubernetesTestComponents.kubernetesClient
.pods()
.withName(driverPod.getMetadata.getName)
.getLog
.contains(e), "The application did not complete.")
}
}
}
protected def doBasicDriverPodCheck(driverPod: Pod): Unit = {
assert(driverPod.getMetadata.getName === driverPodName)
assert(driverPod.getSpec.getContainers.get(0).getImage === image)
assert(driverPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-driver")
assert(driverPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== baseMemory)
}
protected def doBasicDriverPyPodCheck(driverPod: Pod): Unit = {
assert(driverPod.getMetadata.getName === driverPodName)
assert(driverPod.getSpec.getContainers.get(0).getImage === pyImage)
assert(driverPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-driver")
assert(driverPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== standardNonJVMMemory)
}
protected def doBasicDriverRPodCheck(driverPod: Pod): Unit = {
assert(driverPod.getMetadata.getName === driverPodName)
assert(driverPod.getSpec.getContainers.get(0).getImage === rImage)
assert(driverPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-driver")
assert(driverPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== standardNonJVMMemory)
}
protected def doBasicExecutorPodCheck(executorPod: Pod): Unit = {
assert(executorPod.getSpec.getContainers.get(0).getImage === image)
assert(executorPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-executor")
assert(executorPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== baseMemory)
}
protected def doBasicExecutorPyPodCheck(executorPod: Pod): Unit = {
assert(executorPod.getSpec.getContainers.get(0).getImage === pyImage)
assert(executorPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-executor")
assert(executorPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== standardNonJVMMemory)
}
protected def doBasicExecutorRPodCheck(executorPod: Pod): Unit = {
assert(executorPod.getSpec.getContainers.get(0).getImage === rImage)
assert(executorPod.getSpec.getContainers.get(0).getName === "spark-kubernetes-executor")
assert(executorPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== standardNonJVMMemory)
}
protected def doDriverMemoryCheck(driverPod: Pod): Unit = {
assert(driverPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== extraDriverTotalMemory)
}
protected def doExecutorMemoryCheck(executorPod: Pod): Unit = {
assert(executorPod.getSpec.getContainers.get(0).getResources.getRequests.get("memory").getAmount
=== extraExecTotalMemory)
}
protected def checkCustomSettings(pod: Pod): Unit = {
assert(pod.getMetadata.getLabels.get("label1") === "label1-value")
assert(pod.getMetadata.getLabels.get("label2") === "label2-value")
assert(pod.getMetadata.getAnnotations.get("annotation1") === "annotation1-value")
assert(pod.getMetadata.getAnnotations.get("annotation2") === "annotation2-value")
val container = pod.getSpec.getContainers.get(0)
val envVars = container
.getEnv
.asScala
.map { env =>
(env.getName, env.getValue)
}
.toMap
assert(envVars("ENV1") === "VALUE1")
assert(envVars("ENV2") === "VALUE2")
}
private def deleteDriverPod(): Unit = {
kubernetesTestComponents.kubernetesClient.pods().withName(driverPodName).delete()
Eventually.eventually(TIMEOUT, INTERVAL) {
assert(kubernetesTestComponents.kubernetesClient
.pods()
.withName(driverPodName)
.get() == null)
}
}
}
private[spark] object KubernetesSuite {
val k8sTestTag = Tag("k8s")
val SPARK_PI_MAIN_CLASS: String = "org.apache.spark.examples.SparkPi"
val SPARK_REMOTE_MAIN_CLASS: String = "org.apache.spark.examples.SparkRemoteFileTest"
val SPARK_DRIVER_MAIN_CLASS: String = "org.apache.spark.examples.DriverSubmissionTest"
val TIMEOUT = PatienceConfiguration.Timeout(Span(2, Minutes))
val INTERVAL = PatienceConfiguration.Interval(Span(2, Seconds))
}