forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
54 lines (47 loc) · 1.32 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
package hook
import (
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
)
func IsHook(obj *unstructured.Unstructured) bool {
for _, hookType := range types(obj) {
switch v1alpha1.HookType(hookType) {
case v1alpha1.HookTypeSkip:
return !Skip(obj)
default:
return true
}
}
return false
}
func Skip(obj *unstructured.Unstructured) bool {
for _, hookType := range types(obj) {
if v1alpha1.HookType(hookType) == v1alpha1.HookTypeSkip {
return len(types(obj)) == 1
}
}
return false
}
func Types(obj *unstructured.Unstructured) []v1alpha1.HookType {
var hookTypes []v1alpha1.HookType
for _, hookType := range types(obj) {
switch v1alpha1.HookType(hookType) {
case v1alpha1.HookTypePreSync, v1alpha1.HookTypeSync, v1alpha1.HookTypePostSync, v1alpha1.HookTypeSyncFail:
hookTypes = append(hookTypes, v1alpha1.HookType(hookType))
}
}
return hookTypes
}
// returns a normalize list of strings
func types(obj *unstructured.Unstructured) []string {
var hookTypes []string
for _, hookType := range strings.Split(obj.GetAnnotations()[common.AnnotationKeyHook], ",") {
trimmed := strings.TrimSpace(hookType)
if len(trimmed) > 0 {
hookTypes = append(hookTypes, trimmed)
}
}
return hookTypes
}