/
command.go
129 lines (104 loc) · 3.58 KB
/
command.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
// 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 workloadlist implements 'agent workload-list'.
package workloadlist
import (
"encoding/json"
"fmt"
"go.uber.org/fx"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/api/util"
pkgconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// cliParams are the command-line arguments for this subcommand
type cliParams struct {
GlobalParams
verboseList bool
}
// GlobalParams contains the values of agent-global Cobra flags.
//
// A pointer to this type is passed to SubcommandFactory's, but its contents
// are not valid until Cobra calls the subcommand's Run or RunE function.
type GlobalParams struct {
ConfFilePath string
ConfigName string
LoggerName string
}
// MakeCommand returns a `workload-list` command to be used by agent binaries.
func MakeCommand(globalParamsGetter func() GlobalParams) *cobra.Command {
cliParams := &cliParams{}
workloadListCommand := &cobra.Command{
Use: "workload-list",
Short: "Print the workload content of a running agent",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
globalParams := globalParamsGetter()
cliParams.GlobalParams = globalParams
return fxutil.OneShot(workloadList,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewAgentParams(
globalParams.ConfFilePath,
config.WithConfigName(globalParams.ConfigName),
),
LogParams: logimpl.ForOneShot(globalParams.LoggerName, "off", true)}),
core.Bundle(),
)
},
}
workloadListCommand.Flags().BoolVarP(&cliParams.verboseList, "verbose", "v", false, "print out a full dump of the workload store")
return workloadListCommand
}
func workloadList(_ log.Component, config config.Component, cliParams *cliParams) error {
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
err := util.SetAuthToken(config)
if err != nil {
return err
}
url, err := workloadURL(cliParams.verboseList)
if err != nil {
return err
}
r, err := util.DoGet(c, url, util.LeaveConnectionOpen)
if err != nil {
if r != nil && string(r) != "" {
fmt.Fprintf(color.Output, "The agent ran into an error while getting the workload store information: %s\n", string(r))
} else {
fmt.Fprintf(color.Output, "Failed to query the agent (running?): %s\n", err)
}
}
workload := workloadmeta.WorkloadDumpResponse{}
err = json.Unmarshal(r, &workload)
if err != nil {
return err
}
workload.Write(color.Output)
return nil
}
func workloadURL(verbose bool) (string, error) {
ipcAddress, err := pkgconfig.GetIPCAddress()
if err != nil {
return "", err
}
var prefix string
if flavor.GetFlavor() == flavor.ClusterAgent {
prefix = fmt.Sprintf("https://%v:%v/workload-list", ipcAddress, pkgconfig.Datadog.GetInt("cluster_agent.cmd_port"))
} else {
prefix = fmt.Sprintf("https://%v:%v/agent/workload-list", ipcAddress, pkgconfig.Datadog.GetInt("cmd_port"))
}
if verbose {
return prefix + "?verbose=true", nil
}
return prefix, nil
}