Skip to content

Instantly share code, notes, and snippets.

@jonhiggs
Created April 29, 2020 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonhiggs/ad410b5cc7ad104daed69fdcefc4b48c to your computer and use it in GitHub Desktop.
Save jonhiggs/ad410b5cc7ad104daed69fdcefc4b48c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
[[ $# -eq 0 ]] && echo "Usage: $0 <kubernetes_manifest_file> ..." && exit 1
_error() {
echo "$@" >&2
}
api_versions() {
awk 'BEGIN {}
($1~/kind:/) { kind=$2 }
($1~/apiVersion:/) { api_version=$2; nr=NR }
(kind!="" && api_version!="") {
print nr, kind, api_version
kind=""; api_version=""
}
' /dev/stdin
}
api_new_versions() {
awk '($2=="Deployment") && ($3=="apps/v1beta1") { $3="apps/v1" }
($2=="Deployment") && ($3=="extensions/v1beta1") { $3="apps/v1" }
($2=="DaemonSet") && ($3=="extensions/v1beta1") { $3="apps/v1" }
($2=="Ingress") && ($3=="extensions/v1beta1") { $3="networking.k8s.io/v1beta1" }
{ print $0 }' \
/dev/stdin
}
for file in "$@"; do
[[ ! -f "${file}" ]] && _error "Skipping non-existent '${file}'" && continue
new_file="$1.tmp"
cp "${file}" "${new_file}"
IFS=$'\n'
for l in $(cat "${file}" | api_versions | api_new_versions); do
nr=$(awk '{ print $1 }' <(echo "$l"))
v=$(awk '{ print $3 }' <(echo "$l"))
sed -i".bak" "${nr}s!:.*!: ${v}!" "${new_file}"; rm "${new_file}.bak"
done
diff "${new_file}" "${file}"
mv "${new_file}" "${file}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment