Skip to content

Commit 3ff91a3

Browse files
author
Salah Aldeen Al Saleh
authored
support bundle subdirectory (#285)
1 parent 2020049 commit 3ff91a3

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,21 @@ func canTryInsecure() bool {
376376
}
377377

378378
func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, additionalRedactors *troubleshootv1beta2.Redactor, progressChan chan interface{}) (string, error) {
379-
bundlePath, err := ioutil.TempDir("", "troubleshoot")
379+
tmpDir, err := ioutil.TempDir("", "troubleshoot")
380380
if err != nil {
381381
return "", errors.Wrap(err, "create temp dir")
382382
}
383-
defer os.RemoveAll(bundlePath)
383+
defer os.RemoveAll(tmpDir)
384+
385+
filename, err := findFileName("support-bundle-"+time.Now().Format("2006-01-02T15_04_05"), "tar.gz")
386+
if err != nil {
387+
return "", errors.Wrap(err, "find file name")
388+
}
389+
390+
bundlePath := filepath.Join(tmpDir, strings.TrimSuffix(filename, ".tar.gz"))
391+
if err := os.MkdirAll(bundlePath, 0777); err != nil {
392+
return "", errors.Wrap(err, "create bundle dir")
393+
}
384394

385395
if err = writeVersionFile(bundlePath); err != nil {
386396
return "", errors.Wrap(err, "write version file")
@@ -403,6 +413,7 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, ad
403413
Collect: desiredCollector,
404414
ClientConfig: config,
405415
Namespace: v.GetString("namespace"),
416+
PathPrefix: filepath.Base(bundlePath),
406417
}
407418
cleanedCollectors = append(cleanedCollectors, &collector)
408419
}
@@ -447,19 +458,15 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, ad
447458
}
448459

449460
if result != nil {
450-
err = saveCollectorOutput(result, bundlePath, collector)
461+
// results already contain the bundle dir name in their paths
462+
err = saveCollectorOutput(result, filepath.Dir(bundlePath), collector)
451463
if err != nil {
452464
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
453465
continue
454466
}
455467
}
456468
}
457469

458-
filename, err := findFileName("support-bundle-"+time.Now().Format("2006-01-02T15_04_05"), "tar.gz")
459-
if err != nil {
460-
return "", errors.Wrap(err, "find file name")
461-
}
462-
463470
if err := tarSupportBundleDir(bundlePath, filename); err != nil {
464471
return "", errors.Wrap(err, "create bundle file")
465472
}
@@ -662,22 +669,7 @@ func tarSupportBundleDir(inputDir, outputFilename string) error {
662669
},
663670
}
664671

665-
paths := []string{
666-
filepath.Join(inputDir, VersionFilename), // version file should be first in tar archive for quick extraction
667-
}
668-
669-
topLevelFiles, err := ioutil.ReadDir(inputDir)
670-
if err != nil {
671-
return errors.Wrap(err, "list bundle directory contents")
672-
}
673-
for _, f := range topLevelFiles {
674-
if f.Name() == VersionFilename {
675-
continue
676-
}
677-
paths = append(paths, filepath.Join(inputDir, f.Name()))
678-
}
679-
680-
if err := tarGz.Archive(paths, outputFilename); err != nil {
672+
if err := tarGz.Archive([]string{inputDir}, outputFilename); err != nil {
681673
return errors.Wrap(err, "create archive")
682674
}
683675

pkg/analyze/download.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ type fileContentProvider struct {
2323

2424
// Analyze local will analyze a locally available (already downloaded) bundle
2525
func AnalyzeLocal(localBundlePath string, analyzers []*troubleshootv1beta2.Analyze) ([]*AnalyzeResult, error) {
26-
fcp := fileContentProvider{rootDir: localBundlePath}
26+
rootDir, err := FindBundleRootDir(localBundlePath)
27+
if err != nil {
28+
return nil, errors.Wrap(err, "failed to find root dir")
29+
}
30+
31+
fcp := fileContentProvider{rootDir: rootDir}
2732

2833
analyzeResults := []*AnalyzeResult{}
2934
for _, analyzer := range analyzers {
@@ -52,7 +57,12 @@ func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResul
5257
return nil, errors.Wrap(err, "failed to download bundle")
5358
}
5459

55-
_, err = os.Stat(filepath.Join(tmpDir, "version.yaml"))
60+
rootDir, err := FindBundleRootDir(tmpDir)
61+
if err != nil {
62+
return nil, errors.Wrap(err, "failed to find root dir")
63+
}
64+
65+
_, err = os.Stat(filepath.Join(rootDir, "version.yaml"))
5666
if err != nil {
5767
return nil, errors.Wrap(err, "failed to read version.yaml")
5868
}
@@ -73,7 +83,7 @@ func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResul
7383
analyzers = parsedAnalyzers
7484
}
7585

76-
return AnalyzeLocal(tmpDir, analyzers)
86+
return AnalyzeLocal(rootDir, analyzers)
7787
}
7888

7989
func downloadTroubleshootBundle(bundleURL string, destDir string) error {
@@ -197,6 +207,39 @@ spec:
197207
return parseAnalyzers(spec)
198208
}
199209

210+
// FindBundleRootDir detects whether the bundle is stored inside a subdirectory or not.
211+
// returns the subdirectory path if so, otherwise, returns the path unchanged
212+
func FindBundleRootDir(localBundlePath string) (string, error) {
213+
f, err := os.Open(localBundlePath)
214+
if err != nil {
215+
return "", errors.Wrap(err, "failed to open bundle dir")
216+
}
217+
defer f.Close()
218+
219+
names, err := f.Readdirnames(0)
220+
if err != nil {
221+
return "", errors.Wrap(err, "failed to read dirnames")
222+
}
223+
224+
if len(names) == 0 {
225+
return "", errors.New("bundle directory is empty")
226+
}
227+
228+
isInSubDir := true
229+
for _, name := range names {
230+
if name == "version.yaml" {
231+
isInSubDir = false
232+
break
233+
}
234+
}
235+
236+
if isInSubDir {
237+
return filepath.Join(localBundlePath, names[0]), nil
238+
}
239+
240+
return localBundlePath, nil
241+
}
242+
200243
func (f fileContentProvider) getFileContents(fileName string) ([]byte, error) {
201244
return ioutil.ReadFile(filepath.Join(f.rootDir, fileName))
202245
}

pkg/collect/collector.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package collect
22

33
import (
44
"context"
5+
"path/filepath"
56
"strconv"
67

78
"github.com/pkg/errors"
@@ -19,6 +20,7 @@ type Collector struct {
1920
RBACErrors []error
2021
ClientConfig *rest.Config
2122
Namespace string
23+
PathPrefix string
2224
}
2325

2426
type Collectors []*Collector
@@ -155,10 +157,19 @@ func (c *Collector) RunCollectorSync(globalRedactors []*troubleshootv1beta2.Reda
155157
} else {
156158
return nil, errors.New("no spec found to run")
157159
}
158-
159160
if err != nil {
160161
return nil, err
161162
}
163+
164+
if c.PathPrefix != "" {
165+
// prefix file paths
166+
prefixed := map[string][]byte{}
167+
for k, v := range unRedacted {
168+
prefixed[filepath.Join(c.PathPrefix, k)] = v
169+
}
170+
unRedacted = prefixed
171+
}
172+
162173
if c.Redact {
163174
return redactMap(unRedacted, globalRedactors)
164175
}
-6.45 MB
Binary file not shown.

0 commit comments

Comments
 (0)