Skip to content

Instantly share code, notes, and snippets.

@keegancsmith
Created October 28, 2016 07:58
Show Gist options
  • Save keegancsmith/81fed05aa33b460f7697156fbd016091 to your computer and use it in GitHub Desktop.
Save keegancsmith/81fed05aa33b460f7697156fbd016091 to your computer and use it in GitHub Desktop.
A way to more easily bulk update `k8s` specs, requires modification for your use case. Thought I'd share, since there is no nice way in k8s to partially update a bunch of resources. In this case I wanted to update resource constraints of most our deployments. `find some-magic-here | xargs -n1 ./k8s-patch.sh force`
#!/bin/bash
# Use this script as an example of how to patch k8s resources. It currently
# patches the resource constraints.
set -e
ns=$(cat "$2" | jq -r '.metadata.namespace')
name=$(cat "$2" | jq -r '.kind + "/" + .metadata.name')
patch=$(cat "$2" | jq -c '{spec: {template: {spec: {containers: [.spec.template.spec.containers[] | {name: .name, resources: .resources}]}}}}')
if [ "$1" = "dry-run" ]; then
echo kubectl -n $ns patch $name -p "$patch"
elif [ "$1" = "force" ]; then
set -x
kubectl -n $ns patch $name -p "$patch"
else
echo "USAGE: k8s-patch.sh [dry-run|force] spec.json"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment