Skip to content

Commit 98f926b

Browse files
committed
Support for optional collectors and analyzers
1 parent d0a0d92 commit 98f926b

File tree

8 files changed

+83
-12
lines changed

8 files changed

+83
-12
lines changed

cmd/preflight/cli/run.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func runPreflights(v *viper.Viper, arg string) error {
112112
continue
113113
}
114114

115-
analyzeResults = append(analyzeResults, analyzeResult)
115+
if analyzeResult != nil {
116+
analyzeResults = append(analyzeResults, analyzeResult)
117+
}
116118
}
117119

118120
finishedCh <- true
@@ -156,12 +158,14 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
156158
return nil, errors.Wrap(err, "failed to run collector")
157159
}
158160

159-
output, err := parseCollectorOutput(string(result))
160-
if err != nil {
161-
return nil, errors.Wrap(err, "failed to parse collector output")
162-
}
163-
for k, v := range output {
164-
allCollectedData[k] = v
161+
if result != nil {
162+
output, err := parseCollectorOutput(string(result))
163+
if err != nil {
164+
return nil, errors.Wrap(err, "failed to parse collector output")
165+
}
166+
for k, v := range output {
167+
allCollectedData[k] = v
168+
}
165169
}
166170
}
167171

cmd/troubleshoot/cli/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
179179
continue
180180
}
181181

182-
err = parseAndSaveCollectorOutput(string(result), bundlePath)
183-
if err != nil {
184-
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
185-
continue
182+
if result != nil {
183+
err = parseAndSaveCollectorOutput(string(result), bundlePath)
184+
if err != nil {
185+
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
186+
continue
187+
}
186188
}
187189
}
188190

pkg/analyze/analyzer.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,63 @@ type getChildCollectedFileContents func(string) (map[string][]byte, error)
2020

2121
func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) (*AnalyzeResult, error) {
2222
if analyzer.ClusterVersion != nil {
23+
if analyzer.ClusterVersion.Exclude {
24+
return nil, nil
25+
}
2326
return analyzeClusterVersion(analyzer.ClusterVersion, getFile)
2427
}
2528
if analyzer.StorageClass != nil {
29+
if analyzer.StorageClass.Exclude {
30+
return nil, nil
31+
}
2632
return analyzeStorageClass(analyzer.StorageClass, getFile)
2733
}
2834
if analyzer.CustomResourceDefinition != nil {
35+
if analyzer.CustomResourceDefinition.Exclude {
36+
return nil, nil
37+
}
2938
return analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getFile)
3039
}
3140
if analyzer.Ingress != nil {
41+
if analyzer.Ingress.Exclude {
42+
return nil, nil
43+
}
3244
return analyzeIngress(analyzer.Ingress, getFile)
3345
}
3446
if analyzer.Secret != nil {
47+
if analyzer.Secret.Exclude {
48+
return nil, nil
49+
}
3550
return analyzeSecret(analyzer.Secret, getFile)
3651
}
3752
if analyzer.ImagePullSecret != nil {
53+
if analyzer.ImagePullSecret.Exclude {
54+
return nil, nil
55+
}
3856
return analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
3957
}
4058
if analyzer.DeploymentStatus != nil {
59+
if analyzer.DeploymentStatus.Exclude {
60+
return nil, nil
61+
}
4162
return analyzeDeploymentStatus(analyzer.DeploymentStatus, getFile)
4263
}
4364
if analyzer.StatefulsetStatus != nil {
65+
if analyzer.StatefulsetStatus.Exclude {
66+
return nil, nil
67+
}
4468
return analyzeStatefulsetStatus(analyzer.StatefulsetStatus, getFile)
4569
}
4670
if analyzer.ContainerRuntime != nil {
71+
if analyzer.ContainerRuntime.Exclude {
72+
return nil, nil
73+
}
4774
return analyzeContainerRuntime(analyzer.ContainerRuntime, getFile)
4875
}
4976
if analyzer.Distribution != nil {
77+
if analyzer.Distribution.Exclude {
78+
return nil, nil
79+
}
5080
return analyzeDistribution(analyzer.Distribution, getFile)
5181
}
5282

pkg/analyze/download.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResul
6262
continue
6363
}
6464

65-
analyzeResults = append(analyzeResults, analyzeResult)
65+
if analyzeResult != nil {
66+
analyzeResults = append(analyzeResults, analyzeResult)
67+
}
6668
}
6769

6870
return analyzeResults, nil

pkg/apis/troubleshoot/v1beta1/analyzer_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type Distribution struct {
7676

7777
type AnalyzeMeta struct {
7878
CheckName string `json:"checkName,omitempty" yaml:"checkName,omitempty"`
79+
Exclude bool `json:"exclude,omitempty" yaml:"exclude,omitempty"`
7980
}
8081

8182
type Analyze struct {

pkg/apis/troubleshoot/v1beta1/collector_shared.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package v1beta1
22

33
type CollectorMeta struct {
44
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
5+
Exclude bool `json:"when,omitmempty" yaml:"when,omitempty"`
56
}
67

78
type ClusterInfo struct {
9+
CollectorMeta `json:",inline" yaml:",inline"`
810
}
911

1012
type ClusterResources struct {
13+
CollectorMeta `json:",inline" yaml:",inline"`
1114
}
1215

1316
type Secret struct {

pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ func (in *AnalyzerStatus) DeepCopy() *AnalyzerStatus {
348348
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
349349
func (in *ClusterInfo) DeepCopyInto(out *ClusterInfo) {
350350
*out = *in
351+
out.CollectorMeta = in.CollectorMeta
351352
}
352353

353354
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterInfo.
@@ -363,6 +364,7 @@ func (in *ClusterInfo) DeepCopy() *ClusterInfo {
363364
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
364365
func (in *ClusterResources) DeepCopyInto(out *ClusterResources) {
365366
*out = *in
367+
out.CollectorMeta = in.CollectorMeta
366368
}
367369

368370
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterResources.

pkg/collect/collector.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,57 @@ type Context struct {
2525

2626
func (c *Collector) RunCollectorSync() ([]byte, error) {
2727
if c.Collect.ClusterInfo != nil {
28+
if c.Collect.ClusterInfo.Exclude {
29+
return nil, nil
30+
}
2831
return ClusterInfo(c.GetContext())
2932
}
3033
if c.Collect.ClusterResources != nil {
34+
if c.Collect.ClusterResources.Exclude {
35+
return nil, nil
36+
}
3137
return ClusterResources(c.GetContext())
3238
}
3339
if c.Collect.Secret != nil {
40+
if c.Collect.Secret.Exclude {
41+
return nil, nil
42+
}
3443
return Secret(c.GetContext(), c.Collect.Secret)
3544
}
3645
if c.Collect.Logs != nil {
46+
if c.Collect.Logs.Exclude {
47+
return nil, nil
48+
}
3749
return Logs(c.GetContext(), c.Collect.Logs)
3850
}
3951
if c.Collect.Run != nil {
52+
if c.Collect.Run.Exclude {
53+
return nil, nil
54+
}
4055
return Run(c.GetContext(), c.Collect.Run)
4156
}
4257
if c.Collect.Exec != nil {
58+
if c.Collect.Exec.Exclude {
59+
return nil, nil
60+
}
4361
return Exec(c.GetContext(), c.Collect.Exec)
4462
}
4563
if c.Collect.Data != nil {
64+
if c.Collect.Data.Exclude {
65+
return nil, nil
66+
}
4667
return Data(c.GetContext(), c.Collect.Data)
4768
}
4869
if c.Collect.Copy != nil {
70+
if c.Collect.Copy.Exclude {
71+
return nil, nil
72+
}
4973
return Copy(c.GetContext(), c.Collect.Copy)
5074
}
5175
if c.Collect.HTTP != nil {
76+
if c.Collect.HTTP.Exclude {
77+
return nil, nil
78+
}
5279
return HTTP(c.GetContext(), c.Collect.HTTP)
5380
}
5481

0 commit comments

Comments
 (0)