|
1 | 1 | # Kubectl Real Name Diff
|
2 | 2 |
|
| 3 | + |
| 4 | + |
| 5 | +A kubectl plugin that diffs live and local resources ignoring Kustomize hash-suffixes. |
| 6 | + |
3 | 7 | ## What's this ?
|
4 |
| -If realname is specified in the label, diff between resources with the same label. |
5 |
| -Other than that, it works just like normal kubectl diff. |
| 8 | +Normally, `kubectl realname-diff` works the same as `kubectl diff`, but if you set |
| 9 | +"real name" as a label, local and live resources with the same label will be compared. |
| 10 | + |
| 11 | +This is especially beneficial if you use the Kustomize and enable the hash suffixed |
| 12 | +ConfigMap/Secret name. In case of `kubectl diff`, local and live resources with hash |
| 13 | +suffixed name are considered as irrelevant. So you will not be able to get any results |
| 14 | +comparing them. |
| 15 | + |
| 16 | +However, with realname-diff, you can compare the resources with hash suffixed name by |
| 17 | +specifying the comparison target with "real name" labels. |
| 18 | + |
| 19 | +## Usage |
| 20 | +First, you have set the label `realname-diff/realname: [real name]` to the resources |
| 21 | +you want to diff ignoring difference of `{.metadata.name}`. |
| 22 | + |
| 23 | +In Kustomize, you can set the label in ConfigMap/Secret Generator fields. |
| 24 | + |
| 25 | +```yaml |
| 26 | +resources: |
| 27 | + # resources that have references to the ConfigMap "nginx-conf" |
| 28 | + - deployment.yaml |
| 29 | + |
| 30 | +configMapGenerator: |
| 31 | + - name: nginx-conf |
| 32 | + files: |
| 33 | + - conf/nginx.conf |
| 34 | + options: |
| 35 | + labels: |
| 36 | + realname-diff/realname: nginx-conf |
| 37 | + # hash suffix is enabled by default |
| 38 | +``` |
| 39 | + |
| 40 | +Then, apply the manifests to your kubernetes cluster. |
| 41 | + |
| 42 | +```bash |
| 43 | +# assume you have placed the kustomization.yaml in "./example" directory |
| 44 | +$ kustomize build -f ./example | kubectl apply -f - |
| 45 | + |
| 46 | +# you can get the ConfigMap with hash suffixed name. |
| 47 | +$ kubectl get configmap --show-labels |
| 48 | +NAME DATA AGE LABELS |
| 49 | +nginx-conf-m5d2cggb7k 1 18s realname-diff/realname=nginx-conf |
| 50 | +``` |
6 | 51 |
|
7 |
| -This kubectl plugin assumes that you want to create ConfigMap, Secret with hash |
8 |
| -suffix in Kustomize. |
9 |
| -By specifying the real name without the hash suffix as a label, you can compare |
10 |
| -the old and new resources regardless of the hash value.`) |
| 52 | +Next, edit the content you want to pack into ConfigMap and use `kubectl realname-diff` |
| 53 | +to diff the local ConfigMap and the live one. |
11 | 54 |
|
12 |
| -## Example |
| 55 | +```bash |
| 56 | +$ echo "# test" >> ./example/conf/nginx.conf |
13 | 57 |
|
| 58 | +$ kustomize build -f ./example | kubectl realname-diff -f - |
14 | 59 | ```
|
15 |
| -# Diff resources included in the result of kustomize build |
16 |
| -$ kustomize build ./example | kubectl realname-diff -f - |
17 | 60 |
|
18 |
| -# Also you can use kubectl's built-in kustomize |
19 |
| -$ kubectl realname-diff -k ./example`) |
| 61 | +You can see the diff result comparing contents in the ConfigMap. The local ConfigMap |
| 62 | +is not treated as new one. |
| 63 | + |
| 64 | +```diff |
| 65 | +diff -u -N /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/LIVE-116798495/apps.v1.Deployment.default.nginx /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/MERGED-2431241138/apps.v1.Deployment.default.nginx |
| 66 | +--- /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/LIVE-116798495/apps.v1.Deployment.default.nginx 2021-12-24 00:04:23.000000000 +0900 |
| 67 | ++++ /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/MERGED-2431241138/apps.v1.Deployment.default.nginx 2021-12-24 00:04:23.000000000 +0900 |
| 68 | +@@ -6,7 +6,7 @@ |
| 69 | + kubectl.kubernetes.io/last-applied-configuration: | |
| 70 | + {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx","prunable":"true"},"name":"nginx","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"app":"nginx","prunable":"true"}},"template":{"metadata":{"labels":{"app":"nginx","prunable":"true"}},"spec":{"containers":[{"image":"nginx:latest","name":"nginx","ports":[{"containerPort":80}],"volumeMounts":[{"mountPath":"/etc/nginx/htpasswd","name":"htpasswd","readOnly":true},{"mountPath":"/etc/nginx","name":"nginx-conf","readOnly":true}]}],"volumes":[{"name":"htpasswd","secret":{"secretName":"htpasswd-k7mbh9mm68"}},{"configMap":{"name":"nginx-conf-m5d2cggb7k"},"name":"nginx-conf"}]}}}} |
| 71 | + creationTimestamp: "2021-12-23T15:00:12Z" |
| 72 | +- generation: 1 |
| 73 | ++ generation: 2 |
| 74 | + labels: |
| 75 | + app: nginx |
| 76 | + prunable: "true" |
| 77 | +@@ -177,7 +177,7 @@ |
| 78 | + secretName: htpasswd-k7mbh9mm68 |
| 79 | + - configMap: |
| 80 | + defaultMode: 420 |
| 81 | +- name: nginx-conf-m5d2cggb7k |
| 82 | ++ name: nginx-conf-b6gmtkgcd5 |
| 83 | + name: nginx-conf |
| 84 | + status: |
| 85 | + availableReplicas: 2 |
| 86 | +diff -u -N /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/LIVE-116798495/v1.ConfigMap.default.nginx-conf-m5d2cggb7k /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/MERGED-2431241138/v1.ConfigMap.default.nginx-conf-m5d2cggb7k |
| 87 | +--- /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/LIVE-116798495/v1.ConfigMap.default.nginx-conf-m5d2cggb7k 2021-12-24 00:04:23.000000000 +0900 |
| 88 | ++++ /var/folders/2n/lgqgy6f151l5mw1x4dj_7ztw0000gn/T/MERGED-2431241138/v1.ConfigMap.default.nginx-conf-m5d2cggb7k 2021-12-24 00:04:23.000000000 +0900 |
| 89 | +@@ -16,12 +16,10 @@ |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | ++ # test |
| 94 | + kind: ConfigMap |
| 95 | + metadata: |
| 96 | + ...(snip last-applied-configuration label).. |
| 97 | + labels: |
| 98 | + prunable: "true" |
| 99 | + realname-diff/realname: nginx-conf |
| 100 | +@@ -33,17 +31,13 @@ |
| 101 | + ...(snip managed fields)... |
| 102 | + manager: kubectl-client-side-apply |
| 103 | + operation: Update |
| 104 | +- time: "2021-12-23T15:00:12Z" |
| 105 | +- name: nginx-conf-m5d2cggb7k |
| 106 | ++ time: "2021-12-23T15:04:23Z" |
| 107 | ++ name: nginx-conf-b6gmtkgcd5 |
| 108 | + namespace: default |
| 109 | +- resourceVersion: "82594" |
| 110 | +- uid: 6a5d347c-d936-49c6-825b-70e2e39eb676 |
| 111 | ++ uid: 0d69cf40-d201-47fe-bad2-8c3333ef0d07 |
20 | 112 | ```
|
| 113 | + |
| 114 | +For a complete example, see the [example directory](./example). |
| 115 | + |
| 116 | +## License |
| 117 | +Kubectl Real Name Diff is licensed under the Apache License 2.0, and includes works |
| 118 | +distributed under same one. |
0 commit comments