Skip to content

Commit 2f230cb

Browse files
authored
Merge pull request #243 from alvarocabanas/main
feat: allow split-namespace gathering for AgentControl
2 parents 0dffe76 + 833cfc3 commit 2f230cb

13 files changed

Lines changed: 111 additions & 88 deletions

config/config.go

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type userFlags struct {
5858
Script string
5959
ScriptFlags string
6060
K8sNamespace string
61+
ACAgentsNamespace string
6162
InNewRelicCLI bool
6263
}
6364

@@ -74,53 +75,55 @@ func (f userFlags) MarshalJSON() ([]byte, error) {
7475
}
7576

7677
return json.Marshal(&struct {
77-
Verbose bool
78-
Quiet bool
79-
VeryQuiet bool
80-
YesToAll bool
81-
ShowOverrideHelp bool
82-
AutoAttach bool
83-
ProxySpecified bool
84-
SkipVersionCheck bool
85-
Run bool
86-
ListScripts bool
87-
Tasks string
88-
ConfigFile string
89-
Override string
90-
OutputPath string
91-
Filter string
92-
BrowserURL string
93-
Suites string
94-
APIKey string
95-
Include string
96-
Region string
97-
Script string
98-
ScriptFlags string
99-
K8sNamespace string
78+
Verbose bool
79+
Quiet bool
80+
VeryQuiet bool
81+
YesToAll bool
82+
ShowOverrideHelp bool
83+
AutoAttach bool
84+
ProxySpecified bool
85+
SkipVersionCheck bool
86+
Run bool
87+
ListScripts bool
88+
Tasks string
89+
ConfigFile string
90+
Override string
91+
OutputPath string
92+
Filter string
93+
BrowserURL string
94+
Suites string
95+
APIKey string
96+
Include string
97+
Region string
98+
Script string
99+
ScriptFlags string
100+
K8sNamespace string
101+
ACAgentsNamespace string
100102
}{
101-
Verbose: f.Verbose,
102-
Quiet: f.Quiet,
103-
VeryQuiet: f.VeryQuiet,
104-
YesToAll: f.YesToAll,
105-
ShowOverrideHelp: f.ShowOverrideHelp,
106-
AutoAttach: f.AutoAttach,
107-
ProxySpecified: proxySpecified,
108-
SkipVersionCheck: f.SkipVersionCheck,
109-
Run: f.Run,
110-
ListScripts: f.ListScripts,
111-
Tasks: f.Tasks,
112-
ConfigFile: f.ConfigFile,
113-
Override: f.Override,
114-
OutputPath: f.OutputPath,
115-
Filter: f.Filter,
116-
BrowserURL: f.BrowserURL,
117-
Suites: f.Suites,
118-
Include: f.Include,
119-
APIKey: f.APIKey,
120-
Region: f.Region,
121-
Script: f.Script,
122-
ScriptFlags: f.ScriptFlags,
123-
K8sNamespace: f.K8sNamespace,
103+
Verbose: f.Verbose,
104+
Quiet: f.Quiet,
105+
VeryQuiet: f.VeryQuiet,
106+
YesToAll: f.YesToAll,
107+
ShowOverrideHelp: f.ShowOverrideHelp,
108+
AutoAttach: f.AutoAttach,
109+
ProxySpecified: proxySpecified,
110+
SkipVersionCheck: f.SkipVersionCheck,
111+
Run: f.Run,
112+
ListScripts: f.ListScripts,
113+
Tasks: f.Tasks,
114+
ConfigFile: f.ConfigFile,
115+
Override: f.Override,
116+
OutputPath: f.OutputPath,
117+
Filter: f.Filter,
118+
BrowserURL: f.BrowserURL,
119+
Suites: f.Suites,
120+
Include: f.Include,
121+
APIKey: f.APIKey,
122+
Region: f.Region,
123+
Script: f.Script,
124+
ScriptFlags: f.ScriptFlags,
125+
K8sNamespace: f.K8sNamespace,
126+
ACAgentsNamespace: f.ACAgentsNamespace,
124127
})
125128
}
126129

@@ -217,7 +220,9 @@ func ParseFlags() {
217220

218221
flag.StringVar(&Flags.BrowserURL, "browser-url", defaultString, "Specify a URL to check for the presence of a New Relic Browser agent")
219222

220-
flag.StringVar(&Flags.K8sNamespace, "k8s-namespace", defaultString, "Specify a namespace to use when executing the kubectl command")
223+
flag.StringVar(&Flags.K8sNamespace, "k8s-namespace", defaultString, "Specify the namespace from where to scrape the New Relic resources. If you are using Agent-control, you can also set the '-ac-agents-namespace' flag to specify the namespace where Agent-control Agents are running.")
224+
225+
flag.StringVar(&Flags.ACAgentsNamespace, "ac-agents-namespace", defaultString, "Specify the namespace from where to scrape the Agent-control running agents.")
221226

222227
flag.BoolVar(&Flags.UsageOptOut, "usage-opt-out", false, "Decline to send anonymous New Relic Diagnostic tool usage data to New Relic for this run")
223228

@@ -309,6 +314,7 @@ func (f userFlags) UsagePayload() []ConfigFlag {
309314
{Name: "region", Value: f.Region},
310315
{Name: "script", Value: f.Script},
311316
{Name: "k8sNamespace", Value: f.K8sNamespace},
317+
{Name: "aCAgentsNamespace", Value: f.ACAgentsNamespace},
312318
}
313319
}
314320

config/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Test_userFlags_UsagePayload(t *testing.T) {
3434
Region string
3535
Script string
3636
K8sNamespace string
37+
ACAgentsNamespace string
3738
}
3839

3940
sampleFlags := fields{
@@ -62,6 +63,7 @@ func Test_userFlags_UsagePayload(t *testing.T) {
6263
Region: "string",
6364
Script: "string",
6465
K8sNamespace: "string",
66+
ACAgentsNamespace: "string",
6567
}
6668

6769
samplePreparedConfig := []ConfigFlag{
@@ -89,6 +91,7 @@ func Test_userFlags_UsagePayload(t *testing.T) {
8991
{Name: "region", Value: "string"},
9092
{Name: "script", Value: "string"},
9193
{Name: "k8sNamespace", Value: "string"},
94+
{Name: "aCAgentsNamespace", Value: "string"},
9295
}
9396

9497
tests := []struct {
@@ -131,6 +134,7 @@ func Test_userFlags_UsagePayload(t *testing.T) {
131134
Region: tt.fields.Region,
132135
Script: tt.fields.Script,
133136
K8sNamespace: tt.fields.K8sNamespace,
137+
ACAgentsNamespace: tt.fields.ACAgentsNamespace,
134138
}
135139
if got := f.UsagePayload(); !reflect.DeepEqual(got, tt.want) {
136140
t.Errorf("userFlags.UsagePayload() = %v, want %v", got, tt.want)

output/fixtures/test-output.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Region": "",
2525
"Script": "",
2626
"ScriptFlags": "",
27-
"K8sNamespace": ""
27+
"K8sNamespace": "",
28+
"ACAgentsNamespace": ""
2829
},
2930
"Results": [
3031
{

output/fixtures/test-output_windows.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Region": "",
2525
"Script": "",
2626
"ScriptFlags": "",
27-
"K8sNamespace": ""
27+
"K8sNamespace": "",
28+
"ACAgentsNamespace": ""
2829
},
2930
"Results": [
3031
{

output/fixtures/test-stream-output.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Region": "",
2525
"Script": "",
2626
"ScriptFlags": "",
27-
"K8sNamespace": ""
27+
"K8sNamespace": "",
28+
"ACAgentsNamespace": ""
2829
},
2930
"Results": [
3031
{

output/fixtures/test-stream-output_windows.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Region": "",
2525
"Script": "",
2626
"ScriptFlags": "",
27-
"K8sNamespace": ""
27+
"K8sNamespace": "",
28+
"ACAgentsNamespace": ""
2829
},
2930
"Results": [
3031
{

processOptions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ func processOverrides() (tasks.Options, []override) {
253253
options.Options["k8sNamespace"] = config.Flags.K8sNamespace
254254
}
255255

256+
if config.Flags.ACAgentsNamespace != "" {
257+
log.Debug("Manually setting ACAgentsNamespace to ", config.Flags.ACAgentsNamespace)
258+
options.Options["ACAgentsNamespace"] = config.Flags.ACAgentsNamespace
259+
}
260+
256261
// Pass in Proxy file override value
257262
if config.Flags.Proxy != "" {
258263
log.Debug("Manually setting Proxy to ", config.Flags.Proxy)

tasks/k8s/flux/flux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const (
1111

1212
// RegisterWith - will register any plugins in this package
1313
func RegisterWith(registrationFunc func(tasks.Task, bool)) {
14-
log.Debug("Registering K8s/Helm/*")
14+
log.Debug("Registering K8s/Flux/*")
1515
registrationFunc(FluxCharts{
1616
cmdExec: tasks.CmdExecutor,
1717
}, true)

tasks/k8s/resources/config.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (p K8sConfigs) Identifier() tasks.Identifier {
1616

1717
// Explain - Returns the help text for each individual task
1818
func (p K8sConfigs) Explain() string {
19-
return "Collects K8s configMaps for the given namespace in YAML format."
19+
return "Collects K8s configMaps for the given namespaces in YAML format."
2020
}
2121

2222
// Dependencies - Returns the dependencies for each task.
@@ -26,22 +26,17 @@ func (p K8sConfigs) Dependencies() []string {
2626

2727
// Execute - The core work within each task
2828
func (p K8sConfigs) Execute(options tasks.Options, upstream map[string]tasks.Result) tasks.Result {
29-
var (
30-
res []byte
31-
err error
32-
)
29+
stream := make(chan string)
3330

34-
namespace := options.Options["k8sNamespace"]
35-
res, err = p.runCommand(namespace)
31+
result, err := getResources(options, p.runCommand)
3632
if err != nil {
3733
return tasks.Result{
3834
Summary: "Error retrieving configMaps: " + err.Error(),
3935
Status: tasks.Error,
4036
}
4137
}
4238

43-
stream := make(chan string)
44-
go tasks.StreamBlob(string(res), stream)
39+
go tasks.StreamBlob(string(result), stream)
4540

4641
return tasks.Result{
4742
Summary: "Successfully collected K8s configMaps ",

tasks/k8s/resources/daemonset.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,17 @@ func (p K8sDaemonset) Dependencies() []string {
2626

2727
// Execute - The core work within each task
2828
func (p K8sDaemonset) Execute(options tasks.Options, upstream map[string]tasks.Result) tasks.Result {
29-
var (
30-
res []byte
31-
err error
32-
)
29+
stream := make(chan string)
3330

34-
namespace := options.Options["k8sNamespace"]
35-
res, err = p.runCommand(namespace)
31+
result, err := getResources(options, p.runCommand)
3632
if err != nil {
3733
return tasks.Result{
38-
Summary: "Error retrieving daemonset details: " + err.Error(),
34+
Summary: "Error retrieving daemonsets details: " + err.Error(),
3935
Status: tasks.Error,
4036
}
4137
}
4238

43-
stream := make(chan string)
44-
go tasks.StreamBlob(string(res), stream)
39+
go tasks.StreamBlob(string(result), stream)
4540

4641
return tasks.Result{
4742
Summary: "Successfully collected K8s newrelic-infrastructure daemonset",

0 commit comments

Comments
 (0)