-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
63 lines (51 loc) · 1.38 KB
/
events.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
package events
import (
"encoding/hex"
"fmt"
"hash/fnv"
"sort"
"strings"
"time"
"k8s.io/apimachinery/pkg/types"
)
type EventData map[string]string
func (d *EventData) Get(key string) string {
return (*d)[key]
}
type Event struct {
Name string `json:"name,omitempty"`
ActiveTime time.Time `json:"activeTime,omitempty"`
// Data is a pointer ref so these events can be added into the workqueue of the Dispatcher
Data *EventData `json:"data,omitempty"`
}
// Key hash the EventData into a key that can be used to de-duplicate events.
func (e Event) Key() string {
if (e.Data == nil || len(*e.Data) == 0) && len(e.Name) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString(e.Name)
if e.Data != nil {
keys := make([]string, 0, len(*e.Data))
for k := range *e.Data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
sb.WriteString((*e.Data)[k])
}
}
h := fnv.New32a()
h.Write([]byte(sb.String()))
es := hex.EncodeToString(h.Sum(nil))
return es
}
// CreateFullyQualifiedTopicName creates a FQTN from the topic name and event source
// but if the source is empty then the topic will be returned as is.
func CreateFullyQualifiedTopicName(topic string, source types.NamespacedName) string {
fqtn := topic
if (source != types.NamespacedName{}) {
fqtn = fmt.Sprintf("%s:%s:%s", topic, source.Namespace, source.Name)
}
return fqtn
}