Skip to content

Commit

Permalink
feat: add warning for files with '-errors'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrabovcin committed Apr 3, 2023
1 parent f9fe802 commit 86b01f4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
15 changes: 7 additions & 8 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func (bundle) Layout() Layout {
// bundle from a directory or a `tar.gz` archive, which is automatically extracted
// to a temporary folder.
func New(path string) (Bundle, error) {
var fs afero.Fs

switch {
case strings.HasSuffix(path, ".tar.gz"):
fi, err := os.Stat(path)
Expand Down Expand Up @@ -79,7 +77,7 @@ func New(path string) (Bundle, error) {
return nil, fmt.Errorf("more than 1 directory in archive, cannot infer bundle directory")
}

fs = fromDir(filepath.Join(tmpDir, entries[0].Name()))
return FromFs(fromDir(filepath.Join(tmpDir, entries[0].Name()))), nil
default:
absPath, err := filepath.Abs(path)
if err != nil {
Expand All @@ -95,14 +93,15 @@ func New(path string) (Bundle, error) {
break
}

fs = fromDir(absPath)
return FromFs(fromDir(absPath)), nil
}

if fs == nil {
return nil, ErrUnknownBundleFormat
}
return nil, ErrUnknownBundleFormat
}

return bundle{fs}, nil
// FromFs allows to create bundle form provided afero.Fs.
func FromFs(fs afero.Fs) Bundle {
return bundle{fs}
}

func unarchiveToDirectory(archive, destDir string) error {
Expand Down
30 changes: 30 additions & 0 deletions pkg/cli/warn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cli

import (
"fmt"
"path/filepath"
"strings"

"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/afero"

"github.com/mhrabovcin/troubleshoot-live/pkg/bundle"
)

// WarnOnErrorsFilePresence is a helper function that will issue a CLI warn
// message if for given file from bundle exists a file with `-errors` suffix.
// This is `troubleshoot`s way of recording errors in the bundle.
// E.g. `cluster-resources/pods.json` => `cluster-resources/pods-errors.json`.
func WarnOnErrorsFilePresence(b bundle.Bundle, out output.Output, path string) {
ext := filepath.Ext(path)
base := strings.TrimSuffix(filepath.Base(path), ext)
baseDir := filepath.Dir(path)
errorsPath := filepath.Join(baseDir, fmt.Sprintf("%s-errors%s", base, ext))

if ok, _ := afero.Exists(b, errorsPath); ok {
out.Warnf(
"The file %q suggests that there were some error when collecting data for %q. The import may not be complete.",
errorsPath, path,
)
}
}
46 changes: 46 additions & 0 deletions pkg/cli/warn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"bytes"
"testing"

"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/mhrabovcin/troubleshoot-live/pkg/bundle"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWarnOnErrorsFilePresence(t *testing.T) {
stdOut, stdErr, out := newOutput(0)
assert.Empty(t, stdOut.String())
assert.Empty(t, stdErr.String())
fs, err := newMemFs(map[string][]byte{
"cluster-resources/pods.json": nil,
"cluster-resources/pods-errors.json": nil,
})
require.NoError(t, err)
b := bundle.FromFs(fs)

WarnOnErrorsFilePresence(b, out, "cluster-resources/pods.json")
assert.Empty(t, stdOut.String())
expectedErr := `WRN The file "cluster-resources/pods-errors.json" suggests that there were ` +
`some error when collecting data for "cluster-resources/pods.json"`
assert.Contains(t, stdErr.String(), expectedErr)
}

func newOutput(verbose int) (*bytes.Buffer, *bytes.Buffer, output.Output) {
stdOut := &bytes.Buffer{}
stdErr := &bytes.Buffer{}
return stdOut, stdErr, output.NewNonInteractiveShell(stdOut, stdErr, verbose)
}

func newMemFs(files map[string][]byte) (afero.Fs, error) {
fs := afero.NewMemMapFs()
for path, data := range files {
if err := afero.WriteReader(fs, path, bytes.NewReader(data)); err != nil {
return nil, err
}
}
return fs, nil
}
5 changes: 5 additions & 0 deletions pkg/importer/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/mhrabovcin/troubleshoot-live/pkg/bundle"
"github.com/mhrabovcin/troubleshoot-live/pkg/cli"
)

func loadCRDs(b bundle.Bundle) (*unstructured.UnstructuredList, error) {
Expand Down Expand Up @@ -68,6 +69,10 @@ func importCRDs(
) error {
list, err := loadCRDs(cfg.bundle)
if err != nil {
cli.WarnOnErrorsFilePresence(
cfg.bundle, cfg.out,
filepath.Join(cfg.bundle.Layout().ClusterResources(), "custom-resource-definitions.json"),
)
return err
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/importer/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/utils/strings/slices"

"github.com/mhrabovcin/troubleshoot-live/pkg/bundle"
"github.com/mhrabovcin/troubleshoot-live/pkg/cli"
"github.com/mhrabovcin/troubleshoot-live/pkg/utils"
)

Expand Down Expand Up @@ -79,11 +80,10 @@ func importNamespaces(
ctx context.Context,
cfg *importerConfig,
) error {
list, err := bundle.LoadResourcesFromFile(
cfg.bundle,
filepath.Join(cfg.bundle.Layout().ClusterResources(), "namespaces.json"),
)
namespacesPath := filepath.Join(cfg.bundle.Layout().ClusterResources(), "namespaces.json")
list, err := bundle.LoadResourcesFromFile(cfg.bundle, namespacesPath)
if err != nil {
cli.WarnOnErrorsFilePresence(cfg.bundle, cfg.out, namespacesPath)
return err
}

Expand Down Expand Up @@ -151,6 +151,7 @@ func importClusterResources(

list, err := bundle.LoadResourcesFromFile(cfg.bundle, path)
if err != nil {
cli.WarnOnErrorsFilePresence(cfg.bundle, cfg.out, path)
cfg.out.Errorf(utils.MaxErrorString(err, 200), "Failed to load resources from file %q", path)
return nil
}
Expand Down

0 comments on commit 86b01f4

Please sign in to comment.