From f02566c7128a67b39fc4a273d3b30c647016abc3 Mon Sep 17 00:00:00 2001 From: divolgin Date: Fri, 17 Jun 2022 09:36:30 -0700 Subject: [PATCH] Use reflection instead of hardcoding all alnalyzers --- pkg/analyze/analyzer.go | 23 ++++++ pkg/analyze/analyzer_test.go | 67 ++++++++++++++++ .../troubleshoot/v1beta2/analyzer_shared.go | 76 ------------------- 3 files changed, 90 insertions(+), 76 deletions(-) create mode 100644 pkg/analyze/analyzer_test.go diff --git a/pkg/analyze/analyzer.go b/pkg/analyze/analyzer.go index c1fe6bf15..b0bcf1144 100644 --- a/pkg/analyze/analyzer.go +++ b/pkg/analyze/analyzer.go @@ -2,6 +2,7 @@ package analyzer import ( "fmt" + "reflect" "strconv" "github.com/pkg/errors" @@ -502,3 +503,25 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont return nil, errors.New("invalid analyzer") } + +func GetExcludeFlag(analyzer *troubleshootv1beta2.Analyze) *multitype.BoolOrString { + if analyzer == nil { + return nil + } + + reflected := reflect.ValueOf(analyzer).Elem() + for i := 0; i < reflected.NumField(); i++ { + if reflected.Field(i).IsNil() { + continue + } + + field := reflect.Indirect(reflected.Field(i)).FieldByName("Exclude") + exclude, ok := field.Interface().(*multitype.BoolOrString) + if !ok { + continue + } + return exclude + } + + return nil +} diff --git a/pkg/analyze/analyzer_test.go b/pkg/analyze/analyzer_test.go new file mode 100644 index 000000000..13e7c47c3 --- /dev/null +++ b/pkg/analyze/analyzer_test.go @@ -0,0 +1,67 @@ +package analyzer + +import ( + "testing" + + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + "github.com/replicatedhq/troubleshoot/pkg/multitype" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetExcludeFlag(t *testing.T) { + tests := []struct { + name string + analyzer *troubleshootv1beta2.Analyze + want bool + }{ + { + name: "nil case", + analyzer: nil, + want: false, + }, + { + name: "true is set", + analyzer: &troubleshootv1beta2.Analyze{ + TextAnalyze: &troubleshootv1beta2.TextAnalyze{ + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ + Exclude: multitype.FromBool(true), + }, + }, + }, + want: true, + }, + { + name: "false is set", + analyzer: &troubleshootv1beta2.Analyze{ + ClusterVersion: &troubleshootv1beta2.ClusterVersion{ + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ + Exclude: multitype.FromBool(false), + }, + }, + }, + want: false, + }, + { + name: "nothing is set", + analyzer: &troubleshootv1beta2.Analyze{ + Postgres: &troubleshootv1beta2.DatabaseAnalyze{ + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{}, + }, + }, + want: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + req := require.New(t) + + gotWrapped := GetExcludeFlag(test.analyzer) + got, err := gotWrapped.Bool() + req.NoError(err) + + assert.Equal(t, test.want, got) + }) + } +} diff --git a/pkg/apis/troubleshoot/v1beta2/analyzer_shared.go b/pkg/apis/troubleshoot/v1beta2/analyzer_shared.go index 9e370ba25..79dece57e 100644 --- a/pkg/apis/troubleshoot/v1beta2/analyzer_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/analyzer_shared.go @@ -227,79 +227,3 @@ type Analyze struct { WeaveReport *WeaveReportAnalyze `json:"weaveReport,omitempty" yaml:"weaveReport,omitempty"` Sysctl *SysctlAnalyze `json:"sysctl,omitempty" yaml:"sysctl,omitempty"` } - -func (a *Analyze) GetExclude() *multitype.BoolOrString { - if a.ClusterVersion != nil { - return a.ClusterVersion.Exclude - } - if a.StorageClass != nil { - return a.StorageClass.Exclude - } - if a.CustomResourceDefinition != nil { - return a.CustomResourceDefinition.Exclude - } - if a.Ingress != nil { - return a.Ingress.Exclude - } - if a.Secret != nil { - return a.Secret.Exclude - } - if a.ConfigMap != nil { - return a.ConfigMap.Exclude - } - if a.ImagePullSecret != nil { - return a.ImagePullSecret.Exclude - } - if a.DeploymentStatus != nil { - return a.DeploymentStatus.Exclude - } - if a.StatefulsetStatus != nil { - return a.StatefulsetStatus.Exclude - } - if a.JobStatus != nil { - return a.JobStatus.Exclude - } - if a.ReplicaSetStatus != nil { - return a.ReplicaSetStatus.Exclude - } - if a.ClusterPodStatuses != nil { - return a.ClusterPodStatuses.Exclude - } - if a.ContainerRuntime != nil { - return a.ContainerRuntime.Exclude - } - if a.Distribution != nil { - return a.Distribution.Exclude - } - if a.NodeResources != nil { - return a.NodeResources.Exclude - } - if a.TextAnalyze != nil { - return a.TextAnalyze.Exclude - } - if a.Postgres != nil { - return a.Postgres.Exclude - } - if a.Mysql != nil { - return a.Mysql.Exclude - } - if a.Redis != nil { - return a.Redis.Exclude - } - if a.CephStatus != nil { - return a.CephStatus.Exclude - } - if a.Longhorn != nil { - return a.Longhorn.Exclude - } - if a.RegistryImages != nil { - return a.RegistryImages.Exclude - } - if a.WeaveReport != nil { - return a.WeaveReport.Exclude - } - if a.Sysctl != nil { - return a.Sysctl.Exclude - } - return nil -}