From 1d6da2fdca6c20f73750aa13d73c9ffc13048964 Mon Sep 17 00:00:00 2001 From: Max Dudin Date: Fri, 19 Jun 2020 15:53:55 +0300 Subject: [PATCH] CLOUD-535 Rework archive and dumper --- src/go/debug-collector/archive/archive.go | 63 +++++++---------------- src/go/debug-collector/dumper/dumper.go | 51 ++---------------- src/go/debug-collector/main.go | 4 +- 3 files changed, 25 insertions(+), 93 deletions(-) diff --git a/src/go/debug-collector/archive/archive.go b/src/go/debug-collector/archive/archive.go index 9470edbb..215da807 100644 --- a/src/go/debug-collector/archive/archive.go +++ b/src/go/debug-collector/archive/archive.go @@ -2,57 +2,32 @@ package archive import ( "archive/tar" - "bytes" "compress/gzip" - "io" "os" - "path/filepath" ) -func Create(src string) error { - var buf bytes.Buffer - - zr := gzip.NewWriter(&buf) - tw := tar.NewWriter(zr) - - filepath.Walk(src, func(file string, fi os.FileInfo, err error) error { - header, err := tar.FileInfoHeader(fi, file) - if err != nil { - return err - } - header.Name = filepath.ToSlash(file) - - err = tw.WriteHeader(header) - if err != nil { - return err - } - - if !fi.IsDir() { - data, err := os.Open(file) - if err != nil { - return err - } - if _, err := io.Copy(tw, data); err != nil { - return err - } - } - return nil - }) - - if err := tw.Close(); err != nil { - return err - } - if err := zr.Close(); err != nil { - return err - } - - file, err := os.OpenFile("./cluster-dump.tar.gzip", os.O_CREATE|os.O_RDWR, os.FileMode(777)) +func TarWrite(path string, data map[string][]byte) error { + tarFile, err := os.Create(path + ".tar.gz") if err != nil { return err } - if _, err := io.Copy(file, &buf); err != nil { - return err + defer tarFile.Close() + zr := gzip.NewWriter(tarFile) + tw := tar.NewWriter(zr) + defer zr.Close() + defer tw.Close() + for name, content := range data { + hdr := &tar.Header{ + Name: name, + Mode: 0600, + Size: int64(len(content)), + } + if err := tw.WriteHeader(hdr); err != nil { + return err + } + if _, err := tw.Write(content); err != nil { + return err + } } - return nil } diff --git a/src/go/debug-collector/dumper/dumper.go b/src/go/debug-collector/dumper/dumper.go index 2a57bc17..70ffa396 100644 --- a/src/go/debug-collector/dumper/dumper.go +++ b/src/go/debug-collector/dumper/dumper.go @@ -3,9 +3,7 @@ package dumper import ( "bytes" "encoding/json" - "errors" "log" - "os" "os/exec" corev1 "k8s.io/api/core/v1" @@ -17,6 +15,7 @@ type Dumper struct { resources []string location string Errors map[string]string + Files map[string][]byte } // New return new Dumper object @@ -37,6 +36,7 @@ func New(location string) Dumper { }, location: directory, Errors: make(map[string]string), + Files: make(map[string][]byte), } } @@ -61,11 +61,6 @@ func (d *Dumper) DumpCluster() error { } for _, ns := range nss.Items { - err = os.MkdirAll(d.location+"/"+ns.Name, 0755) - if err != nil { - return err - } - output, err = d.runCmd("get", "pods", "-o", "json", "--namespace", ns.Name) if err != nil { continue // runCmd already stored this error in Dumper.Errors @@ -81,20 +76,7 @@ func (d *Dumper) DumpCluster() error { if err != nil { continue // runCmd already stored this error in Dumper.Errors } - err = os.MkdirAll(d.location+"/"+ns.Name+"/"+pod.Name, 0755) - if err != nil { - return err - } - - f, err := os.Create(d.location + "/" + ns.Name + "/" + pod.Name + "/logs.txt") - if err != nil { - return err - } - _, err = f.Write(output) - if err != nil { - return err - } - + d.Files[d.location+"/"+ns.Name+"/"+pod.Name+"/logs.txt"] = output } for _, resource := range d.resources { @@ -149,14 +131,7 @@ func (d *Dumper) getAndWriteToFile(name, namespace string) error { return nil // runCmd already stored this error in Dumper.Errors } - f, err := os.Create(location + "/" + name + ".yaml") - if err != nil { - return err - } - _, err = f.Write(output) - if err != nil { - return err - } + d.Files[location+"/"+name+".yaml"] = output return nil } @@ -175,27 +150,11 @@ func (d *Dumper) writeErrorsToFile() error { for cmd, errS := range d.Errors { errStr += cmd + ": " + errS + "\n" } - f, err := os.Create(d.location + "/errors.txt") - if err != nil { - return err - } - _, err = f.WriteString(errStr) - if err != nil { - return err - } + d.Files[d.location+"/errors.txt"] = []byte(errStr) return nil } -// DeleteDumpDir delete Dumper.location -func (d *Dumper) DeleteDumpDir() error { - if d.location == "/" { - return errors.New("don't do this, please") // just for being sure - } - - return os.RemoveAll(d.location) -} - // GetLocation return Dumper.location func (d *Dumper) GetLocation() string { return d.location diff --git a/src/go/debug-collector/main.go b/src/go/debug-collector/main.go index 0d4ededb..5f5b75e8 100644 --- a/src/go/debug-collector/main.go +++ b/src/go/debug-collector/main.go @@ -21,9 +21,7 @@ func main() { os.Exit(1) } - archive.Create(d.GetLocation()) - - err = d.DeleteDumpDir() + err = archive.TarWrite(d.GetLocation(), d.Files) if err != nil { log.Println(err) os.Exit(1)