Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#13617] feat(k8s-job-executor): Pass the environment variables of a job template to the Spark driver and executors (#13948)

Jerry Shao7555479b9d38
diff --git a/plugins/k8s-job-executor/src/main/java/org/apache/gravitino/job/k8s/spark/SparkApplicationUtils.java b/plugins/k8s-job-executor/src/main/java/org/apache/gravitino/job/k8s/spark/SparkApplicationUtils.java
index 1a10510520..8ce2a8a6d2 100644
--- a/plugins/k8s-job-executor/src/main/java/org/apache/gravitino/job/k8s/spark/SparkApplicationUtils.java
+++ b/plugins/k8s-job-executor/src/main/java/org/apache/gravitino/job/k8s/spark/SparkApplicationUtils.java
@@ -86,6 +86,15 @@ public final class SparkApplicationUtils {
private static final String SPARK_JARS = "spark.jars";
private static final String SPARK_FILES = "spark.files";
private static final String SPARK_ARCHIVES = "spark.archives";
+ private static final String SPARK_DRIVER_ENV_PREFIX = "spark.kubernetes.driverEnv.";
+ private static final String SPARK_EXECUTOR_ENV_PREFIX = "spark.executorEnv.";
+
+ // The environment variable names that Spark passes to the executors: Spark 3.5 drops any other
+ // spark.executorEnv.* key with only a warning, while the driver would still get it. Kubernetes
+ // without the relaxed environment variable validation accepts the same names, except '.' and
+ // the names starting with '..', which are rejected separately.
+ private static final Pattern ENVIRONMENT_NAME_PATTERN =
+ Pattern.compile("[-._a-zA-Z][-._a-zA-Z0-9]*");
private static final Pattern URI_USER_INFO = Pattern.compile("(?<=://)[^/?#@]*@");
@@ -98,6 +107,12 @@ public final class SparkApplicationUtils {
* the Gravitino server, runs the jar of {@link K8sJobExecutorConfigs#SPARK_BUILTIN_JOBS_JAR}
* instead.
*
+ * <p>The environment variables of the job template are set for the driver and the executors
+ * through the Spark configurations {@code spark.kubernetes.driverEnv.*} and {@code
+ * spark.executorEnv.*}, which means their values are visible in the SparkApplication and in its
+ * pods. They override the default Spark configurations of the job executor, and the Spark
+ * configurations of the job template override them.
+ *
* @param context the context of the job run
* @param template the runtime job template, whose resources are the URIs given in the template
* @param configs the job executor configurations
@@ -106,15 +121,6 @@ public final class SparkApplicationUtils {
*/
public static GenericKubernetesResource buildSparkApplication(
JobContext context, SparkJobTemplate template, K8sJobExecutorConfigs configs) {
- if (!template.environments().isEmpty()) {
- throw new IllegalArgumentException(
- String.format(
- "Job template %s sets environment variables %s, which the k8s job executor doesn't"
- + " support yet. Pass them as Spark configurations instead, for example"
- + " spark.kubernetes.driverEnv.<name> and spark.executorEnv.<name>",
- template.name(), template.environments().keySet()));
- }
-
String executable = getExecutableUri(context, template, configs);
checkClusterReachable(executable);
template.jars().forEach(SparkApplicationUtils::checkClusterReachable);
@@ -122,6 +128,14 @@ public final class SparkApplicationUtils {
template.archives().forEach(SparkApplicationUtils::checkClusterReachable);
Map<String, String> sparkConf = new TreeMap<>(configs.sparkConf());
+ template
+ .environments()
+ .forEach(
+ (name, value) -> {
+ checkEnvironmentName(template, name);
+ sparkConf.put(SPARK_DRIVER_ENV_PREFIX + name, value);
+ sparkConf.put(SPARK_EXECUTOR_ENV_PREFIX + name, value);
+ });
sparkConf.putAll(template.configs());
// The operator only supports cluster mode, so a job that asks for client mode is rejected
// rather than silently run in cluster mode.
@@ -224,6 +238,20 @@ public final class SparkApplicationUtils {
return template.executable();
}
+ private static void checkEnvironmentName(SparkJobTemplate template, String name) {
+ // Checked here, as an invalid name would otherwise only miss the executors, or fail the job
+ // when the operator creates the driver pod.
+ if (!ENVIRONMENT_NAME_PATTERN.matcher(name).matches()
+ || ".".equals(name)
+ || name.startsWith("..")) {
+ throw new IllegalArgumentException(
+ String.format(
+ "Environment variable name '%s' of job template %s is invalid: it must match %s,"
+ + " must not be '.' and must not start with '..'",
+ name, template.name(), ENVIRONMENT_NAME_PATTERN.pattern()));
+ }
+ }
+
/**
* Checks that a resource of the job, or each of them if it is a comma-separated list as Spark
* accepts, is a URI that the cluster can reach rather than a path on the Gravitino server.
diff --git a/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java b/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
index fdb6b2e65b..39da670461 100644
--- a/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
+++ b/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
@@ -514,20 +514,76 @@ public class TestSparkApplicationUtils {
}
@Test
- public void testRejectEnvironments() {
+ @SuppressWarnings("unchecked")
+ public void testEnvironments() {
+ K8sJobExecutorConfigs withDefaults =
+ newConfigs(
+ ImmutableMap.of(
+ "spark.conf.spark.kubernetes.driverEnv.REGION",
+ "default-region",
+ "spark.conf.spark.executorEnv.STAGE",
+ "default-stage"));
SparkJobTemplate template =
SparkJobTemplate.builder()
.withName("user-job")
.withExecutable("https://repo/app.jar")
.withClassName("org.example.App")
- .withEnvironments(ImmutableMap.of("TOKEN", "secret"))
+ .withEnvironments(ImmutableMap.of("REGION", "us-west", "app.mode-1", "", "TZ", "UTC"))
+ .withConfigs(ImmutableMap.of("spark.executorEnv.TZ", "Asia/Shanghai"))
.build();
- IllegalArgumentException e =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () -> SparkApplicationUtils.buildSparkApplication(CONTEXT, template, configs));
- Assertions.assertTrue(e.getMessage().contains("TOKEN"));
- Assertions.assertFalse(e.getMessage().contains("secret"));
+
+ Map<String, String> sparkConf =
+ (Map<String, String>) getSpec(template, withDefaults).get("sparkConf");
+
+ // Set for the driver and the executors, in place of the defaults of the job executor.
+ Assertions.assertEquals("us-west", sparkConf.get("spark.kubernetes.driverEnv.REGION"));
+ Assertions.assertEquals("us-west", sparkConf.get("spark.executorEnv.REGION"));
+ Assertions.assertEquals("", sparkConf.get("spark.kubernetes.driverEnv.app.mode-1"));
+ Assertions.assertEquals("", sparkConf.get("spark.executorEnv.app.mode-1"));
+ // The Spark configurations of the job template take precedence.
+ Assertions.assertEquals("UTC", sparkConf.get("spark.kubernetes.driverEnv.TZ"));
+ Assertions.assertEquals("Asia/Shanghai", sparkConf.get("spark.executorEnv.TZ"));
+ // A default that the job template doesn't set is kept.
+ Assertions.assertEquals("default-stage", sparkConf.get("spark.executorEnv.STAGE"));
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testAcceptEnvironmentNames() {
+ for (String valid : new String[] {"-X", ".X", "_X", "a.b-c_1", ".a.."}) {
+ SparkJobTemplate template =
+ SparkJobTemplate.builder()
+ .withName("user-job")
+ .withExecutable("https://repo/app.jar")
+ .withClassName("org.example.App")
+ .withEnvironments(ImmutableMap.of(valid, "v"))
+ .build();
+ Map<String, String> sparkConf =
+ (Map<String, String>) getSpec(template, configs).get("sparkConf");
+ Assertions.assertEquals("v", sparkConf.get("spark.kubernetes.driverEnv." + valid), valid);
+ Assertions.assertEquals("v", sparkConf.get("spark.executorEnv." + valid), valid);
+ }
+ }
+
+ @Test
+ public void testRejectInvalidEnvironmentName() {
+ for (String invalid : new String[] {"", "MY VAR", "A=B", "1ST", "PATH/X", ".", "..", "..CFG"}) {
+ SparkJobTemplate template =
+ SparkJobTemplate.builder()
+ .withName("user-job")
+ .withExecutable("https://repo/app.jar")
+ .withClassName("org.example.App")
+ .withEnvironments(ImmutableMap.of(invalid, "secret-value"))
+ .build();
+ IllegalArgumentException e =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> SparkApplicationUtils.buildSparkApplication(CONTEXT, template, configs));
+ Assertions.assertTrue(e.getMessage().contains("user-job"), e.getMessage());
+ Assertions.assertTrue(e.getMessage().contains("'" + invalid + "'"), e.getMessage());
+ // The value of an environment variable never goes into an error message.
+ Assertions.assertFalse(e.getMessage().contains("secret-value"), e.getMessage());
+ }
}
public static K8sJobExecutorConfigs newConfigs(Map<String, String> extra) {