-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dump.go
107 lines (89 loc) · 2.69 KB
/
dump.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package workloadmeta
import (
"fmt"
"io"
"github.com/fatih/color"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// WorkloadDumpResponse is used to dump the store content.
type WorkloadDumpResponse struct {
Entities map[string]WorkloadEntity `json:"entities"`
}
// WorkloadEntity contains entity data.
type WorkloadEntity struct {
Infos map[string]string `json:"infos"`
}
// Write writes the stores content in a given writer.
// Useful for agent's CLI and Flare.
func (wdr WorkloadDumpResponse) Write(writer io.Writer) {
if writer != color.Output {
color.NoColor = true
}
for kind, entities := range wdr.Entities {
for entity, info := range entities.Infos {
fmt.Fprintf(writer, "\n=== Entity %s %s ===\n", color.GreenString(kind), color.GreenString(entity))
fmt.Fprint(writer, info)
fmt.Fprintln(writer, "===")
}
}
}
// Dump implements Store#Dump
func (w *workloadmeta) Dump(verbose bool) WorkloadDumpResponse {
workloadList := WorkloadDumpResponse{
Entities: make(map[string]WorkloadEntity),
}
entityToString := func(entity Entity) (string, error) {
var info string
switch e := entity.(type) {
case *Container:
info = e.String(verbose)
case *KubernetesPod:
info = e.String(verbose)
case *KubernetesNode:
info = e.String(verbose)
case *ECSTask:
info = e.String(verbose)
case *ContainerImageMetadata:
info = e.String(verbose)
case *Process:
info = e.String(verbose)
case *KubernetesDeployment:
info = e.String(verbose)
case *KubernetesNamespace:
info = e.String(verbose)
default:
return "", fmt.Errorf("unsupported type %T", e)
}
return info, nil
}
w.storeMut.RLock()
defer w.storeMut.RUnlock()
for kind, store := range w.store {
entities := WorkloadEntity{Infos: make(map[string]string)}
for id, cachedEntity := range store {
if verbose && len(cachedEntity.sources) > 1 {
for source, entity := range cachedEntity.sources {
info, err := entityToString(entity)
if err != nil {
log.Debugf("Ignoring entity %s: %v", entity.GetID().ID, err)
continue
}
entities.Infos["source:"+string(source)+" id: "+id] = info
}
}
e := cachedEntity.cached
info, err := entityToString(e)
if err != nil {
log.Debugf("Ignoring entity %s: %v", e.GetID().ID, err)
continue
}
entities.Infos[fmt.Sprintf("sources(merged):%v", cachedEntity.sortedSources)+" id: "+id] = info
}
workloadList.Entities[string(kind)] = entities
}
return workloadList
}