Use reflection instead of hardcoding all alnalyzers

This commit is contained in:
divolgin
2022-06-17 13:54:23 -07:00
parent 354a996edc
commit f02566c712
3 changed files with 90 additions and 76 deletions
+23
View File
@@ -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
}
+67
View File
@@ -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)
})
}
}
@@ -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
}