-
-
Save thikade/6a619d7550c50d8d15e19822248e6624 to your computer and use it in GitHub Desktop.
helm-vdiff: A shell script to compare Helm chart values across versions, whether remote, OCI, or installed releases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| helm-vdiff() { | |
| local namespace="default" values="" value_key="" | |
| local diff_cmd=(dyff between --omit-header) | |
| local positional=() | |
| # argument parsing | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -n|--namespace) namespace="$2"; shift 2;; | |
| -v|--values) values="$2"; shift 2;; | |
| -k|--value-key) value_key="$2"; shift 2;; | |
| -i|--icdiff) diff_cmd=(icdiff --highlight -U5); shift;; | |
| -*) echo "Unknown option: $1"; return 1;; | |
| *) positional+=("$1"); shift;; | |
| esac | |
| done | |
| # restore positional parameters | |
| set -- "${positional[@]}" | |
| local left="$1" | |
| local right="$2" | |
| if [[ -z "$left" || -z "$right" ]]; then | |
| echo "Usage:" | |
| echo " helm-vdiff [options] <source> <target>" | |
| echo | |
| echo "Remote vs Remote:" | |
| echo " helm-vdiff cnpg/cloudnative-pg:0.18.0 cnpg/cloudnative-pg:0.18.1" | |
| echo " helm-vdiff cnpg/cloudnative-pg:0.18.0 oci://registry-1.docker.io/bitnamicharts/postgresql:18.0.9" | |
| echo | |
| echo "OCI vs OCI:" | |
| echo " helm-vdiff oci://quay.io/cilium/charts/cilium:1.19.0 oci://quay.io/cilium/charts/cilium:1.19.1" | |
| echo | |
| echo "Repo vs OCI:" | |
| echo " helm-vdiff cilium/cilium:1.19.0 oci://quay.io/cilium/charts/cilium:1.19.1" | |
| echo | |
| echo "Installed Release vs Remote:" | |
| echo " helm-vdiff -n kube-system kube-system-cilium oci://quay.io/cilium/charts/cilium:1.18.6" | |
| echo | |
| echo "With extra values:" | |
| echo " helm-vdiff cnpg/cloudnative-pg:0.23.1 cnpg/cloudnative-pg:0.23.2 -v values.yaml" | |
| echo | |
| echo "With values subkey:" | |
| echo " helm-vdiff cnpg/cloudnative-pg:0.23.1 cnpg/cloudnative-pg:0.23.2 -v values.yaml -k cloudnative-pg" | |
| echo | |
| echo "Useful:" | |
| echo " oras repo tags quay.io/cilium/charts/cilium" | |
| echo " helm repo add cilium https://helm.cilium.io/" | |
| echo " helm search repo cilium --versions" | |
| echo | |
| echo "Options:" | |
| echo " -n, --namespace Namespace for installed release" | |
| echo " -v, --values Extra values.yaml" | |
| echo " -k, --value-key Only compare subkey of values" | |
| echo " -i, --icdiff Use icdiff instead of dyff" | |
| return 1 | |
| fi | |
| get_values() { | |
| local ref="$1" | |
| # OCI or repo/chart:version | |
| if [[ "$ref" == oci://* || "$ref" == */*:* ]]; then | |
| local chart="${ref%:*}" | |
| local version="${ref##*:}" | |
| helm show values "$chart" --version "$version" 2> /dev/null | yq --prettyPrint eval '... comments="" | sortKeys(..)' | sed 's/: ~$/: null/' | |
| # repo/chart (no version) | |
| elif [[ "$ref" == */* ]]; then | |
| helm show values "$ref" 2> /dev/null | yq --prettyPrint eval '... comments="" | sortKeys(..)' | sed 's/: ~$/: null/' | |
| # assume installed release | |
| else | |
| helm get values --all "$ref" -n "$namespace" | tail -n +2 | |
| fi | |
| } | |
| left_values=$(get_values "$left") | |
| right_values=$(get_values "$right") | |
| # merge extra values (only into left side) | |
| if [[ -n "$values" ]]; then | |
| if [[ -n "$value_key" ]]; then | |
| left_values=$(yq eval-all \ | |
| "select(fileIndex == 0) * select(fileIndex == 1)" \ | |
| <(echo "$left_values") \ | |
| <(yq eval ".${value_key}" "$values")) | |
| else | |
| left_values=$(yq eval-all \ | |
| "select(fileIndex == 0) * select(fileIndex == 1)" \ | |
| <(echo "$left_values") \ | |
| "$values") | |
| fi | |
| fi | |
| "${diff_cmd[@]}" \ | |
| <(echo "$left_values" | yq eval 'sortKeys(..)' -) \ | |
| <(echo "$right_values" | yq eval 'sortKeys(..)' -) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment