Reputation: 3063
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.
Example below.
Shell Script:
#!/bin/bash
APPCENTER=(pa nyc co nc md sc mi)
for ac in "${APPCENTER[@]}"
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
YAML deployment file:
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: "400Mi"
cpu: "10m"
requests:
cpu: 11m
memory: 641Mi
Upvotes: 0
Views: 1334
Reputation: 15490
Your script can use envsubst and be effective:
for i in pa nyc co nc md sc mi
do
export APPCENTER=$i
envsubst < your_sample_in_the_question.yaml | kubectl apply -f -
done
Side note:
...
spec:
containers:
- name: deployment-name
resources:
limits:
memory: "400Mi"
cpu: "10m" # <-- This limit should be greater than requested.
requests:
cpu: 11m # <-- ??
memory: 641Mi
Upvotes: 1
Reputation: 40111
kubectl apply
doesn't accept an --env
flag (kubectl apply --help
).
You have various choices:
sed
-- treats the YAML as a text fileyq
-- treats YAML as YAMLDefine constants:
# The set of YAML files you want to change
FILES=(
"jobs-reqtweaks.yaml"
"queue-reqtweaks.yaml"
"web-reqtweaks.yaml"
)
# The set of data-centers
APPCENTER=(pa nyc co nc md sc mi)
Then, either use sed
:
# Iterate over the data-centers
for ac in "${APPCENTER[@]}"
do
# Iterate over the files
for FILE in ${FILES[@]}
do
sed \
--expression="s|\$APPCENTER|${ac}|g" \
${FILE}| kubectl apply --filename=-
done
done
Or use yq
:
# Iterate over the data-centers
for ac in "${APPCENTER[@]}"
do
# Iterate over the files
for FILE in ${FILES[@]}
do
UPDATE="with(select(documentIndex==0)
; .metadata.labels.appcenter=\"${ac}\"
| .metadata.name=\"${ac}-deployment-name\"
| .spec.selector.matchLabels.appcenter=\"${ac}\"
| .spec.template.metadata.labels.appcenter=\"${ac}\"
)"
yq eval "${UPDATE}" \
${FILE}| kubectl apply --filename=-
done
done
Upvotes: 3