4

kustomize's docs provides a nice one-liner that compares two different overlays...

diff \
  <(kustomize build $OVERLAYS/staging) \
  <(kustomize build $OVERLAYS/production)

is there a way to do the same but against what is running within a specific kubernetes namespace and that of a defined overlay on disk?

more specifically, knowing what an kubectl apply -k . would do without actually doing it? using --dry-run just says spits out a list of the objects rather than a real diff.

yee379
  • 6,498
  • 10
  • 56
  • 101

5 Answers5

5
kustomize build ./ | kubectl diff -f  -

In Kustomize version 4.x.x

dotdat
  • 51
  • 1
  • 2
  • Can now be done with kubectl too, no need to install kustomize CLI: `kubectl kustomize | kubectl diff -f - ` – Maggie Jul 27 '23 at 20:33
4

If you're looking for a way to do this visually, I highly recommend trying the Compare & Sync feature from Monokle:

enter image description here

In the picture above you can see an example where I'm comparing the output of the cluster-install kustomization to the objects in my minikube cluster.

You can easily determine which resources are missing in your cluster and which ones are different.

On top of that, you're not limited to only comparing kustomizations to clusters. You can also compare two clusters, two kustomizations, helm charts, etc.

Dev Catalin
  • 1,265
  • 11
  • 25
1

I have a small function on my shell config to do this:

kdiff() {
  overlay="${1}"
  kustomize build ${overlay} \
    | kubectl diff -f - ${@:2} \
    | sed '/kubectl.kubernetes.io\/last-applied-configuration/,+1 d' \
    | sed -r "s/(^\+[^\+].*|^\+$)/$(printf '\e[0;32m')\1$(printf '\e[0m')/g" \
    | sed -r "s/(^\-[^\-].*|^\-$)/$(printf '\e[0;31m')\1$(printf '\e[0m')/g"
}

It drops the last-applied-configuration annotation and adds some color.

ITChap
  • 4,057
  • 1
  • 21
  • 46
0

I'm not sure if this is what you are looking for, but in Kubernetes you have kubectl diff.

It's nicely explained on APIServer dry-run and kubectl diff.

You can use option -k, --kustomize which does:

Process the kustomization directory. This flag can't be used together with -f or -R.

Or maybe something similar to one-liner to set context for specific namespace:

$ kubectl config set-context staging --user=cluster-admin --namespace=staging
$ kubectl config set-context prod --user=cluster-admin --namespace=prod

Once you have context setup you could use them maybe in a following way:

kubectl config use-context staging; cat patched_k8s.yaml | kubectl config use-context prod; kubectl diff -f -

This is just an example which I did not tested.

Crou
  • 10,232
  • 2
  • 26
  • 31
0

Try this kustomize command, currently in alpha:

KUSTOMIZE_ENABLE_ALPHA_COMMANDS=true kustomize resources diff -k your/kustomize/overlay

via https://kubernetes.slack.com/archives/C9A5ALABG/p1582738327027200?thread_ts=1582695987.023600&cid=C9A5ALABG