argoproj / argo-cd

Declarative Continuous Deployment for Kubernetes
https://argo-cd.readthedocs.io
Apache License 2.0
17.6k stars 5.36k forks source link

Server-Side Diff with `RespectIgnoreDifferences` can fail diff due to missing required fields #17362

Open llavaud opened 7 months ago

llavaud commented 7 months ago

Describe the bug

An application that set an ignoreDifferences on a required field of a manifest spec will fail to diff if both RespectIgnoreDifferences and ServerSideDiff are enabled

To Reproduce

Create an application with the following spec:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  annotations:
    argocd.argoproj.io/compare-options: ServerSideDiff=true
...
spec:
  ignoreDifferences:
    - group: apps
      kind: Deployment
      name: foo
      jqPathExpressions:
        - '.spec.template.spec.containers[] | select(.name == "foo").image'
  syncPolicy:
    syncOptions:
    - CreateNamespace=true
    - ApplyOutOfSyncOnly=true
    - RespectIgnoreDifferences=true
    - ServerSideApply=true
    - Validate=false
...

Expected behavior

Diff app without errors

Version

argocd: v2.10.1+a79e0ea
  BuildDate: 2024-02-14T17:37:43Z
  GitCommit: a79e0eaca415461dc36615470cecc25d6d38cefb
  GitTreeState: clean
  GoVersion: go1.21.3
  Compiler: gc
  Platform: linux/amd64

Logs

Failed to compare desired state to live state: failed to calculate diff: error calculating server side diff: serverSideDiff error: error running server side apply in dryrun mode for resource Deployment/foo: Deployment.apps "foo" is invalid: spec.template.spec.containers[0].image: Required value
llavaud commented 7 months ago

After some tests, I noticed that if I take the ownership on the spec.template.spec.containers.image field using the following command, Argo CD succeed to do the diff.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: foo
spec:
  template:
    spec:
      containers:
      - name: foo
        image: <my_image>
kubectl -f deployment.yaml --server-side --field-manager='myself'

I guess, if the image field is managed by another controller, it is added when doing the server-side diff otherwise if no one manage this field and as the respectIgnoreDifferences is active, the image field is not added and cause the invalid manifest error, so not sure it is a bug, seems more like the normal behavior of the server-side feature, don't know if Argo can manage this situation in some way...