diff

diffs the online configuration with local config

Diff configurations specified by filename or stdin between the current online configuration, and the configuration as it would be if applied.

Output is always YAML.

KUBECTL_EXTERNAL_DIFF environment variable can be used to select your own diff command. By default, the “diff” command available in your path will be run with “-u” (unified diff) and “-N” (treat absent files as empty) options.

Exit status

  • 0 No differences were found.
  • 1 Differences were found.
  • >1 Kubectl or diff failed with an error.

Command

  1. $ kubectl diff -f FILENAME

Example

Input File

  1. # deployment.yaml - online configuration
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-dev
  6. labels:
  7. app: nginx
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1.14.2
  1. # deployment.yaml - local configuration
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-dev
  6. labels:
  7. app: nginx
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:latest

Notice that the local configuration refers to the latest nginx container from the registry.

Command

  1. kubectl diff -f deployment.yaml

Output

  1. diff -u -N /tmp/LIVE-435797985/apps.v1.Deployment.default.nginx-dev /tmp/MERGED-822429644/apps.v1.Deployment.default.nginx-dev
  2. /tmp/LIVE-435797985/apps.v1.Deployment.default.nginx-dev 2020-09-20 14:50:30.160820677 +0000
  3. +++ /tmp/MERGED-822429644/apps.v1.Deployment.default.nginx-dev 2020-09-20 14:50:30.172820784 +0000
  4. @@ -6,7 +6,7 @@
  5. kubectl.kubernetes.io/last-applied-configuration: |
  6. {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-dev","namespace":"default"},"spec":{"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.14.2","name":"nginx"}]}}}}
  7. creationTimestamp: "2020-09-20T14:48:35Z"
  8. - generation: 1
  9. + generation: 2
  10. name: nginx-dev
  11. namespace: default
  12. resourceVersion: "2180"
  13. @@ -31,7 +31,7 @@
  14. app: nginx
  15. spec:
  16. containers:
  17. - - image: nginx:1.14.2
  18. + - image: nginx:latest
  19. imagePullPolicy: IfNotPresent
  20. name: nginx
  21. resources: {}
  22. exit status 1

Generating a Diff

Use the diff program in a user’s path to display a diff of the changes that will be made by Apply.

  1. kubectl diff -k ./dir/

Setting the Diff Program

The KUBECTL_EXTERNAL_DIFF environment variable can be used to select your own diff command. By default, the “diff” command available in your path will be run with “-u” (unified) and “-N” (treat new files as empty) options.

  1. export KUBECTL_EXTERNAL_DIFF=meld; kubectl diff -k ./dir/