From bb2db5ca7923e2049308d1674bb59ae8154e415c Mon Sep 17 00:00:00 2001 From: Thomas Schuetz <38893055+thschue@users.noreply.github.com> Date: Mon, 27 Mar 2023 21:00:28 +0200 Subject: [PATCH] feat: add generation of api-keys to cli (#87) * feat: add generate option in cli and the documentation Signed-off-by: Thomas Schuetz * chore: release 0.1.0 Release-As: 0.1.0 Signed-off-by: Thomas Schuetz * fix: apply suggestions from code review Co-authored-by: Alex Jones Signed-off-by: Thomas Schuetz --------- Signed-off-by: Thomas Schuetz Co-authored-by: Alex Jones --- README.md | 24 +++++++++++++--- cmd/generate/generate.go | 62 ++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 cmd/generate/generate.go diff --git a/README.md b/README.md index 3a5c453f4e..2fccf352e8 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,22 @@ Text changing depending on mode. Light: 'So light!' Dark: 'So dark!' -_Install it now_ +`k8sgpt` is a tool for scanning your kubernetes clusters, diagnosing and triaging issues in simple english. + +It has SRE experience codified into it's analyzers and helps to pull out the most relevent information to enrich it with AI. + +## Quick Start ``` brew tap k8sgpt-ai/k8sgpt brew install k8sgpt ``` -`k8sgpt` is a tool for scanning your kubernetes clusters, diagnosing and triaging issues in simple english. - -It has SRE experience codified into it's analyzers and helps to pull out the most relevent information to enrich it with AI. +* 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 analyze` to run a scan. +* And use `k8sgpt analyze --explain` to get a more detailed explanation of the issues. @@ -40,13 +46,23 @@ 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 + generate Generate Key for your chosen backend (opens browser) help Help about any command +Flags: + --config string config file (default is $HOME/.k8sgpt.git.yaml) + -h, --help help for k8sgpt + --kubeconfig string Path to a kubeconfig. Only required if out-of-cluster. + --master string The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster. + -t, --toggle Help message for toggle + +Use "k8sgpt [command] --help" for more information about a command. ``` _Run a scan with the default analyzers_ ``` +k8sgpt generate k8sgpt auth k8sgpt analyze --explain ``` diff --git a/cmd/generate/generate.go b/cmd/generate/generate.go new file mode 100644 index 0000000000..9164387fdb --- /dev/null +++ b/cmd/generate/generate.go @@ -0,0 +1,62 @@ +package generate + +import ( + "fmt" + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os/exec" + "runtime" + "time" +) + +var ( + backend string +) + +// generateCmd represents the auth command +var GenerateCmd = &cobra.Command{ + Use: "generate", + Short: "Generate Key for your chosen backend (opens browser)", + Long: `Opens your browser to generate a key for your chosen backend.`, + Run: func(cmd *cobra.Command, args []string) { + + backendType := viper.GetString("backend_type") + if backendType == "" { + // Set the default backend + backend = "openai" + } + // override the default backend if a flag is provided + if backend != "" { + backendType = backend + } + fmt.Println("") + color.Green("Opening: https://beta.openai.com/account/api-keys to generate a key for %s", backendType) + color.Green("Please copy the generated key and run `k8sgpt auth` to add it to your config file") + fmt.Println("") + time.Sleep(5 * time.Second) + openbrowser("https://beta.openai.com/account/api-keys") + }, +} + +func init() { + // add flag for backend + GenerateCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") +} + +func openbrowser(url string) { + var err error + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + fmt.Println(err) + } +} diff --git a/cmd/root.go b/cmd/root.go index a359a88b01..eae1a03f0d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/k8sgpt-ai/k8sgpt/cmd/generate" "os" "github.com/fatih/color" @@ -44,6 +45,7 @@ func init() { // will be global for your application. rootCmd.AddCommand(auth.AuthCmd) rootCmd.AddCommand(analyze.AnalyzeCmd) + rootCmd.AddCommand(generate.GenerateCmd) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.git.yaml)") rootCmd.PersistentFlags().StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")