CLOUD-535 Add contexts to errors, small fixes

This commit is contained in:
Max Dudin
2020-06-19 16:22:24 +03:00
parent 1d6da2fdca
commit a65dbefbd9
2 changed files with 18 additions and 17 deletions

View File

@@ -4,12 +4,14 @@ import (
"archive/tar" "archive/tar"
"compress/gzip" "compress/gzip"
"os" "os"
"github.com/pkg/errors"
) )
func TarWrite(path string, data map[string][]byte) error { func TarWrite(path string, data map[string][]byte) error {
tarFile, err := os.Create(path + ".tar.gz") tarFile, err := os.Create(path + ".tar.gz")
if err != nil { if err != nil {
return err return errors.Wrap(err, "create tar file")
} }
defer tarFile.Close() defer tarFile.Close()
zr := gzip.NewWriter(tarFile) zr := gzip.NewWriter(tarFile)
@@ -23,10 +25,10 @@ func TarWrite(path string, data map[string][]byte) error {
Size: int64(len(content)), Size: int64(len(content)),
} }
if err := tw.WriteHeader(hdr); err != nil { if err := tw.WriteHeader(hdr); err != nil {
return err return errors.Wrap(err, "write header")
} }
if _, err := tw.Write(content); err != nil { if _, err := tw.Write(content); err != nil {
return err return errors.Wrap(err, "write content")
} }
} }
return nil return nil

View File

@@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os/exec" "os/exec"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
) )
@@ -52,12 +54,12 @@ type namespaces struct {
func (d *Dumper) DumpCluster() error { func (d *Dumper) DumpCluster() error {
output, err := d.runCmd("get", "namespaces", "-o", "json") output, err := d.runCmd("get", "namespaces", "-o", "json")
if err != nil { if err != nil {
return err return errors.Wrap(err, "get namespaces")
} }
var nss namespaces var nss namespaces
err = json.Unmarshal(output, &nss) err = json.Unmarshal(output, &nss)
if err != nil { if err != nil {
return err return errors.Wrap(err, "unmarshal namespaces")
} }
for _, ns := range nss.Items { for _, ns := range nss.Items {
@@ -68,7 +70,7 @@ func (d *Dumper) DumpCluster() error {
var pods k8sPods var pods k8sPods
err = json.Unmarshal(output, &pods) err = json.Unmarshal(output, &pods)
if err != nil { if err != nil {
return err log.Println(errors.Wrap(err, "unmarshal pods"))
} }
for _, pod := range pods.Items { for _, pod := range pods.Items {
@@ -80,21 +82,21 @@ func (d *Dumper) DumpCluster() error {
} }
for _, resource := range d.resources { for _, resource := range d.resources {
err = d.getAndWriteToFile(resource, ns.Name) err = d.getResource(resource, ns.Name)
if err != nil { if err != nil {
log.Println(err) log.Println(errors.Wrapf(err, "get %s resource", resource))
} }
} }
} }
err = d.getAndWriteToFile("nodes", "") err = d.getResource("nodes", "")
if err != nil { if err != nil {
log.Println(err) log.Println(errors.Wrapf(err, "get nodes"))
} }
err = d.writeErrorsToFile() err = d.writeErrorsToFile()
if err != nil { if err != nil {
log.Println(err) log.Println(errors.Wrap(err, "write errors"))
} }
return nil return nil
@@ -111,7 +113,7 @@ func (d *Dumper) runCmd(args ...string) ([]byte, error) {
d.saveCommandError(err.Error()+" "+outb.String(), args...) d.saveCommandError(err.Error()+" "+outb.String(), args...)
return outb.Bytes(), err return outb.Bytes(), err
} }
if len(errb.String()) > 0 { if errb.Len() > 0 {
d.saveCommandError(errb.String()+" "+outb.String(), args...) d.saveCommandError(errb.String()+" "+outb.String(), args...)
return outb.Bytes(), err return outb.Bytes(), err
} }
@@ -119,7 +121,7 @@ func (d *Dumper) runCmd(args ...string) ([]byte, error) {
return outb.Bytes(), nil return outb.Bytes(), nil
} }
func (d *Dumper) getAndWriteToFile(name, namespace string) error { func (d *Dumper) getResource(name, namespace string) error {
location := d.location location := d.location
args := []string{"get", name, "-o", "yaml"} args := []string{"get", name, "-o", "yaml"}
if len(namespace) > 0 { if len(namespace) > 0 {
@@ -137,10 +139,7 @@ func (d *Dumper) getAndWriteToFile(name, namespace string) error {
} }
func (d *Dumper) saveCommandError(err string, args ...string) { func (d *Dumper) saveCommandError(err string, args ...string) {
command := d.cmd command := d.cmd + " " + strings.Join(args, " ")
for _, arg := range args {
command += " " + arg
}
d.Errors[command] = err d.Errors[command] = err
} }