forked from rancher/agent
/
handlers.go
executable file
·168 lines (153 loc) · 4.94 KB
/
handlers.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
package handlers
import (
"fmt"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
goUUID "github.com/nu7hatch/gouuid"
"github.com/pkg/errors"
"github.com/rancher/agent/host_info"
"github.com/rancher/agent/utils"
revents "github.com/rancher/event-subscriber/events"
"github.com/rancher/go-rancher/v3"
"golang.org/x/net/context"
)
func GetHandlers() (map[string]revents.EventHandler, error) {
handler := initializeHandlers()
utils.SerializeCompute = hostInfo.DockerData.Info.Driver == "devicemapper"
logrus.Infof("Serialize compute requests: %v, driver: %s", utils.SerializeCompute, hostInfo.DockerData.Info.Driver)
return map[string]revents.EventHandler{
"compute.instance.activate": cleanLog(logRequest(handler.compute.InstanceActivate)),
"compute.instance.deactivate": utils.SerializeHandler(cleanLog(logRequest(handler.compute.InstanceDeactivate))),
"compute.instance.inspect": cleanLog(logRequest(handler.compute.InstanceInspect)),
"compute.instance.pull": cleanLog(logRequest(handler.compute.InstancePull)),
"compute.instance.remove": utils.SerializeHandler(cleanLog(logRequest(handler.compute.InstanceRemove))),
"storage.volume.remove": cleanLog(logRequest(handler.storage.VolumeRemove)),
"ping": cleanLog(handler.ping.Ping),
}, nil
}
func logRequest(f revents.EventHandler) revents.EventHandler {
return func(event *revents.Event, cli *client.RancherClient) error {
logrus.Infof("Received event: Name: %s, Event Id: %s, Resource Id: %s", event.Name, event.ID, event.ResourceID)
startTime := time.Now()
err := f(event, cli)
logrus.Infof("Name: %s, Event Id: %s, Resource Id: %s, Process duration: %.4f seconds", event.Name, event.ID, event.ResourceID, time.Now().Sub(startTime).Seconds())
return err
}
}
func cleanLog(f revents.EventHandler) revents.EventHandler {
return func(event *revents.Event, cli *client.RancherClient) error {
err := f(event, cli)
if err != nil {
logrus.WithFields(logrus.Fields{"err": err}).Info("Verbose error message")
}
return errors.Cause(err)
}
}
func reply(replyData map[string]interface{}, event *revents.Event, cli *client.RancherClient) error {
if replyData == nil {
replyData = make(map[string]interface{})
}
uuid, err := getUUID()
if err != nil {
return errors.Wrap(err, "can not assign uuid to reply event")
}
reply := &client.Publish{
ResourceId: event.ResourceID,
PreviousIds: []string{event.ID},
ResourceType: event.ResourceType,
Name: event.ReplyTo,
Data: replyData,
Time: time.Now().UnixNano() / int64(time.Millisecond),
Resource: client.Resource{Id: uuid},
}
if reply.ResourceType != "agent" {
logrus.Infof("Reply: %v, %v, %v:%v", event.ID, event.Name, reply.ResourceId, reply.ResourceType)
}
logrus.Debugf("Reply: %+v", reply)
err = publishReply(reply, cli)
if err != nil {
return fmt.Errorf("Error sending reply %v: %v", event.ID, err)
}
return nil
}
func initializeHandlers() *Handler {
runtime := utils.DefaultValue("RUNTIME", "docker")
ver := utils.DefaultValue("RUNTIME_VERSION", "v1.22")
runtimeClient := utils.GetRuntimeClient(runtime, ver)
// initialize the info and version so we don't have to call docker API every time a ping request comes
info := types.Info{}
version := types.Version{}
flags := [2]bool{}
for i := 0; i < 10; i++ {
in, err := runtimeClient.Info(context.Background())
if err == nil {
info = in
flags[0] = true
break
}
logrus.Error(err)
time.Sleep(time.Duration(1) * time.Second)
}
for i := 0; i < 10; i++ {
v, err := runtimeClient.ServerVersion(context.Background())
if err == nil {
version = v
flags[1] = true
break
}
logrus.Error(err)
time.Sleep(time.Duration(1) * time.Second)
}
// if we can't get the initialization data the program should exit
if !flags[0] || !flags[1] {
logrus.Fatalf("Failed to initialize handlers. Exiting go-agent")
os.Exit(1)
}
hostInfo.DockerData.Info = info
hostInfo.DockerData.Version = version
Collectors := []hostInfo.Collector{
hostInfo.CPUCollector{},
hostInfo.DiskCollector{
Unit: 1048576,
},
hostInfo.IopsCollector{},
hostInfo.MemoryCollector{
Unit: 1024.00,
},
hostInfo.OSCollector{},
hostInfo.KeyCollector{},
hostInfo.CloudProviderCollector{},
}
computerHandler := ComputeHandler{
dockerClient: runtimeClient,
}
storageHandler := StorageHandler{
dockerClient: runtimeClient,
}
pingHandler := PingHandler{
dockerClient: runtimeClient,
collectors: Collectors,
}
handler := Handler{
compute: &computerHandler,
storage: &storageHandler,
ping: &pingHandler,
}
return &handler
}
func getUUID() (string, error) {
newUUID, err := goUUID.NewV4()
if err != nil {
return "", errors.Wrap(err, "can't generate uuid")
}
return newUUID.String(), nil
}
func publishReply(reply *client.Publish, apiClient *client.RancherClient) error {
_, err := apiClient.Publish.Create(reply)
if err != nil {
logrus.Error(err)
}
return err
}