Skip to content

Commit 7ec0084

Browse files
authored
Merge pull request #601 from replicatedhq/divolgin/reflect
Use reflection instead of hardcoding all alnalyzers
2 parents 354a996 + f02566c commit 7ec0084

File tree

3 files changed

+90
-76
lines changed

3 files changed

+90
-76
lines changed

pkg/analyze/analyzer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"fmt"
5+
"reflect"
56
"strconv"
67

78
"github.com/pkg/errors"
@@ -502,3 +503,25 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
502503

503504
return nil, errors.New("invalid analyzer")
504505
}
506+
507+
func GetExcludeFlag(analyzer *troubleshootv1beta2.Analyze) *multitype.BoolOrString {
508+
if analyzer == nil {
509+
return nil
510+
}
511+
512+
reflected := reflect.ValueOf(analyzer).Elem()
513+
for i := 0; i < reflected.NumField(); i++ {
514+
if reflected.Field(i).IsNil() {
515+
continue
516+
}
517+
518+
field := reflect.Indirect(reflected.Field(i)).FieldByName("Exclude")
519+
exclude, ok := field.Interface().(*multitype.BoolOrString)
520+
if !ok {
521+
continue
522+
}
523+
return exclude
524+
}
525+
526+
return nil
527+
}

pkg/analyze/analyzer_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
7+
"github.com/replicatedhq/troubleshoot/pkg/multitype"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test_GetExcludeFlag(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
analyzer *troubleshootv1beta2.Analyze
16+
want bool
17+
}{
18+
{
19+
name: "nil case",
20+
analyzer: nil,
21+
want: false,
22+
},
23+
{
24+
name: "true is set",
25+
analyzer: &troubleshootv1beta2.Analyze{
26+
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
27+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
28+
Exclude: multitype.FromBool(true),
29+
},
30+
},
31+
},
32+
want: true,
33+
},
34+
{
35+
name: "false is set",
36+
analyzer: &troubleshootv1beta2.Analyze{
37+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
38+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
39+
Exclude: multitype.FromBool(false),
40+
},
41+
},
42+
},
43+
want: false,
44+
},
45+
{
46+
name: "nothing is set",
47+
analyzer: &troubleshootv1beta2.Analyze{
48+
Postgres: &troubleshootv1beta2.DatabaseAnalyze{
49+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{},
50+
},
51+
},
52+
want: false,
53+
},
54+
}
55+
56+
for _, test := range tests {
57+
t.Run(test.name, func(t *testing.T) {
58+
req := require.New(t)
59+
60+
gotWrapped := GetExcludeFlag(test.analyzer)
61+
got, err := gotWrapped.Bool()
62+
req.NoError(err)
63+
64+
assert.Equal(t, test.want, got)
65+
})
66+
}
67+
}

pkg/apis/troubleshoot/v1beta2/analyzer_shared.go

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -227,79 +227,3 @@ type Analyze struct {
227227
WeaveReport *WeaveReportAnalyze `json:"weaveReport,omitempty" yaml:"weaveReport,omitempty"`
228228
Sysctl *SysctlAnalyze `json:"sysctl,omitempty" yaml:"sysctl,omitempty"`
229229
}
230-
231-
func (a *Analyze) GetExclude() *multitype.BoolOrString {
232-
if a.ClusterVersion != nil {
233-
return a.ClusterVersion.Exclude
234-
}
235-
if a.StorageClass != nil {
236-
return a.StorageClass.Exclude
237-
}
238-
if a.CustomResourceDefinition != nil {
239-
return a.CustomResourceDefinition.Exclude
240-
}
241-
if a.Ingress != nil {
242-
return a.Ingress.Exclude
243-
}
244-
if a.Secret != nil {
245-
return a.Secret.Exclude
246-
}
247-
if a.ConfigMap != nil {
248-
return a.ConfigMap.Exclude
249-
}
250-
if a.ImagePullSecret != nil {
251-
return a.ImagePullSecret.Exclude
252-
}
253-
if a.DeploymentStatus != nil {
254-
return a.DeploymentStatus.Exclude
255-
}
256-
if a.StatefulsetStatus != nil {
257-
return a.StatefulsetStatus.Exclude
258-
}
259-
if a.JobStatus != nil {
260-
return a.JobStatus.Exclude
261-
}
262-
if a.ReplicaSetStatus != nil {
263-
return a.ReplicaSetStatus.Exclude
264-
}
265-
if a.ClusterPodStatuses != nil {
266-
return a.ClusterPodStatuses.Exclude
267-
}
268-
if a.ContainerRuntime != nil {
269-
return a.ContainerRuntime.Exclude
270-
}
271-
if a.Distribution != nil {
272-
return a.Distribution.Exclude
273-
}
274-
if a.NodeResources != nil {
275-
return a.NodeResources.Exclude
276-
}
277-
if a.TextAnalyze != nil {
278-
return a.TextAnalyze.Exclude
279-
}
280-
if a.Postgres != nil {
281-
return a.Postgres.Exclude
282-
}
283-
if a.Mysql != nil {
284-
return a.Mysql.Exclude
285-
}
286-
if a.Redis != nil {
287-
return a.Redis.Exclude
288-
}
289-
if a.CephStatus != nil {
290-
return a.CephStatus.Exclude
291-
}
292-
if a.Longhorn != nil {
293-
return a.Longhorn.Exclude
294-
}
295-
if a.RegistryImages != nil {
296-
return a.RegistryImages.Exclude
297-
}
298-
if a.WeaveReport != nil {
299-
return a.WeaveReport.Exclude
300-
}
301-
if a.Sysctl != nil {
302-
return a.Sysctl.Exclude
303-
}
304-
return nil
305-
}

0 commit comments

Comments
 (0)