Skip to content

Commit 0113624

Browse files
authored
chore(support-bundle): respect using load-cluster-specs=false (#1634)
* fix: Allow using load-cluster-specs=false Signed-off-by: Evans Mungai <[email protected]> * Some more simplification Signed-off-by: Evans Mungai <[email protected]> * Ensure error in loading specs is printed in CLI Signed-off-by: Evans Mungai <[email protected]> * Run linter Signed-off-by: Evans Mungai <[email protected]> * Fix failing tests Signed-off-by: Evans Mungai <[email protected]> * Remove unnecessary test case rename Signed-off-by: Evans Mungai <[email protected]> * Fix error wrapping Signed-off-by: Evans Mungai <[email protected]> * Check if load-cluster-specs was provided in cli Signed-off-by: Evans Mungai <[email protected]> * Better wording in comments Signed-off-by: Evans Mungai <[email protected]> --------- Signed-off-by: Evans Mungai <[email protected]>
1 parent 52efd16 commit 0113624

File tree

5 files changed

+42
-35
lines changed

5 files changed

+42
-35
lines changed

cmd/troubleshoot/cli/root.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ If no arguments are provided, specs are automatically loaded from the cluster by
4444
RunE: func(cmd *cobra.Command, args []string) error {
4545
v := viper.GetViper()
4646

47+
// If there are no locations to load specs from passed in the cli args, we should
48+
// load them from the cluster by setting "load-cluster-specs=true". If the caller
49+
// provided "--load-cluster-specs" cli option, we should respect it.
50+
if len(args) == 0 {
51+
// Check if --load-cluster-specs was set by the cli caller to avoid overriding it
52+
flg := cmd.Flags().Lookup("load-cluster-specs")
53+
if flg != nil && !flg.Changed {
54+
// Load specs from the cluster if no spec(s) is(are) provided in the cli args
55+
v.Set("load-cluster-specs", true)
56+
}
57+
}
58+
4759
closer, err := traces.ConfigureTracing("support-bundle")
4860
if err != nil {
4961
// Do not fail running support-bundle if tracing fails
@@ -77,7 +89,7 @@ If no arguments are provided, specs are automatically loaded from the cluster by
7789
cmd.Flags().Bool("interactive", true, "enable/disable interactive mode")
7890
cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions")
7991
cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.sh/kind=support-bundle"}, "selector to filter on for loading additional support bundle specs found in secrets within the cluster")
80-
cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional troubleshoot specs found within the cluster. This is the default behavior if no spec is provided as an argument")
92+
cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional troubleshoot specs found within the cluster. Do not load by default unless no specs are provided in the cli args")
8193
cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
8294
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
8395
cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle")

cmd/troubleshoot/cli/run.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,24 +292,9 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
292292
err error
293293
)
294294

295-
if len(args) < 1 {
296-
fmt.Println("\r\033[36mNo specs provided, attempting to load from cluster...\033[m")
297-
kinds, err = specs.LoadFromCluster(ctx, client, vp.GetStringSlice("selector"), vp.GetString("namespace"))
298-
if err != nil {
299-
return nil, nil, errors.Wrap(err, "failed to load specs from cluster, and no specs were provided as arguments")
300-
}
301-
if len(redactors) > 0 {
302-
additionalKinds, err := specs.LoadFromCLIArgs(ctx, client, allArgs, vp)
303-
if err != nil {
304-
return nil, nil, errors.Wrap(err, "failed to load redactors from CLI args")
305-
}
306-
kinds.RedactorsV1Beta2 = append(kinds.RedactorsV1Beta2, additionalKinds.RedactorsV1Beta2...)
307-
}
308-
} else {
309-
kinds, err = specs.LoadFromCLIArgs(ctx, client, allArgs, vp)
310-
if err != nil {
311-
return nil, nil, errors.Wrap(err, "failed to load specs from CLI args")
312-
}
295+
kinds, err = specs.LoadFromCLIArgs(ctx, client, allArgs, vp)
296+
if err != nil {
297+
return nil, nil, errors.Wrap(err, "failed to load specs from CLI args")
313298
}
314299

315300
// Load additional specs from support bundle URIs
@@ -326,7 +311,10 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
326311
if len(kinds.CollectorsV1Beta2) == 0 &&
327312
len(kinds.HostCollectorsV1Beta2) == 0 &&
328313
len(kinds.SupportBundlesV1Beta2) == 0 {
329-
return nil, nil, types.NewExitCodeError(constants.EXIT_CODE_CATCH_ALL, errors.Wrap(err, "no collectors specified to run. Use --debug and/or -v=2 to see more information"))
314+
return nil, nil, types.NewExitCodeError(
315+
constants.EXIT_CODE_CATCH_ALL,
316+
errors.New("no collectors specified to run. Use --debug and/or -v=2 to see more information"),
317+
)
330318
}
331319

332320
// Merge specs

internal/specs/specs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ func downloadFromHttpURL(ctx context.Context, url string, headers map[string]str
268268
// to list & read secrets and configmaps from all namespaces, we will fallback to trying each
269269
// namespace individually, and eventually default to the configured kubeconfig namespace.
270270
func LoadFromCluster(ctx context.Context, client kubernetes.Interface, selectors []string, ns string) (*loader.TroubleshootKinds, error) {
271+
klog.V(1).Infof("Load troubleshoot specs from the cluster using selectors: %v", selectors)
272+
271273
if reflect.DeepEqual(selectors, []string{"troubleshoot.sh/kind=support-bundle"}) {
272274
// Its the default selector so we append troubleshoot.io/kind=support-bundle to it due to backwards compatibility
273275
selectors = append(selectors, "troubleshoot.io/kind=support-bundle")

pkg/loader/loader.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ type TroubleshootKinds struct {
8383
}
8484

8585
func (kinds *TroubleshootKinds) IsEmpty() bool {
86-
return len(kinds.AnalyzersV1Beta2) == 0 &&
87-
len(kinds.CollectorsV1Beta2) == 0 &&
88-
len(kinds.HostCollectorsV1Beta2) == 0 &&
89-
len(kinds.HostPreflightsV1Beta2) == 0 &&
90-
len(kinds.PreflightsV1Beta2) == 0 &&
91-
len(kinds.RedactorsV1Beta2) == 0 &&
92-
len(kinds.RemoteCollectorsV1Beta2) == 0 &&
93-
len(kinds.SupportBundlesV1Beta2) == 0
86+
return kinds.Len() == 0
87+
}
88+
89+
func (kinds *TroubleshootKinds) Len() int {
90+
return len(kinds.AnalyzersV1Beta2) +
91+
len(kinds.CollectorsV1Beta2) +
92+
len(kinds.HostCollectorsV1Beta2) +
93+
len(kinds.HostPreflightsV1Beta2) +
94+
len(kinds.PreflightsV1Beta2) +
95+
len(kinds.RedactorsV1Beta2) +
96+
len(kinds.RemoteCollectorsV1Beta2) +
97+
len(kinds.SupportBundlesV1Beta2)
9498
}
9599

96100
func (kinds *TroubleshootKinds) Add(other *TroubleshootKinds) {
@@ -200,7 +204,7 @@ func (l *specLoader) loadFromStrings(rawSpecs ...string) (*TroubleshootKinds, er
200204
// If it's not a configmap or secret, just append it to the splitdocs
201205
splitdocs = append(splitdocs, rawDoc)
202206
} else {
203-
klog.V(1).Infof("Skip loading %q kind", parsed.Kind)
207+
klog.V(2).Infof("Skip loading %q kind", parsed.Kind)
204208
}
205209
}
206210

@@ -254,11 +258,7 @@ func (l *specLoader) loadFromSplitDocs(splitdocs []string) (*TroubleshootKinds,
254258
}
255259
}
256260

257-
if kinds.IsEmpty() {
258-
klog.V(1).Info("No troubleshoot specs were loaded")
259-
} else {
260-
klog.V(1).Info("Loaded troubleshoot specs successfully")
261-
}
261+
klog.V(2).Infof("Loaded %d troubleshoot specs successfully", kinds.Len())
262262

263263
return kinds, nil
264264
}

pkg/types/types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type ExitError interface {
2020
type ExitCodeError struct {
2121
Msg string
2222
Code int
23+
Err error
2324
}
2425

2526
type ExitCodeWarning struct {
@@ -30,6 +31,10 @@ func (e *ExitCodeError) Error() string {
3031
return e.Msg
3132
}
3233

34+
func (e *ExitCodeError) Unwrap() error {
35+
return e.Err
36+
}
37+
3338
func (e *ExitCodeError) ExitStatus() int {
3439
return e.Code
3540
}
@@ -39,7 +44,7 @@ func NewExitCodeError(exitCode int, theErr error) *ExitCodeError {
3944
if theErr != nil {
4045
useErr = theErr.Error()
4146
}
42-
return &ExitCodeError{Msg: useErr, Code: exitCode}
47+
return &ExitCodeError{Msg: useErr, Code: exitCode, Err: theErr}
4348
}
4449

4550
func NewExitCodeWarning(theErrMsg string) *ExitCodeWarning {

0 commit comments

Comments
 (0)