forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
55 lines (48 loc) · 1.45 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
package hook
import (
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/argo-cd/common"
argoappv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
)
// IsHook indicates if the object is either a Argo CD or Helm hook
func IsHook(obj *unstructured.Unstructured) bool {
return IsArgoHook(obj) || IsHelmHook(obj)
}
// IsHelmHook indicates if the supplied object is a helm hook
func IsHelmHook(obj *unstructured.Unstructured) bool {
annotations := obj.GetAnnotations()
if annotations == nil {
return false
}
hooks, ok := annotations[common.AnnotationKeyHelmHook]
if ok && hasHook(hooks, common.AnnotationValueHelmHookCRDInstall) {
return false
}
return ok
}
func hasHook(hooks string, hook string) bool {
for _, item := range strings.Split(hooks, ",") {
if strings.TrimSpace(item) == hook {
return true
}
}
return false
}
// IsArgoHook indicates if the supplied object is an Argo CD application lifecycle hook
// (vs. a normal, synced application resource)
func IsArgoHook(obj *unstructured.Unstructured) bool {
annotations := obj.GetAnnotations()
if annotations == nil {
return false
}
resHookTypes := strings.Split(annotations[common.AnnotationKeyHook], ",")
for _, hookType := range resHookTypes {
hookType = strings.TrimSpace(hookType)
switch argoappv1.HookType(hookType) {
case argoappv1.HookTypePreSync, argoappv1.HookTypeSync, argoappv1.HookTypePostSync:
return true
}
}
return false
}