Skip to content

Commit

Permalink
Show partial results when fetching of api resources fails (#44)
Browse files Browse the repository at this point in the history
Show partial results when fetching of api resources fails
  • Loading branch information
corneliusweig authored Jan 10, 2020
2 parents 8fa7944 + 30c7f62 commit bce10e5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
17 changes: 9 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ func Execute() error {
func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&ketallOptions.CfgFile, "config", "", "config file (default is $HOME/.kube/ketall.yaml)")
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic)")

rootCmd.Flags().BoolVar(&ketallOptions.UseCache, constants.FlagUseCache, false, "use cached list of server resources")
rootCmd.Flags().StringVar(&ketallOptions.Scope, constants.FlagScope, "", "only resources with scope cluster|namespace")
rootCmd.Flags().StringVar(&ketallOptions.Since, constants.FlagSince, "", "only resources younger than given age")
rootCmd.Flags().StringVarP(&ketallOptions.Selector, constants.FlagSelector, "l", "", "selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
rootCmd.PersistentFlags().StringVar(&ketallOptions.CfgFile, "config", "", "Config file (default \"$HOME/.kube/ketall.yaml)\"")
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic).")

rootCmd.Flags().BoolVar(&ketallOptions.UseCache, constants.FlagUseCache, false, "Use cached list of server resources.")
rootCmd.Flags().BoolVar(&ketallOptions.AllowIncomplete, constants.FlagAllowIncomplete, true, "Show partial results when fetching of API resources fails.")
rootCmd.Flags().StringVar(&ketallOptions.Scope, constants.FlagScope, "", "Only resources with scope cluster|namespace.")
rootCmd.Flags().StringVar(&ketallOptions.Since, constants.FlagSince, "", "Only resources younger than given age.")
rootCmd.Flags().StringVarP(&ketallOptions.Selector, constants.FlagSelector, "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2).")
rootCmd.Flags().StringVar(&ketallOptions.FieldSelector, constants.FlagFieldSelector, "", "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The common field queries for all types are metadata.name and metadata.namespace.")
rootCmd.Flags().StringSliceVar(&ketallOptions.Exclusions, constants.FlagExclude, []string{"events"}, "filter by resource name (plural form or short name)")
rootCmd.Flags().StringSliceVar(&ketallOptions.Exclusions, constants.FlagExclude, []string{"events"}, "Filter by resource name (plural form or short name).")

ketallOptions.GenericCliFlags.AddFlags(rootCmd.Flags())
ketallOptions.PrintFlags.AddFlags(rootCmd)
Expand Down
1 change: 1 addition & 0 deletions doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kubectl get-all
- `--exclude` will filter out the given resources (either plural names `componentstatuses` or short form `cs`). Defaults to `events` because those are rarely useful.
- ...and many standard `kubectl` options. Have a look at `kubectl get-all --help` for a full list of supported flags.
- `--use-cache` will consider the http cache to determine the server resources to look at. Disabled by default.
- `--allow-incomplete` will show partial results when fetching the list of API resources fails. Enabled by default.
- `--verbosity` set the log level (one of debug, info, warn, error, fatal, panic).

**Hint**: If you do not have access to all resources, bulk fetching needs to be disabled. You can speed things up by explicitly excluding all resources which you may not access.
Expand Down
5 changes: 4 additions & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func fetchAvailableGroupResources(cache bool, scope string, flags *genericcliopt

resources, err := client.ServerPreferredResources()
if err != nil {
return nil, errors.Wrap(err, "get preferred resources")
if resources == nil || !viper.GetBool(constants.FlagAllowIncomplete) {
return nil, errors.Wrap(err, "get preferred resources")
}
logrus.Warnf("Could not fetch complete list of API resources, results will be incomplete: %s", err)
}

var grs []groupResource
Expand Down
15 changes: 8 additions & 7 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import "github.com/sirupsen/logrus"
const (
DefaultLogLevel = logrus.WarnLevel

FlagExclude = "exclude"
FlagNamespace = "namespace"
FlagScope = "only-scope"
FlagSince = "since"
FlagUseCache = "use-cache"
FlagSelector = "selector"
FlagFieldSelector = "field-selector"
FlagExclude = "exclude"
FlagNamespace = "namespace"
FlagScope = "only-scope"
FlagSince = "since"
FlagUseCache = "use-cache"
FlagAllowIncomplete = "allow-incomplete"
FlagSelector = "selector"
FlagFieldSelector = "field-selector"
)
1 change: 1 addition & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type KetallOptions struct {
GenericCliFlags *genericclioptions.ConfigFlags
PrintFlags KAPrintFlags
UseCache bool
AllowIncomplete bool
Scope string
Since string
Selector string
Expand Down

0 comments on commit bce10e5

Please sign in to comment.