From 0071e25992fc86c3882c2066873a2b04b43fe476 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Sat, 15 Apr 2023 12:26:51 +0100 Subject: [PATCH 1/2] feat: envs to initialise server Signed-off-by: Alex Jones --- cmd/root.go | 4 +++- cmd/serve/serve.go | 32 +++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b76f1f4..8aa5780 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,10 +1,11 @@ package cmd import ( - "github.com/k8sgpt-ai/k8sgpt/cmd/serve" "os" "path/filepath" + "github.com/k8sgpt-ai/k8sgpt/cmd/serve" + "github.com/k8sgpt-ai/k8sgpt/cmd/analyze" "github.com/k8sgpt-ai/k8sgpt/cmd/auth" "github.com/k8sgpt-ai/k8sgpt/cmd/filters" @@ -81,6 +82,7 @@ func initConfig() { viper.Set("kubecontext", kubecontext) viper.Set("kubeconfig", kubeconfig) + viper.SetEnvPrefix("K8SGPT") viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index a57091b..e84984d 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -28,17 +28,31 @@ var ServeCmd = &cobra.Command{ color.Red("Error: %v", err) os.Exit(1) } - + var aiProvider *ai.AIProvider if len(configAI.Providers) == 0 { - color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") - os.Exit(1) + // Check for env injection + backend = os.Getenv("K8SGPT_BACKEND") + password := os.Getenv("K8SGPT_PASSWORD") + model := os.Getenv("K8SGPT_MODEL") + // If the envs are set, alocate in place to the aiProvider + // else exit with error + if backend != "" || password != "" || model != "" { + aiProvider = &ai.AIProvider{ + Name: backend, + Password: password, + Model: model, + } + } else { + color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") + os.Exit(1) + } } - - var aiProvider ai.AIProvider - for _, provider := range configAI.Providers { - if backend == provider.Name { - aiProvider = provider - break + if aiProvider == nil { + for _, provider := range configAI.Providers { + if backend == provider.Name { + aiProvider = &provider + break + } } } From 960ba568d0dcc2ace722dc5c9b7c846366a98070 Mon Sep 17 00:00:00 2001 From: Matthis Holleville Date: Sat, 15 Apr 2023 15:49:39 +0200 Subject: [PATCH 2/2] fix: resolve issue with duplicated integration filters. Signed-off-by: Matthis Holleville --- pkg/integration/integration.go | 40 ++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/pkg/integration/integration.go b/pkg/integration/integration.go index 0f7fe00..fc1b0d4 100644 --- a/pkg/integration/integration.go +++ b/pkg/integration/integration.go @@ -3,10 +3,12 @@ package integration import ( "errors" "os" + "strings" "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/common" "github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy" + "github.com/k8sgpt-ai/k8sgpt/pkg/util" "github.com/spf13/viper" ) @@ -56,16 +58,24 @@ func (*Integration) Activate(name string, namespace string) error { return errors.New("integration not found") } - if err := integrations[name].Deploy(namespace); err != nil { - return err - } - // Update filters activeFilters := viper.GetStringSlice("active_filters") - activeFilters = append(activeFilters, integrations[name].GetAnalyzerName()) + mergedFilters := append(activeFilters, integrations[name].GetAnalyzerName()) - viper.Set("active_filters", activeFilters) + uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(mergedFilters) + + // Verify dupplicate + if len(dupplicatedFilters) != 0 { + color.Red("Integration already activated : %s", strings.Join(dupplicatedFilters, ", ")) + os.Exit(1) + } + + viper.Set("active_filters", uniqueFilters) + + if err := integrations[name].Deploy(namespace); err != nil { + return err + } if err := viper.WriteConfig(); err != nil { color.Red("Error writing config file: %s", err.Error()) @@ -80,21 +90,27 @@ func (*Integration) Deactivate(name string, namespace string) error { return errors.New("integration not found") } - if err := integrations[name].UnDeploy(namespace); err != nil { - return err - } + activeFilters := viper.GetStringSlice("active_filters") // Update filters // This might be a bad idea, but we cannot reference analyzer here - activeFilters := viper.GetStringSlice("active_filters") - - // Remove filter + foundFilter := false for i, v := range activeFilters { if v == integrations[name].GetAnalyzerName() { + foundFilter = true activeFilters = append(activeFilters[:i], activeFilters[i+1:]...) break } } + if !foundFilter { + color.Red("Ingregation %s does not exist in configuration file. Please use k8sgpt integration add.", name) + os.Exit(1) + } + + if err := integrations[name].UnDeploy(namespace); err != nil { + return err + } + viper.Set("active_filters", activeFilters) if err := viper.WriteConfig(); err != nil {