Skip to content

Commit 401dfe2

Browse files
authored
feat: add loader APIs to load specs from raw troubleshoot spec (#1202)
* feat: add loader APIs to load specs from a list of yaml docs The change introduces a loader package that will contain loader public APIs. The aim of these APIs will be to, given any source of troubleshoot specs, the loaders will fetch the specs and parse out all troubleshoot objects that can be extracted. * Some refactoring * Some more changes * More changes caught when testing vendor portal * Add tests and rename Troubleshoot kinds struct * Additional test * Handle ConfigMap and Secrets with multiple specs in them * Fix failing test * Revert multidoc split implementation * Fix merge conflict * Change LoadFromXXX functions to a single LoadSpecs function
1 parent 145bad7 commit 401dfe2

File tree

22 files changed

+1313
-140
lines changed

22 files changed

+1313
-140
lines changed

cmd/analyze/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88

99
"github.com/pkg/errors"
10-
"github.com/replicatedhq/troubleshoot/cmd/util"
10+
"github.com/replicatedhq/troubleshoot/internal/util"
1111
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1212
"github.com/spf13/viper"
1313
)

cmd/collect/cli/run.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/pkg/errors"
13-
"github.com/replicatedhq/troubleshoot/cmd/util"
13+
"github.com/replicatedhq/troubleshoot/internal/util"
1414
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1515
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
16-
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
1716
"github.com/replicatedhq/troubleshoot/pkg/collect"
1817
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
1918
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -88,7 +87,6 @@ func runCollect(v *viper.Viper, arg string) error {
8887

8988
multidocs := strings.Split(string(collectorContent), "\n---\n")
9089

91-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
9290
decode := scheme.Codecs.UniversalDeserializer().Decode
9391

9492
redactors, err := supportbundle.GetRedactorsFromURIs(v.GetStringSlice("redactors"))

cmd/troubleshoot/cli/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/http"
88
"os"
99

10-
"github.com/replicatedhq/troubleshoot/cmd/util"
10+
"github.com/replicatedhq/troubleshoot/internal/util"
1111
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1212
"github.com/replicatedhq/troubleshoot/pkg/convert"
1313
"github.com/spf13/cobra"

cmd/troubleshoot/cli/interactive_results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/pkg/errors"
1111
ui "github.com/replicatedhq/termui/v3"
1212
"github.com/replicatedhq/termui/v3/widgets"
13-
"github.com/replicatedhq/troubleshoot/cmd/util"
13+
"github.com/replicatedhq/troubleshoot/internal/util"
1414
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
1515
)
1616

cmd/troubleshoot/cli/run.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/pkg/errors"
2020
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
2121
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
22-
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
23-
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2422
"github.com/replicatedhq/troubleshoot/pkg/convert"
2523
"github.com/replicatedhq/troubleshoot/pkg/httputil"
2624
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -79,7 +77,6 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
7977

8078
var mainBundle *troubleshootv1beta2.SupportBundle
8179

82-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
8380
additionalRedactors := &troubleshootv1beta2.Redactor{}
8481

8582
// Defining `v` below will render using `v` in reference to Viper unusable.

cmd/util/util.go renamed to internal/util/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ func AppName(name string) string {
4040

4141
return strings.Join(casedWords, " ")
4242
}
43+
44+
func SplitYAML(doc string) []string {
45+
return strings.Split(doc, "\n---\n")
46+
}

pkg/collect/load.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import (
44
"github.com/pkg/errors"
55
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
66
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
7-
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
87
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
8+
"k8s.io/apimachinery/pkg/runtime"
99
)
1010

11+
var decoder runtime.Decoder
12+
13+
func init() {
14+
decoder = scheme.Codecs.UniversalDeserializer()
15+
}
16+
1117
func ParseCollectorFromDoc(doc []byte) (*troubleshootv1beta2.Collector, error) {
1218
doc, err := docrewrite.ConvertToV1Beta2(doc)
1319
if err != nil {
1420
return nil, errors.Wrap(err, "failed to convert to v1beta2")
1521
}
1622

17-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
18-
decode := scheme.Codecs.UniversalDeserializer().Decode
19-
20-
obj, _, err := decode(doc, nil, nil)
23+
obj, _, err := decoder.Decode(doc, nil, nil)
2124
if err != nil {
2225
return nil, errors.Wrap(err, "failed to parse document")
2326
}
@@ -36,10 +39,7 @@ func ParseHostCollectorFromDoc(doc []byte) (*troubleshootv1beta2.HostCollector,
3639
return nil, errors.Wrap(err, "failed to convert to v1beta2")
3740
}
3841

39-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
40-
decode := scheme.Codecs.UniversalDeserializer().Decode
41-
42-
obj, _, err := decode(doc, nil, nil)
42+
obj, _, err := decoder.Decode(doc, nil, nil)
4343
if err != nil {
4444
return nil, errors.Wrap(err, "failed to parse document")
4545
}
@@ -58,10 +58,7 @@ func ParseRemoteCollectorFromDoc(doc []byte) (*troubleshootv1beta2.RemoteCollect
5858
return nil, errors.Wrap(err, "failed to convert to v1beta2")
5959
}
6060

61-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
62-
decode := scheme.Codecs.UniversalDeserializer().Decode
63-
64-
obj, _, err := decode(doc, nil, nil)
61+
obj, _, err := decoder.Decode(doc, nil, nil)
6562
if err != nil {
6663
return nil, errors.Wrap(err, "failed to parse document")
6764
}

pkg/constants/constants.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const (
1515
DEFAULT_LOGS_COLLECTOR_TIMEOUT = 60 * time.Second
1616

1717
// Tracing constants
18-
1918
LIB_TRACER_NAME = "github.com/replicatedhq/troubleshoot"
2019
TROUBLESHOOT_ROOT_SPAN_NAME = "ReplicatedTroubleshootRootSpan"
2120
EXCLUDED = "excluded"
@@ -58,4 +57,12 @@ const (
5857
EXIT_CODE_SPEC_ISSUES = 2
5958
EXIT_CODE_FAIL = 3
6059
EXIT_CODE_WARN = 4
60+
61+
// Troubleshoot label constants
62+
SupportBundleKey = "support-bundle-spec"
63+
RedactorKey = "redactor-spec"
64+
PreflightKey = "preflight.yaml" // Shouldn't this be "preflight-spec"?
65+
66+
// Troubleshoot spec constants
67+
Troubleshootv1beta2Kind = "troubleshoot.sh/v1beta2"
6168
)

0 commit comments

Comments
 (0)