forked from AnthonyWC/trireme-example
/
external.go
63 lines (50 loc) · 1.69 KB
/
external.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/docker/docker/api/types"
"github.com/headingy/trireme/constants"
"github.com/headingy/trireme/policy"
)
/*
Standard example of an external docker metadata extractor.
INPUT as arg[1]: The docker Container JSON with all the related information as defined in github.com/docker/docker/api/types
OUTPUT on STDPUT: The JSON representation (marshalled) of the PURuntime as defined in github.com/headingy/trireme/policy
*/
func main() {
jsonFromDocker := os.Args[1]
var m types.ContainerJSON
// Getting the Docker information out of the JSON format in the STDIN.
err := json.Unmarshal([]byte(jsonFromDocker), &m)
if err != nil {
fmt.Printf("Received error unmarshal: %s \n", err)
}
// Use this local function to fill-in the Extractor.
extractorResult, err := exampleExternalDockerMetadataExtractor(&m)
if err != nil {
fmt.Printf("Received error extractor: %s \n", err)
}
// Transfer the resultin PURuntime in JSON format.
jsonResult, err := json.Marshal(extractorResult)
if err != nil {
fmt.Printf("Received error marshal: %s \n", err)
}
// Write it out on STDOUT.
if _, err = os.Stdout.Write(jsonResult); err != nil {
fmt.Printf("Failed to write JSON to stdout")
}
}
func exampleExternalDockerMetadataExtractor(info *types.ContainerJSON) (*policy.PURuntime, error) {
tagsMap := policy.NewTagsMap(map[string]string{
"image": info.Config.Image,
"name": info.Name,
})
for k, v := range info.Config.Labels {
tagsMap.Add(k, v)
}
ipa := policy.NewIPMap(map[string]string{
"bridge": info.NetworkSettings.IPAddress,
})
return policy.NewPURuntime(info.Name, info.State.Pid, tagsMap, ipa, constants.ContainerPU, nil), nil
}