Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
* Improve error messages
* Util function appending elements to a nil slice that allows adding
  specs to an empty slice of collectors/analysers/redactors
  • Loading branch information
banjoh committed Nov 24, 2023
1 parent cc24ec4 commit 840dca4
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/analyze/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func runAnalyzers(v *viper.Viper, bundlePath string) error {
} else {
if !util.IsURL(specPath) {
// TODO: Better error message when we do not have a file/url etc
return fmt.Errorf("%s is not a URL and was not found (err %s)", specPath, err)
return fmt.Errorf("%s is not a URL and was not found", specPath)
}

req, err := http.NewRequest("GET", specPath, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/collect/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func runCollect(v *viper.Viper, arg string) error {
collectorContent = b
} else {
if !util.IsURL(arg) {
return fmt.Errorf("%s is not a URL and was not found (err %s)", arg, err)
return fmt.Errorf("%s is not a URL and was not found", arg)
}

req, err := http.NewRequest("GET", arg, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/troubleshoot/cli/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func downloadAnalyzerSpec(specPath string) (string, error) {
specContent = string(b)
} else {
if !util.IsURL(specPath) {
return "", fmt.Errorf("%s is not a URL and was not found (err %s)", specPath, err)
return "", fmt.Errorf("%s is not a URL and was not found", specPath)
}

req, err := http.NewRequest("GET", specPath, nil)
Expand Down
24 changes: 14 additions & 10 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ the %s Admin Console to begin analysis.`
}

// loadSupportBundleSpecsFromURIs loads support bundle specs from URIs
func loadSupportBundleSpecsFromURIs(ctx context.Context, kinds *loader.TroubleshootKinds) (*loader.TroubleshootKinds, error) {
func loadSupportBundleSpecsFromURIs(ctx context.Context, kinds *loader.TroubleshootKinds) error {
remoteRawSpecs := []string{}
for _, s := range kinds.SupportBundlesV1Beta2 {
if s.Spec.Uri != "" && util.IsURL(s.Spec.Uri) {
Expand All @@ -252,12 +252,18 @@ func loadSupportBundleSpecsFromURIs(ctx context.Context, kinds *loader.Troublesh
}

if len(remoteRawSpecs) == 0 {
return kinds, nil
return nil
}

return loader.LoadSpecs(ctx, loader.LoadOptions{
moreKinds, err := loader.LoadSpecs(ctx, loader.LoadOptions{
RawSpecs: remoteRawSpecs,
})
if err != nil {
return err
}

kinds.Add(moreKinds)
return nil
}

func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface) (*troubleshootv1beta2.SupportBundle, *troubleshootv1beta2.Redactor, error) {
Expand All @@ -270,11 +276,9 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)

// Load additional specs from support bundle URIs
if !viper.GetBool("no-uri") {
moreKinds, err := loadSupportBundleSpecsFromURIs(ctx, kinds)
err := loadSupportBundleSpecsFromURIs(ctx, kinds)
if err != nil {
klog.Warningf("unable to load support bundles from URIs: %v", err)
} else {
kinds.Add(moreKinds)
}
}

Expand Down Expand Up @@ -304,14 +308,14 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
}

for _, c := range kinds.CollectorsV1Beta2 {
mainBundle.Spec.Collectors = append(mainBundle.Spec.Collectors, c.Spec.Collectors...)
mainBundle.Spec.Collectors = util.Append(mainBundle.Spec.Collectors, c.Spec.Collectors...)
}

for _, hc := range kinds.HostCollectorsV1Beta2 {
mainBundle.Spec.HostCollectors = append(mainBundle.Spec.HostCollectors, hc.Spec.Collectors...)
mainBundle.Spec.HostCollectors = util.Append(mainBundle.Spec.HostCollectors, hc.Spec.Collectors...)
}

if len(mainBundle.Spec.Collectors) > 0 {
if mainBundle.Spec.Collectors != nil {
// If we have in-cluster collectors, ensure cluster info and cluster resources
// collectors are in the merged spec. We need to add them here so when we --dry-run,
// these collectors are included. supportbundle.runCollectors duplicates this bit.
Expand All @@ -336,7 +340,7 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
},
}
for _, r := range kinds.RedactorsV1Beta2 {
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, r.Spec.Redactors...)
additionalRedactors.Spec.Redactors = util.Append(additionalRedactors.Spec.Redactors, r.Spec.Redactors...)
}

return mainBundle, additionalRedactors, nil
Expand Down
18 changes: 13 additions & 5 deletions cmd/troubleshoot/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -48,11 +49,12 @@ spec:
require.NoError(t, err)
require.NotNil(t, kinds)

moreKinds, err := loadSupportBundleSpecsFromURIs(ctx, kinds)
assert.Len(t, kinds.SupportBundlesV1Beta2, 0)
err = loadSupportBundleSpecsFromURIs(ctx, kinds)
require.NoError(t, err)

require.Len(t, moreKinds.SupportBundlesV1Beta2, 1)
assert.NotNil(t, moreKinds.SupportBundlesV1Beta2[0].Spec.Collectors[0].ClusterInfo)
require.Len(t, kinds.SupportBundlesV1Beta2, 1)
assert.NotNil(t, kinds.SupportBundlesV1Beta2[0].Spec.Collectors[0].ClusterInfo)
}

func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
Expand All @@ -76,8 +78,14 @@ func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
httputil.GetHttpClient().Timeout = before
}()

kindsAfter, err := loadSupportBundleSpecsFromURIs(ctx, kinds)
beforeJSON, err := json.Marshal(kinds)
require.NoError(t, err)

assert.Equal(t, kinds, kindsAfter)
err = loadSupportBundleSpecsFromURIs(ctx, kinds)
require.NoError(t, err)

afterJSON, err := json.Marshal(kinds)
require.NoError(t, err)

assert.JSONEq(t, string(beforeJSON), string(afterJSON))
}
2 changes: 1 addition & 1 deletion internal/specs/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
rawSpecs = append(rawSpecs, content...)
} else {
if !util.IsURL(v) {
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, fmt.Errorf("%s is not a URL and was not found (err %s)", v, err))
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, fmt.Errorf("%s is not a URL and was not found", v))
}

// Download preflight specs
Expand Down
10 changes: 10 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ func EstimateNumberOfLines(text string) int {
}
return n
}

// Append appends the elements to the slice.
// If the slice is nil, a new slice is created
// as opposed to the standard library's append
func Append[T any](slice []T, v ...T) []T {
if slice == nil {
slice = []T{}
}
return append(slice, v...)
}
4 changes: 4 additions & 0 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (l *specLoader) loadFromStrings(rawSpecs ...string) (*TroubleshootKinds, er
// For secrets and configmaps, extract support bundle, redactor or preflight specs
// For troubleshoot kinds, pass them through
for _, rawDoc := range multiRawDocs {
if rawDoc == "" {
continue
}

var parsed parsedDoc

err := yaml.Unmarshal([]byte(rawDoc), &parsed)
Expand Down
2 changes: 1 addition & 1 deletion pkg/supportbundle/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func loadSpec(arg string) ([]byte, error) {
}

if !util.IsURL(arg) {
return nil, fmt.Errorf("%s is not a URL and was not found (err %s)", arg, err)
return nil, fmt.Errorf("%s is not a URL and was not found", arg)
}

spec, err := loadSpecFromURL(arg)
Expand Down
5 changes: 4 additions & 1 deletion pkg/supportbundle/supportbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func CollectSupportBundleFromSpec(
} else if hostFiles != nil {
result = hostFiles
} else {
return nil, errors.Wrap(err, "failed to generate support bundle")
if len(collectorsErrs) > 0 {
return nil, fmt.Errorf("failed to generate support bundle: %s", strings.Join(collectorsErrs, "\n"))
}
return nil, fmt.Errorf("failed to generate support bundle")
}

version, err := getVersionFile()
Expand Down

0 comments on commit 840dca4

Please sign in to comment.