From c9b01f981d1f257277b18a9520fad6b23ee286d3 Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Fri, 6 Mar 2020 00:49:53 +0000 Subject: [PATCH 1/2] Remove unused code --- .../v1beta1/analyzer_shared_test.go | 58 ------------------- pkg/collect/collector.go | 11 ---- pkg/collect/collector_test.go | 43 -------------- 3 files changed, 112 deletions(-) delete mode 100644 pkg/apis/troubleshoot/v1beta1/analyzer_shared_test.go delete mode 100644 pkg/collect/collector_test.go diff --git a/pkg/apis/troubleshoot/v1beta1/analyzer_shared_test.go b/pkg/apis/troubleshoot/v1beta1/analyzer_shared_test.go deleted file mode 100644 index 486aa9af8..000000000 --- a/pkg/apis/troubleshoot/v1beta1/analyzer_shared_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package v1beta1 - -import ( - "encoding/json" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "go.undefinedlabs.com/scopeagent" -) - -func TestAnalyze_Unmarshal(t *testing.T) { - tests := []struct { - name string - spec string - expectObject Analyze - }{ - { - name: "clusterVersion", - spec: `clusterVersion: - outcomes: - - fail: - message: failed - - pass: - message: passed`, - expectObject: Analyze{ - ClusterVersion: &ClusterVersion{ - Outcomes: []*Outcome{ - &Outcome{ - Fail: &SingleOutcome{ - Message: "failed", - }, - }, - &Outcome{ - Pass: &SingleOutcome{ - Message: "passed", - }, - }, - }, - }, - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - scopetest := scopeagent.StartTest(t) - defer scopetest.End() - req := require.New(t) - - a := Analyze{} - err := json.Unmarshal([]byte(test.spec), &a) - req.NoError(err) - - assert.Equal(t, test.expectObject, a) - }) - } -} diff --git a/pkg/collect/collector.go b/pkg/collect/collector.go index d4a1722e1..e5e1f88cc 100644 --- a/pkg/collect/collector.go +++ b/pkg/collect/collector.go @@ -1,7 +1,6 @@ package collect import ( - "encoding/json" "strconv" "github.com/pkg/errors" @@ -190,13 +189,3 @@ func (cs Collectors) CheckRBAC() error { } return nil } - -func ParseSpec(specContents string) (*troubleshootv1beta1.Collect, error) { - collect := troubleshootv1beta1.Collect{} - - if err := json.Unmarshal([]byte(specContents), &collect); err != nil { - return nil, err - } - - return &collect, nil -} diff --git a/pkg/collect/collector_test.go b/pkg/collect/collector_test.go deleted file mode 100644 index f06a76139..000000000 --- a/pkg/collect/collector_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package collect - -import ( - "testing" - - troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" - "github.com/stretchr/testify/assert" - "go.undefinedlabs.com/scopeagent" -) - -func Test_ParseSpec(t *testing.T) { - tests := []struct { - name string - spec string - expectError bool - expectObject interface{} - }{ - { - name: "clusterInfo", - spec: "clusterInfo: {}", - expectError: false, - expectObject: &troubleshootv1beta1.Collect{ - ClusterInfo: &troubleshootv1beta1.ClusterInfo{}, - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - scopetest := scopeagent.StartTest(t) - defer scopetest.End() - c, err := ParseSpec(test.spec) - - if test.expectError { - assert.Error(t, err) - } else { - assert.NoError(t, err) - } - - assert.Equal(t, test.expectObject, c) - }) - } -} From 6c212e24f0b7fc6957fd96ed398ac96b759b741b Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Fri, 6 Mar 2020 01:15:49 +0000 Subject: [PATCH 2/2] Better marshaling --- cmd/preflight/cli/run.go | 14 ++++++++++---- cmd/troubleshoot/cli/run.go | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cmd/preflight/cli/run.go b/cmd/preflight/cli/run.go index f5d51d5b5..16121c99b 100644 --- a/cmd/preflight/cli/run.go +++ b/cmd/preflight/cli/run.go @@ -16,9 +16,11 @@ import ( "github.com/pkg/errors" analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze" troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" + troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" "github.com/replicatedhq/troubleshoot/pkg/collect" "github.com/spf13/viper" spin "github.com/tj/go-spin" + "k8s.io/client-go/kubernetes/scheme" ) func runPreflights(v *viper.Viper, arg string) error { @@ -57,11 +59,15 @@ func runPreflights(v *viper.Viper, arg string) error { preflightContent = string(body) } - preflight := troubleshootv1beta1.Preflight{} - if err := json.Unmarshal([]byte(preflightContent), &preflight); err != nil { - return errors.Wrapf(err, "failed to parse %s as a preflight", arg) + troubleshootclientsetscheme.AddToScheme(scheme.Scheme) + decode := scheme.Codecs.UniversalDeserializer().Decode + obj, _, err := decode([]byte(preflightContent), nil, nil) + if err != nil { + return errors.Wrapf(err, "failed to parse %s", arg) } + preflight := obj.(*troubleshootv1beta1.Preflight) + s := spin.New() finishedCh := make(chan bool, 1) progressChan := make(chan interface{}, 0) // non-zero buffer will result in missed messages @@ -92,7 +98,7 @@ func runPreflights(v *viper.Viper, arg string) error { close(finishedCh) }() - allCollectedData, err := runCollectors(v, preflight, progressChan) + allCollectedData, err := runCollectors(v, *preflight, progressChan) if err != nil { return err } diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 648263283..9e72face5 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -19,11 +19,12 @@ import ( "github.com/mattn/go-isatty" "github.com/mholt/archiver" "github.com/pkg/errors" - "github.com/spf13/viper" - spin "github.com/tj/go-spin" - troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" + "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" + troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" "github.com/replicatedhq/troubleshoot/pkg/collect" + "github.com/spf13/viper" + spin "github.com/tj/go-spin" ) var ( @@ -47,11 +48,15 @@ func runTroubleshoot(v *viper.Viper, arg string) error { return errors.Wrap(err, "failed to load collector spec") } - collector := troubleshootv1beta1.Collector{} - if err := json.Unmarshal(collectorContent, &collector); err != nil { - return errors.Wrapf(err, "failed to parse %s collectors", arg) + troubleshootclientsetscheme.AddToScheme(scheme.Scheme) + decode := scheme.Codecs.UniversalDeserializer().Decode + obj, _, err := decode([]byte(collectorContent), nil, nil) + if err != nil { + return errors.Wrapf(err, "failed to parse %s", arg) } + collector := obj.(*troubleshootv1beta1.Collector) + s := spin.New() finishedCh := make(chan bool, 1) progressChan := make(chan interface{}, 0) // non-zero buffer can result in missed messages @@ -83,7 +88,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error { close(finishedCh) }() - archivePath, err := runCollectors(v, collector, progressChan) + archivePath, err := runCollectors(v, *collector, progressChan) if err != nil { return errors.Wrap(err, "run collectors") }