zaki work log

作業ログやら生活ログやらなんやら

kubectl patchコマンド実行のときの差分を確認する

本来であればマニフェストファイルを更新してkubectl applyの替わりにkubectl diffを実行すれば、リソースの変更箇所をunified diff形式で確認できる。
しかし、いろいろな事情でマニフェストファイルを管理できておらず、リソースの変更をkubectl patchでバッチ処理で変更したい…という場面で、変更差分ってそういえばスマートに確認できないな?と思ったので、kubectl patch実行時の差分を見るためにどうするのが良いか試してみた。

結論としては、「kubectl patchをドライラン + "-o yaml"でYAML出力し、kubectl diffの入力に渡す」のが単純で良さそう。

お題

先日のkube-rbac-proxyコンテナイメージのアドレス変更の差分を確認する。
(実際は"gcr.io"から"quay.io"への更新だが、手元の環境はすでに更新済みのため、タグを書き換えてみる)

実行するコマンドは以下で、--dry-run=server -o yaml | kubectl diff -f -が追加分。

kubectl patch -n awx deploy awx-operator-controller-manager -p \
  '{ "spec": { "template": {"spec": { "containers": [ {"name":"kube-rbac-proxy", "image":"quay.io/brancz/kube-rbac-proxy:v0.16.0"}] }} }}' \
  --dry-run=server -o yaml | kubectl diff -f -

実行結果

この通り、(メタデータを除いて)image:の行が更新されることを確認できる。

$ kubectl patch -n awx deploy awx-operator-controller-manager -p \
  '{ "spec": { "template": {"spec": { "containers": [ {"name":"kube-rbac-proxy", "image":"quay.io/brancz/kube-rbac-proxy:v0.16.0"}] }} }}' \
  --dry-run=server -o yaml | kubectl diff -f -
diff -u -N /tmp/LIVE-1763242934/apps.v1.Deployment.awx.awx-operator-controller-manager /tmp/MERGED-2888458641/apps.v1.Deployment.awx.awx-operator-controller-manager
--- /tmp/LIVE-1763242934/apps.v1.Deployment.awx.awx-operator-controller-manager 2026-04-10 12:43:10.035810285 +0900
+++ /tmp/MERGED-2888458641/apps.v1.Deployment.awx.awx-operator-controller-manager       2026-04-10 12:43:10.036810354 +0900
@@ -6,7 +6,7 @@
     kubectl.kubernetes.io/last-applied-configuration: |
       {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"control-plane":"controller-manager"},"name":"awx-operator-controller-manager","namespace":"awx"},"spec":{"replicas":1,"selector":{"matchLabels":{"control-plane":"controller-manager"}},"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/default-container":"awx-manager"},"labels":{"control-plane":"controller-manager"}},"spec":{"containers":[{"args":["--secure-listen-address=0.0.0.0:8443","--upstream=http://127.0.0.1:8080/","--logtostderr=true","--v=0"],"image":"gcr.io/kubebuilder/kube-rbac-proxy:v0.15.0","name":"kube-rbac-proxy","ports":[{"containerPort":8443,"name":"https","protocol":"TCP"}],"resources":{"limits":{"cpu":"500m","memory":"128Mi"},"requests":{"cpu":"5m","memory":"64Mi"}},"securityContext":{"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]}}},{"args":["--health-probe-bind-address=:6789","--metrics-bind-address=127.0.0.1:8080","--leader-elect","--leader-election-id=awx-operator"],"env":[{"name":"ANSIBLE_GATHERING","value":"explicit"},{"name":"ANSIBLE_DEBUG_LOGS","value":"false"},{"name":"WATCH_NAMESPACE","valueFrom":{"fieldRef":{"fieldPath":"metadata.namespace"}}}],"image":"quay.io/ansible/awx-operator:2.19.1","imagePullPolicy":"IfNotPresent","livenessProbe":{"httpGet":{"path":"/healthz","port":6789},"initialDelaySeconds":15,"periodSeconds":20},"name":"awx-manager","readinessProbe":{"httpGet":{"path":"/readyz","port":6789},"initialDelaySeconds":5,"periodSeconds":10},"resources":{"limits":{"cpu":"1500m","memory":"960Mi"},"requests":{"cpu":"50m","memory":"32Mi"}},"securityContext":{"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]}}}],"imagePullSecrets":[{"name":"redhat-operators-pull-secret"}],"securityContext":{"runAsNonRoot":true},"serviceAccountName":"awx-operator-controller-manager","terminationGracePeriodSeconds":10}}}}
   creationTimestamp: "2024-11-24T08:53:09Z"
-  generation: 2
+  generation: 3
   labels:
     control-plane: controller-manager
   name: awx-operator-controller-manager
@@ -39,7 +39,7 @@
         - --upstream=http://127.0.0.1:8080/
         - --logtostderr=true
         - --v=0
-        image: quay.io/brancz/kube-rbac-proxy:v0.15.0
+        image: quay.io/brancz/kube-rbac-proxy:v0.16.0
         imagePullPolicy: IfNotPresent
         name: kube-rbac-proxy
         ports:

まぁこれ、バッドノウハウに近いんで、マニフェストファイル管理しようね。。