mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-18 16:34:31 +00:00
chore: analyzer and ai interfacing (#200)
* chore: analyzer and ai interfacing Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> * fix: backend variable for aiProvider in cmd Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> * fix: changed folders for analyzers Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> * chore: renamed analyzers Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> * fix: fixed ingress tests after rebase Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> * fix: fixed ingress tests after rebase Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu> --------- Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
This commit is contained in:
@@ -1,15 +1,8 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
|
||||
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
type IAnalyzer interface {
|
||||
Analyze(analysis Analyzer) ([]Result, error)
|
||||
}
|
||||
|
||||
var coreAnalyzerMap = map[string]IAnalyzer{
|
||||
"Pod": PodAnalyzer{},
|
||||
@@ -24,85 +17,6 @@ var additionalAnalyzerMap = map[string]IAnalyzer{
|
||||
"PodDisruptionBudget": PdbAnalyzer{},
|
||||
}
|
||||
|
||||
func RunAnalysis(ctx context.Context, filters []string, config *AnalysisConfiguration,
|
||||
client *kubernetes.Client,
|
||||
aiClient ai.IAI, analysisResults *[]Analysis) error {
|
||||
|
||||
activeFilters := viper.GetStringSlice("active_filters")
|
||||
|
||||
analyzerMap := getAnalyzerMap()
|
||||
|
||||
// if there are no filters selected and no active_filters then run all of them
|
||||
if len(filters) == 0 && len(activeFilters) == 0 {
|
||||
for _, analyzer := range analyzerMap {
|
||||
if err := analyzer.RunAnalysis(ctx, config, client, aiClient, analysisResults); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// if the filters flag is specified
|
||||
if len(filters) != 0 {
|
||||
for _, filter := range filters {
|
||||
if analyzer, ok := analyzerMap[filter]; ok {
|
||||
if err := analyzer.RunAnalysis(ctx, config, client, aiClient, analysisResults); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// use active_filters
|
||||
for _, filter := range activeFilters {
|
||||
if analyzer, ok := analyzerMap[filter]; ok {
|
||||
if err := analyzer.RunAnalysis(ctx, config, client, aiClient, analysisResults); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseViaAI(ctx context.Context, config *AnalysisConfiguration,
|
||||
aiClient ai.IAI, prompt []string) (string, error) {
|
||||
// parse the text with the AI backend
|
||||
inputKey := strings.Join(prompt, " ")
|
||||
// Check for cached data
|
||||
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
|
||||
// find in viper cache
|
||||
if viper.IsSet(sEnc) && !config.NoCache {
|
||||
// retrieve data from cache
|
||||
response := viper.GetString(sEnc)
|
||||
if response == "" {
|
||||
color.Red("error retrieving cached data")
|
||||
return "", nil
|
||||
}
|
||||
output, err := base64.StdEncoding.DecodeString(response)
|
||||
if err != nil {
|
||||
color.Red("error decoding cached data: %v", err)
|
||||
return "", nil
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
response, err := aiClient.GetCompletion(ctx, inputKey)
|
||||
if err != nil {
|
||||
color.Red("error getting completion: %v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !viper.IsSet(sEnc) {
|
||||
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
color.Red("error writing config: %v", err)
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func ListFilters() ([]string, []string) {
|
||||
coreKeys := make([]string, 0, len(coreAnalyzerMap))
|
||||
for k := range coreAnalyzerMap {
|
||||
@@ -116,7 +30,7 @@ func ListFilters() ([]string, []string) {
|
||||
return coreKeys, additionalKeys
|
||||
}
|
||||
|
||||
func getAnalyzerMap() map[string]IAnalyzer {
|
||||
func GetAnalyzerMap() map[string]IAnalyzer {
|
||||
|
||||
mergedMap := make(map[string]IAnalyzer)
|
||||
|
||||
|
Reference in New Issue
Block a user