kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.75k stars 894 forks source link

kubectl diff fails to detect differences in Service objects #1601

Open jrcast opened 1 month ago

jrcast commented 1 month ago

What happened: kubectl diff is not detecting diffs between a service's manifest file and the modified service object in Kubernetes. I tried both client-side or server-side mode without success.

What you expected to happen: kubectl diff identifies the diff

How to reproduce:

  1. Create a K8s service using a manifest kubectl create -f my-svc.yaml. Where my-svc.yaml is:
    apiVersion: v1
    kind: Service
    metadata:
      name: example
      namespace: default
    spec:
      ports:
        - name: service-port
          port: 80
          protocol: TCP
          targetPort: service-port
      selector:
        app.kubernetes.io/instance: example
      type: ClusterIP
  2. Using kubectl edit, modify the K8s service. i.e. Add an extra port.
    apiVersion: v1
    kind: Service
    metadata:
      name: example
      namespace: default
    spec:
      ports:
        - name: service-port
          port: 80
          protocol: TCP
          targetPort: service-port
        - name: shouldntbehere    # <<<< THIS 
          port: 8080
          protocol: TCP
          targetPort: service-port
      selector:
        app.kubernetes.io/instance: example
      type: ClusterIP
  3. Run:
    kubectl diff -f my-svc.yaml
    # or
    kubectl diff -f my-svc.yaml --server-side --force-conflicts
  4. No diff shows up.

Anything else we need to know?:

Environment:

ardaguclu commented 1 month ago

Have you verified that the kubectl edit actually updated your service?

kundan2707 commented 1 month ago

@jrcast have you checked if kubectl command executed successfully without error ?

kundan2707 commented 1 month ago

/remove-kind bug

kundan2707 commented 1 month ago

/kind support

brianpursley commented 1 month ago

Can confirm this happens when you make changes using kubectl edit, and it does seem to have something to do with server side apply.

If you use the --save-config flag with edit, then kubectl diff will work:

kubectl edit service example --save-config
$ kubectl diff -f my-svc.yaml 
diff -u -N /tmp/LIVE-3530718821/v1.Service.default.example /tmp/MERGED-1768176721/v1.Service.default.example
--- /tmp/LIVE-3530718821/v1.Service.default.example 2024-05-28 11:44:49.052930430 -0400
+++ /tmp/MERGED-1768176721/v1.Service.default.example   2024-05-28 11:44:49.056930322 -0400
@@ -22,10 +22,6 @@
     port: 80
     protocol: TCP
     targetPort: service-port
-  - name: shouldntbehere
-    port: 8080
-    protocol: TCP
-    targetPort: service-port
   selector:
     app.kubernetes.io/instance: example
   sessionAffinity: None

But if you use that flag, it's going to add the kubectl.kubernetes.io/last-applied-configuration annotation, which shouldn't be a requirement for diff.

/remove-kind support /kind bug

ardaguclu commented 4 weeks ago

/triage accepted /priority backlog