diff --git a/cmd/troubleshoot/cli/receive.go b/cmd/troubleshoot/cli/receive.go index 270339db..79aae9a7 100644 --- a/cmd/troubleshoot/cli/receive.go +++ b/cmd/troubleshoot/cli/receive.go @@ -10,6 +10,7 @@ import ( "path/filepath" "github.com/mholt/archiver" + "github.com/pkg/errors" "github.com/replicatedhq/troubleshoot/pkg/logger" kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -131,7 +132,12 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string) paths = append(paths, filepath.Join(bundlePath, id)) } - if err := tarGz.Archive(paths, "support-bundle.tar.gz"); err != nil { + filename, err := findFileName("support-bundle", "tar.gz") + if err != nil { + return errors.Wrap(err, "find file name") + } + + if err := tarGz.Archive(paths, filename); err != nil { return err } return nil diff --git a/cmd/troubleshoot/cli/run_nocrd.go b/cmd/troubleshoot/cli/run_nocrd.go index bcc78daf..db26f52a 100644 --- a/cmd/troubleshoot/cli/run_nocrd.go +++ b/cmd/troubleshoot/cli/run_nocrd.go @@ -193,11 +193,16 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog paths = append(paths, collectorDir) } - if err := tarGz.Archive(paths, "support-bundle.tar.gz"); err != nil { + filename, err := findFileName("support-bundle", "tar.gz") + if err != nil { + return "", errors.Wrap(err, "find file name") + } + + if err := tarGz.Archive(paths, filename); err != nil { return "", errors.Wrap(err, "create archive") } - return "support-bundle.tar.gz", nil + return filename, nil } func parseAndSaveCollectorOutput(output string, bundlePath string) (string, error) { diff --git a/cmd/troubleshoot/cli/util.go b/cmd/troubleshoot/cli/util.go index b9a7b3b4..9f9519c0 100644 --- a/cmd/troubleshoot/cli/util.go +++ b/cmd/troubleshoot/cli/util.go @@ -1,9 +1,11 @@ package cli import ( + "fmt" "net/url" "os" + "github.com/pkg/errors" troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1" "github.com/spf13/viper" "k8s.io/client-go/tools/clientcmd" @@ -39,3 +41,19 @@ func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1bet return troubleshootClient, nil } + +func findFileName(basename, extension string) (string, error) { + n := 1 + name := basename + for { + filename := name + "." + extension + if _, err := os.Stat(filename); os.IsNotExist(err) { + return filename, nil + } else if err != nil { + return "", errors.Wrap(err, "check file exists") + } + + name = fmt.Sprintf("%s (%d)", basename, n) + n = n + 1 + } +}