github.com/argoproj/argo-cd/v2@v2.10.9/docs/user-guide/sync-options.md (about)

     1  # Sync Options
     2  
     3  Argo CD allows users to customize some aspects of how it syncs the desired state in the target cluster. Some Sync Options can defined as annotations in a specific resource. Most of the Sync Options are configured in the Application resource `spec.syncPolicy.syncOptions` attribute. Multiple Sync Options which are configured with the `argocd.argoproj.io/sync-options` annotation can be concatenated with a `,` in the annotation value; white spaces will be trimmed.
     4  
     5  Below you can find details about each available Sync Option:
     6  
     7  ## No Prune Resources
     8  
     9  >v1.1
    10  
    11  You may wish to prevent an object from being pruned:
    12  
    13  ```yaml
    14  metadata:
    15    annotations:
    16      argocd.argoproj.io/sync-options: Prune=false
    17  ```
    18  
    19  In the UI, the pod will simply appear as out-of-sync:
    20  
    21  ![sync option no prune](../assets/sync-option-no-prune.png)
    22  
    23  
    24  The sync-status panel shows that pruning was skipped, and why:
    25  
    26  ![sync option no prune](../assets/sync-option-no-prune-sync-status.png)
    27  
    28  The app will be out of sync if Argo CD expects a resource to be pruned. You may wish to use this along with [compare options](compare-options.md).
    29  
    30  ## Disable Kubectl Validation
    31  
    32  For a certain class of objects, it is necessary to `kubectl apply` them using the `--validate=false` flag. Examples of this are Kubernetes types which uses `RawExtension`, such as [ServiceCatalog](https://github.com/kubernetes-incubator/service-catalog/blob/master/pkg/apis/servicecatalog/v1beta1/types.go#L497). You can do using this annotations:
    33  
    34  
    35  ```yaml
    36  metadata:
    37    annotations:
    38      argocd.argoproj.io/sync-options: Validate=false
    39  ```
    40  
    41  If you want to exclude a whole class of objects globally, consider setting `resource.customizations` in [system level configuration](../user-guide/diffing.md#system-level-configuration).
    42  
    43  ## Skip Dry Run for new custom resources types
    44  
    45  When syncing a custom resource which is not yet known to the cluster, there are generally two options:
    46  
    47  1) The CRD manifest is part of the same sync. Then Argo CD will automatically skip the dry run, the CRD will be applied and the resource can be created.
    48  2) In some cases the CRD is not part of the sync, but it could be created in another way, e.g. by a controller in the cluster. An example is [gatekeeper](https://github.com/open-policy-agent/gatekeeper),
    49  which creates CRDs in response to user defined `ConstraintTemplates`. Argo CD cannot find the CRD in the sync and will fail with the error `the server could not find the requested resource`.
    50  
    51  To skip the dry run for missing resource types, use the following annotation:
    52  
    53  ```yaml
    54  metadata:
    55    annotations:
    56      argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
    57  ```
    58  
    59  The dry run will still be executed if the CRD is already present in the cluster.
    60  
    61  ## No Resource Deletion
    62  
    63  For certain resources you might want to retain them even after your application is deleted, for eg. Persistent Volume Claims.
    64  In such situations you can stop those resources from being cleaned up during app deletion by using the following annotation:
    65  
    66  
    67  ```yaml
    68  metadata:
    69    annotations:
    70      argocd.argoproj.io/sync-options: Delete=false
    71  ```
    72  
    73  ## Selective Sync
    74  
    75  Currently when syncing using auto sync Argo CD applies every object in the application.
    76  For applications containing thousands of objects this takes quite a long time and puts undue pressure on the api server.
    77  Turning on selective sync option which will sync only out-of-sync resources.
    78  
    79  You can add this option by following ways
    80  
    81  1) Add `ApplyOutOfSyncOnly=true` in manifest
    82  
    83  Example:
    84  
    85  ```yaml
    86  apiVersion: argoproj.io/v1alpha1
    87  kind: Application
    88  spec:
    89    syncPolicy:
    90      syncOptions:
    91      - ApplyOutOfSyncOnly=true
    92  ```
    93  
    94  2) Set sync option via argocd cli
    95  
    96  Example:
    97  
    98  ```bash
    99  $ argocd app set guestbook --sync-option ApplyOutOfSyncOnly=true
   100  ```
   101  
   102  ## Resources Prune Deletion Propagation Policy
   103  
   104  By default, extraneous resources get pruned using foreground deletion policy. The propagation policy can be controlled
   105  using `PrunePropagationPolicy` sync option. Supported policies are background, foreground and orphan.
   106  More information about those policies could be found [here](https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#controlling-how-the-garbage-collector-deletes-dependents).
   107  
   108  ```yaml
   109  apiVersion: argoproj.io/v1alpha1
   110  kind: Application
   111  spec:
   112    syncPolicy:
   113      syncOptions:
   114      - PrunePropagationPolicy=foreground
   115  ```
   116  
   117  ## Prune Last
   118  
   119  This feature is to allow the ability for resource pruning to happen as a final, implicit wave of a sync operation,
   120  after the other resources have been deployed and become healthy, and after all other waves completed successfully.
   121  
   122  ```yaml
   123  apiVersion: argoproj.io/v1alpha1
   124  kind: Application
   125  spec:
   126    syncPolicy:
   127      syncOptions:
   128      - PruneLast=true
   129  ```
   130  
   131  This can also be configured at individual resource level.
   132  ```yaml
   133  metadata:
   134    annotations:
   135      argocd.argoproj.io/sync-options: PruneLast=true
   136  ```
   137  
   138  ## Replace Resource Instead Of Applying Changes
   139  
   140  By default, Argo CD executes `kubectl apply` operation to apply the configuration stored in Git. In some cases
   141  `kubectl apply` is not suitable. For example, resource spec might be too big and won't fit into
   142  `kubectl.kubernetes.io/last-applied-configuration` annotation that is added by `kubectl apply`. In such cases you
   143  might use `Replace=true` sync option:
   144  
   145  
   146  ```yaml
   147  apiVersion: argoproj.io/v1alpha1
   148  kind: Application
   149  spec:
   150    syncPolicy:
   151      syncOptions:
   152      - Replace=true
   153  ```
   154  
   155  If the `Replace=true` sync option is set the Argo CD will use `kubectl replace` or `kubectl create` command to apply changes.
   156  
   157  !!! warning
   158        During the sync process, the resources will be synchronized using the 'kubectl replace/create' command.
   159        This sync option has the potential to be destructive and might lead to resources having to be recreated, which could cause an outage for your application.
   160  
   161  This can also be configured at individual resource level.
   162  ```yaml
   163  metadata:
   164    annotations:
   165      argocd.argoproj.io/sync-options: Replace=true
   166  ```
   167  
   168  ## Server-Side Apply
   169  
   170  This option enables Kubernetes
   171  [Server-Side Apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/).
   172  
   173  By default, Argo CD executes `kubectl apply` operation to apply the configuration stored in Git.
   174  This is a client side operation that relies on `kubectl.kubernetes.io/last-applied-configuration`
   175  annotation to store the previous resource state.
   176  
   177  However, there are some cases where you want to use `kubectl apply --server-side` over `kubectl apply`:
   178  
   179  - Resource is too big to fit in 262144 bytes allowed annotation size. In this case
   180    server-side apply can be used to avoid this issue as the annotation is not used in this case.
   181  - Patching of existing resources on the cluster that are not fully managed by Argo CD.
   182  - Use a more declarative approach, which tracks a user's field management, rather than a user's last
   183    applied state.
   184  
   185  If `ServerSideApply=true` sync option is set, Argo CD will use `kubectl apply --server-side`
   186  command to apply changes.
   187  
   188  It can be enabled at the application level like in the example below:
   189  
   190  ```yaml
   191  apiVersion: argoproj.io/v1alpha1
   192  kind: Application
   193  spec:
   194    syncPolicy:
   195      syncOptions:
   196      - ServerSideApply=true
   197  ```
   198  
   199  To enable ServerSideApply just for an individual resource, the sync-option annotation
   200  can be used:
   201  
   202  ```yaml
   203  metadata:
   204    annotations:
   205      argocd.argoproj.io/sync-options: ServerSideApply=true
   206  ```
   207  
   208  ServerSideApply can also be used to patch existing resources by providing a partial
   209  yaml. For example, if there is a requirement to update just the number of replicas
   210  in a given Deployment, the following yaml can be provided to Argo CD:
   211  
   212  ```yaml
   213  apiVersion: apps/v1
   214  kind: Deployment
   215  metadata:
   216    name: my-deployment
   217  spec:
   218    replicas: 3
   219  ```
   220  
   221  Note that by the Deployment schema specification, this isn't a valid manifest. In this
   222  case an additional sync option *must* be provided to skip schema validation. The example
   223  below shows how to configure the application to enable the two necessary sync options:
   224  
   225  ```yaml
   226  apiVersion: argoproj.io/v1alpha1
   227  kind: Application
   228  spec:
   229    syncPolicy:
   230      syncOptions:
   231      - ServerSideApply=true
   232      - Validate=false
   233  ```
   234  
   235  In this case, Argo CD will use `kubectl apply --server-side --validate=false` command
   236  to apply changes.
   237  
   238  Note: [`Replace=true`](#replace-resource-instead-of-applying-changes) takes precedence over `ServerSideApply=true`.
   239  
   240  ## Fail the sync if a shared resource is found
   241  
   242  By default, Argo CD will apply all manifests found in the git path configured in the Application regardless if the resources defined in the yamls are already applied by another Application. If the `FailOnSharedResource` sync option is set, Argo CD will fail the sync whenever it finds a resource in the current Application that is already applied in the cluster by another Application.
   243  
   244  ```yaml
   245  apiVersion: argoproj.io/v1alpha1
   246  kind: Application
   247  spec:
   248    syncPolicy:
   249      syncOptions:
   250      - FailOnSharedResource=true
   251  ```
   252  
   253  ## Respect ignore difference configs
   254  
   255  This sync option is used to enable Argo CD to consider the configurations made in the `spec.ignoreDifferences` attribute also during the sync stage. By default, Argo CD uses the `ignoreDifferences` config just for computing the diff between the live and desired state which defines if the application is synced or not. However during the sync stage, the desired state is applied as-is. The patch is calculated using a 3-way-merge between the live state the desired state and the `last-applied-configuration` annotation. This sometimes leads to an undesired results. This behavior can be changed by setting the `RespectIgnoreDifferences=true` sync option like in the example below:
   256  
   257  ```yaml
   258  apiVersion: argoproj.io/v1alpha1
   259  kind: Application
   260  spec:
   261  
   262    ignoreDifferences:
   263    - group: "apps"
   264      kind: "Deployment"
   265      jsonPointers:
   266      - /spec/replicas
   267  
   268    syncPolicy:
   269      syncOptions:
   270      - RespectIgnoreDifferences=true
   271  ```
   272  
   273  The example above shows how an Argo CD Application can be configured so it will ignore the `spec.replicas` field from the desired state (git) during the sync stage. This is achieve by calculating and pre-patching the desired state before applying it in the cluster. Note that the `RespectIgnoreDifferences` sync option is only effective when the resource is already created in the cluster. If the Application is being created and no live state exists, the desired state is applied as-is.
   274  
   275  ## Create Namespace
   276  
   277  ```yaml
   278  apiVersion: argoproj.io/v1alpha1
   279  kind: Application
   280  metadata:
   281    namespace: argocd
   282  spec:
   283    destination:
   284      server: https://kubernetes.default.svc
   285      namespace: some-namespace
   286    syncPolicy:
   287      syncOptions:
   288      - CreateNamespace=true
   289  ```
   290  
   291  The example above shows how an Argo CD Application can be configured so it will create the namespace specified in `spec.destination.namespace` if it doesn't exist already. Without this either declared in the Application manifest or passed in the CLI via `--sync-option CreateNamespace=true`, the Application will fail to sync if the namespace doesn't exist.
   292  
   293  Note that the namespace to be created must be informed in the `spec.destination.namespace` field of the Application resource. The `metadata.namespace` field in the Application's child manifests must match this value, or can be omitted, so resources are created in the proper destination.
   294  
   295  ### Namespace Metadata
   296  
   297  We can also add labels and annotations to the namespace through `managedNamespaceMetadata`. If we extend the example above
   298  we could potentially do something like below:
   299  
   300  ```yaml
   301  apiVersion: argoproj.io/v1alpha1
   302  kind: Application
   303  metadata:
   304    namespace: test
   305  spec:
   306    syncPolicy:
   307      managedNamespaceMetadata:
   308        labels: # The labels to set on the application namespace
   309          any: label
   310          you: like
   311        annotations: # The annotations to set on the application namespace
   312          the: same
   313          applies: for
   314          annotations: on-the-namespace
   315      syncOptions:
   316      - CreateNamespace=true
   317  ```
   318  
   319  In order for Argo CD to manage the labels and annotations on the namespace, `CreateNamespace=true` needs to be set as a
   320  sync option, otherwise nothing will happen. If the namespace doesn't already exist, or if it already exists and doesn't
   321  already have labels and/or annotations set on it, you're good to go. Using `managedNamespaceMetadata` will also set the
   322  resource tracking label (or annotation) on the namespace, so you can easily track which namespaces are managed by Argo CD.
   323  
   324  In the case you do not have any custom annotations or labels but would nonetheless want to have resource tracking set on
   325  your namespace, that can be done by setting `managedNamespaceMetadata` with an empty `labels` and/or `annotations` map,
   326  like the example below:
   327  
   328  ```yaml
   329  apiVersion: argoproj.io/v1alpha1
   330  kind: Application
   331  metadata:
   332    namespace: test
   333  spec:
   334    syncPolicy:
   335      managedNamespaceMetadata:
   336        labels: # The labels to set on the application namespace
   337        annotations: # The annotations to set on the application namespace
   338      syncOptions:
   339      - CreateNamespace=true
   340  ```
   341  
   342  In the case where Argo CD is "adopting" an existing namespace which already has metadata set on it, you should first
   343  [upgrade the resource to server-side apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/#upgrading-from-client-side-apply-to-server-side-apply)
   344  before enabling `managedNamespaceMetadata`. Argo CD relies on `kubectl`, which does not support managing 
   345  client-side-applied resources with server-side-applies. If you do not upgrade the resource to server-side apply, Argo CD
   346  may remove existing labels/annotations, which may or may not be the desired behavior.
   347  
   348  Another thing to keep mind of is that if you have a k8s manifest for the same namespace in your Argo CD application, that
   349  will take precedence and *overwrite whatever values that have been set in `managedNamespaceMetadata`*. In other words, if
   350  you have an application that sets `managedNamespaceMetadata`
   351  
   352  ```yaml
   353  apiVersion: argoproj.io/v1alpha1
   354  kind: Application
   355  spec:
   356    syncPolicy:
   357      managedNamespaceMetadata:
   358        annotations:
   359          abc: 123
   360      syncOptions:
   361        - CreateNamespace=true
   362  ```
   363  
   364  But you also have a k8s manifest with a matching name
   365  
   366  ```yaml
   367  apiVersion: v1
   368  kind: Namespace
   369  metadata:
   370    name: foobar
   371    annotations:
   372      foo: bar
   373      something: completely-different
   374  ```
   375  
   376  The resulting namespace will have its annotations set to
   377  
   378  ```yaml
   379    annotations:
   380      foo: bar
   381      something: completely-different
   382  ```