-
Notifications
You must be signed in to change notification settings - Fork 214
/
types.go
194 lines (165 loc) · 5.18 KB
/
types.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package types
import (
"encoding/json"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type WatchEventType string
const (
WatchEventAdded WatchEventType = "Added"
WatchEventModified WatchEventType = "Modified"
WatchEventDeleted WatchEventType = "Deleted"
)
type KubeEventType string
const (
TypeSynchronization KubeEventType = "Synchronization"
TypeEvent KubeEventType = "Event"
)
// TODO remove this type with cleanup of v0.
type KubeEventMode string
const (
ModeV0 KubeEventMode = "v0" // No first Synchronization, only Event.
ModeIncremental KubeEventMode = "Incremental" // Send Synchronization with existed object and Event for each followed event.
)
type ObjectAndFilterResult struct {
Metadata struct {
JqFilter string
Checksum string
ResourceId string // Used for sorting
RemoveObject bool
}
Object *unstructured.Unstructured // here is a pointer because of MarshalJSON receiver
FilterResult interface{}
// since len() return int, there is no reason to use larger int
FilterResultSize int
ObjectSize int
}
func (o ObjectAndFilterResult) Map() map[string]interface{} {
m := map[string]interface{}{}
if !o.Metadata.RemoveObject {
m["object"] = o.Object
}
if o.Metadata.JqFilter == "" {
return m
}
// Add filterResult field only if it was requested
filterResString, ok := o.FilterResult.(string)
if !ok || filterResString == "" {
m["filterResult"] = nil
return m
}
if filterResString == "" {
m["filterResult"] = nil
return m
}
var res interface{}
err := json.Unmarshal([]byte(filterResString), &res)
if err != nil {
log.Errorf("Possible bug!!! Cannot unmarshal jq filter '%s' result: %s", o.Metadata.JqFilter, err)
m["filterResult"] = nil
return m
}
m["filterResult"] = res
return m
}
func (o ObjectAndFilterResult) MarshalJSON() ([]byte, error) {
return json.Marshal(o.Map())
}
func (o *ObjectAndFilterResult) RemoveFullObject() {
o.Object = nil
o.ObjectSize = 0
o.Metadata.RemoveObject = true
}
type ObjectAndFilterResults map[string]*ObjectAndFilterResult
func (a ObjectAndFilterResults) Bytes() (size int64) {
for _, o := range a {
size += int64(o.FilterResultSize)
size += int64(o.ObjectSize)
}
return
}
// ByNamespaceAndName implements sort.Interface for []ObjectAndFilterResult
// based on Namespace and Name of Object field.
// TODO use special fields instead of ResourceId. ResourceId can be changed in the future.
type ByNamespaceAndName []ObjectAndFilterResult
func (a ByNamespaceAndName) Len() int { return len(a) }
func (a ByNamespaceAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByNamespaceAndName) Less(i, j int) bool {
p, q := &a[i], &a[j]
if p.Object == nil || q.Object == nil {
return p.Metadata.ResourceId < q.Metadata.ResourceId
}
switch {
case p.Object.GetNamespace() < q.Object.GetNamespace():
return true
case p.Object.GetNamespace() > q.Object.GetNamespace():
return false
}
// Namespaces are equal, so compare names.
return p.Object.GetName() < q.Object.GetName()
}
// KubeEvent contains MonitorId from monitor configuration, event type
// and involved k8s objects.
type KubeEvent struct {
MonitorId string
Type KubeEventType // Event or Synchronization
WatchEvents []WatchEventType
Objects []ObjectAndFilterResult
}
func (k KubeEvent) String() string {
msgs := []string{}
switch k.Type {
case TypeSynchronization:
if len(k.Objects) > 0 {
kind := k.Objects[0].Object.GetKind()
msgs = append(msgs, fmt.Sprintf("Synchronization with %d objects of kind '%s'", len(k.Objects), kind))
} else {
msgs = append(msgs, "Synchronization with 0 objects")
}
case TypeEvent:
if len(k.Objects) == 1 {
obj := k.Objects[0].Object
if len(k.WatchEvents) > 0 {
if obj != nil {
msgs = append(msgs, fmt.Sprintf("Event '%s' for %s/%s/%s", k.WatchEvents[0], obj.GetNamespace(), obj.GetKind(), obj.GetName()))
} else {
msgs = append(msgs, fmt.Sprintf("Event '%s' without object", k.WatchEvents[0]))
}
} else {
if obj != nil {
msgs = append(msgs, fmt.Sprintf("Event with no kubernetes event type for %s/%s/%s", obj.GetNamespace(), obj.GetKind(), obj.GetName()))
} else {
msgs = append(msgs, "Event with no kubernetes event type")
}
}
} else {
if len(k.WatchEvents) > 0 {
msgs = append(msgs, fmt.Sprintf("Event '%s' without objects", k.WatchEvents[0]))
} else {
msgs = append(msgs, "Event without objects and kubernetes event type")
}
}
default:
msgs = append(msgs, fmt.Sprintf("unknown type: '%s' from monitor '%s'", k.Type, k.MonitorId))
}
return strings.Join(msgs, " ")
}
// Monitor configuration
type NameSelector struct {
MatchNames []string `json:"matchNames"`
}
type FieldSelectorRequirement struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value string `json:"value,omitempty"`
}
type FieldSelector struct {
MatchExpressions []FieldSelectorRequirement `json:"matchExpressions"`
}
type NamespaceSelector struct {
NameSelector *NameSelector `json:"nameSelector,omitempty"`
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
}