From 48f18e29055d01dfacf882c3dcac2d6c2f6f3e0c Mon Sep 17 00:00:00 2001 From: Vikas Rao <100244218+vikasrao23@users.noreply.github.com> Date: Tue, 7 Apr 2026 03:12:32 -0700 Subject: [PATCH] feat: add toggle-auto-sync resource action for Application (#21564) (#26477) Signed-off-by: vikasrao23 Co-authored-by: vikasrao23 --- .../resource_actions_builtin.md | 1 + .../Application/actions/actions_test.yaml | 76 +++++++++++++++++++ .../Application/actions/discovery.lua | 11 +++ .../actions/toggle-auto-sync/action.lua | 23 ++++++ server/application/application.go | 5 ++ 5 files changed, 116 insertions(+) create mode 100644 resource_customizations/argoproj.io/Application/actions/actions_test.yaml create mode 100644 resource_customizations/argoproj.io/Application/actions/discovery.lua create mode 100644 resource_customizations/argoproj.io/Application/actions/toggle-auto-sync/action.lua diff --git a/docs/operator-manual/resource_actions_builtin.md b/docs/operator-manual/resource_actions_builtin.md index 5d1898dc35..380d4c5044 100644 --- a/docs/operator-manual/resource_actions_builtin.md +++ b/docs/operator-manual/resource_actions_builtin.md @@ -6,6 +6,7 @@ - [apps/StatefulSet/restart](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/apps/StatefulSet/actions/restart/action.lua) - [apps/StatefulSet/scale](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/apps/StatefulSet/actions/scale/action.lua) - [argoproj.io/AnalysisRun/terminate](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/argoproj.io/AnalysisRun/actions/terminate/action.lua) +- [argoproj.io/Application/toggle-auto-sync](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/argoproj.io/Application/actions/toggle-auto-sync/action.lua) - [argoproj.io/CronWorkflow/create-workflow](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/argoproj.io/CronWorkflow/actions/create-workflow/action.lua) - [argoproj.io/Rollout/abort](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/argoproj.io/Rollout/actions/abort/action.lua) - [argoproj.io/Rollout/pause](https://github.com/argoproj/argo-cd/blob/master/resource_customizations/argoproj.io/Rollout/actions/pause/action.lua) diff --git a/resource_customizations/argoproj.io/Application/actions/actions_test.yaml b/resource_customizations/argoproj.io/Application/actions/actions_test.yaml new file mode 100644 index 0000000000..40a07aab2f --- /dev/null +++ b/resource_customizations/argoproj.io/Application/actions/actions_test.yaml @@ -0,0 +1,76 @@ +tests: + # Scenario 1: autosync enabled (enabled=nil, i.e. default) with prune+selfHeal -> disable (set enabled=false, preserve prune/selfHeal) + - given: + apiVersion: argoproj.io/v1alpha1 + kind: Application + metadata: + name: test-app + spec: + syncPolicy: + automated: + prune: true + selfHeal: true + when: + action: toggle-auto-sync + expect: + spec: + syncPolicy: + automated: + prune: true + selfHeal: true + enabled: false + + # Scenario 2: autosync disabled (enabled=false) with prune+selfHeal -> enable (set enabled=true, preserve prune/selfHeal) + - given: + apiVersion: argoproj.io/v1alpha1 + kind: Application + metadata: + name: test-app + spec: + syncPolicy: + automated: + prune: true + selfHeal: true + enabled: false + when: + action: toggle-auto-sync + expect: + spec: + syncPolicy: + automated: + prune: true + selfHeal: true + enabled: true + + # Scenario 3: autosync explicitly enabled (enabled=true) -> disable + - given: + apiVersion: argoproj.io/v1alpha1 + kind: Application + metadata: + name: test-app + spec: + syncPolicy: + automated: + enabled: true + when: + action: toggle-auto-sync + expect: + spec: + syncPolicy: + automated: + enabled: false + + # Scenario 4: no automated block (autosync off) -> enable (create automated with enabled=true) + - given: + apiVersion: argoproj.io/v1alpha1 + kind: Application + metadata: + name: test-app + spec: {} + when: + action: toggle-auto-sync + expect: + spec: + syncPolicy: + automated: + enabled: true diff --git a/resource_customizations/argoproj.io/Application/actions/discovery.lua b/resource_customizations/argoproj.io/Application/actions/discovery.lua new file mode 100644 index 0000000000..c305844190 --- /dev/null +++ b/resource_customizations/argoproj.io/Application/actions/discovery.lua @@ -0,0 +1,11 @@ +local actions = {} +local autoSyncEnabled = false +if obj.spec ~= nil and obj.spec.syncPolicy ~= nil and obj.spec.syncPolicy.automated ~= nil then + local enabled = obj.spec.syncPolicy.automated.enabled + autoSyncEnabled = (enabled == true or enabled == nil) +end +actions["toggle-auto-sync"] = { + ["displayName"] = autoSyncEnabled and "Disable Auto-Sync" or "Enable Auto-Sync", + ["disabled"] = false +} +return actions diff --git a/resource_customizations/argoproj.io/Application/actions/toggle-auto-sync/action.lua b/resource_customizations/argoproj.io/Application/actions/toggle-auto-sync/action.lua new file mode 100644 index 0000000000..eca3c21b97 --- /dev/null +++ b/resource_customizations/argoproj.io/Application/actions/toggle-auto-sync/action.lua @@ -0,0 +1,23 @@ +if obj.spec == nil then + obj.spec = {} +end +if obj.spec.syncPolicy == nil then + obj.spec.syncPolicy = {} +end + +local automated = obj.spec.syncPolicy.automated +local isEnabled = automated ~= nil and (automated.enabled == true or automated.enabled == nil) + +if isEnabled then + -- Currently ENABLED (enabled=true or enabled=nil): disable it + automated.enabled = false +else + -- Currently DISABLED (automated absent or enabled=false): enable it + if automated == nil then + obj.spec.syncPolicy.automated = { enabled = true } + else + automated.enabled = true + end +end + +return obj diff --git a/server/application/application.go b/server/application/application.go index 7029a57bd8..d4c598f804 100644 --- a/server/application/application.go +++ b/server/application/application.go @@ -2540,6 +2540,11 @@ func (s *Server) getUnstructuredLiveResourceOrApp(ctx context.Context, rbacReque if err != nil { return nil, nil, nil, nil, err } + app.SetGroupVersionKind(schema.GroupVersionKind{ + Group: applicationType.Group, + Version: v1alpha1.SchemeGroupVersion.Version, + Kind: applicationType.ApplicationKind, + }) err = s.enf.EnforceErr(ctx.Value("claims"), rbac.ResourceApplications, rbacRequest, app.RBACName(s.ns)) if err != nil { return nil, nil, nil, nil, err