github.com/argoproj/argo-cd/v3@v3.2.1/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 be 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-space will be trimmed.
     4  
     5  Below you can find details about each available Sync Option:
     6  
     7  ## No Prune Resources
     8  
     9  You may wish to prevent an object from being pruned:
    10  
    11  ```yaml
    12  metadata:
    13    annotations:
    14      argocd.argoproj.io/sync-options: Prune=false
    15  ```
    16  
    17  The sync-status panel shows that pruning was skipped, and why:
    18  
    19  ![sync option no prune](../assets/sync-option-no-prune-sync-status.png)
    20  
    21  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).
    22  
    23  ## Resource Pruning With Confirmation
    24  
    25  Resources such as Namespaces are critical and should not be pruned without confirmation. You can set the `Prune=confirm`
    26  sync option to require manual confirmation before pruning.
    27  
    28  ```yaml
    29  metadata:
    30    annotations:
    31      argocd.argoproj.io/sync-options: Prune=confirm
    32  ```
    33  
    34  To confirm the pruning you can use Argo CD UI, CLI or manually apply the `argocd.argoproj.io/deletion-approved: <ISO formatted timestamp>`
    35  annotation to the application.
    36  
    37  ## Disable Kubectl Validation
    38  
    39  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 that using this annotation:
    40  
    41  ```yaml
    42  metadata:
    43    annotations:
    44      argocd.argoproj.io/sync-options: Validate=false
    45  ```
    46  
    47  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).
    48  
    49  ## Skip Dry Run for new custom resources types
    50  
    51  When syncing a custom resource which is not yet known to the cluster, there are generally two options:
    52  
    53  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.
    54  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),
    55  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`.
    56  
    57  To skip the dry run for missing resource types, use the following annotation:
    58  
    59  ```yaml
    60  metadata:
    61    annotations:
    62      argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
    63  ```
    64  
    65  The dry run will still be executed if the CRD is already present in the cluster.
    66  
    67  It is also possible to skip dry run on missing resource for all application resources. You can set the `SkipDryRunOnMissingResource=true`
    68  sync option to skip dry run on missing resource
    69  
    70  ```yaml
    71  apiVersion: argoproj.io/v1alpha1
    72  kind: Application
    73  spec:
    74    syncPolicy:
    75      syncOptions:
    76      - SkipDryRunOnMissingResource=true
    77  ```
    78  
    79  ## No Resource Deletion
    80  
    81  For certain resources you might want to retain them even after your application is deleted, e.g. for Persistent Volume Claims.
    82  In such situations you can stop those resources from being cleaned up during app deletion by using the following annotation:
    83  
    84  ```yaml
    85  metadata:
    86    annotations:
    87      argocd.argoproj.io/sync-options: Delete=false
    88  ```
    89  
    90  ## Resource Deletion With Confirmation
    91  
    92  Resources such as Namespaces are critical and should not be deleted without confirmation. You can set the `Delete=confirm`
    93  sync option to require manual confirmation before deletion.
    94  
    95  ```yaml
    96  metadata:
    97    annotations:
    98      argocd.argoproj.io/sync-options: Delete=confirm
    99  ```
   100  
   101  To confirm the deletion you can use Argo CD UI, CLI or manually apply the `argocd.argoproj.io/deletion-approved: <ISO formatted timestamp>`
   102  annotation to the application.
   103  
   104  ## Selective Sync
   105  
   106  Currently, when syncing using auto sync Argo CD applies every object in the application.
   107  For applications containing thousands of objects this takes quite a long time and puts undue pressure on the api server.
   108  Turning on the selective sync option will sync only out-of-sync resources.
   109  
   110  You can add this option in the following ways:
   111  
   112  1) Add `ApplyOutOfSyncOnly=true` in manifest
   113  
   114  Example:
   115  
   116  ```yaml
   117  apiVersion: argoproj.io/v1alpha1
   118  kind: Application
   119  spec:
   120    syncPolicy:
   121      syncOptions:
   122      - ApplyOutOfSyncOnly=true
   123  ```
   124  
   125  2) Set sync option via argocd cli
   126  
   127  Example:
   128  
   129  ```bash
   130  $ argocd app set guestbook --sync-option ApplyOutOfSyncOnly=true
   131  ```
   132  
   133  ## Resources Prune Deletion Propagation Policy
   134  
   135  By default, extraneous resources get pruned using the foreground deletion policy. The propagation policy can be controlled
   136  using the `PrunePropagationPolicy` sync option. Supported policies are background, foreground, and orphan.
   137  More information about those policies can be found [here](https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#controlling-how-the-garbage-collector-deletes-dependents).
   138  
   139  ```yaml
   140  apiVersion: argoproj.io/v1alpha1
   141  kind: Application
   142  spec:
   143    syncPolicy:
   144      syncOptions:
   145      - PrunePropagationPolicy=foreground
   146  ```
   147  
   148  ## Prune Last
   149  
   150  This feature is to allow the ability for resource pruning to happen as a final, implicit wave of a sync operation,
   151  after the other resources have been deployed and become healthy, and after all other waves completed successfully.
   152  
   153  ```yaml
   154  apiVersion: argoproj.io/v1alpha1
   155  kind: Application
   156  spec:
   157    syncPolicy:
   158      syncOptions:
   159      - PruneLast=true
   160  ```
   161  
   162  This can also be configured at individual resource level.
   163  ```yaml
   164  metadata:
   165    annotations:
   166      argocd.argoproj.io/sync-options: PruneLast=true
   167  ```
   168  
   169  ## Replace Resource Instead Of Applying Changes
   170  
   171  By default, Argo CD executes the `kubectl apply` operation to apply the configuration stored in Git. In some cases
   172  `kubectl apply` is not suitable. For example, a resource spec might be too large and won't fit into the
   173  `kubectl.kubernetes.io/last-applied-configuration` annotation that is added by `kubectl apply`. In such cases you
   174  might use `Replace=true` sync option:
   175  
   176  
   177  ```yaml
   178  apiVersion: argoproj.io/v1alpha1
   179  kind: Application
   180  spec:
   181    syncPolicy:
   182      syncOptions:
   183      - Replace=true
   184  ```
   185  
   186  If the `Replace=true` sync option is set, Argo CD will use `kubectl replace` or `kubectl create` command to apply changes.
   187  
   188  !!! warning
   189        During the sync process, the resources will be synchronized using the 'kubectl replace/create' command.
   190        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.
   191  
   192  This can also be configured at individual resource level.
   193  ```yaml
   194  metadata:
   195    annotations:
   196      argocd.argoproj.io/sync-options: Replace=true
   197  ```
   198  
   199  ## Force Sync
   200  
   201  For certain resources you might want to delete and recreate, e.g. job resources that should run every time when syncing.
   202  
   203  !!! warning
   204        During the sync process, the resources will be synchronized using the 'kubectl delete/create' command.
   205        This sync option has a destructive action, which could cause an outage for your application.
   206  
   207  In such cases you might use `Force=true` sync option in target resources annotation:
   208  ```yaml
   209  metadata:
   210    annotations:
   211      argocd.argoproj.io/sync-options: Force=true,Replace=true
   212  ```
   213  
   214  ## Server-Side Apply
   215  
   216  This option enables Kubernetes
   217  [Server-Side Apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/).
   218  
   219  By default, Argo CD executes the `kubectl apply` operation to apply the configuration stored in Git.
   220  This is a client side operation that relies on the `kubectl.kubernetes.io/last-applied-configuration`
   221  annotation to store the previous resource state.
   222  
   223  However, there are some cases where you want to use `kubectl apply --server-side` over `kubectl apply`:
   224  
   225  - Resource is too big to fit in 262144 bytes allowed annotation size. In this case
   226    server-side apply can be used to avoid this issue as the annotation is not used in this case.
   227  - Patching of existing resources on the cluster that are not fully managed by Argo CD.
   228  - Use a more declarative approach, which tracks a user's field management, rather than a user's last
   229    applied state.
   230  
   231  If the `ServerSideApply=true` sync option is set, Argo CD will use the `kubectl apply --server-side`
   232  command to apply changes.
   233  
   234  It can be enabled at the application level like in the example below:
   235  
   236  ```yaml
   237  apiVersion: argoproj.io/v1alpha1
   238  kind: Application
   239  spec:
   240    syncPolicy:
   241      syncOptions:
   242      - ServerSideApply=true
   243  ```
   244  
   245  To enable ServerSideApply just for an individual resource, the sync-option annotation
   246  can be used:
   247  
   248  ```yaml
   249  metadata:
   250    annotations:
   251      argocd.argoproj.io/sync-options: ServerSideApply=true
   252  ```
   253  
   254  If you want to disable ServerSideApply for a specific resource, while it is enabled at the application level, 
   255  add the following sync-option annotation in it:
   256  
   257  ```yaml
   258  metadata:
   259    annotations:
   260      argocd.argoproj.io/sync-options: ServerSideApply=false
   261  ```
   262  
   263  
   264  ServerSideApply can also be used to patch existing resources by providing a partial
   265  yaml. For example, if there is a requirement to update just the number of replicas
   266  in a given Deployment, the following yaml can be provided to Argo CD:
   267  
   268  ```yaml
   269  apiVersion: apps/v1
   270  kind: Deployment
   271  metadata:
   272    name: my-deployment
   273  spec:
   274    replicas: 3
   275  ```
   276  
   277  Note that by the Deployment schema specification, this isn't a valid manifest. In this
   278  case an additional sync option *must* be provided to skip schema validation. The example
   279  below shows how to configure the application to enable the two necessary sync options:
   280  
   281  ```yaml
   282  apiVersion: argoproj.io/v1alpha1
   283  kind: Application
   284  spec:
   285    syncPolicy:
   286      syncOptions:
   287      - ServerSideApply=true
   288      - Validate=false
   289  ```
   290  
   291  In this case, Argo CD will use the `kubectl apply --server-side --validate=false` command
   292  to apply changes.
   293  
   294  Note: [`Replace=true`](#replace-resource-instead-of-applying-changes) takes precedence over `ServerSideApply=true`.
   295  
   296  ### Client-Side Apply Migration
   297  
   298  Argo CD supports client-side apply migration, which helps transitioning from client-side apply to server-side apply by moving a resource's managed fields from one manager to Argo CD's manager. This feature is particularly useful when you need to migrate existing resources that were created using kubectl client-side apply to server-side apply with Argo CD.
   299  
   300  By default, client-side apply migration is enabled. You can disable it using the sync option:
   301  
   302  ```yaml
   303  apiVersion: argoproj.io/v1alpha1
   304  kind: Application
   305  spec:
   306    syncPolicy:
   307      syncOptions:
   308      - DisableClientSideApplyMigration=true
   309  ```
   310  
   311  You can specify a custom field manager for the client-side apply migration using an annotation:
   312  
   313  ```yaml
   314  apiVersion: argoproj.io/v1alpha1
   315  kind: Application
   316  metadata:
   317    annotations:
   318      argocd.argoproj.io/client-side-apply-migration-manager: "my-custom-manager"
   319  ```
   320  
   321  This is useful when you have other operators managing resources that are no longer in use and would like Argo CD to own all the fields for that operator.
   322  
   323  ### How it works
   324  
   325  When client-side apply migration is enabled:
   326  1. Argo CD will use the specified field manager (or default if not specified) to perform migration
   327  2. During a server-side apply sync operation, it will:
   328     - Perform a client-side-apply with the specified field manager
   329     - Move the 'last-applied-configuration' annotation to be managed by the specified manager
   330     - Perform the server-side apply, which will auto migrate all the fields under the manager that owns the 'last-applied-configration' annotation.
   331  
   332  This feature is based on Kubernetes' [client-side apply migration KEP](https://github.com/alexzielenski/enhancements/blob/03df8820b9feca6d2cab78e303c99b2c9c0c4c5c/keps/sig-cli/3517-kubectl-client-side-apply-migration/README.md), which provides the auto migration from client-side to server-side apply.
   333  
   334  ## Fail the sync if a shared resource is found
   335  
   336  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.
   337  
   338  ```yaml
   339  apiVersion: argoproj.io/v1alpha1
   340  kind: Application
   341  spec:
   342    syncPolicy:
   343      syncOptions:
   344      - FailOnSharedResource=true
   345  ```
   346  
   347  ## Respect ignore differences configs
   348  
   349  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:
   350  
   351  ```yaml
   352  apiVersion: argoproj.io/v1alpha1
   353  kind: Application
   354  spec:
   355  
   356    ignoreDifferences:
   357    - group: "apps"
   358      kind: "Deployment"
   359      jsonPointers:
   360      - /spec/replicas
   361  
   362    syncPolicy:
   363      syncOptions:
   364      - RespectIgnoreDifferences=true
   365  ```
   366  
   367  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 achieved 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.
   368  
   369  ## Create Namespace
   370  
   371  ```yaml
   372  apiVersion: argoproj.io/v1alpha1
   373  kind: Application
   374  metadata:
   375    namespace: argocd
   376  spec:
   377    destination:
   378      server: https://kubernetes.default.svc
   379      namespace: some-namespace
   380    syncPolicy:
   381      syncOptions:
   382      - CreateNamespace=true
   383  ```
   384  
   385  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.
   386  
   387  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.
   388  
   389  ### Namespace Metadata
   390  
   391  We can also add labels and annotations to the namespace through `managedNamespaceMetadata`. If we extend the example above
   392  we could potentially do something like below:
   393  
   394  ```yaml
   395  apiVersion: argoproj.io/v1alpha1
   396  kind: Application
   397  metadata:
   398    namespace: test
   399  spec:
   400    syncPolicy:
   401      managedNamespaceMetadata:
   402        labels: # The labels to set on the application namespace
   403          any: label
   404          you: like
   405        annotations: # The annotations to set on the application namespace
   406          the: same
   407          applies: for
   408          annotations: on-the-namespace
   409      syncOptions:
   410      - CreateNamespace=true
   411  ```
   412  
   413  In order for Argo CD to manage the labels and annotations on the namespace, `CreateNamespace=true` needs to be set as a
   414  sync option, otherwise nothing will happen. If the namespace doesn't already exist, or if it already exists and doesn't
   415  already have labels and/or annotations set on it, you're good to go. Using `managedNamespaceMetadata` will also set the
   416  resource tracking label (or annotation) on the namespace, so you can easily track which namespaces are managed by Argo CD.
   417  
   418  In the case you do not have any custom annotations or labels but would nonetheless want to have resource tracking set on
   419  your namespace, that can be done by setting `managedNamespaceMetadata` with an empty `labels` and/or `annotations` map,
   420  like the example below:
   421  
   422  ```yaml
   423  apiVersion: argoproj.io/v1alpha1
   424  kind: Application
   425  metadata:
   426    namespace: test
   427  spec:
   428    syncPolicy:
   429      managedNamespaceMetadata:
   430        labels: # The labels to set on the application namespace
   431        annotations: # The annotations to set on the application namespace
   432      syncOptions:
   433      - CreateNamespace=true
   434  ```
   435  
   436  In the case where Argo CD is "adopting" an existing namespace which already has metadata set on it, you should first
   437  [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)
   438  before enabling `managedNamespaceMetadata`. Argo CD relies on `kubectl`, which does not support managing 
   439  client-side-applied resources with server-side-applies. If you do not upgrade the resource to server-side apply, Argo CD
   440  may remove existing labels/annotations, which may or may not be the desired behavior.
   441  
   442  Another thing to keep mind of is that if you have a k8s manifest for the same namespace in your Argo CD application, that
   443  will take precedence and *overwrite whatever values that have been set in `managedNamespaceMetadata`*. In other words, if
   444  you have an application that sets `managedNamespaceMetadata`
   445  
   446  ```yaml
   447  apiVersion: argoproj.io/v1alpha1
   448  kind: Application
   449  spec:
   450    syncPolicy:
   451      managedNamespaceMetadata:
   452        annotations:
   453          abc: 123
   454      syncOptions:
   455        - CreateNamespace=true
   456  ```
   457  
   458  But you also have a k8s manifest with a matching name
   459  
   460  ```yaml
   461  apiVersion: v1
   462  kind: Namespace
   463  metadata:
   464    name: foobar
   465    annotations:
   466      foo: bar
   467      something: completely-different
   468  ```
   469  
   470  The resulting namespace will have its annotations set to
   471  
   472  ```yaml
   473    annotations:
   474      foo: bar
   475      something: completely-different
   476  ```