|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.deploy.kubernetes.submit.submitsteps |
| 18 | + |
| 19 | +import io.fabric8.kubernetes.api.model.Service |
| 20 | +import org.mockito.{Mock, MockitoAnnotations} |
| 21 | +import org.mockito.Mockito.when |
| 22 | +import org.scalatest.BeforeAndAfter |
| 23 | +import scala.collection.JavaConverters._ |
| 24 | + |
| 25 | +import org.apache.spark.{SparkConf, SparkFunSuite} |
| 26 | +import org.apache.spark.deploy.kubernetes.config._ |
| 27 | +import org.apache.spark.deploy.kubernetes.constants._ |
| 28 | +import org.apache.spark.util.Clock |
| 29 | + |
| 30 | +private[spark] class DriverAddressConfigurationStepSuite |
| 31 | + extends SparkFunSuite with BeforeAndAfter { |
| 32 | + |
| 33 | + private val SHORT_RESOURCE_NAME_PREFIX = |
| 34 | + "a" * (DriverAddressConfigurationStep.MAX_SERVICE_NAME_LENGTH - |
| 35 | + DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX.length) |
| 36 | + |
| 37 | + private val LONG_RESOURCE_NAME_PREFIX = |
| 38 | + "a" * (DriverAddressConfigurationStep.MAX_SERVICE_NAME_LENGTH - |
| 39 | + DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX.length + 1) |
| 40 | + private val DRIVER_LABELS = Map( |
| 41 | + "label1key" -> "label1value", |
| 42 | + "label2key" -> "label2value") |
| 43 | + |
| 44 | + @Mock |
| 45 | + private var clock: Clock = _ |
| 46 | + |
| 47 | + private var sparkConf: SparkConf = _ |
| 48 | + |
| 49 | + before { |
| 50 | + MockitoAnnotations.initMocks(this) |
| 51 | + sparkConf = new SparkConf(false) |
| 52 | + } |
| 53 | + |
| 54 | + test("Headless service has a port for the driver RPC and the block manager.") { |
| 55 | + val configurationStep = new DriverAddressConfigurationStep( |
| 56 | + SHORT_RESOURCE_NAME_PREFIX, |
| 57 | + DRIVER_LABELS, |
| 58 | + sparkConf |
| 59 | + .set("spark.driver.port", "9000") |
| 60 | + .set(org.apache.spark.internal.config.DRIVER_BLOCK_MANAGER_PORT, 8080), |
| 61 | + clock) |
| 62 | + val baseDriverSpec = KubernetesDriverSpec.initialSpec(sparkConf.clone()) |
| 63 | + val resolvedDriverSpec = configurationStep.configureDriver(baseDriverSpec) |
| 64 | + assert(resolvedDriverSpec.otherKubernetesResources.size === 1) |
| 65 | + assert(resolvedDriverSpec.otherKubernetesResources.head.isInstanceOf[Service]) |
| 66 | + val driverService = resolvedDriverSpec.otherKubernetesResources.head.asInstanceOf[Service] |
| 67 | + verifyService( |
| 68 | + 9000, |
| 69 | + 8080, |
| 70 | + s"$SHORT_RESOURCE_NAME_PREFIX${DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX}", |
| 71 | + driverService) |
| 72 | + } |
| 73 | + |
| 74 | + test("Hostname and ports are set according to the service name.") { |
| 75 | + val configurationStep = new DriverAddressConfigurationStep( |
| 76 | + SHORT_RESOURCE_NAME_PREFIX, |
| 77 | + DRIVER_LABELS, |
| 78 | + sparkConf |
| 79 | + .set("spark.driver.port", "9000") |
| 80 | + .set(org.apache.spark.internal.config.DRIVER_BLOCK_MANAGER_PORT, 8080) |
| 81 | + .set(KUBERNETES_NAMESPACE, "my-namespace"), |
| 82 | + clock) |
| 83 | + val baseDriverSpec = KubernetesDriverSpec.initialSpec(sparkConf.clone()) |
| 84 | + val resolvedDriverSpec = configurationStep.configureDriver(baseDriverSpec) |
| 85 | + val expectedServiceName = SHORT_RESOURCE_NAME_PREFIX + |
| 86 | + DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX |
| 87 | + val expectedHostName = s"$expectedServiceName.my-namespace.svc.cluster.local" |
| 88 | + verifySparkConfHostNames(resolvedDriverSpec.driverSparkConf, expectedHostName) |
| 89 | + } |
| 90 | + |
| 91 | + test("Ports should resolve to defaults in SparkConf and in the service.") { |
| 92 | + val configurationStep = new DriverAddressConfigurationStep( |
| 93 | + SHORT_RESOURCE_NAME_PREFIX, |
| 94 | + DRIVER_LABELS, |
| 95 | + sparkConf, |
| 96 | + clock) |
| 97 | + val baseDriverSpec = KubernetesDriverSpec.initialSpec(sparkConf.clone()) |
| 98 | + val resolvedDriverSpec = configurationStep.configureDriver(baseDriverSpec) |
| 99 | + verifyService( |
| 100 | + DEFAULT_DRIVER_PORT, |
| 101 | + DEFAULT_BLOCKMANAGER_PORT, |
| 102 | + s"$SHORT_RESOURCE_NAME_PREFIX${DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX}", |
| 103 | + resolvedDriverSpec.otherKubernetesResources.head.asInstanceOf[Service]) |
| 104 | + assert(resolvedDriverSpec.driverSparkConf.get("spark.driver.port") === |
| 105 | + DEFAULT_DRIVER_PORT.toString) |
| 106 | + assert(resolvedDriverSpec.driverSparkConf.get( |
| 107 | + org.apache.spark.internal.config.DRIVER_BLOCK_MANAGER_PORT) === DEFAULT_BLOCKMANAGER_PORT) |
| 108 | + } |
| 109 | + |
| 110 | + test("Long prefixes should switch to using a generated name.") { |
| 111 | + val configurationStep = new DriverAddressConfigurationStep( |
| 112 | + LONG_RESOURCE_NAME_PREFIX, |
| 113 | + DRIVER_LABELS, |
| 114 | + sparkConf.set(KUBERNETES_NAMESPACE, "my-namespace"), |
| 115 | + clock) |
| 116 | + when(clock.getTimeMillis()).thenReturn(10000) |
| 117 | + val baseDriverSpec = KubernetesDriverSpec.initialSpec(sparkConf.clone()) |
| 118 | + val resolvedDriverSpec = configurationStep.configureDriver(baseDriverSpec) |
| 119 | + val driverService = resolvedDriverSpec.otherKubernetesResources.head.asInstanceOf[Service] |
| 120 | + val expectedServiceName = s"spark-10000${DriverAddressConfigurationStep.DRIVER_SVC_POSTFIX}" |
| 121 | + assert(driverService.getMetadata.getName === expectedServiceName) |
| 122 | + val expectedHostName = s"$expectedServiceName.my-namespace.svc.cluster.local" |
| 123 | + verifySparkConfHostNames(resolvedDriverSpec.driverSparkConf, expectedHostName) |
| 124 | + } |
| 125 | + |
| 126 | + test("Disallow bind address and driver host to be set explicitly.") { |
| 127 | + val configurationStep = new DriverAddressConfigurationStep( |
| 128 | + LONG_RESOURCE_NAME_PREFIX, |
| 129 | + DRIVER_LABELS, |
| 130 | + sparkConf.set(org.apache.spark.internal.config.DRIVER_BIND_ADDRESS, "host"), |
| 131 | + clock) |
| 132 | + try { |
| 133 | + configurationStep.configureDriver(KubernetesDriverSpec.initialSpec(sparkConf)) |
| 134 | + fail("The driver bind address should not be allowed.") |
| 135 | + } catch { |
| 136 | + case e: Throwable => |
| 137 | + assert(e.getMessage === |
| 138 | + s"requirement failed: ${DriverAddressConfigurationStep.DRIVER_BIND_ADDRESS_KEY} is" + |
| 139 | + s" not supported in Kubernetes mode, as the driver's hostname will be managed via" + |
| 140 | + s" a Kubernetes service.") |
| 141 | + } |
| 142 | + sparkConf.remove(org.apache.spark.internal.config.DRIVER_BIND_ADDRESS) |
| 143 | + sparkConf.set(org.apache.spark.internal.config.DRIVER_HOST_ADDRESS, "host") |
| 144 | + try { |
| 145 | + configurationStep.configureDriver(KubernetesDriverSpec.initialSpec(sparkConf)) |
| 146 | + fail("The driver host address should not be allowed.") |
| 147 | + } catch { |
| 148 | + case e: Throwable => |
| 149 | + assert(e.getMessage === |
| 150 | + s"requirement failed: ${DriverAddressConfigurationStep.DRIVER_HOST_KEY} is" + |
| 151 | + s" not supported in Kubernetes mode, as the driver's hostname will be managed via" + |
| 152 | + s" a Kubernetes service.") |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private def verifyService( |
| 157 | + driverPort: Int, |
| 158 | + blockManagerPort: Int, |
| 159 | + expectedServiceName: String, |
| 160 | + service: Service): Unit = { |
| 161 | + assert(service.getMetadata.getName === expectedServiceName) |
| 162 | + assert(service.getSpec.getClusterIP === "None") |
| 163 | + assert(service.getSpec.getSelector.asScala === DRIVER_LABELS) |
| 164 | + assert(service.getSpec.getPorts.size() === 2) |
| 165 | + val driverServicePorts = service.getSpec.getPorts.asScala |
| 166 | + assert(driverServicePorts.head.getName === DRIVER_PORT_NAME) |
| 167 | + assert(driverServicePorts.head.getPort.intValue() === driverPort) |
| 168 | + assert(driverServicePorts.head.getTargetPort.getIntVal === driverPort) |
| 169 | + assert(driverServicePorts(1).getName === BLOCK_MANAGER_PORT_NAME) |
| 170 | + assert(driverServicePorts(1).getPort.intValue() === blockManagerPort) |
| 171 | + assert(driverServicePorts(1).getTargetPort.getIntVal === blockManagerPort) |
| 172 | + } |
| 173 | + |
| 174 | + private def verifySparkConfHostNames( |
| 175 | + driverSparkConf: SparkConf, expectedHostName: String): Unit = { |
| 176 | + assert(driverSparkConf.get( |
| 177 | + org.apache.spark.internal.config.DRIVER_HOST_ADDRESS) === expectedHostName) |
| 178 | + assert(driverSparkConf.get( |
| 179 | + org.apache.spark.internal.config.DRIVER_BIND_ADDRESS) === expectedHostName) |
| 180 | + } |
| 181 | +} |
0 commit comments