-
Notifications
You must be signed in to change notification settings - Fork 27
/
hook_metadata.go
112 lines (94 loc) · 3.57 KB
/
hook_metadata.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
package task
import (
"fmt"
"sort"
"strings"
log "github.com/sirupsen/logrus"
. "github.com/flant/shell-operator/pkg/hook/binding_context"
"github.com/flant/shell-operator/pkg/hook/task_metadata"
. "github.com/flant/shell-operator/pkg/hook/types"
"github.com/flant/shell-operator/pkg/task"
)
// HookMetadata is a metadata for addon-operator tasks
type HookMetadata struct {
EventDescription string // event name for informative queue dump
HookName string
ModuleName string
Binding string // binding name from configuration
BindingType BindingType
BindingContext []BindingContext
AllowFailure bool // Task considered as 'ok' if hook failed. False by default. Can be true for some schedule hooks.
DoModuleStartup bool // Execute onStartup and kubernetes@Synchronization hooks for module
IsReloadAll bool // ModuleRun task is a part of 'Reload all modules' process.
ValuesChecksum string // checksum of global values before first afterAll hook execution
DynamicEnabledChecksum string // checksum of dynamicEnabled before first afterAll hook execution
LastAfterAllHook bool // true if task is a last afterAll hook in sequence
ReloadAllOnValuesChanges bool // whether to run DiscoverModules process if hook change global values
KubernetesBindingId string // Unique id for kubernetes bindings
WaitForSynchronization bool // kubernetes.Synchronization task should be waited
MonitorIDs []string // an array of monitor IDs to unlock Kubernetes events after Synchronization.
ExecuteOnSynchronization bool // A flag to skip hook execution in Synchronization tasks.
}
var (
_ task_metadata.HookNameAccessor = HookMetadata{}
_ task_metadata.BindingContextAccessor = HookMetadata{}
_ task_metadata.MonitorIDAccessor = HookMetadata{}
_ task.MetadataDescriptable = HookMetadata{}
)
func HookMetadataAccessor(t task.Task) (meta HookMetadata) {
taskMeta := t.GetMetadata()
if taskMeta == nil {
log.Errorf("Possible Bug! task metadata is nil")
return
}
meta, ok := taskMeta.(HookMetadata)
if !ok {
log.Errorf("Possible Bug! task '%s' metadata is not of type ModuleHookMetadata: got %T", t.GetType(), meta)
return
}
return
}
func (hm HookMetadata) GetDescription() string {
bindingsMap := make(map[string]struct{})
bindings := make([]string, 0)
for _, bc := range hm.BindingContext {
bindingsMap[bc.Binding] = struct{}{}
}
for bindingName := range bindingsMap {
bindings = append(bindings, bindingName)
}
sort.Strings(bindings)
bindingNames := ""
if len(bindings) > 0 {
bindingNames = ":" + strings.Join(bindings, ",")
}
if len(hm.BindingContext) > 1 {
bindingNames = fmt.Sprintf("%s in %d contexts", bindingNames, len(hm.BindingContext))
}
if hm.ModuleName == "" {
// global hook
return fmt.Sprintf("%s:%s%s:%s", string(hm.BindingType), hm.HookName, bindingNames, hm.EventDescription)
}
if hm.HookName == "" {
// module run
osh := ""
if hm.DoModuleStartup {
osh = ":doStartup"
}
return fmt.Sprintf("%s%s%s:%s", hm.ModuleName, osh, bindingNames, hm.EventDescription)
}
// module hook
return fmt.Sprintf("%s:%s%s:%s", string(hm.BindingType), hm.HookName, bindingNames, hm.EventDescription)
}
func (hm HookMetadata) GetHookName() string {
return hm.HookName
}
func (hm HookMetadata) GetBindingContext() []BindingContext {
return hm.BindingContext
}
func (hm HookMetadata) GetMonitorIDs() []string {
return hm.MonitorIDs
}
func (hm HookMetadata) IsSynchronization() bool {
return len(hm.BindingContext) > 0 && hm.BindingContext[0].IsSynchronization()
}