From 3b1ba08a6ba67de797cccf37a72406aaba885782 Mon Sep 17 00:00:00 2001 From: diamonwiggins Date: Thu, 12 May 2022 03:39:19 +0000 Subject: [PATCH] hardcoding system hostcollector filenames --- pkg/analyze/host_block_devices.go | 2 +- pkg/analyze/host_cpu.go | 2 +- pkg/analyze/host_ipv4interfaces.go | 3 +- pkg/analyze/host_kernel_modules.go | 2 +- pkg/analyze/host_memory.go | 2 +- pkg/analyze/host_os_info.go | 2 +- pkg/analyze/host_services.go | 9 +----- pkg/analyze/host_time.go | 2 +- .../v1beta2/hostanalyzer_shared.go | 31 ++++++++++++------- pkg/collect/host_block_device.go | 12 ++----- pkg/collect/host_cpu.go | 13 +++----- pkg/collect/host_ipv4interfaces.go | 13 +++----- pkg/collect/host_kernel_modules.go | 12 +++---- pkg/collect/host_memory.go | 13 +++----- pkg/collect/host_os_info.go | 13 +++----- pkg/collect/host_services.go | 12 ++----- pkg/collect/host_time.go | 13 +++----- 17 files changed, 58 insertions(+), 98 deletions(-) diff --git a/pkg/analyze/host_block_devices.go b/pkg/analyze/host_block_devices.go index 1f531834..06d84595 100644 --- a/pkg/analyze/host_block_devices.go +++ b/pkg/analyze/host_block_devices.go @@ -27,7 +27,7 @@ func (a *AnalyzeHostBlockDevices) IsExcluded() (bool, error) { func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/block_devices.json") + contents, err := getCollectedFileContents(collect.HostBlockDevicesPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_cpu.go b/pkg/analyze/host_cpu.go index 035e261e..a767731d 100644 --- a/pkg/analyze/host_cpu.go +++ b/pkg/analyze/host_cpu.go @@ -25,7 +25,7 @@ func (a *AnalyzeHostCPU) IsExcluded() (bool, error) { func (a *AnalyzeHostCPU) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/cpu.json") + contents, err := getCollectedFileContents(collect.HostCPUPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_ipv4interfaces.go b/pkg/analyze/host_ipv4interfaces.go index e313dcf0..1e9ca1f7 100644 --- a/pkg/analyze/host_ipv4interfaces.go +++ b/pkg/analyze/host_ipv4interfaces.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + "github.com/replicatedhq/troubleshoot/pkg/collect" ) type AnalyzeHostIPV4Interfaces struct { @@ -26,7 +27,7 @@ func (a *AnalyzeHostIPV4Interfaces) IsExcluded() (bool, error) { func (a *AnalyzeHostIPV4Interfaces) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/ipv4Interfaces.json") + contents, err := getCollectedFileContents(collect.HostIPV4InterfacesPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_kernel_modules.go b/pkg/analyze/host_kernel_modules.go index 569a36e1..53af7cc8 100644 --- a/pkg/analyze/host_kernel_modules.go +++ b/pkg/analyze/host_kernel_modules.go @@ -43,7 +43,7 @@ func (a *AnalyzeHostKernelModules) IsExcluded() (bool, error) { // func (a *AnalyzeHostKernelModules) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/kernel_modules.json") + contents, err := getCollectedFileContents(collect.HostKernelModulesPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_memory.go b/pkg/analyze/host_memory.go index 098bb3b7..86e7c22c 100644 --- a/pkg/analyze/host_memory.go +++ b/pkg/analyze/host_memory.go @@ -26,7 +26,7 @@ func (a *AnalyzeHostMemory) IsExcluded() (bool, error) { func (a *AnalyzeHostMemory) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/memory.json") + contents, err := getCollectedFileContents(collect.HostMemoryPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_os_info.go b/pkg/analyze/host_os_info.go index 74a2b4f2..fed480d3 100644 --- a/pkg/analyze/host_os_info.go +++ b/pkg/analyze/host_os_info.go @@ -29,7 +29,7 @@ func (a *AnalyzeHostOS) Analyze(getCollectedFileContents func(string) ([]byte, e result := AnalyzeResult{} result.Title = a.Title() - contents, err := getCollectedFileContents("system/hostos_info.json") + contents, err := getCollectedFileContents(collect.HostOSInfoPath) if err != nil { return []*AnalyzeResult{&result}, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_services.go b/pkg/analyze/host_services.go index c3bd4297..fba165df 100644 --- a/pkg/analyze/host_services.go +++ b/pkg/analyze/host_services.go @@ -3,7 +3,6 @@ package analyzer import ( "encoding/json" "fmt" - "path/filepath" "strings" "github.com/pkg/errors" @@ -26,13 +25,7 @@ func (a *AnalyzeHostServices) IsExcluded() (bool, error) { func (a *AnalyzeHostServices) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - collectorName := hostAnalyzer.CollectorName - if collectorName == "" { - collectorName = "systemctl_services" - } - name := filepath.Join("system", collectorName+".json") - - contents, err := getCollectedFileContents(name) + contents, err := getCollectedFileContents(collect.HostServicesPath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/analyze/host_time.go b/pkg/analyze/host_time.go index c39f9c37..bad5846f 100644 --- a/pkg/analyze/host_time.go +++ b/pkg/analyze/host_time.go @@ -32,7 +32,7 @@ func (a *AnalyzeHostTime) IsExcluded() (bool, error) { func (a *AnalyzeHostTime) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) { hostAnalyzer := a.hostAnalyzer - contents, err := getCollectedFileContents("system/time.json") + contents, err := getCollectedFileContents(collect.HostTimePath) if err != nil { return nil, errors.Wrap(err, "failed to get collected file") } diff --git a/pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go b/pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go index 2b1de5dc..87bd1464 100644 --- a/pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go @@ -1,13 +1,15 @@ package v1beta2 type CPUAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type MemoryAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type TCPLoadBalancerAnalyze struct { @@ -41,12 +43,14 @@ type HTTPAnalyze struct { } type TimeAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type BlockDevicesAnalyze struct { AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` MinimumAcceptableSize uint64 `json:"minimumAcceptableSize" yaml:"minimumAcceptableSize"` IncludeUnmountedPartitions bool `json:"includeUnmountedPartitions" yaml:"includeUnmountedPartitions"` Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` @@ -59,8 +63,9 @@ type SystemPackagesAnalyze struct { } type KernelModulesAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type TCPConnectAnalyze struct { @@ -70,8 +75,9 @@ type TCPConnectAnalyze struct { } type IPV4InterfacesAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type FilesystemPerformanceAnalyze struct { @@ -93,8 +99,9 @@ type HostServicesAnalyze struct { } type HostOSAnalyze struct { - AnalyzeMeta `json:",inline" yaml:",inline"` - Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + AnalyzeMeta `json:",inline" yaml:",inline"` + CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` } type HostAnalyze struct { CPU *CPUAnalyze `json:"cpu,omitempty" yaml:"cpu,omitempty"` diff --git a/pkg/collect/host_block_device.go b/pkg/collect/host_block_device.go index d427e824..ebf7fc2f 100644 --- a/pkg/collect/host_block_device.go +++ b/pkg/collect/host_block_device.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "os/exec" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -29,6 +28,7 @@ type BlockDeviceInfo struct { const lsblkColumns = "NAME,KNAME,PKNAME,TYPE,MAJ:MIN,SIZE,FSTYPE,MOUNTPOINT,SERIAL,RO,RM" const lsblkFormat = `NAME=%q KNAME=%q PKNAME=%q TYPE=%q MAJ:MIN="%d:%d" SIZE="%d" FSTYPE=%q MOUNTPOINT=%q SERIAL=%q RO="%d" RM="%d0"` +const HostBlockDevicesPath = `system/block_devices.json` type CollectHostBlockDevices struct { hostCollector *troubleshootv1beta2.HostBlockDevices @@ -85,16 +85,10 @@ func (c *CollectHostBlockDevices) Collect(progressChan chan<- interface{}) (map[ return nil, errors.Wrap(err, "failed to marshal block device info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "block_devices" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostBlockDevicesPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostBlockDevicesPath: b, }, nil } diff --git a/pkg/collect/host_cpu.go b/pkg/collect/host_cpu.go index ecb271e9..1205b6d8 100644 --- a/pkg/collect/host_cpu.go +++ b/pkg/collect/host_cpu.go @@ -3,7 +3,6 @@ package collect import ( "bytes" "encoding/json" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -15,6 +14,8 @@ type CPUInfo struct { PhysicalCount int `json:"physicalCount"` } +const HostCPUPath = `system/cpu.json` + type CollectHostCPU struct { hostCollector *troubleshootv1beta2.CPU BundlePath string @@ -48,16 +49,10 @@ func (c *CollectHostCPU) Collect(progressChan chan<- interface{}) (map[string][] return nil, errors.Wrap(err, "failed to marshal cpu info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "cpu" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostCPUPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostCPUPath: b, }, nil } diff --git a/pkg/collect/host_ipv4interfaces.go b/pkg/collect/host_ipv4interfaces.go index d4fbfcab..7ae9f79e 100644 --- a/pkg/collect/host_ipv4interfaces.go +++ b/pkg/collect/host_ipv4interfaces.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "net" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" ) +const HostIPV4InterfacesPath = `system/ipv4Interfaces.json` + type CollectHostIPV4Interfaces struct { hostCollector *troubleshootv1beta2.IPV4Interfaces BundlePath string @@ -50,16 +51,10 @@ func (c *CollectHostIPV4Interfaces) Collect(progressChan chan<- interface{}) (ma return nil, errors.Wrap(err, "failed to marshal network interfaces") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "ipv4Interfaces" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostIPV4InterfacesPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostIPV4InterfacesPath: b, }, nil } diff --git a/pkg/collect/host_kernel_modules.go b/pkg/collect/host_kernel_modules.go index ea54b8f8..0c4bc209 100644 --- a/pkg/collect/host_kernel_modules.go +++ b/pkg/collect/host_kernel_modules.go @@ -30,6 +30,8 @@ type KernelModuleInfo struct { Status KernelModuleStatus `json:"status"` } +const HostKernelModulesPath = `system/time.json` + // kernelModuleCollector defines the interface used to collect modules from the // underlying host. type kernelModuleCollector interface { @@ -96,17 +98,11 @@ func (c *CollectHostKernelModules) Collect(progressChan chan<- interface{}) (map return nil, errors.Wrap(err, "failed to marshal kernel modules") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "kernel_modules" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostKernelModulesPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostKernelModulesPath: b, }, nil } diff --git a/pkg/collect/host_memory.go b/pkg/collect/host_memory.go index 4974e097..25a6ea04 100644 --- a/pkg/collect/host_memory.go +++ b/pkg/collect/host_memory.go @@ -3,7 +3,6 @@ package collect import ( "bytes" "encoding/json" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -14,6 +13,8 @@ type MemoryInfo struct { Total uint64 `json:"total"` } +const HostMemoryPath = `system/memory.json` + type CollectHostMemory struct { hostCollector *troubleshootv1beta2.Memory BundlePath string @@ -41,16 +42,10 @@ func (c *CollectHostMemory) Collect(progressChan chan<- interface{}) (map[string return nil, errors.Wrap(err, "failed to marshal memory info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "memory" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostMemoryPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostMemoryPath: b, }, nil } diff --git a/pkg/collect/host_os_info.go b/pkg/collect/host_os_info.go index c1b9a998..52528dae 100644 --- a/pkg/collect/host_os_info.go +++ b/pkg/collect/host_os_info.go @@ -3,7 +3,6 @@ package collect import ( "bytes" "encoding/json" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -17,6 +16,8 @@ type HostOSInfo struct { Platform string `json:"platform"` } +const HostOSInfoPath = `system/hostos_info.json` + type CollectHostOS struct { hostCollector *troubleshootv1beta2.HostOS BundlePath string @@ -46,16 +47,10 @@ func (c *CollectHostOS) Collect(progressChan chan<- interface{}) (map[string][]b return nil, errors.Wrap(err, "failed to marshal host os info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "hostos_info" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostOSInfoPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostOSInfoPath: b, }, nil } diff --git a/pkg/collect/host_services.go b/pkg/collect/host_services.go index 7f0e5e80..2b7501b1 100644 --- a/pkg/collect/host_services.go +++ b/pkg/collect/host_services.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "os/exec" - "path/filepath" "github.com/pkg/errors" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" @@ -20,6 +19,7 @@ type ServiceInfo struct { } const systemctlFormat = `%s %s %s %s` // this leaves off the description +const HostServicesPath = `system/systemctl_services.json` type CollectHostServices struct { hostCollector *troubleshootv1beta2.HostServices @@ -64,16 +64,10 @@ func (c *CollectHostServices) Collect(progressChan chan<- interface{}) (map[stri return nil, errors.Wrap(err, "failed to marshal systemctl service info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "systemctl_services" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostServicesPath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostServicesPath: b, }, nil } diff --git a/pkg/collect/host_time.go b/pkg/collect/host_time.go index 83ac2558..c6923c1e 100644 --- a/pkg/collect/host_time.go +++ b/pkg/collect/host_time.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "log" - "path/filepath" "strings" "github.com/godbus/dbus" @@ -21,6 +20,8 @@ type TimeInfo struct { NTPActive bool `json:"ntp_active"` } +const HostTimePath = `system/time.json` + type CollectHostTime struct { hostCollector *troubleshootv1beta2.HostTime BundlePath string @@ -93,16 +94,10 @@ func (c *CollectHostTime) Collect(progressChan chan<- interface{}) (map[string][ return nil, errors.Wrap(err, "failed to marshal time info") } - collectorName := c.hostCollector.CollectorName - if collectorName == "" { - collectorName = "time" - } - name := filepath.Join("system", collectorName+".json") - output := NewResult() - output.SaveResult(c.BundlePath, name, bytes.NewBuffer(b)) + output.SaveResult(c.BundlePath, HostTimePath, bytes.NewBuffer(b)) return map[string][]byte{ - name: b, + HostTimePath: b, }, nil }