Skip to content

Commit 3b6ab41

Browse files
authored
Merge pull request #72 from replicatedhq/divolgin/output-names
use file paths specified by the spec instead of hardcoded ones
2 parents 36e3bf2 + 60ca35a commit 3b6ab41

File tree

10 files changed

+164
-166
lines changed

10 files changed

+164
-166
lines changed

cmd/troubleshoot/cli/receive.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"os"
1010
"path/filepath"
1111

12-
"github.com/mholt/archiver"
13-
"github.com/pkg/errors"
1412
"github.com/replicatedhq/troubleshoot/pkg/logger"
1513
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
1614
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,8 +27,7 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string)
2927
}
3028
defer os.RemoveAll(bundlePath)
3129

32-
versionFilename, err := writeVersionFile(bundlePath)
33-
if err != nil {
30+
if err = writeVersionFile(bundlePath); err != nil {
3431
return err
3532
}
3633

@@ -118,28 +115,15 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string)
118115
}
119116

120117
if len(job.Status.Running) == 0 {
121-
tarGz := archiver.TarGz{
122-
Tar: &archiver.Tar{
123-
ImplicitTopLevelFolder: false,
124-
},
125-
}
126-
127-
// version file should be first in tar archive for quick extraction
128-
paths := []string{
129-
versionFilename,
130-
}
131-
for _, id := range receivedCollectors {
132-
paths = append(paths, filepath.Join(bundlePath, id))
133-
}
134-
135118
filename, err := findFileName("support-bundle", "tar.gz")
136119
if err != nil {
137-
return errors.Wrap(err, "find file name")
120+
return err
138121
}
139122

140-
if err := tarGz.Archive(paths, filename); err != nil {
123+
if err := tarSupportBundleDir(bundlePath, filename); err != nil {
141124
return err
142125
}
126+
143127
return nil
144128
}
145129
}

cmd/troubleshoot/cli/run_nocrd.go

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
146146
}
147147
defer os.RemoveAll(bundlePath)
148148

149-
versionFilename, err := writeVersionFile(bundlePath)
150-
if err != nil {
149+
if err = writeVersionFile(bundlePath); err != nil {
151150
return "", errors.Wrap(err, "write version file")
152151
}
153152

@@ -158,8 +157,6 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
158157
desiredCollectors = ensureCollectorInList(desiredCollectors, troubleshootv1beta1.Collect{ClusterInfo: &troubleshootv1beta1.ClusterInfo{}})
159158
desiredCollectors = ensureCollectorInList(desiredCollectors, troubleshootv1beta1.Collect{ClusterResources: &troubleshootv1beta1.ClusterResources{}})
160159

161-
collectorDirs := []string{}
162-
163160
config, err := KubernetesConfigFlags.ToRESTConfig()
164161
if err != nil {
165162
return "", errors.Wrap(err, "failed to convert kube flags to rest config")
@@ -173,107 +170,77 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
173170
ClientConfig: config,
174171
}
175172

173+
progressChan <- collector.GetDisplayName()
174+
176175
result, err := collector.RunCollectorSync()
177176
if err != nil {
178177
progressChan <- fmt.Errorf("failed to run collector %q: %v", collector.GetDisplayName(), err)
179178
continue
180179
}
181180

182-
newCollectorDirs, err := parseAndSaveCollectorOutput(string(result), bundlePath)
181+
err = parseAndSaveCollectorOutput(string(result), bundlePath)
183182
if err != nil {
184183
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
185184
continue
186185
}
187-
188-
if len(newCollectorDirs) == 0 {
189-
continue
190-
}
191-
192-
// TODO: better progress....
193-
for _, d := range newCollectorDirs {
194-
progressChan <- d
195-
}
196-
collectorDirs = append(collectorDirs, newCollectorDirs...)
197-
}
198-
199-
tarGz := archiver.TarGz{
200-
Tar: &archiver.Tar{
201-
ImplicitTopLevelFolder: false,
202-
},
203-
}
204-
205-
// version file should be first in tar archive for quick extraction
206-
paths := []string{
207-
versionFilename,
208-
}
209-
for _, collectorDir := range collectorDirs {
210-
paths = append(paths, collectorDir)
211186
}
212187

213188
filename, err := findFileName("support-bundle", "tar.gz")
214189
if err != nil {
215190
return "", errors.Wrap(err, "find file name")
216191
}
217192

218-
if err := tarGz.Archive(paths, filename); err != nil {
219-
return "", errors.Wrap(err, "create archive")
193+
if err := tarSupportBundleDir(bundlePath, filename); err != nil {
194+
return "", errors.Wrap(err, "create bundle file")
220195
}
221196

222197
return filename, nil
223198
}
224199

225-
func parseAndSaveCollectorOutput(output string, bundlePath string) ([]string, error) {
226-
rootDirs := make(map[string]bool)
227-
200+
func parseAndSaveCollectorOutput(output string, bundlePath string) error {
228201
input := make(map[string]interface{})
229202
if err := json.Unmarshal([]byte(output), &input); err != nil {
230-
return nil, errors.Wrap(err, "unmarshal output")
203+
return errors.Wrap(err, "unmarshal output")
231204
}
232205

233206
for filename, maybeContents := range input {
234207
fileDir, fileName := filepath.Split(filename)
235208
outPath := filepath.Join(bundlePath, fileDir)
236-
rootDirs[outPath] = true
237209

238210
if err := os.MkdirAll(outPath, 0777); err != nil {
239-
return nil, errors.Wrap(err, "create output file")
211+
return errors.Wrap(err, "create output file")
240212
}
241213

242214
switch maybeContents.(type) {
243215
case string:
244216
decoded, err := base64.StdEncoding.DecodeString(maybeContents.(string))
245217
if err != nil {
246-
return nil, errors.Wrap(err, "decode collector output")
218+
return errors.Wrap(err, "decode collector output")
247219
}
248220

249221
if err := writeFile(filepath.Join(outPath, fileName), decoded); err != nil {
250-
return nil, errors.Wrap(err, "write collector output")
222+
return errors.Wrap(err, "write collector output")
251223
}
252224

253225
case map[string]interface{}:
254226
for k, v := range maybeContents.(map[string]interface{}) {
255227
s, _ := filepath.Split(filepath.Join(outPath, fileName, k))
256228
if err := os.MkdirAll(s, 0777); err != nil {
257-
return nil, errors.Wrap(err, "write output directories")
229+
return errors.Wrap(err, "write output directories")
258230
}
259231

260232
decoded, err := base64.StdEncoding.DecodeString(v.(string))
261233
if err != nil {
262-
return nil, errors.Wrap(err, "decode output")
234+
return errors.Wrap(err, "decode output")
263235
}
264236
if err := writeFile(filepath.Join(outPath, fileName, k), decoded); err != nil {
265-
return nil, errors.Wrap(err, "write output")
237+
return errors.Wrap(err, "write output")
266238
}
267239
}
268240
}
269241
}
270242

271-
dirs := make([]string, 0)
272-
for dir := range rootDirs {
273-
dirs = append(dirs, dir)
274-
}
275-
276-
return dirs, nil
243+
return nil
277244
}
278245

279246
func uploadSupportBundle(r *troubleshootv1beta1.ResultRequest, archivePath string) error {
@@ -339,3 +306,32 @@ func callbackSupportBundleAPI(r *troubleshootv1beta1.ResultRequest, archivePath
339306

340307
return nil
341308
}
309+
310+
func tarSupportBundleDir(inputDir, outputFilename string) error {
311+
tarGz := archiver.TarGz{
312+
Tar: &archiver.Tar{
313+
ImplicitTopLevelFolder: false,
314+
},
315+
}
316+
317+
paths := []string{
318+
filepath.Join(inputDir, VersionFilename), // version file should be first in tar archive for quick extraction
319+
}
320+
321+
topLevelFiles, err := ioutil.ReadDir(inputDir)
322+
if err != nil {
323+
return errors.Wrap(err, "list bundle directory contents")
324+
}
325+
for _, f := range topLevelFiles {
326+
if f.Name() == VersionFilename {
327+
continue
328+
}
329+
paths = append(paths, filepath.Join(inputDir, f.Name()))
330+
}
331+
332+
if err := tarGz.Archive(paths, outputFilename); err != nil {
333+
return errors.Wrap(err, "create archive")
334+
}
335+
336+
return nil
337+
}

cmd/troubleshoot/cli/version.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import (
77
"path/filepath"
88
)
99

10-
func writeVersionFile(path string) (string, error) {
10+
const VersionFilename = "version.yaml"
11+
12+
func writeVersionFile(path string) error {
1113
version := troubleshootv1beta1.SupportBundleVersion{
1214
ApiVersion: "troubleshoot.replicated.com/v1beta1",
1315
Kind: "SupportBundle",
1416
}
1517
b, err := yaml.Marshal(version)
1618
if err != nil {
17-
return "", err
19+
return err
1820
}
1921

20-
filename := filepath.Join(path, "version.yaml")
22+
filename := filepath.Join(path, VersionFilename)
2123
err = ioutil.WriteFile(filename, b, 0644)
2224
if err != nil {
23-
return "", err
25+
return err
2426
}
2527

26-
return filename, nil
28+
return nil
2729
}

pkg/apis/troubleshoot/v1beta1/collector_shared.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type LogLimits struct {
2525

2626
type Logs struct {
2727
CollectorMeta `json:",inline" yaml:",inline"`
28+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
2829
Selector []string `json:"selector" yaml:"selector"`
2930
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
3031
Containers []string `json:"containers,omitempty" yaml:"containers,omitempty"`
@@ -33,6 +34,7 @@ type Logs struct {
3334

3435
type Run struct {
3536
CollectorMeta `json:",inline" yaml:",inline"`
37+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
3638
Namespace string `json:"namespace" yaml:"namespace"`
3739
Image string `json:"image" yaml:"image"`
3840
Command []string `json:"command,omitempty" yaml:"command,omitempty"`
@@ -43,6 +45,7 @@ type Run struct {
4345

4446
type Exec struct {
4547
CollectorMeta `json:",inline" yaml:",inline"`
48+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
4649
Selector []string `json:"selector" yaml:"selector"`
4750
Namespace string `json:"namespace" yaml:"namespace"`
4851
ContainerName string `json:"containerName,omitempty" yaml:"containerName,omitempty"`
@@ -53,6 +56,7 @@ type Exec struct {
5356

5457
type Copy struct {
5558
CollectorMeta `json:",inline" yaml:",inline"`
59+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
5660
Selector []string `json:"selector" yaml:"selector"`
5761
Namespace string `json:"namespace" yaml:"namespace"`
5862
ContainerPath string `json:"containerPath" yaml:"containerPath"`
@@ -61,9 +65,10 @@ type Copy struct {
6165

6266
type HTTP struct {
6367
CollectorMeta `json:",inline" yaml:",inline"`
64-
Get *Get `json:"get,omitempty" yaml:"get,omitempty"`
65-
Post *Post `json:"post,omitempty" yaml:"post,omitempty"`
66-
Put *Put `json:"put,omitempty" yaml:"put,omitempty"`
68+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
69+
Get *Get `json:"get,omitempty" yaml:"get,omitempty"`
70+
Post *Post `json:"post,omitempty" yaml:"post,omitempty"`
71+
Put *Put `json:"put,omitempty" yaml:"put,omitempty"`
6772
}
6873

6974
type Get struct {

0 commit comments

Comments
 (0)