Merge branch 'main' into feat/manifests

This commit is contained in:
Alex Jones
2023-04-15 16:59:13 +01:00
committed by GitHub
3 changed files with 54 additions and 22 deletions

View File

@@ -1,10 +1,11 @@
package cmd package cmd
import ( import (
"github.com/k8sgpt-ai/k8sgpt/cmd/serve"
"os" "os"
"path/filepath" "path/filepath"
"github.com/k8sgpt-ai/k8sgpt/cmd/serve"
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze" "github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
"github.com/k8sgpt-ai/k8sgpt/cmd/auth" "github.com/k8sgpt-ai/k8sgpt/cmd/auth"
"github.com/k8sgpt-ai/k8sgpt/cmd/filters" "github.com/k8sgpt-ai/k8sgpt/cmd/filters"
@@ -81,6 +82,7 @@ func initConfig() {
viper.Set("kubecontext", kubecontext) viper.Set("kubecontext", kubecontext)
viper.Set("kubeconfig", kubeconfig) viper.Set("kubeconfig", kubeconfig)
viper.SetEnvPrefix("K8SGPT")
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in. // If a config file is found, read it in.

View File

@@ -28,19 +28,33 @@ var ServeCmd = &cobra.Command{
color.Red("Error: %v", err) color.Red("Error: %v", err)
os.Exit(1) os.Exit(1)
} }
var aiProvider *ai.AIProvider
if len(configAI.Providers) == 0 { if len(configAI.Providers) == 0 {
// 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") color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth")
os.Exit(1) os.Exit(1)
} }
}
var aiProvider ai.AIProvider if aiProvider == nil {
for _, provider := range configAI.Providers { for _, provider := range configAI.Providers {
if backend == provider.Name { if backend == provider.Name {
aiProvider = provider aiProvider = &provider
break break
} }
} }
}
if aiProvider.Name == "" { if aiProvider.Name == "" {
color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", backend) color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", backend)

View File

@@ -3,10 +3,12 @@ package integration
import ( import (
"errors" "errors"
"os" "os"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common" "github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy" "github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@@ -56,16 +58,24 @@ func (*Integration) Activate(name string, namespace string) error {
return errors.New("integration not found") return errors.New("integration not found")
} }
if err := integrations[name].Deploy(namespace); err != nil {
return err
}
// Update filters // Update filters
activeFilters := viper.GetStringSlice("active_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 { if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error()) 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") return errors.New("integration not found")
} }
if err := integrations[name].UnDeploy(namespace); err != nil { activeFilters := viper.GetStringSlice("active_filters")
return err
}
// Update filters // Update filters
// This might be a bad idea, but we cannot reference analyzer here // This might be a bad idea, but we cannot reference analyzer here
activeFilters := viper.GetStringSlice("active_filters") foundFilter := false
// Remove filter
for i, v := range activeFilters { for i, v := range activeFilters {
if v == integrations[name].GetAnalyzerName() { if v == integrations[name].GetAnalyzerName() {
foundFilter = true
activeFilters = append(activeFilters[:i], activeFilters[i+1:]...) activeFilters = append(activeFilters[:i], activeFilters[i+1:]...)
break 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) viper.Set("active_filters", activeFilters)
if err := viper.WriteConfig(); err != nil { if err := viper.WriteConfig(); err != nil {