-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
145 lines (134 loc) · 4.23 KB
/
plugin.go
File metadata and controls
145 lines (134 loc) · 4.23 KB
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
package qcache_inventory
import (
"fmt"
"time"
"github.com/zpatrick/go-config"
"github.com/docker/docker/client"
"golang.org/x/net/context"
"github.com/deckarep/golang-set"
"github.com/qframe/types/constants"
"github.com/qframe/types/docker-events"
"github.com/qframe/types/qchannel"
"github.com/qframe/types/plugin"
"reflect"
"strings"
"github.com/docker/docker/api/types"
)
const (
version = "0.3.3"
pluginTyp = qtypes_constants.CACHE
pluginPkg = "inventory"
dockerAPI = "v1.29"
)
var (
ctx = context.Background()
)
type Plugin struct {
*qtypes_plugin.Plugin
Inventory Inventory
engCli *client.Client
engInfo types.Info
}
func New(qChan qtypes_qchannel.QChan, cfg *config.Config, name string) (Plugin, error) {
return Plugin{
Plugin: qtypes_plugin.NewNamedPlugin(qChan, cfg, pluginTyp, pluginPkg, name, version),
Inventory: NewInventory(),
}, nil
}
// Run fetches everything from the Data channel and flushes it to stdout
func (p *Plugin) Run() {
p.Log("notice", fmt.Sprintf("Start inventory v%s", p.Version))
dc := p.QChan.Data.Join()
tickerTime := p.CfgIntOr("ticker-ms", 500)
ticker := time.NewTicker(time.Millisecond * time.Duration(tickerTime)).C
dockerHost := p.CfgStringOr("docker-host", "unix:///var/run/docker.sock")
var err error
p.engCli, err = client.NewClient(dockerHost, dockerAPI, nil, nil)
if err != nil {
p.Log("error", fmt.Sprintf("Could not connect docker/docker/client to '%s': %v", dockerHost, err))
return
}
p.engInfo, err = p.engCli.Info(ctx)
if err != nil {
p.Log("error", fmt.Sprintf("Error during Info(): %v >err> %s", p.engInfo, err.Error()))
return
} else {
p.Log("info", fmt.Sprintf("Connected to '%s' / v'%s'", p.engInfo.Name, p.engInfo.ServerVersion))
}
for {
select {
case val := <-dc.Read:
switch val.(type) {
case qtypes_docker_events.ContainerEvent:
ce := val.(qtypes_docker_events.ContainerEvent)
if ce.Event.Type == "container" && ce.Event.Action == "start" {
go p.LookUpContainer(&ce.Container)
}
case ContainerRequest:
req := val.(ContainerRequest)
p.Log("debug", fmt.Sprintf("Received InventoryRequest for %v", req))
err := p.Inventory.ServeRequest(req)
if err != nil {
p.Log("error", fmt.Sprintf("Error when ServeRequest(): %s", err.Error()))
}
default:
p.Log("trace", fmt.Sprintf("Dunno type '%s': %v", reflect.TypeOf(val), val))
}
case <- ticker:
p.Log("trace", "Ticker came along: p.Inventory.CheckRequests()")
p.Inventory.CheckRequests()
continue
}
}
}
func (p *Plugin) LookUpContainer(cnt *types.ContainerJSON) {
ipSet := mapset.NewSet()
for _,v := range cnt.NetworkSettings.Networks {
ipSet.Add(v.IPAddress)
}
ips, err := p.AddNetworkIPs(ipSet, cnt)
if err != nil {
p.Log("error", fmt.Sprintf("Error during AddNetworkIPs(): %s", err.Error()))
}
p.Log("debug", fmt.Sprintf("Add CntID:%s into Inventory (name:%s, IPs:%s)",cnt.ID[:13], cnt.Name, strings.Join(ips,",")))
p.Inventory.SetItem(cnt.ID, cnt, p.engInfo, ips)
}
func (p *Plugin) AddNetworkIPs(ips mapset.Set, container *types.ContainerJSON) (res []string, err error) {
p.Log("debug", fmt.Sprintf("List before lookup: %v", GetList(ips)))
nets, err := p.engCli.NetworkList(ctx, types.NetworkListOptions{})
if err != nil {
p.Log("error", err.Error())
return GetList(ips), err
}
for _, net := range nets {
p.Log("trace", fmt.Sprintf(">> Network: %s", net.Name))
netInspect, err := p.engCli.NetworkInspect(ctx, net.ID, types.NetworkInspectOptions{})
if err != nil {
p.Log("error", fmt.Sprintf("Error during NetworkList(): %s", err.Error()))
continue
}
for cntID, cnt := range netInspect.Containers {
if cntID == container.ID {
if cnt.IPv4Address == "" {
continue
}
ip4 := strings.Split(cnt.IPv4Address, "/")
if len(ip4) != 2 {
p.Log("error", fmt.Sprintf("Container '%s' has unexpected IP '%s'... skip", cntID, cnt.IPv4Address))
continue
}
p.Log("trace", fmt.Sprintf(" Name:%-15s || %s==%s: %s", cnt.Name, cntID[:12], container.ID[:12], ip4[0]))
ips.Add(ip4[0])
}
}
}
p.Log("debug", fmt.Sprintf("List after lookup: %v", GetList(ips)))
return GetList(ips), err
}
func GetList(s mapset.Set) (res []string) {
it := s.Iterator()
for elem := range it.C {
res = append(res, elem.(string))
}
return
}