/
events.go
70 lines (63 loc) · 2.05 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
64
65
66
67
68
69
70
package manager
import "sync"
// AllEvents contains all events received for each individual registry, to read
// or write from the list of events the mutex needs to be used to ensure consistency
var AllEvents Events
func init() {
AllEvents = Events{
Events: make(map[string]map[string]Event),
}
}
// Events contains event information in the structure registry->event_id->event
type Events struct {
sync.Mutex
Events map[string]map[string]Event
}
// Envelope contains all grouped events sent by the registries.
// see https://docs.docker.com/registry/notifications/#envelope for more details
type Envelope struct {
Events []Event
}
// Process adds each individual event to the map of all events
func (e *Envelope) Process() {
AllEvents.Lock()
for _, event := range e.Events {
if (event.Action == "push" || event.Action == "pull") && event.Request.Useragent != "Go-http-client/1.1" && event.Request.Method != "HEAD" {
if _, ok := AllEvents.Events[event.Request.Host]; !ok {
AllEvents.Events[event.Request.Host] = make(map[string]Event)
}
// Add the event
AllEvents.Events[event.Request.Host][event.ID] = event
}
}
AllEvents.Unlock()
}
// Event contains all information for the given event (e.g pull/pushes to the registry)
// see https://docs.docker.com/registry/notifications/#events for more details
type Event struct {
ID string `json:"id"`
Timestamp string `json:"timestamp"`
Action string `json:"action"`
Target struct {
MediaType string `json:"mediaType"`
Size int `json:"size"`
Digest string `json:"digest"`
Length int `json:"length"`
Repository string `json:"repository"`
URL string `json:"url"`
Tag string `json:"tag"`
} `json:"target"`
Request struct {
ID string `json:"id"`
Addr string `json:"addr"`
Host string `json:"host"`
Method string `json:"method"`
Useragent string `json:"useragent"`
} `json:"request"`
Actor struct {
} `json:"actor"`
Source struct {
Addr string `json:"addr"`
InstanceID string `json:"instanceID"`
} `json:"source"`
}