Skip to content

Commit 7032191

Browse files
authored
Merge pull request #251 from replicatedhq/divolgin/v1beta2
Adding troubleshoot.sh/v1beta2
2 parents b1e251e + a0ce85a commit 7032191

File tree

117 files changed

+13488
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+13488
-375
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ generate: controller-gen client-gen
7474
--clientset-name troubleshootclientset \
7575
--input-base github.com/replicatedhq/troubleshoot/pkg/apis \
7676
--input troubleshoot/v1beta1 \
77+
--input troubleshoot/v1beta2 \
7778
-h ./hack/boilerplate.go.txt
7879

7980
.PHONY: openapischema
8081
openapischema: controller-gen
8182
controller-gen crd +output:dir=./config/crds paths=./pkg/apis/troubleshoot/v1beta1
83+
controller-gen crd +output:dir=./config/crds paths=./pkg/apis/troubleshoot/v1beta2
8284

8385
.PHONY: schemas
8486
schemas: fmt vet openapischema

cmd/preflight/cli/run.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
"github.com/fatih/color"
1212
"github.com/pkg/errors"
1313
"github.com/replicatedhq/troubleshoot/cmd/util"
14-
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
14+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1515
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
16+
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
1617
"github.com/replicatedhq/troubleshoot/pkg/preflight"
1718
"github.com/spf13/viper"
1819
spin "github.com/tj/go-spin"
@@ -23,15 +24,15 @@ func runPreflights(v *viper.Viper, arg string) error {
2324
fmt.Print(cursor.Hide())
2425
defer fmt.Print(cursor.Show())
2526

26-
preflightContent := ""
27+
var preflightContent []byte
2728
var err error
2829
if _, err = os.Stat(arg); err == nil {
2930
b, err := ioutil.ReadFile(arg)
3031
if err != nil {
3132
return err
3233
}
3334

34-
preflightContent = string(b)
35+
preflightContent = b
3536
} else {
3637
if !util.IsURL(arg) {
3738
return fmt.Errorf("%s is not a URL and was not found (err %s)", arg, err)
@@ -41,7 +42,7 @@ func runPreflights(v *viper.Viper, arg string) error {
4142
if err != nil {
4243
return err
4344
}
44-
req.Header.Set("User-Agent", "Replicated_Preflight/v1beta1")
45+
req.Header.Set("User-Agent", "Replicated_Preflight/v1beta2")
4546
resp, err := http.DefaultClient.Do(req)
4647
if err != nil {
4748
return err
@@ -53,7 +54,12 @@ func runPreflights(v *viper.Viper, arg string) error {
5354
return err
5455
}
5556

56-
preflightContent = string(body)
57+
preflightContent = body
58+
}
59+
60+
preflightContent, err = docrewrite.ConvertToV1Beta2(preflightContent)
61+
if err != nil {
62+
return errors.Wrap(err, "failed to convert to v1beta2")
5763
}
5864

5965
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
@@ -63,7 +69,7 @@ func runPreflights(v *viper.Viper, arg string) error {
6369
return errors.Wrapf(err, "failed to parse %s", arg)
6470
}
6571

66-
preflightSpec := obj.(*troubleshootv1beta1.Preflight)
72+
preflightSpec := obj.(*troubleshootv1beta2.Preflight)
6773

6874
s := spin.New()
6975
finishedCh := make(chan bool, 1)

cmd/preflight/cli/util.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

cmd/schemagen/cli/root.go

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,60 @@ func generateSchemas(v *viper.Viper) error {
6262
return errors.Wrap(err, "failed to get workdir")
6363
}
6464

65-
preflightContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "troubleshoot.replicated.com_preflights.yaml"))
66-
if err != nil {
67-
return errors.Wrap(err, "failed to read preflight crd")
68-
}
69-
if err := generateSchemaFromCRD(preflightContents, filepath.Join(workdir, v.GetString("output-dir"), "preflight-troubleshoot-v1beta1.json")); err != nil {
70-
return errors.Wrap(err, "failed to write preflight schema")
71-
}
72-
73-
analyzersContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "troubleshoot.replicated.com_analyzers.yaml"))
74-
if err != nil {
75-
return errors.Wrap(err, "failed to read analyzers crd")
76-
}
77-
if err := generateSchemaFromCRD(analyzersContents, filepath.Join(workdir, v.GetString("output-dir"), "analyzer-troubleshoot-v1beta1.json")); err != nil {
78-
return errors.Wrap(err, "failed to write analyzers schema")
79-
}
80-
81-
collectorsContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "troubleshoot.replicated.com_collectors.yaml"))
82-
if err != nil {
83-
return errors.Wrap(err, "failed to read collectors crd")
84-
}
85-
if err := generateSchemaFromCRD(collectorsContents, filepath.Join(workdir, v.GetString("output-dir"), "collector-troubleshoot-v1beta1.json")); err != nil {
86-
return errors.Wrap(err, "failed to write collectors schema")
87-
}
88-
89-
redactorsContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "troubleshoot.replicated.com_redactors.yaml"))
90-
if err != nil {
91-
return errors.Wrap(err, "failed to read redactors crd")
92-
}
93-
if err := generateSchemaFromCRD(redactorsContents, filepath.Join(workdir, v.GetString("output-dir"), "redactor-troubleshoot-v1beta1.json")); err != nil {
94-
return errors.Wrap(err, "failed to write redactors schema")
65+
files := []struct {
66+
inFilename string
67+
outFilename string
68+
}{
69+
{
70+
"troubleshoot.replicated.com_preflights.yaml",
71+
"preflight-troubleshoot-v1beta1.json",
72+
},
73+
{
74+
"troubleshoot.replicated.com_analyzers.yaml",
75+
"analyzer-troubleshoot-v1beta1.json",
76+
},
77+
{
78+
"troubleshoot.replicated.com_collectors.yaml",
79+
"collector-troubleshoot-v1beta1.json",
80+
},
81+
{
82+
"troubleshoot.replicated.com_redactors.yaml",
83+
"redactor-troubleshoot-v1beta1.json",
84+
},
85+
{
86+
"troubleshoot.replicated.com_supportbundles.yaml",
87+
"supportbundle-troubleshoot-v1beta1.json",
88+
},
89+
{
90+
"troubleshoot.sh_analyzers.yaml",
91+
"analyzer-troubleshoot-v1beta2.json",
92+
},
93+
{
94+
"troubleshoot.sh_collectors.yaml",
95+
"collector-troubleshoot-v1beta2.json",
96+
},
97+
{
98+
"troubleshoot.sh_preflights.yaml",
99+
"preflight-troubleshoot-v1beta2.json",
100+
},
101+
{
102+
"troubleshoot.sh_redactors.yaml",
103+
"redactor-troubleshoot-v1beta2.json",
104+
},
105+
{
106+
"troubleshoot.sh_supportbundles.yaml",
107+
"supportbundle-troubleshoot-v1beta2.json",
108+
},
95109
}
96110

97-
supportBundlesContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "troubleshoot.replicated.com_supportbundles.yaml"))
98-
if err != nil {
99-
return errors.Wrap(err, "failed to read supportbundles crd")
100-
}
101-
if err := generateSchemaFromCRD(supportBundlesContents, filepath.Join(workdir, v.GetString("output-dir"), "supportbundle-troubleshoot-v1beta1.json")); err != nil {
102-
return errors.Wrap(err, "failed to write supportbundles schema")
111+
for _, file := range files {
112+
contents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", file.inFilename))
113+
if err != nil {
114+
return errors.Wrapf(err, "failed to read crd from %s", file.inFilename)
115+
}
116+
if err := generateSchemaFromCRD(contents, filepath.Join(workdir, v.GetString("output-dir"), file.outFilename)); err != nil {
117+
return errors.Wrapf(err, "failed to write crd schema to %s", file.outFilename)
118+
}
103119
}
104120

105121
return nil

cmd/troubleshoot/cli/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"strings"
77

8-
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
8+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
99
"github.com/replicatedhq/troubleshoot/pkg/logger"
1010
"github.com/spf13/cobra"
1111
"github.com/spf13/viper"
@@ -69,7 +69,7 @@ func initConfig() {
6969
viper.AutomaticEnv()
7070
}
7171

72-
func ensureCollectorInList(list []*troubleshootv1beta1.Collect, collector troubleshootv1beta1.Collect) []*troubleshootv1beta1.Collect {
72+
func ensureCollectorInList(list []*troubleshootv1beta2.Collect, collector troubleshootv1beta2.Collect) []*troubleshootv1beta2.Collect {
7373
for _, inList := range list {
7474
if collector.ClusterResources != nil && inList.ClusterResources != nil {
7575
return list

cmd/troubleshoot/cli/run.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import (
2525
"github.com/pkg/errors"
2626
"github.com/replicatedhq/troubleshoot/cmd/util"
2727
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
28-
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
28+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
2929
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
3030
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
3131
"github.com/replicatedhq/troubleshoot/pkg/collect"
3232
"github.com/replicatedhq/troubleshoot/pkg/convert"
33+
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
3334
"github.com/replicatedhq/troubleshoot/pkg/redact"
3435
"github.com/spf13/viper"
3536
spin "github.com/tj/go-spin"
@@ -57,7 +58,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
5758
return errors.Wrap(err, "failed to load collector spec")
5859
}
5960

60-
multidocs := strings.Split(string(collectorContent), "---")
61+
multidocs := strings.Split(string(collectorContent), "\n---\n")
6162

6263
// we suppory both raw collector kinds and supportbundle kinds here
6364
supportBundleSpec, err := parseSupportBundleFromDoc([]byte(multidocs[0]))
@@ -68,31 +69,42 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
6869
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
6970
decode := scheme.Codecs.UniversalDeserializer().Decode
7071

71-
additionalRedactors := &troubleshootv1beta1.Redactor{}
72+
additionalRedactors := &troubleshootv1beta2.Redactor{}
7273
for idx, redactor := range v.GetStringSlice("redactors") {
7374
redactorContent, err := loadSpec(v, redactor)
7475
if err != nil {
7576
return errors.Wrapf(err, "failed to load redactor spec #%d", idx)
7677
}
78+
redactorContent, err = docrewrite.ConvertToV1Beta2(redactorContent)
79+
if err != nil {
80+
return errors.Wrap(err, "failed to convert to v1beta2")
81+
}
7782
obj, _, err := decode([]byte(redactorContent), nil, nil)
7883
if err != nil {
7984
return errors.Wrapf(err, "failed to parse redactors %s", redactor)
8085
}
81-
loopRedactors, ok := obj.(*troubleshootv1beta1.Redactor)
86+
loopRedactors, ok := obj.(*troubleshootv1beta2.Redactor)
8287
if !ok {
83-
return fmt.Errorf("%s is not a troubleshootv1beta1 redactor type", redactor)
88+
return fmt.Errorf("%s is not a troubleshootv1beta2 redactor type", redactor)
8489
}
8590
if loopRedactors != nil {
8691
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, loopRedactors.Spec.Redactors...)
8792
}
8893
}
8994

90-
for i, additionalDoc := range multidocs[1:] {
91-
obj, _, err := decode([]byte(additionalDoc), nil, nil)
95+
for i, additionalDoc := range multidocs {
96+
if i == 0 {
97+
continue
98+
}
99+
additionalDoc, err := docrewrite.ConvertToV1Beta2([]byte(additionalDoc))
100+
if err != nil {
101+
return errors.Wrap(err, "failed to convert to v1beta2")
102+
}
103+
obj, _, err := decode(additionalDoc, nil, nil)
92104
if err != nil {
93105
return errors.Wrapf(err, "failed to parse additional doc %d", i)
94106
}
95-
multidocRedactors, ok := obj.(*troubleshootv1beta1.Redactor)
107+
multidocRedactors, ok := obj.(*troubleshootv1beta2.Redactor)
96108
if !ok {
97109
continue
98110
}
@@ -274,7 +286,12 @@ func loadSpec(v *viper.Viper, arg string) ([]byte, error) {
274286
}
275287
}
276288

277-
func parseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta1.SupportBundle, error) {
289+
func parseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta2.SupportBundle, error) {
290+
doc, err := docrewrite.ConvertToV1Beta2(doc)
291+
if err != nil {
292+
return nil, errors.Wrap(err, "failed to convert to v1beta2")
293+
}
294+
278295
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
279296
decode := scheme.Codecs.UniversalDeserializer().Decode
280297

@@ -283,25 +300,25 @@ func parseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta1.SupportBundle,
283300
return nil, errors.Wrap(err, "failed to parse document")
284301
}
285302

286-
collector, ok := obj.(*troubleshootv1beta1.Collector)
303+
collector, ok := obj.(*troubleshootv1beta2.Collector)
287304
if ok {
288-
supportBundle := troubleshootv1beta1.SupportBundle{
305+
supportBundle := troubleshootv1beta2.SupportBundle{
289306
TypeMeta: metav1.TypeMeta{
290-
APIVersion: "troubleshoot.replicated.com/v1beta1",
307+
APIVersion: "troubleshoot.sh/v1beta2",
291308
Kind: "SupportBundle",
292309
},
293310
ObjectMeta: collector.ObjectMeta,
294-
Spec: troubleshootv1beta1.SupportBundleSpec{
311+
Spec: troubleshootv1beta2.SupportBundleSpec{
295312
Collectors: collector.Spec.Collectors,
296-
Analyzers: []*troubleshootv1beta1.Analyze{},
313+
Analyzers: []*troubleshootv1beta2.Analyze{},
297314
AfterCollection: collector.Spec.AfterCollection,
298315
},
299316
}
300317

301318
return &supportBundle, nil
302319
}
303320

304-
supportBundle, ok := obj.(*troubleshootv1beta1.SupportBundle)
321+
supportBundle, ok := obj.(*troubleshootv1beta2.SupportBundle)
305322
if ok {
306323
return supportBundle, nil
307324
}
@@ -326,7 +343,7 @@ func canTryInsecure(v *viper.Viper) bool {
326343
return true
327344
}
328345

329-
func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta1.Collect, additionalRedactors *troubleshootv1beta1.Redactor, progressChan chan interface{}) (string, error) {
346+
func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, additionalRedactors *troubleshootv1beta2.Redactor, progressChan chan interface{}) (string, error) {
330347
bundlePath, err := ioutil.TempDir("", "troubleshoot")
331348
if err != nil {
332349
return "", errors.Wrap(err, "create temp dir")
@@ -337,10 +354,10 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta1.Collect, ad
337354
return "", errors.Wrap(err, "write version file")
338355
}
339356

340-
collectSpecs := make([]*troubleshootv1beta1.Collect, 0, 0)
357+
collectSpecs := make([]*troubleshootv1beta2.Collect, 0, 0)
341358
collectSpecs = append(collectSpecs, collectors...)
342-
collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta1.Collect{ClusterInfo: &troubleshootv1beta1.ClusterInfo{}})
343-
collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta1.Collect{ClusterResources: &troubleshootv1beta1.ClusterResources{}})
359+
collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}})
360+
collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}})
344361

345362
config, err := KubernetesConfigFlags.ToRESTConfig()
346363
if err != nil {
@@ -374,7 +391,7 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta1.Collect, ad
374391
return "", errors.New("insufficient permissions to run all collectors")
375392
}
376393

377-
globalRedactors := []*troubleshootv1beta1.Redact{}
394+
globalRedactors := []*troubleshootv1beta2.Redact{}
378395
if additionalRedactors != nil {
379396
globalRedactors = additionalRedactors.Spec.Redactors
380397
}
@@ -441,6 +458,7 @@ func saveCollectorOutput(output map[string][]byte, bundlePath string, c *collect
441458

442459
return nil
443460
}
461+
444462
func untarAndSave(tarFile []byte, bundlePath string) error {
445463
keys := make([]string, 0)
446464
dirs := make(map[string]*tar.Header)
@@ -494,7 +512,7 @@ func untarAndSave(tarFile []byte, bundlePath string) error {
494512
}
495513
return nil
496514
}
497-
func uploadSupportBundle(r *troubleshootv1beta1.ResultRequest, archivePath string) error {
515+
func uploadSupportBundle(r *troubleshootv1beta2.ResultRequest, archivePath string) error {
498516
contentType := getExpectedContentType(r.URI)
499517
if contentType != "" && contentType != "application/tar+gzip" {
500518
return fmt.Errorf("cannot upload content type %s", contentType)
@@ -567,7 +585,7 @@ func getExpectedContentType(uploadURL string) string {
567585
return parsedURL.Query().Get("Content-Type")
568586
}
569587

570-
func callbackSupportBundleAPI(r *troubleshootv1beta1.ResultRequest, archivePath string) error {
588+
func callbackSupportBundleAPI(r *troubleshootv1beta2.ResultRequest, archivePath string) error {
571589
req, err := http.NewRequest(r.Method, r.URI, nil)
572590
if err != nil {
573591
return errors.Wrap(err, "create request")
@@ -615,6 +633,6 @@ func tarSupportBundleDir(inputDir, outputFilename string) error {
615633
}
616634

617635
type CollectorFailure struct {
618-
Collector *troubleshootv1beta1.Collect
636+
Collector *troubleshootv1beta2.Collect
619637
Failure string
620638
}

0 commit comments

Comments
 (0)