/
docker.go
57 lines (46 loc) · 1.36 KB
/
docker.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
package docker
import (
"context"
"docker.io/go-docker"
"docker.io/go-docker/api/types"
"github.com/fatih/structs"
"github.com/twhiston/factd/lib/common"
"github.com/twhiston/factd/lib/plugin"
)
// The Docker plugin provides information about docker images and containers on the host
type Docker struct{}
// Name returns the plugin printable name, also used as the map key in the master fact list
func (p *Docker) Name() string {
return plugin.GetPluginName(&p)
}
// Report writes a set (or subset) of facts to a channel
func (p *Docker) Report(facts chan<- plugin.ReportedFact) {
plugin.PollingReport(p, facts)
}
// Facts gathers the actual fact data related to the plugin type
func (p *Docker) Facts() (common.FactList, error) {
data := make(common.FactList)
cli, err := docker.NewEnvClient()
if err != nil {
return data, err
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
return data, err
}
cs := make(common.FactList)
for _, container := range containers {
cs[container.ID] = structs.Map(container)
}
data["Containers"] = cs
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
return data, err
}
is := make(common.FactList)
for _, image := range images {
is[image.ID] = structs.Map(image)
}
data["Images"] = is
return data, nil
}