diff --git a/README.md b/README.md index a648b5edc2..de9c76350f 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ If you install gcc as suggested, the problem will persist. Therefore, you need t * Currently the default AI provider is OpenAI, you will need to generate an API key from [OpenAI](https://openai.com) * You can do this by running `k8sgpt generate` to open a browser link to generate it * Run `k8sgpt auth` to set it in k8sgpt. +* Run `k8sgpt filters` to manage filters. * Run `k8sgpt analyze` to run a scan. * And use `k8sgpt analyze --explain` to get a more detailed explanation of the issues. @@ -76,6 +77,7 @@ Available Commands: analyze This command will find problems within your Kubernetes cluster auth Authenticate with your chosen backend completion Generate the autocompletion script for the specified shell + filters Manage filters for analyzing Kubernetes resources generate Generate Key for your chosen backend (opens browser) help Help about any command version Print the version number of k8sgpt @@ -98,6 +100,12 @@ k8sgpt auth k8sgpt analyze --explain ``` +_List filters_ + +``` +k8sgpt filters list +``` + _Filter on resource_ ``` diff --git a/cmd/filters/filters.go b/cmd/filters/filters.go new file mode 100644 index 0000000000..f2146ffff4 --- /dev/null +++ b/cmd/filters/filters.go @@ -0,0 +1,23 @@ +package filters + +import ( + "github.com/spf13/cobra" +) + +var FiltersCmd = &cobra.Command{ + Use: "filters", + Aliases: []string{"filters"}, + Short: "Manage filters for analyzing Kubernetes resources", + Long: `The filters command allows you to manage filters that are used to analyze Kubernetes resources. + You can list available filters to analyze resources.`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + cmd.Help() + return + } + }, +} + +func init() { + FiltersCmd.AddCommand(filterListCmd) +} diff --git a/cmd/filters/filtersList.go b/cmd/filters/filtersList.go new file mode 100644 index 0000000000..3f230b522c --- /dev/null +++ b/cmd/filters/filtersList.go @@ -0,0 +1,21 @@ +package filters + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/k8sgpt-ai/k8sgpt/pkg/analyzer" + "github.com/spf13/cobra" +) + +var filterListCmd = &cobra.Command{ + Use: "list", + Short: "List available filters", + Long: `The list command displays a list of available filters that can be used to analyze Kubernetes resources.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Available filters : \n") + for _, analyzer := range analyzer.ListFilters() { + fmt.Printf("> %s\n", color.GreenString(analyzer)) + } + }, +} diff --git a/cmd/root.go b/cmd/root.go index e4030c4c30..aa1c75ef9a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" + "github.com/k8sgpt-ai/k8sgpt/cmd/filters" "github.com/k8sgpt-ai/k8sgpt/cmd/generate" "k8s.io/client-go/util/homedir" @@ -51,6 +52,7 @@ func init() { } rootCmd.AddCommand(auth.AuthCmd) rootCmd.AddCommand(analyze.AnalyzeCmd) + rootCmd.AddCommand(filters.FiltersCmd) rootCmd.AddCommand(generate.GenerateCmd) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)") rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.") diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 89bfaed065..e5a7d954d9 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -81,3 +81,11 @@ func ParseViaAI(ctx context.Context, config *AnalysisConfiguration, } return response, nil } + +func ListFilters() []string { + keys := make([]string, 0, len(analyzerMap)) + for k := range analyzerMap { + keys = append(keys, k) + } + return keys +}