Skip to content

Commit 1cdd636

Browse files
committed
feat: use resource actions for auto-sync toggle instead of update permission (#21564)
Signed-off-by: ebracha <[email protected]>
1 parent fc147e3 commit 1cdd636

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
tests:
2+
- given:
3+
apiVersion: argoproj.io/v1alpha1
4+
kind: Application
5+
metadata:
6+
name: test-app
7+
spec:
8+
syncPolicy:
9+
automated:
10+
enabled: true
11+
prune: true
12+
selfHeal: true
13+
when:
14+
action: toggle-auto-sync
15+
expect:
16+
spec:
17+
syncPolicy:
18+
automated:
19+
enabled: false
20+
prune: true
21+
selfHeal: true
22+
23+
- given:
24+
apiVersion: argoproj.io/v1alpha1
25+
kind: Application
26+
metadata:
27+
name: test-app
28+
spec:
29+
syncPolicy:
30+
automated:
31+
enabled: false
32+
prune: true
33+
selfHeal: true
34+
when:
35+
action: toggle-auto-sync
36+
expect:
37+
spec:
38+
syncPolicy:
39+
automated:
40+
enabled: true
41+
prune: true
42+
selfHeal: true
43+
44+
- given:
45+
apiVersion: argoproj.io/v1alpha1
46+
kind: Application
47+
metadata:
48+
name: test-app
49+
spec: {}
50+
when:
51+
action: toggle-auto-sync
52+
expect:
53+
spec:
54+
syncPolicy:
55+
automated:
56+
enabled: false
57+
prune: false
58+
selfHeal: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
actions = {}
2+
actions["toggle-auto-sync"] = {}
3+
return actions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function toggleAutoSync(obj)
2+
if obj.spec.syncPolicy and obj.spec.syncPolicy.automated then
3+
obj.spec.syncPolicy.automated = nil
4+
if not next(obj.spec.syncPolicy) then
5+
obj.spec.syncPolicy = nil
6+
end
7+
else
8+
if not obj.spec.syncPolicy then
9+
obj.spec.syncPolicy = {}
10+
end
11+
obj.spec.syncPolicy.automated = {
12+
enabled = true
13+
}
14+
end
15+
return obj
16+
end
17+
18+
return toggleAutoSync(obj)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
health_status = {}
2+
if obj.status ~= nil then
3+
if obj.status.health ~= nil then
4+
health_status.status = obj.status.health.status
5+
health_status.message = obj.status.health.message
6+
end
7+
end
8+
if health_status.status == nil then
9+
health_status.status = "Progressing"
10+
health_status.message = "Waiting for application to be reconciled"
11+
end
12+
return health_status
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
tests = {
2+
{
3+
given = {
4+
status = {
5+
health = {
6+
status = "Healthy",
7+
message = "Application is healthy"
8+
}
9+
}
10+
},
11+
want = {
12+
status = "Healthy",
13+
message = "Application is healthy"
14+
}
15+
},
16+
{
17+
given = {
18+
status = {
19+
health = {
20+
status = "Degraded",
21+
message = "Application has issues"
22+
}
23+
}
24+
},
25+
want = {
26+
status = "Degraded",
27+
message = "Application has issues"
28+
}
29+
},
30+
{
31+
given = {},
32+
want = {
33+
status = "Progressing",
34+
message = "Waiting for application to be reconciled"
35+
}
36+
}
37+
}
38+
39+
for _, test in ipairs(tests) do
40+
local state = health(test.given)
41+
if state.status ~= test.want.status then
42+
error(string.format("Expected status %s but got %s", test.want.status, state.status))
43+
end
44+
if state.message ~= test.want.message then
45+
error(string.format("Expected message '%s' but got '%s'", test.want.message, state.message))
46+
end
47+
end

Diff for: server/application/application.go

+7
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,13 @@ func (s *Server) getUnstructuredLiveResourceOrApp(ctx context.Context, rbacReque
23802380
if err != nil {
23812381
return nil, nil, nil, nil, err
23822382
}
2383+
2384+
app.SetGroupVersionKind(schema.GroupVersionKind{
2385+
Group: applicationType.Group,
2386+
Version: v1alpha1.SchemeGroupVersion.Version,
2387+
Kind: applicationType.ApplicationKind,
2388+
})
2389+
23832390
if err = s.enf.EnforceErr(ctx.Value("claims"), rbac.ResourceApplications, rbacRequest, app.RBACName(s.ns)); err != nil {
23842391
return nil, nil, nil, nil, err
23852392
}

0 commit comments

Comments
 (0)