-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
hook.go
158 lines (142 loc) · 4.68 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package controller
import (
"context"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/argoproj/gitops-engine/pkg/sync/common"
"github.com/argoproj/gitops-engine/pkg/sync/hook"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
log "github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
"github.com/argoproj/argo-cd/v2/util/lua"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)
var (
postDeleteHook = "PostDelete"
postDeleteHooks = map[string]string{
"argocd.argoproj.io/hook": postDeleteHook,
"helm.sh/hook": "post-delete",
}
)
func isHook(obj *unstructured.Unstructured) bool {
return hook.IsHook(obj) || isPostDeleteHook(obj)
}
func isPostDeleteHook(obj *unstructured.Unstructured) bool {
if obj == nil || obj.GetAnnotations() == nil {
return false
}
for k, v := range postDeleteHooks {
if val, ok := obj.GetAnnotations()[k]; ok && val == v {
return true
}
}
return false
}
func (ctrl *ApplicationController) executePostDeleteHooks(app *v1alpha1.Application, proj *v1alpha1.AppProject, liveObjs map[kube.ResourceKey]*unstructured.Unstructured, config *rest.Config, logCtx *log.Entry) (bool, error) {
appLabelKey, err := ctrl.settingsMgr.GetAppInstanceLabelKey()
if err != nil {
return false, err
}
var revisions []string
for _, src := range app.Spec.GetSources() {
revisions = append(revisions, src.TargetRevision)
}
targets, _, err := ctrl.appStateManager.GetRepoObjs(app, app.Spec.GetSources(), appLabelKey, revisions, false, false, false, proj)
if err != nil {
return false, err
}
runningHooks := map[kube.ResourceKey]*unstructured.Unstructured{}
for key, obj := range liveObjs {
if isPostDeleteHook(obj) {
runningHooks[key] = obj
}
}
expectedHook := map[kube.ResourceKey]*unstructured.Unstructured{}
for _, obj := range targets {
if obj.GetNamespace() == "" {
obj.SetNamespace(app.Spec.Destination.Namespace)
}
if !isPostDeleteHook(obj) {
continue
}
if runningHook := runningHooks[kube.GetResourceKey(obj)]; runningHook == nil {
expectedHook[kube.GetResourceKey(obj)] = obj
}
}
createdCnt := 0
for _, obj := range expectedHook {
_, err = ctrl.kubectl.CreateResource(context.Background(), config, obj.GroupVersionKind(), obj.GetName(), obj.GetNamespace(), obj, v1.CreateOptions{})
if err != nil {
return false, err
}
createdCnt++
}
if createdCnt > 0 {
logCtx.Infof("Created %d post-delete hooks", createdCnt)
return false, nil
}
resourceOverrides, err := ctrl.settingsMgr.GetResourceOverrides()
if err != nil {
return false, err
}
healthOverrides := lua.ResourceHealthOverrides(resourceOverrides)
progressingHooksCnt := 0
for _, obj := range runningHooks {
hookHealth, err := health.GetResourceHealth(obj, healthOverrides)
if err != nil {
return false, err
}
if hookHealth.Status == health.HealthStatusProgressing {
progressingHooksCnt++
}
}
if progressingHooksCnt > 0 {
logCtx.Infof("Waiting for %d post-delete hooks to complete", progressingHooksCnt)
return false, nil
}
return true, nil
}
func (ctrl *ApplicationController) cleanupPostDeleteHooks(liveObjs map[kube.ResourceKey]*unstructured.Unstructured, config *rest.Config, logCtx *log.Entry) (bool, error) {
resourceOverrides, err := ctrl.settingsMgr.GetResourceOverrides()
if err != nil {
return false, err
}
healthOverrides := lua.ResourceHealthOverrides(resourceOverrides)
pendingDeletionCount := 0
aggregatedHealth := health.HealthStatusHealthy
var hooks []*unstructured.Unstructured
for _, obj := range liveObjs {
if !isPostDeleteHook(obj) {
continue
}
hookHealth, err := health.GetResourceHealth(obj, healthOverrides)
if err != nil {
return false, err
}
if health.IsWorse(aggregatedHealth, hookHealth.Status) {
aggregatedHealth = hookHealth.Status
}
hooks = append(hooks, obj)
}
for _, obj := range hooks {
for _, policy := range hook.DeletePolicies(obj) {
if policy == common.HookDeletePolicyHookFailed && aggregatedHealth == health.HealthStatusDegraded || policy == common.HookDeletePolicyHookSucceeded && aggregatedHealth == health.HealthStatusHealthy {
pendingDeletionCount++
if obj.GetDeletionTimestamp() != nil {
continue
}
logCtx.Infof("Deleting post-delete hook %s/%s", obj.GetNamespace(), obj.GetName())
err = ctrl.kubectl.DeleteResource(context.Background(), config, obj.GroupVersionKind(), obj.GetName(), obj.GetNamespace(), v1.DeleteOptions{})
if err != nil {
return false, err
}
}
}
}
if pendingDeletionCount > 0 {
logCtx.Infof("Waiting for %d post-delete hooks to be deleted", pendingDeletionCount)
return false, nil
}
return true, nil
}