-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
archive_dca.go
195 lines (161 loc) · 6.35 KB
/
archive_dca.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
// 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 flare
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
flarehelpers "github.com/DataDog/datadog-agent/comp/core/flare/helpers"
flaretypes "github.com/DataDog/datadog-agent/comp/core/flare/types"
"github.com/DataDog/datadog-agent/comp/core/status"
apiv1 "github.com/DataDog/datadog-agent/pkg/clusteragent/api/v1"
"github.com/DataDog/datadog-agent/pkg/clusteragent/autoscaling/custommetrics"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/diagnose"
"github.com/DataDog/datadog-agent/pkg/status/render"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// ProfileData maps (pprof) profile names to the profile data
type ProfileData map[string][]byte
// CreateDCAArchive packages up the files
func CreateDCAArchive(local bool, distPath, logFilePath string, pdata ProfileData, diagnoseDeps diagnose.SuitesDeps, statusComponent status.Component) (string, error) {
fb, err := flarehelpers.NewFlareBuilder(local)
if err != nil {
return "", err
}
confSearchPaths := map[string]string{
"": config.Datadog.GetString("confd_path"),
"dist": filepath.Join(distPath, "conf.d"),
}
createDCAArchive(fb, confSearchPaths, logFilePath, pdata, diagnoseDeps, statusComponent)
return fb.Save()
}
func createDCAArchive(fb flaretypes.FlareBuilder, confSearchPaths map[string]string, logFilePath string, pdata ProfileData, diagnoseDeps diagnose.SuitesDeps, statusComponent status.Component) {
// If the request against the API does not go through we don't collect the status log.
if fb.IsLocal() {
fb.AddFile("local", nil)
} else {
// The Status will be unavailable unless the agent is running.
// Only zip it up if the agent is running
err := fb.AddFileFromFunc("cluster-agent-status.log", func() ([]byte, error) { return statusComponent.GetStatus("text", true) })
if err != nil {
log.Errorf("Error getting the status of the DCA, %q", err)
return
}
}
getLogFiles(fb, logFilePath)
getConfigFiles(fb, confSearchPaths)
getClusterAgentConfigCheck(fb) //nolint:errcheck
getExpVar(fb) //nolint:errcheck
getMetadataMap(fb) //nolint:errcheck
getClusterAgentClusterChecks(fb) //nolint:errcheck
getClusterAgentDiagnose(fb, diagnoseDeps) //nolint:errcheck
fb.AddFileFromFunc("agent-daemonset.yaml", getAgentDaemonSet)
fb.AddFileFromFunc("cluster-agent-deployment.yaml", getClusterAgentDeployment)
fb.AddFileFromFunc("helm-values.yaml", getHelmValues)
fb.AddFileFromFunc("envvars.log", getEnvVars)
fb.AddFileFromFunc("telemetry.log", QueryDCAMetrics)
fb.AddFileFromFunc("tagger-list.json", getDCATaggerList)
fb.AddFileFromFunc("workload-list.log", getDCAWorkloadList)
getPerformanceProfileDCA(fb, pdata)
if config.Datadog.GetBool("external_metrics_provider.enabled") {
getHPAStatus(fb) //nolint:errcheck
}
}
// QueryDCAMetrics gets the metrics payload exposed by the cluster agent
func QueryDCAMetrics() ([]byte, error) {
r, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", config.Datadog.GetInt("metrics_port")))
if err != nil {
return nil, err
}
defer r.Body.Close()
return io.ReadAll(r.Body)
}
func getMetadataMap(fb flaretypes.FlareBuilder) error {
metaList := apiv1.NewMetadataResponse()
cl, err := apiserver.GetAPIClient()
if err != nil {
metaList.Errors = fmt.Sprintf("Can't create client to query the API Server: %s", err.Error())
} else {
// Grab the metadata map for all nodes.
metaList, err = apiserver.GetMetadataMapBundleOnAllNodes(cl)
if err != nil {
log.Infof("Error while collecting the cluster level metadata: %q", err)
}
}
metaBytes, err := json.Marshal(metaList)
if err != nil {
return log.Errorf("Error while marshalling the cluster level metadata: %q", err)
}
str, err := render.FormatMetadataMapCLI(metaBytes)
if err != nil {
return log.Errorf("Error while rendering the cluster level metadata: %q", err)
}
return fb.AddFile("cluster-agent-metadatamapper.log", []byte(str))
}
func getClusterAgentClusterChecks(fb flaretypes.FlareBuilder) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
GetClusterChecks(writer, "") //nolint:errcheck
writer.Flush()
return fb.AddFile("clusterchecks.log", b.Bytes())
}
func getHPAStatus(fb flaretypes.FlareBuilder) error {
stats := make(map[string]interface{})
apiCl, err := apiserver.GetAPIClient()
if err != nil {
stats["custommetrics"] = map[string]string{"Error": err.Error()}
} else {
stats["custommetrics"] = custommetrics.GetStatus(apiCl.Cl)
}
statsBytes, err := json.Marshal(stats)
if err != nil {
return log.Errorf("Error while marshalling the cluster level metadata: %q", err)
}
str, err := render.FormatHPAStatus(statsBytes)
if err != nil {
return log.Errorf("Could not collect custommetricsprovider.log: %s", err)
}
return fb.AddFile("custommetricsprovider.log", []byte(str))
}
func getClusterAgentConfigCheck(fb flaretypes.FlareBuilder) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
GetClusterAgentConfigCheck(writer, true) //nolint:errcheck
writer.Flush()
return fb.AddFile("config-check.log", b.Bytes())
}
func getClusterAgentDiagnose(fb flaretypes.FlareBuilder, deps diagnose.SuitesDeps) error {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
GetClusterAgentDiagnose(writer, deps) //nolint:errcheck
writer.Flush()
return fb.AddFile("diagnose.log", b.Bytes())
}
func getDCATaggerList() ([]byte, error) {
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return nil, err
}
taggerListURL := fmt.Sprintf("https://%v:%v/tagger-list", ipcAddress, config.Datadog.GetInt("cluster_agent.cmd_port"))
return getTaggerList(taggerListURL)
}
func getDCAWorkloadList() ([]byte, error) {
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return nil, err
}
return getWorkloadList(fmt.Sprintf("https://%v:%v/workload-list?verbose=true", ipcAddress, config.Datadog.GetInt("cluster_agent.cmd_port")))
}
func getPerformanceProfileDCA(fb flaretypes.FlareBuilder, pdata ProfileData) {
for name, data := range pdata {
fb.AddFileWithoutScrubbing(filepath.Join("profiles", name), data)
}
}