forked from flant/shell-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_config.go
164 lines (143 loc) · 4.53 KB
/
monitor_config.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
159
160
161
162
163
164
package kube_events_manager
import (
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
. "github.com/flant/shell-operator/pkg/kube_events_manager/types"
)
// KubeEventMonitorConfig is a config that suits the latest
// version of OnKubernetesEventConfig.
type MonitorConfig struct {
Metadata struct {
MonitorId string
DebugName string
LogLabels map[string]string
MetricLabels map[string]string
}
EventTypes []WatchEventType
ApiVersion string
Kind string
NameSelector *NameSelector
NamespaceSelector *NamespaceSelector
LabelSelector *metav1.LabelSelector
FieldSelector *FieldSelector
JqFilter string
LogEntry *log.Entry
Mode KubeEventMode
KeepFullObjectsInMemory bool
FilterFunc func(*unstructured.Unstructured) (interface{}, error)
}
func (c *MonitorConfig) WithEventTypes(types []WatchEventType) *MonitorConfig {
if types == nil {
c.EventTypes = []WatchEventType{
WatchEventAdded,
WatchEventModified,
WatchEventDeleted,
}
} else {
c.EventTypes = []WatchEventType{}
c.EventTypes = append(c.EventTypes, types...)
}
return c
}
// WithNamespaceSelector copies input NamespaceSelector into monitor.NamespaceSelector
func (c *MonitorConfig) WithNameSelector(nSel *NameSelector) {
if nSel != nil {
c.NameSelector = &NameSelector{
MatchNames: nSel.MatchNames,
}
}
}
// WithNamespaceSelector copies input NamespaceSelector into monitor.NamespaceSelector
func (c *MonitorConfig) WithNamespaceSelector(nsSel *NamespaceSelector) {
if nsSel != nil {
c.NamespaceSelector = &NamespaceSelector{}
if nsSel.NameSelector != nil {
c.NamespaceSelector.NameSelector = &NameSelector{
MatchNames: nsSel.NameSelector.MatchNames,
}
}
if nsSel.LabelSelector != nil {
c.NamespaceSelector.LabelSelector = &metav1.LabelSelector{
MatchLabels: nsSel.LabelSelector.MatchLabels,
MatchExpressions: nsSel.LabelSelector.MatchExpressions,
}
}
}
}
// WithFieldSelector copies input FieldSelector into monitor.FieldSelector
func (c *MonitorConfig) WithFieldSelector(fieldSel *FieldSelector) {
if fieldSel != nil {
c.FieldSelector = &FieldSelector{
MatchExpressions: fieldSel.MatchExpressions,
}
}
}
func (c *MonitorConfig) AddFieldSelectorRequirement(field string, op string, value string) {
if c.FieldSelector == nil {
c.FieldSelector = &FieldSelector{
MatchExpressions: []FieldSelectorRequirement{},
}
}
if c.FieldSelector.MatchExpressions == nil {
c.FieldSelector.MatchExpressions = make([]FieldSelectorRequirement, 0)
}
req := FieldSelectorRequirement{
Field: field,
Operator: op,
Value: value,
}
c.FieldSelector.MatchExpressions = append(c.FieldSelector.MatchExpressions, req)
}
// WithLabelSelector copies input LabelSelector into monitor.LabelSelector
func (c *MonitorConfig) WithLabelSelector(labelSel *metav1.LabelSelector) {
if labelSel != nil {
c.LabelSelector = &metav1.LabelSelector{
MatchLabels: labelSel.MatchLabels,
MatchExpressions: labelSel.MatchExpressions,
}
}
}
func (c *MonitorConfig) IsAnyNamespace() bool {
return c.NamespaceSelector == nil ||
(c.NamespaceSelector.NameSelector == nil && c.NamespaceSelector.LabelSelector == nil) ||
(c.NamespaceSelector.NameSelector != nil && len(c.NamespaceSelector.NameSelector.MatchNames) == 0)
}
// Names returns names of monitored objects if nameSelector.matchNames is defined in config.
func (c *MonitorConfig) Names() []string {
res := []string{}
if c.NameSelector != nil {
res = c.NameSelector.MatchNames
}
return res
}
// Namespaces returns names of namespaces if namescpace.nameSelector
// is defined in config.
//
// If no namespace specified or no namespace.nameSelector or
// length of namespace.nameSeletor.matchNames is 0
// then empty string is returned to monitor all namespaces.
//
// If namespace.labelSelector is specified, then return empty array.
func (c *MonitorConfig) Namespaces() (nsNames []string) {
if c.NamespaceSelector == nil {
return []string{""}
}
if c.NamespaceSelector.LabelSelector != nil {
return []string{}
}
if c.NamespaceSelector.NameSelector == nil {
return []string{""}
}
if len(c.NamespaceSelector.NameSelector.MatchNames) == 0 {
return []string{""}
}
nsNames = c.NamespaceSelector.NameSelector.MatchNames
return nsNames
}
func (c *MonitorConfig) WithMode(mode KubeEventMode) {
if mode == "" {
c.Mode = ModeIncremental
}
c.Mode = mode
}