Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.features

import java.io.File
import java.nio.charset.StandardCharsets

import scala.collection.JavaConverters._

import com.google.common.io.Files
import io.fabric8.kubernetes.api.model._

import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesUtils, SparkPod}
import org.apache.spark.deploy.k8s.Config._
import org.apache.spark.deploy.k8s.Constants._

/**
* Mounts the Hadoop configuration - either a pre-defined config map, or a local configuration
* directory - on the executor pod.
*/
private[spark] class HadoopConfExecutorFeatureStep(conf: KubernetesConf)
extends KubernetesFeatureConfigStep {

private val confDir = Option(conf.sparkConf.getenv(ENV_HADOOP_CONF_DIR))
private val existingConfMap = conf.get(KUBERNETES_HADOOP_CONF_CONFIG_MAP)

KubernetesUtils.requireNandDefined(
confDir,
existingConfMap,
"Do not specify both the `HADOOP_CONF_DIR` in your ENV and the ConfigMap " +
"as the creation of an additional ConfigMap, when one is already specified is extraneous")

private lazy val confFiles: Seq[File] = {
val dir = new File(confDir.get)
if (dir.isDirectory) {
dir.listFiles.filter(_.isFile).toSeq
} else {
Nil
}
}

private def newConfigMapName: String = s"${conf.resourceNamePrefix}-hadoop-config"

private def hasHadoopConf: Boolean = confDir.isDefined || existingConfMap.isDefined

override def configurePod(original: SparkPod): SparkPod = {
original.transform { case pod if hasHadoopConf =>
val confVolume = if (confDir.isDefined) {
val keyPaths = confFiles.map { file =>
new KeyToPathBuilder()
.withKey(file.getName())
.withPath(file.getName())
.build()
}
new VolumeBuilder()
.withName(HADOOP_CONF_VOLUME)
.withNewConfigMap()
.withName(newConfigMapName)
.withItems(keyPaths.asJava)
.endConfigMap()
.build()
} else {
new VolumeBuilder()
.withName(HADOOP_CONF_VOLUME)
.withNewConfigMap()
.withName(existingConfMap.get)
.endConfigMap()
.build()
}

val podWithConf = new PodBuilder(pod.pod)
.editSpec()
.addNewVolumeLike(confVolume)
.endVolume()
.endSpec()
.build()

val containerWithMount = new ContainerBuilder(pod.container)
.addNewVolumeMount()
.withName(HADOOP_CONF_VOLUME)
.withMountPath(HADOOP_CONF_DIR_PATH)
.endVolumeMount()
.addNewEnv()
.withName(ENV_HADOOP_CONF_DIR)
.withValue(HADOOP_CONF_DIR_PATH)
.endEnv()
.build()

SparkPod(podWithConf, containerWithMount)
}
}

override def getAdditionalKubernetesResources(): Seq[HasMetadata] = {
if (confDir.isDefined) {
val fileMap = confFiles.map { file =>
(file.getName(), Files.toString(file, StandardCharsets.UTF_8))
}.toMap.asJava

Seq(new ConfigMapBuilder()
.withNewMetadata()
.withName(newConfigMapName)
.endMetadata()
.withImmutable(true)
.addToData(fileMap)
.build())
} else {
Nil
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ private[spark] class ExecutorPodsAllocator(
s"StorageClass ${pvc.getSpec.getStorageClassName}")
kubernetesClient.persistentVolumeClaims().create(pvc)
}
if (resources.nonEmpty) {
kubernetesClient.resourceList(resources: _*).createOrReplace()
}
newlyCreatedExecutors(newExecutorId) = (resourceProfileId, clock.getTimeMillis())
logDebug(s"Requested executor with id $newExecutorId from Kubernetes.")
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private[spark] class KubernetesExecutorBuilder {
new MountSecretsFeatureStep(conf),
new EnvSecretsFeatureStep(conf),
new MountVolumesFeatureStep(conf),
new HadoopConfExecutorFeatureStep(conf),
new LocalDirsFeatureStep(conf)) ++ userFeatures

val spec = KubernetesExecutorSpec(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.features

import java.io.File
import java.nio.charset.StandardCharsets.UTF_8

import scala.collection.JavaConverters._

import com.google.common.io.Files
import io.fabric8.kubernetes.api.model.ConfigMap

import org.apache.spark.{SparkConf, SparkFunSuite}
import org.apache.spark.deploy.k8s._
import org.apache.spark.deploy.k8s.Constants._
import org.apache.spark.util.{SparkConfWithEnv, Utils}

class HadoopConfExecutorFeatureStepSuite extends SparkFunSuite {

import KubernetesFeaturesTestUtils._
import SecretVolumeUtils._

test("mount hadoop config map if defined") {
val sparkConf = new SparkConf(false)
.set(Config.KUBERNETES_HADOOP_CONF_CONFIG_MAP, "testConfigMap")
val conf = KubernetesTestConf.createExecutorConf(sparkConf = sparkConf)
val step = new HadoopConfExecutorFeatureStep(conf)
checkPod(step.configurePod(SparkPod.initialPod()))
assert(step.getAdditionalKubernetesResources().isEmpty)
}

test("create hadoop config map if config dir is defined") {
val confDir = Utils.createTempDir()
val confFiles = Set("core-site.xml", "hdfs-site.xml")

confFiles.foreach { f =>
Files.write("some data", new File(confDir, f), UTF_8)
}

val sparkConf = new SparkConfWithEnv(Map(ENV_HADOOP_CONF_DIR -> confDir.getAbsolutePath()))
val conf = KubernetesTestConf.createExecutorConf(sparkConf = sparkConf)

val step = new HadoopConfExecutorFeatureStep(conf)
checkPod(step.configurePod(SparkPod.initialPod()))

val hadoopConfMap = filter[ConfigMap](step.getAdditionalKubernetesResources()).head
assert(hadoopConfMap.getData().keySet().asScala === confFiles)
}

private def checkPod(pod: SparkPod): Unit = {
assert(podHasVolume(pod.pod, HADOOP_CONF_VOLUME))
assert(containerHasVolume(pod.container, HADOOP_CONF_VOLUME, HADOOP_CONF_DIR_PATH))
assert(containerHasEnvVar(pod.container, ENV_HADOOP_CONF_DIR))
}

}