From d73d5c6a3a4114a3b67d987696155c01cdf83cbf Mon Sep 17 00:00:00 2001 From: Nathan Sullivan Date: Wed, 4 Jan 2023 04:01:49 +1000 Subject: [PATCH] preflight: fix segfault when collector's are not defined in YAML (#939) * preflight: fix segfault when collector's are not defined in YAML * fix bug with kind: Preflight specs with uploadResultsTo, wrong variable being used :) ref https://github.com/replicatedhq/troubleshoot/pull/894 Co-authored-by: Evans Mungai --- pkg/preflight/collect.go | 12 +++++++++--- pkg/preflight/run.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/preflight/collect.go b/pkg/preflight/collect.go index 93ff31aa..73382218 100644 --- a/pkg/preflight/collect.go +++ b/pkg/preflight/collect.go @@ -84,7 +84,9 @@ func (cr RemoteCollectResult) IsRBACAllowed() bool { // CollectHost runs the collection phase of host preflight checks func CollectHost(opts CollectOpts, p *troubleshootv1beta2.HostPreflight) (CollectResult, error) { collectSpecs := make([]*troubleshootv1beta2.HostCollect, 0, 0) - collectSpecs = append(collectSpecs, p.Spec.Collectors...) + if p != nil && p.Spec.Collectors != nil { + collectSpecs = append(collectSpecs, p.Spec.Collectors...) + } allCollectedData := make(map[string][]byte) @@ -128,7 +130,9 @@ func Collect(opts CollectOpts, p *troubleshootv1beta2.Preflight) (CollectResult, var foundForbidden bool collectSpecs := make([]*troubleshootv1beta2.Collect, 0, 0) - collectSpecs = append(collectSpecs, p.Spec.Collectors...) + if p != nil && p.Spec.Collectors != nil { + collectSpecs = append(collectSpecs, p.Spec.Collectors...) + } collectSpecs = collect.EnsureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}) collectSpecs = collect.EnsureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}}) collectSpecs = collect.EnsureClusterResourcesFirst(collectSpecs) @@ -267,7 +271,9 @@ func Collect(opts CollectOpts, p *troubleshootv1beta2.Preflight) (CollectResult, // Collect runs the collection phase of preflight checks func CollectRemote(opts CollectOpts, p *troubleshootv1beta2.HostPreflight) (CollectResult, error) { collectSpecs := make([]*troubleshootv1beta2.RemoteCollect, 0, 0) - collectSpecs = append(collectSpecs, p.Spec.RemoteCollectors...) + if p != nil && p.Spec.RemoteCollectors != nil { + collectSpecs = append(collectSpecs, p.Spec.RemoteCollectors...) + } allCollectedData := make(map[string][]byte) diff --git a/pkg/preflight/run.go b/pkg/preflight/run.go index 19ded90e..64841ed6 100644 --- a/pkg/preflight/run.go +++ b/pkg/preflight/run.go @@ -132,7 +132,7 @@ func RunPreflights(interactive bool, output string, format string, args []string preflightSpec = ConcatPreflightSpec(preflightSpec, spec) } } else { - uploadResultSpecs = append(uploadResultSpecs, preflightSpec) + uploadResultSpecs = append(uploadResultSpecs, spec) } } else if spec, ok := obj.(*troubleshootv1beta2.HostPreflight); ok { if i == 0 {