forked from rancher/agent
/
ping_common.go
229 lines (211 loc) · 6.71 KB
/
ping_common.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ping
import (
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rancher/agent/core/hostInfo"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/config"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/docker"
"github.com/rancher/agent/utilities/utils"
revents "github.com/rancher/event-subscriber/events"
"github.com/shirou/gopsutil/disk"
"golang.org/x/net/context"
"net"
"os"
"strconv"
"strings"
"time"
)
func addResource(ping *revents.Event, pong *model.PingResponse, dockerClient *client.Client, collectors []hostInfo.Collector) error {
if !pingIncludeResource(ping) {
return nil
}
stats := map[string]interface{}{}
if pingIncludeStats(ping) {
data := hostInfo.CollectData(collectors)
stats = data
}
physicalHost, err := config.PhysicalHost()
if err != nil {
return errors.Wrap(err, constants.AddResourceError+"failed to get physical host")
}
hostname, err := config.Hostname()
if err != nil {
return errors.Wrap(err, constants.AddResourceError+"failed to get hostname")
}
labels, err := getHostLabels(collectors)
if err != nil {
logrus.Warnf("Failed to get Host Labels err msg: %v", err.Error())
}
rancherImage := os.Getenv("RANCHER_AGENT_IMAGE")
labels[constants.RancherAgentImage] = rancherImage
uuid, err := config.DockerUUID()
if err != nil {
return errors.Wrap(err, constants.AddResourceError+"failed to get docker UUID")
}
compute := model.PingResource{
Type: "host",
Kind: "docker",
HostName: hostname,
CreateLabels: config.Labels(),
Labels: labels,
UUID: uuid,
PhysicalHostUUID: physicalHost.UUID,
Info: stats,
APIProxy: config.HostProxy(),
}
if memOverride := getResourceOverride("CATTLE_MEMORY_OVERRIDE"); memOverride != 0 {
compute.Memory = memOverride
} else if stats["memoryInfo"] != nil {
if memTotal, ok := stats["memoryInfo"].(map[string]interface{})["memTotal"]; ok {
compute.Memory = memTotal.(uint64) * 1024 * 1024
}
}
if cpuOverride := getResourceOverride("CATTLE_MILLI_CPU_OVERRIDE"); cpuOverride != 0 {
compute.MilliCPU = cpuOverride
} else if stats["cpuInfo"] != nil {
if cpuCount, ok := stats["cpuInfo"].(map[string]interface{})["count"].(int); ok {
compute.MilliCPU = uint64(cpuCount) * 1000
}
}
if storageOverride := getResourceOverride("CATTLE_LOCAL_STORAGE_MB_OVERRIDE"); storageOverride != 0 {
compute.LocalStorageMb = storageOverride
} else {
usage, err := disk.Usage("/var/lib/docker")
if err != nil {
logrus.Errorf("Error getting local storage usage: %v", err)
} else {
compute.LocalStorageMb = uint64(usage.Free) / 1000
}
}
pool := model.PingResource{
Type: "storagePool",
Kind: "docker",
Name: compute.HostName + " Storage Pool",
HostUUID: compute.UUID,
UUID: compute.UUID + "-pool",
}
resolvedIP, err := net.LookupIP(config.DockerHostIP())
ipAddr := ""
if err != nil {
logrus.Warn(err)
} else {
ipAddr = resolvedIP[0].String()
}
ip := model.PingResource{
Type: "ipAddress",
UUID: ipAddr,
Addresss: ipAddr,
HostUUID: compute.UUID,
}
pong.Resources = append(pong.Resources, physicalHost, compute, pool, ip)
return nil
}
func getResourceOverride(envVar string) uint64 {
var resource uint64
var err error
if val := os.Getenv(envVar); val != "" {
resource, err = strconv.ParseUint(val, 10, 64)
if err != nil {
logrus.Warnf("Couldn't parse %v %v. Will not use it.", envVar, val)
return 0
}
}
return resource
}
func addInstance(ping *revents.Event, pong *model.PingResponse, dockerClient *client.Client) error {
if !pingIncludeInstance(ping) {
return nil
}
uuid, err := config.DockerUUID()
if err != nil {
return errors.Wrap(err, constants.AddInstanceError+"failed to get docker UUID")
}
pong.Resources = append(pong.Resources, model.PingResource{
Type: "hostUuid",
UUID: uuid,
})
containers := []model.PingResource{}
clientTimeout := time.Duration(time.Second * 2)
dc, err := docker.NewEnvClientWithTimeout(clientTimeout)
if err != nil {
return errors.Wrap(err, constants.AddInstanceError+"failed to get docker client")
}
dc.UpdateClientVersion(constants.DefaultVersion)
// if we can not get all container in 2s, we will skip it
done := make(chan bool, 1)
timeout := make(chan bool, 1)
go func() {
time.Sleep(2 * time.Second)
timeout <- true
}()
running, nonrunning, err := getAllContainerByState(dc, done)
select {
case <-done:
if err != nil {
return errors.Wrap(err, constants.AddInstanceError+"failed to get all containers")
}
for _, container := range running {
containers = utils.AddContainer("running", container, containers, dockerClient)
}
for _, container := range nonrunning {
containers = utils.AddContainer("stopped", container, containers, dockerClient)
}
pong.Resources = append(pong.Resources, containers...)
pong.Options.Instances = true
return nil
case <-timeout:
logrus.Warn("Can not get response from docker daemon")
return nil
}
}
func pingIncludeResource(ping *revents.Event) bool {
value, ok := utils.GetFieldsIfExist(ping.Data, "options", "resources")
if !ok {
return false
}
return utils.InterfaceToBool(value)
}
func pingIncludeStats(ping *revents.Event) bool {
value, ok := utils.GetFieldsIfExist(ping.Data, "options", "stats")
if !ok {
return false
}
return utils.InterfaceToBool(value)
}
func pingIncludeInstance(ping *revents.Event) bool {
value, ok := utils.GetFieldsIfExist(ping.Data, "options", "instances")
if !ok {
return false
}
return utils.InterfaceToBool(value)
}
func getHostLabels(collectors []hostInfo.Collector) (map[string]string, error) {
return hostInfo.HostLabels("io.rancher.host", collectors)
}
func getAllContainerByState(dockerClient *client.Client, done chan bool) (map[string]types.Container, map[string]types.Container, error) {
// avoid calling API twice
nonrunningContainers := map[string]types.Container{}
containerList, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
return map[string]types.Container{}, map[string]types.Container{}, errors.Wrap(err, constants.GetAllContainerByStateError+"failed to list containers")
}
for _, c := range containerList {
if c.Status != "" && c.Status != "Created" {
nonrunningContainers[c.ID] = c
}
}
runningContainers := map[string]types.Container{}
// if status is running, it is a running container
for _, c := range containerList {
if strings.Contains(c.Status, "Up") || c.State == "Running" {
runningContainers[c.ID] = c
delete(nonrunningContainers, c.ID)
}
}
done <- true
return runningContainers, nonrunningContainers, nil
}