forked from flant/shell-operator
/
monitor_config.go
139 lines (122 loc) · 3.74 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
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/akaitux/shell-operator/pkg/kube_events_manager/types"
)
// MonitorConfig 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
}
// WithNameSelector copies input NameSelector into monitor.NameSelector
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,
}
}
}
// 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,
}
}
}
// names returns names of monitored objects if nameSelector.matchNames is defined in config.
func (c *MonitorConfig) names() []string {
res := make([]string, 0)
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
}