|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# Prevent any errors from being silently ignored |
| 19 | +set -eo pipefail |
| 20 | + |
| 21 | +attempt_setup_fake_passwd_entry() { |
| 22 | + # Check whether there is a passwd entry for the container UID |
| 23 | + local myuid; myuid="$(id -u)" |
| 24 | + # If there is no passwd entry for the container UID, attempt to fake one |
| 25 | + # You can also refer to the https://github.com/docker-library/official-images/pull/13089#issuecomment-1534706523 |
| 26 | + # It's to resolve OpenShift random UID case. |
| 27 | + # See also: https://github.com/docker-library/postgres/pull/448 |
| 28 | + if ! getent passwd "$myuid" &> /dev/null; then |
| 29 | + local wrapper |
| 30 | + for wrapper in {/usr,}/lib{/*,}/libnss_wrapper.so; do |
| 31 | + if [ -s "$wrapper" ]; then |
| 32 | + NSS_WRAPPER_PASSWD="$(mktemp)" |
| 33 | + NSS_WRAPPER_GROUP="$(mktemp)" |
| 34 | + export LD_PRELOAD="$wrapper" NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP |
| 35 | + local mygid; mygid="$(id -g)" |
| 36 | + printf 'spark:x:%s:%s:${SPARK_USER_NAME:-anonymous uid}:%s:/bin/false\n' "$myuid" "$mygid" "$SPARK_HOME" > "$NSS_WRAPPER_PASSWD" |
| 37 | + printf 'spark:x:%s:\n' "$mygid" > "$NSS_WRAPPER_GROUP" |
| 38 | + break |
| 39 | + fi |
| 40 | + done |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +if [ -z "$JAVA_HOME" ]; then |
| 45 | + JAVA_HOME=$(java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home' | awk '{print $3}') |
| 46 | +fi |
| 47 | + |
| 48 | +SPARK_CLASSPATH="$SPARK_CLASSPATH:${SPARK_HOME}/jars/*" |
| 49 | +for v in "${!SPARK_JAVA_OPT_@}"; do |
| 50 | + SPARK_EXECUTOR_JAVA_OPTS+=( "${!v}" ) |
| 51 | +done |
| 52 | + |
| 53 | +if [ -n "$SPARK_EXTRA_CLASSPATH" ]; then |
| 54 | + SPARK_CLASSPATH="$SPARK_CLASSPATH:$SPARK_EXTRA_CLASSPATH" |
| 55 | +fi |
| 56 | + |
| 57 | +if ! [ -z "${PYSPARK_PYTHON+x}" ]; then |
| 58 | + export PYSPARK_PYTHON |
| 59 | +fi |
| 60 | +if ! [ -z "${PYSPARK_DRIVER_PYTHON+x}" ]; then |
| 61 | + export PYSPARK_DRIVER_PYTHON |
| 62 | +fi |
| 63 | + |
| 64 | +# If HADOOP_HOME is set and SPARK_DIST_CLASSPATH is not set, set it here so Hadoop jars are available to the executor. |
| 65 | +# It does not set SPARK_DIST_CLASSPATH if already set, to avoid overriding customizations of this value from elsewhere e.g. Docker/K8s. |
| 66 | +if [ -n "${HADOOP_HOME}" ] && [ -z "${SPARK_DIST_CLASSPATH}" ]; then |
| 67 | + export SPARK_DIST_CLASSPATH="$($HADOOP_HOME/bin/hadoop classpath)" |
| 68 | +fi |
| 69 | + |
| 70 | +if ! [ -z "${HADOOP_CONF_DIR+x}" ]; then |
| 71 | + SPARK_CLASSPATH="$HADOOP_CONF_DIR:$SPARK_CLASSPATH"; |
| 72 | +fi |
| 73 | + |
| 74 | +if ! [ -z "${SPARK_CONF_DIR+x}" ]; then |
| 75 | + SPARK_CLASSPATH="$SPARK_CONF_DIR:$SPARK_CLASSPATH"; |
| 76 | +elif ! [ -z "${SPARK_HOME+x}" ]; then |
| 77 | + SPARK_CLASSPATH="$SPARK_HOME/conf:$SPARK_CLASSPATH"; |
| 78 | +fi |
| 79 | + |
| 80 | +# SPARK-43540: add current working directory into executor classpath |
| 81 | +SPARK_CLASSPATH="$SPARK_CLASSPATH:$PWD" |
| 82 | + |
| 83 | +# Switch to spark if no USER specified (root by default) otherwise use USER directly |
| 84 | +switch_spark_if_root() { |
| 85 | + if [ $(id -u) -eq 0 ]; then |
| 86 | + echo gosu spark |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +spark_3_2_support(){ |
| 91 | + if ! printf '%s\n%s' "$1" "$2" | sort -C -V |
| 92 | + then |
| 93 | + # + 3.3.0 |
| 94 | + echo "org.apache.spark.scheduler.cluster.k8s.KubernetesExecutorBackend --podName $SPARK_EXECUTOR_POD_NAME" |
| 95 | + else |
| 96 | + # -3.0.0 |
| 97 | + echo "org.apache.spark.executor.CoarseGrainedExecutorBackend" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +KUBERNETES_EXECUTOR_BACKEND="$(spark_3_2_support $SPARK_VERSION '3.2.4')" |
| 102 | + |
| 103 | +case "$1" in |
| 104 | + driver) |
| 105 | + shift 1 |
| 106 | + CMD=( |
| 107 | + "$SPARK_HOME/bin/spark-submit" |
| 108 | + --conf "spark.driver.bindAddress=$SPARK_DRIVER_BIND_ADDRESS" |
| 109 | + --conf "spark.executorEnv.SPARK_DRIVER_POD_IP=$SPARK_DRIVER_BIND_ADDRESS" |
| 110 | + --deploy-mode client |
| 111 | + "$@" |
| 112 | + ) |
| 113 | + attempt_setup_fake_passwd_entry |
| 114 | + # Execute the container CMD under tini for better hygiene |
| 115 | + exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}" |
| 116 | + ;; |
| 117 | + executor) |
| 118 | + shift 1 |
| 119 | + CMD=( |
| 120 | + ${JAVA_HOME}/bin/java |
| 121 | + "${SPARK_EXECUTOR_JAVA_OPTS[@]}" |
| 122 | + -Xms"$SPARK_EXECUTOR_MEMORY" |
| 123 | + -Xmx"$SPARK_EXECUTOR_MEMORY" |
| 124 | + -cp "$SPARK_CLASSPATH:$SPARK_DIST_CLASSPATH" |
| 125 | + $KUBERNETES_EXECUTOR_BACKEND |
| 126 | + --driver-url "$SPARK_DRIVER_URL" |
| 127 | + --executor-id "$SPARK_EXECUTOR_ID" |
| 128 | + --cores "$SPARK_EXECUTOR_CORES" |
| 129 | + --app-id "$SPARK_APPLICATION_ID" |
| 130 | + --hostname "$SPARK_EXECUTOR_POD_IP" |
| 131 | + --resourceProfileId "$SPARK_RESOURCE_PROFILE_ID" |
| 132 | + ) |
| 133 | + attempt_setup_fake_passwd_entry |
| 134 | + # Execute the container CMD under tini for better hygiene |
| 135 | + exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}" |
| 136 | + ;; |
| 137 | + |
| 138 | + *) |
| 139 | + # Non-spark-on-k8s command provided, proceeding in pass-through mode... |
| 140 | + exec "$@" |
| 141 | + ;; |
| 142 | +esac |
0 commit comments