-
Notifications
You must be signed in to change notification settings - Fork 28k
/
KubernetesClientUtils.scala
174 lines (156 loc) · 6.91 KB
/
KubernetesClientUtils.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
/*
* 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.submit
import java.io.{File, StringWriter}
import java.nio.charset.MalformedInputException
import java.util.Properties
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.io.{Codec, Source}
import io.fabric8.kubernetes.api.model.{ConfigMap, ConfigMapBuilder, KeyToPath}
import org.apache.spark.SparkConf
import org.apache.spark.deploy.k8s.{Config, Constants, KubernetesUtils}
import org.apache.spark.deploy.k8s.Constants.ENV_SPARK_CONF_DIR
import org.apache.spark.internal.Logging
private[spark] object KubernetesClientUtils extends Logging {
// Config map name can be 63 chars at max.
def configMapName(prefix: String): String = s"${prefix.take(54)}-conf-map"
val configMapNameExecutor: String = configMapName(s"spark-exec-${KubernetesUtils.uniqueID()}")
val configMapNameDriver: String = configMapName(s"spark-drv-${KubernetesUtils.uniqueID()}")
private def buildStringFromPropertiesMap(configMapName: String,
propertiesMap: Map[String, String]): String = {
val properties = new Properties()
propertiesMap.foreach { case (k, v) =>
properties.setProperty(k, v)
}
val propertiesWriter = new StringWriter()
properties.store(propertiesWriter,
s"Java properties built from Kubernetes config map with name: $configMapName")
propertiesWriter.toString
}
/**
* Build, file -> 'file's content' map of all the selected files in SPARK_CONF_DIR.
*/
def buildSparkConfDirFilesMap(
configMapName: String,
sparkConf: SparkConf,
resolvedPropertiesMap: Map[String, String]): Map[String, String] = synchronized {
val loadedConfFilesMap = KubernetesClientUtils.loadSparkConfDirFiles(sparkConf)
// Add resolved spark conf to the loaded configuration files map.
if (resolvedPropertiesMap.nonEmpty) {
val resolvedProperties: String = KubernetesClientUtils
.buildStringFromPropertiesMap(configMapName, resolvedPropertiesMap)
loadedConfFilesMap ++ Map(Constants.SPARK_CONF_FILE_NAME -> resolvedProperties)
} else {
loadedConfFilesMap
}
}
def buildKeyToPathObjects(confFilesMap: Map[String, String]): Seq[KeyToPath] = {
confFilesMap.map {
case (fileName: String, _: String) =>
val filePermissionMode = 420 // 420 is decimal for octal literal 0644.
new KeyToPath(fileName, filePermissionMode, fileName)
}.toList.sortBy(x => x.getKey) // List is sorted to make mocking based tests work
}
/**
* Build a Config Map that will hold the content for environment variable SPARK_CONF_DIR
* on remote pods.
*/
def buildConfigMap(configMapName: String, confFileMap: Map[String, String],
withLabels: Map[String, String] = Map()): ConfigMap = {
new ConfigMapBuilder()
.withNewMetadata()
.withName(configMapName)
.withLabels(withLabels.asJava)
.endMetadata()
.withImmutable(true)
.addToData(confFileMap.asJava)
.build()
}
private def orderFilesBySize(confFiles: Seq[File]): Seq[File] = {
val fileToFileSizePairs = confFiles.map(f => (f, f.getName.length + f.length()))
// sort first by name and then by length, so that during tests we have consistent results.
fileToFileSizePairs.sortBy(f => f._1).sortBy(f => f._2).map(_._1)
}
// exposed for testing
private[submit] def loadSparkConfDirFiles(conf: SparkConf): Map[String, String] = {
val confDir = Option(conf.getenv(ENV_SPARK_CONF_DIR)).orElse(
conf.getOption("spark.home").map(dir => s"$dir/conf"))
val maxSize = conf.get(Config.CONFIG_MAP_MAXSIZE)
if (confDir.isDefined) {
val confFiles: Seq[File] = listConfFiles(confDir.get, maxSize)
val orderedConfFiles = orderFilesBySize(confFiles)
var truncatedMapSize: Long = 0
val truncatedMap = mutable.HashMap[String, String]()
val skippedFiles = mutable.HashSet[String]()
var source: Source = Source.fromString("") // init with empty source.
for (file <- orderedConfFiles) {
try {
source = Source.fromFile(file)(Codec.UTF8)
val (fileName, fileContent) = file.getName -> source.mkString
if ((truncatedMapSize + fileName.length + fileContent.length) < maxSize) {
truncatedMap.put(fileName, fileContent)
truncatedMapSize = truncatedMapSize + (fileName.length + fileContent.length)
} else {
skippedFiles.add(fileName)
}
} catch {
case e: MalformedInputException =>
logWarning(
s"Unable to read a non UTF-8 encoded file ${file.getAbsolutePath}. Skipping...", e)
None
} finally {
source.close()
}
}
if (truncatedMap.nonEmpty) {
logInfo(s"Spark configuration files loaded from $confDir :" +
s" ${truncatedMap.keys.mkString(",")}")
}
if (skippedFiles.nonEmpty) {
logWarning(s"Skipped conf file(s) ${skippedFiles.mkString(",")}, due to size constraint." +
s" Please see, config: `${Config.CONFIG_MAP_MAXSIZE.key}` for more details.")
}
truncatedMap.toMap
} else {
Map.empty[String, String]
}
}
private def listConfFiles(confDir: String, maxSize: Long): Seq[File] = {
// At the moment configmaps do not support storing binary content (i.e. skip jar,tar,gzip,zip),
// and configMaps do not allow for size greater than 1.5 MiB(configurable).
// https://etcd.io/docs/v3.4.0/dev-guide/limit/
def testIfTooLargeOrBinary(f: File): Boolean = (f.length() + f.getName.length > maxSize) ||
f.getName.matches(".*\\.(gz|zip|jar|tar)")
// We exclude all the template files and user provided spark conf or properties,
// Spark properties are resolved in a different step.
def testIfSparkConfOrTemplates(f: File) = f.getName.matches(".*\\.template") ||
f.getName.matches("spark.*(conf|properties)")
val fileFilter = (f: File) => {
f.isFile && !testIfTooLargeOrBinary(f) && !testIfSparkConfOrTemplates(f)
}
val confFiles: Seq[File] = {
val dir = new File(confDir)
if (dir.isDirectory) {
dir.listFiles.filter(x => fileFilter(x)).toSeq
} else {
Nil
}
}
confFiles
}
}