<p>I am writing a Kubernetes Spark Application using GCP spark on k8s.</p>
<p>Currently, I am stuck at not being able to inject environment variables into my container.</p>
<p>I am following the doc <a href="https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/blob/master/docs/user-guide.md#specifying-environment-variables" rel="nofollow noreferrer">here</a></p>
<p><strong>Manifest:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: &quot;sparkoperator.k8s.io/v1beta2&quot;
kind: SparkApplication
metadata:
  name: spark-search-indexer
  namespace: spark-operator
spec:
  type: Scala
  mode: cluster
  image: &quot;gcr.io/spark-operator/spark:v2.4.5&quot;
  imagePullPolicy: Always
  mainClass: com.quid.indexer.news.jobs.ESIndexingJob
  mainApplicationFile: &quot;https://lala.com/baba-0.0.43.jar&quot;
  arguments:
    - &quot;--esSink&quot;
    - &quot;http://something:9200/mo-sn-{yyyy-MM}-v0.0.43/searchable-article&quot;
    - &quot;-streaming&quot;
    - &quot;--kafkaTopics&quot;
    - &quot;annotated_blogs,annotated_ln_news,annotated_news&quot;
    - &quot;--kafkaBrokers&quot;
    - &quot;10.1.1.1:9092&quot;
  sparkVersion: &quot;2.4.5&quot;
  restartPolicy:
    type: Never
  volumes:
    - name: &quot;test-volume&quot;
      hostPath:
        path: &quot;/tmp&quot;
        type: Directory
  driver:
    cores: 1
    coreLimit: &quot;1200m&quot;
    memory: &quot;512m&quot;
    env:
      - name: &quot;DEMOGRAPHICS_ES_URI&quot;
        value: &quot;somevalue&quot;
    labels:
      version: 2.4.5
    volumeMounts:
      - name: &quot;test-volume&quot;
        mountPath: &quot;/tmp&quot;
  executor:
    cores: 1
    instances: 1
    memory: &quot;512m&quot;
    env:
      - name: &quot;DEMOGRAPHICS_ES_URI&quot;
        value: &quot;somevalue&quot;
    labels:
      version: 2.4.5
    volumeMounts:
      - name: &quot;test-volume&quot;
        mountPath: &quot;/tmp&quot;
</code></pre>
<p><strong>Environment Variables set at pod:</strong></p>
<pre><code>Environment:
      SPARK_DRIVER_BIND_ADDRESS:   (v1:status.podIP)
      SPARK_LOCAL_DIRS:           /var/data/spark-1ed8539d-b157-4fab-9aa6-daff5789bfb5
      SPARK_CONF_DIR:             /opt/spark/conf
</code></pre>

<p>It turns out to use this one must enable <code>webhooks</code> (how to set up in <a href="https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/blob/master/docs/quick-start-guide.md" rel="nofollow noreferrer">quick-start guide here</a>)</p>
<p>The other approach could be to use <code>envVars</code></p>
<p>Example:</p>
<pre class="lang-yaml prettyprint-override"><code>   spec:
       executor:
           envVars:
               DEMOGRAPHICS_ES_URI: &quot;somevalue&quot;
</code></pre>
<p>Ref: <a href="https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/issues/978" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/issues/978</a></p>
