forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_to_rancher_handler.go
executable file
·54 lines (46 loc) · 1.4 KB
/
send_to_rancher_handler.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
package events
import (
log "github.com/Sirupsen/logrus"
"github.com/docker/distribution/context"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/client"
"github.com/rancher/event-subscriber/locks"
rclient "github.com/rancher/go-rancher/client"
)
type SendToRancherHandler struct {
client *client.Client
rancher *rclient.RancherClient
hostUUID string
}
func (h *SendToRancherHandler) Handle(event *events.Message) error {
// rancher_state_watcher sends a simulated event to the event router to initiate ip injection.
// This event should not be sent.
if event.From == simulatedEvent {
return nil
}
// Note: event.ID == container's ID
lock := locks.Lock(event.Status + event.ID)
if lock == nil {
log.Debugf("Container locked. Can't run SendToRancherHandler. Event: [%s], ID: [%s]", event.Status, event.ID)
return nil
}
defer lock.Unlock()
container, err := h.client.ContainerInspect(context.Background(), event.ID)
if err != nil {
if ok := client.IsErrContainerNotFound(err); !ok {
return err
}
}
containerEvent := &rclient.ContainerEvent{
ExternalStatus: event.Status,
ExternalId: event.ID,
ExternalFrom: event.From,
ExternalTimestamp: int64(event.Time),
ReportedHostUuid: h.hostUUID,
}
containerEvent.DockerInspect = container
if _, err := h.rancher.ContainerEvent.Create(containerEvent); err != nil {
return err
}
return nil
}