github.com/argoproj/argo-cd/v2@v2.10.9/docs/user-guide/sync-kubectl.md (about) 1 # Sync Applications with Kubectl 2 3 You can use "kubectl" to ask Argo CD to synchronize applications the same way you can use the CLI or UI. Many configurations like "force", "prune", "apply" and even synchronize a specific list of resources are equally supported. This is done by applying or patching the Argo CD application with a document that defines an "operation". 4 5 This "operation" defines how a synchronization should be done and for what resources these synchronization is to be done. 6 7 There are many configuration options that can be added to the "operation". Next, a few of them are explained. For more details, you can have a look at the CRD [applications.argoproj.io](https://github.com/argoproj/argo-cd/blob/master/manifests/crds/application-crd.yaml). Some of them are required, whereas others are optional. 8 9 To ask Argo CD to synchronize all resources of a given application, we can do: 10 11 ```yaml 12 apiVersion: argoproj.io/v1alpha1 13 kind: Application 14 metadata: 15 name: <app-name> 16 namespace: <namespace> 17 spec: 18 ... 19 operation: 20 initiatedBy: 21 username: <username> 22 sync: 23 syncStrategy: 24 hook: {} 25 ``` 26 27 ```bash 28 $ kubectl apply -f <apply-file> 29 ``` 30 31 The most important part is the "sync" definition in the "operation" field. You can pass optional information like "info" or "initiatedBy". "info" allows you to add information about the operation in the form of a list. "initiatedBy" contains information about who initiated the operation request. 32 33 Or if you prefer, you also can patch: 34 35 ```yaml 36 operation: 37 initiatedBy: 38 username: <username> 39 sync: 40 syncStrategy: 41 hook: {} 42 ``` 43 44 ```bash 45 $ kubectl patch -n <namespace> app <app-name> --patch-file <patch-file> --type merge 46 ``` 47 48 Be aware that patches, specially with merge strategies, may not work the way you expect especially if you change sync strategies or options. 49 In these cases, "kubectl apply" gives better results. 50 51 Either with a "kubectl patch" or "kubectl apply", the state of the synchronization is reported in the "operationState" field in the application object. 52 53 ```bash 54 $ kubectl get -n <namespace> get app <app-name> -o yaml 55 ... 56 status: 57 operationState: 58 finishedAt: "2023-08-03T11:16:17Z" 59 message: successfully synced (all tasks run) 60 phase: Succeeded 61 ``` 62 63 # Apply and Hook synchronization strategies 64 65 There are two types of synchronization strategies: "hook", which is the default value, and "apply". 66 67 An "apply" sync strategy tells Argo CD to "kubectl apply", whereas a "hook" sync strategy informs Argo CD to submit any resource that's referenced in the operation. This way the synchronization of these resources will take into consideration any hook the resource has been annotated with. 68 69 ```yaml 70 operation: 71 sync: 72 syncStrategy: 73 apply: {} 74 ``` 75 76 ```yaml 77 operation: 78 sync: 79 syncStrategy: 80 hook: {} 81 ``` 82 83 Both strategies support "force". However, you need to be aware that a force operation deletes the resource when patch encounters a conflict after having retried 5 times. 84 85 ```yaml 86 operation: 87 sync: 88 syncStrategy: 89 apply: 90 force: true 91 ``` 92 93 ```yaml 94 operation: 95 sync: 96 syncStrategy: 97 hook: 98 force: true 99 ``` 100 101 # Prune 102 103 If you want to prune your resources before applying, you can instruct Argo CD to do so: 104 105 ```yaml 106 operation: 107 sync: 108 prune: true 109 ``` 110 111 # List of resources 112 113 There's always the possibility to pass a list of resources. This list can be all resources the application manages or only a subset, for example resources that remained out of sync for some reason. 114 115 Only "kind" and "name" are required fields when referencing resources, but the fields "groups" and "namespace" can also be defined: 116 117 ```yaml 118 operation: 119 sync: 120 resources: 121 - kind: Namespace 122 name: namespace-name 123 - kind: ServiceAccount 124 name: service-account-name 125 namespace: namespace-name 126 - group: networking.k8s.io 127 kind: NetworkPolicy 128 name: network-policy-name 129 namespace: namespace-name 130 ``` 131 132 # Sync Options 133 134 In an operation, you can also pass sync-options. Each of these options is passed as "name=value" pairs. For example: 135 136 ```yaml 137 operations: 138 sync: 139 syncOptions: 140 - Validate=false 141 - Prune=false 142 ``` 143 144 For more information about sync options, please refer to [sync-options](https://argo-cd.readthedocs.io/en/stable/user-guide/sync-options/)