Skip to content

Commit

Permalink
Merge pull request #601 from replicatedhq/divolgin/reflect
Browse files Browse the repository at this point in the history
Use reflection instead of hardcoding all alnalyzers
  • Loading branch information
divolgin authored Jun 17, 2022
2 parents 354a996 + f02566c commit 7ec0084
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 76 deletions.
23 changes: 23 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"fmt"
"reflect"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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 changes: 67 additions & 0 deletions pkg/analyze/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
76 changes: 0 additions & 76 deletions pkg/apis/troubleshoot/v1beta2/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7ec0084

Please sign in to comment.