github.com/argoproj/argo-cd/v3@v3.2.1/docs/operator-manual/resource_actions.md (about) 1 # Resource Actions 2 3 ## Overview 4 Argo CD allows operators to define custom actions which users can perform on specific resource types. This is used internally to provide actions like `restart` for a `DaemonSet`, or `retry` for an Argo Rollout. 5 6 Operators can add actions to custom resources in form of a Lua script and expand those capabilities. 7 8 ## Built-in Actions 9 10 The following are actions that are built-in to Argo CD. Each action name links to its Lua script definition: 11 12 {!docs/operator-manual/resource_actions_builtin.md!} 13 14 See the [RBAC documentation](rbac.md#the-action-action) for information on how to control access to these actions. 15 16 ## Custom Resource Actions 17 18 Argo CD supports custom resource actions written in [Lua](https://www.lua.org/). This is useful if you: 19 20 * Have a custom resource for which Argo CD does not provide any built-in actions. 21 * Have a commonly performed manual task that might be error prone if executed by users via `kubectl` 22 23 The resource actions act on a single object. 24 25 You can define your own custom resource actions in the `argocd-cm` ConfigMap. 26 27 ### Custom Resource Action Types 28 29 #### An action that modifies the source resource 30 31 This action modifies and returns the source resource. 32 This kind of action was the only one available till 2.8, and it is still supported. 33 34 #### An action that produces a list of new or modified resources 35 36 **An alpha feature, introduced in 2.8.** 37 38 This action returns a list of impacted resources, each impacted resource has a K8S resource and an operation to perform on. 39 Currently supported operations are "create" and "patch", "patch" is only supported for the source resource. 40 Creating new resources is possible, by specifying a "create" operation for each such resource in the returned list. 41 One of the returned resources can be the modified source object, with a "patch" operation, if needed. 42 See the definition examples below. 43 44 ### Define a Custom Resource Action in `argocd-cm` ConfigMap 45 46 Custom resource actions can be defined in `resource.customizations.actions.<group_kind>` field of `argocd-cm`. Following example demonstrates a set of custom actions for `CronJob` resources, each such action returns the modified CronJob. 47 The customizations key is in the format of `resource.customizations.actions.<apiGroup_Kind>`. 48 49 ```yaml 50 resource.customizations.actions.batch_CronJob: | 51 discovery.lua: | 52 actions = {} 53 actions["suspend"] = {["disabled"] = true} 54 actions["resume"] = {["disabled"] = true} 55 56 local suspend = false 57 if obj.spec.suspend ~= nil then 58 suspend = obj.spec.suspend 59 end 60 if suspend then 61 actions["resume"]["disabled"] = false 62 else 63 actions["suspend"]["disabled"] = false 64 end 65 return actions 66 definitions: 67 - name: suspend 68 action.lua: | 69 obj.spec.suspend = true 70 return obj 71 - name: resume 72 action.lua: | 73 if obj.spec.suspend ~= nil and obj.spec.suspend then 74 obj.spec.suspend = false 75 end 76 return obj 77 ``` 78 79 The `discovery.lua` script must return a table where the key name represents the action name. You can optionally include logic to enable or disable certain actions based on the current object state. 80 81 Each action name must be represented in the list of `definitions` with an accompanying `action.lua` script to control the resource modifications. The `obj` is a global variable which contains the resource. Each action script returns an optionally modified version of the resource. In this example, we are simply setting `.spec.suspend` to either `true` or `false`. 82 83 By default, defining a resource action customization will override any built-in action for this resource kind. As of Argo CD version 2.13.0, if you want to retain the built-in actions, you can set the `mergeBuiltinActions` key to `true`. Your custom actions will have precedence over the built-in actions. 84 ```yaml 85 resource.customizations.actions.argoproj.io_Rollout: | 86 mergeBuiltinActions: true 87 discovery.lua: | 88 actions = {} 89 actions["do-things"] = {} 90 return actions 91 definitions: 92 - name: do-things 93 action.lua: | 94 return obj 95 ``` 96 97 #### Creating new resources with a custom action 98 99 !!! important 100 Creating resources via the Argo CD UI is an intentional, strategic departure from GitOps principles. We recommend 101 that you use this feature sparingly and only for resources that are not part of the desired state of the 102 application. 103 104 The resource the action is invoked on would be referred to as the `source resource`. 105 The new resource and all the resources implicitly created as a result, must be permitted on the AppProject level, otherwise the creation will fail. 106 107 ##### Creating a source resource child resources with a custom action 108 109 If the new resource represents a k8s child of the source resource, the source resource ownerReference must be set on the new resource. 110 Here is an example Lua snippet, that takes care of constructing a Job resource that is a child of a source CronJob resource - the `obj` is a global variable, which contains the source resource: 111 112 ```lua 113 -- ... 114 ownerRef = {} 115 ownerRef.apiVersion = obj.apiVersion 116 ownerRef.kind = obj.kind 117 ownerRef.name = obj.metadata.name 118 ownerRef.uid = obj.metadata.uid 119 job = {} 120 job.metadata = {} 121 job.metadata.ownerReferences = {} 122 job.metadata.ownerReferences[1] = ownerRef 123 -- ... 124 ``` 125 126 ##### Creating independent child resources with a custom action 127 128 If the new resource is independent of the source resource, the default behavior of such new resource is that it is not known by the App of the source resource (as it is not part of the desired state and does not have an `ownerReference`). 129 To make the App aware of the new resource, the `app.kubernetes.io/instance` label (or other ArgoCD tracking label, if configured) must be set on the resource. 130 It can be copied from the source resource, like this: 131 132 ```lua 133 -- ... 134 newObj = {} 135 newObj.metadata = {} 136 newObj.metadata.labels = {} 137 newObj.metadata.labels["app.kubernetes.io/instance"] = obj.metadata.labels["app.kubernetes.io/instance"] 138 -- ... 139 ``` 140 141 While the new resource will be part of the App with the tracking label in place, it will be immediately deleted if auto prune is set on the App. 142 To keep the resource, set `Prune=false` annotation on the resource, with this Lua snippet: 143 144 ```lua 145 -- ... 146 newObj.metadata.annotations = {} 147 newObj.metadata.annotations["argocd.argoproj.io/sync-options"] = "Prune=false" 148 -- ... 149 ``` 150 151 (If setting `Prune=false` behavior, the resource will not be deleted upon the deletion of the App, and will require a manual cleanup). 152 153 The resource and the App will now appear out of sync - which is the expected ArgoCD behavior upon creating a resource that is not part of the desired state. 154 155 If you wish to treat such an App as a synced one, add the following resource annotation in Lua code: 156 157 ```lua 158 -- ... 159 newObj.metadata.annotations["argocd.argoproj.io/compare-options"] = "IgnoreExtraneous" 160 -- ... 161 ``` 162 163 #### An action that produces a list of resources - a complete example: 164 165 ```yaml 166 resource.customizations.actions.ConfigMap: | 167 discovery.lua: | 168 actions = {} 169 actions["do-things"] = {} 170 return actions 171 definitions: 172 - name: do-things 173 action.lua: | 174 -- Create a new ConfigMap 175 cm1 = {} 176 cm1.apiVersion = "v1" 177 cm1.kind = "ConfigMap" 178 cm1.metadata = {} 179 cm1.metadata.name = "cm1" 180 cm1.metadata.namespace = obj.metadata.namespace 181 cm1.metadata.labels = {} 182 -- Copy ArgoCD tracking label so that the resource is recognized by the App 183 cm1.metadata.labels["app.kubernetes.io/instance"] = obj.metadata.labels["app.kubernetes.io/instance"] 184 cm1.metadata.annotations = {} 185 -- For Apps with auto-prune, set the prune false on the resource, so it does not get deleted 186 cm1.metadata.annotations["argocd.argoproj.io/sync-options"] = "Prune=false" 187 -- Keep the App synced even though it has a resource that is not in Git 188 cm1.metadata.annotations["argocd.argoproj.io/compare-options"] = "IgnoreExtraneous" 189 cm1.data = {} 190 cm1.data.myKey1 = "myValue1" 191 impactedResource1 = {} 192 impactedResource1.operation = "create" 193 impactedResource1.resource = cm1 194 195 -- Patch the original cm 196 obj.metadata.labels["aKey"] = "aValue" 197 impactedResource2 = {} 198 impactedResource2.operation = "patch" 199 impactedResource2.resource = obj 200 201 result = {} 202 result[1] = impactedResource1 203 result[2] = impactedResource2 204 return result 205 ``` 206 207 ### Action Icons and Display Names 208 209 By default, an action will appear in the UI by the name specified in the `actions` key, and it will have no icon. You 210 can customize the display name and icon of an action by adding the `iconClass` and `displayName` keys to the action 211 definition. 212 213 The icon class name is the name of a FontAwesome icon from [the set of free icons](https://fontawesome.com/search?ic=free). 214 The `fa-fw` class ensures that the icon is displayed with a fixed width, to avoid alignment issues with other icons. 215 216 ```lua 217 local actions = {} 218 actions["create-workflow"] = { 219 ["iconClass"] = "fa fa-fw fa-plus", 220 ["displayName"] = "Create Workflow" 221 } 222 return actions 223 ``` 224 225 ### Action Parameters 226 227 You can define parameters for your custom actions. The parameters are defined in the `parameters` key of the action discovery definition. 228 229 <!-- Link directly to the script for people reading the docs in GitHub where embedding doesn't work. --> 230 See the [Deployment actions discovery script](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/apps/Deployment/actions/discovery.lua): 231 232 <!-- Embed the actual script so ReadTheDocs always has an up-to-date example. --> 233 ```lua 234 {!resource_customizations/apps/Deployment/actions/discovery.lua!} 235 ``` 236 237 The [resource scale actions](../user-guide/scale_application_resources.md) documentation shows how this function behaves in the UI.