<p>I am trying to make quick tweaks to the compute requests for a large number of deployments rather than having to get a PR approved if it turns out we need to change the numbers slightly. I need to be able to pass just one variable to the yaml file that gets applied.</p>
<p>Example below.</p>
<p>Shell Script:</p>
<pre><code>#!/bin/bash

APPCENTER=(pa nyc co nc md sc mi)
for ac in &quot;${APPCENTER[@]}&quot;
do
  kubectl apply -f jobs-reqtweaks.yaml --env=APPCENTER=$ac
  kubectl apply -f queue-reqtweaks.yaml --env=APPCENTER=$ac
  kubectl apply -f web-reqtweaks.yaml --env=APPCENTER=$ac
done
</code></pre>
<p>YAML deployment file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: example-app
    app.kubernetes.io/managed-by: Helm
    appcenter: $APPCENTER
    chart: example-app-0.1.0
    component: deployment-name
    heritage: Tiller
    release: example-app-fb-feature-tp-1121-fb-pod-resizing
  name: $APPCENTER-deployment-name
  namespace: example-app-fb-feature-tp-1121-fb-pod-resizing
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: example-app
      appcenter: $APPCENTER
      chart: example-app-0.1.0
      component: deployment-name
      heritage: Tiller
      release: example-app-fb-feature-tp-1121-fb-pod-resizing
  template:
    metadata:
      labels:
        app: example-app
        appcenter: $APPCENTER
        chart: example-app-0.1.0
        component: deployment-name
        heritage: Tiller
        release: example-app-fb-feature-tp-1121-fb-pod-resizing
    spec:
      containers:
        name: deployment-name
        resources:
          limits:
            memory: &quot;400Mi&quot;
            cpu: &quot;10m&quot;
          requests:
            cpu: 11m
            memory: 641Mi
</code></pre>

Nathan McKaskle
<p><code>kubectl apply</code> doesn't accept an <code>--env</code> flag (<code>kubectl apply --help</code>).</p>
<p>You have various choices:</p>
<ol>
<li><code>sed</code> -- treats the YAML as a text file</li>
<li><a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer"><code>yq</code></a> -- treats YAML as YAML</li>
</ol>
<p>Define constants:</p>
<pre class="lang-sh prettyprint-override"><code># The set of YAML files you want to change
FILES=(
  &quot;jobs-reqtweaks.yaml&quot;
  &quot;queue-reqtweaks.yaml&quot;
  &quot;web-reqtweaks.yaml&quot;
)

# The set of data-centers
APPCENTER=(pa nyc co nc md sc mi)
</code></pre>
<p>Then, either use <code>sed</code>:</p>
<pre class="lang-sh prettyprint-override"><code># Iterate over the data-centers
for ac in &quot;${APPCENTER[@]}&quot;
do
  # Iterate over the files
  for FILE in ${FILES[@]}
  do
    sed \
    --expression=&quot;s|\$APPCENTER|${ac}|g&quot; \
    ${FILE}| kubectl apply --filename=-
  done
done
</code></pre>
<p>Or use <a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer"><code>yq</code></a>:</p>
<pre class="lang-sh prettyprint-override"><code># Iterate over the data-centers
for ac in &quot;${APPCENTER[@]}&quot;
do
  # Iterate over the files
  for FILE in ${FILES[@]}
  do
    UPDATE=&quot;with(select(documentIndex==0)
    ; .metadata.labels.appcenter=\&quot;${ac}\&quot;
    | .metadata.name=\&quot;${ac}-deployment-name\&quot;
    | .spec.selector.matchLabels.appcenter=\&quot;${ac}\&quot;
    | .spec.template.metadata.labels.appcenter=\&quot;${ac}\&quot;
    )&quot;
    yq eval &quot;${UPDATE}&quot; \
    ${FILE}| kubectl apply --filename=-
  done
done
</code></pre>
