/
go_hook.go
144 lines (125 loc) · 4.34 KB
/
go_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
package go_hook
import (
"time"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/flant/addon-operator/pkg/module_manager/go_hook/metrics"
"github.com/flant/shell-operator/pkg/kube/object_patch"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
)
type GoHook interface {
Config() *HookConfig
Run(input *HookInput) error
}
// MetricsCollector collects metric's records for exporting them as a batch
type MetricsCollector interface {
// Inc increments the specified Counter metric
Inc(name string, labels map[string]string, opts ...metrics.Option)
// Add adds custom value for the specified Counter metric
Add(name string, value float64, labels map[string]string, opts ...metrics.Option)
// Set specifies the custom value for the Gauge metric
Set(name string, value float64, labels map[string]string, opts ...metrics.Option)
// Expire marks metric's group as expired
Expire(group string)
}
type HookMetadata struct {
Name string
Path string
Global bool
Module bool
ModuleName string
}
type FilterResult interface{}
type Snapshots map[string][]FilterResult
type HookInput struct {
Snapshots Snapshots
Values *PatchableValues
ConfigValues *PatchableValues
MetricsCollector MetricsCollector
PatchCollector *object_patch.PatchCollector
LogEntry *logrus.Entry
BindingActions *[]BindingAction
}
type BindingAction struct {
Name string // binding name
Action string // Disable / UpdateKind
Kind string
ApiVersion string
}
type HookConfig struct {
Schedule []ScheduleConfig
Kubernetes []KubernetesConfig
// OnStartup runs hook on module/global startup
// Attention! During the startup you don't have snapshots available
// use native KubeClient to fetch resources
OnStartup *OrderedConfig
OnBeforeHelm *OrderedConfig
OnAfterHelm *OrderedConfig
OnAfterDeleteHelm *OrderedConfig
OnBeforeAll *OrderedConfig
OnAfterAll *OrderedConfig
AllowFailure bool
Queue string
Settings *HookConfigSettings
}
type HookConfigSettings struct {
ExecutionMinInterval time.Duration
ExecutionBurst int
// EnableSchedulesOnStartup
// set to true, if you need to run 'Schedule' hooks without waiting addon-operator readiness
EnableSchedulesOnStartup bool
}
type ScheduleConfig struct {
Name string
// Crontab is a schedule config in crontab format. (5 or 6 fields)
Crontab string
}
type FilterFunc func(*unstructured.Unstructured) (FilterResult, error)
type KubernetesConfig struct {
// Name is a key in snapshots map.
Name string
// ApiVersion of objects. "v1" is used if not set.
ApiVersion string
// Kind of objects.
Kind string
// NameSelector used to subscribe on object by its name.
NameSelector *types.NameSelector
// NamespaceSelector used to subscribe on objects in namespaces.
NamespaceSelector *types.NamespaceSelector
// LabelSelector used to subscribe on objects by matching their labels.
LabelSelector *v1.LabelSelector
// FieldSelector used to subscribe on objects by matching specific fields (the list of fields is narrow, see shell-operator documentation).
FieldSelector *types.FieldSelector
// ExecuteHookOnEvents is true by default. Set to false if only snapshot update is needed.
ExecuteHookOnEvents *bool
// ExecuteHookOnSynchronization is true by default. Set to false if only snapshot update is needed.
ExecuteHookOnSynchronization *bool
// WaitForSynchronization is true by default. Set to false if beforeHelm is not required this snapshot on start.
WaitForSynchronization *bool
// FilterFunc used to filter object content for snapshot. Addon-operator use checksum of this filtered result to ignore irrelevant events.
FilterFunc FilterFunc
}
type OrderedConfig struct {
Order float64
}
type HookBindingContext struct {
// Type of binding context: [Event, Synchronization, Group, Schedule]
Type string
// Binding is a related binding name.
Binding string
// Snapshots contain all objects for all bindings.
Snapshots map[string][]types.ObjectAndFilterResult
}
// Bool returns a pointer to a bool.
func Bool(b bool) *bool {
return &b
}
// BoolDeref dereferences the bool ptr and returns it if not nil, or else
// returns def.
func BoolDeref(ptr *bool, def bool) bool {
if ptr != nil {
return *ptr
}
return def
}