1
0
mirror of https://github.com/k8sgpt-ai/k8sgpt.git synced 2025-05-11 17:44:47 +00:00
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
Alex Jones 2023-04-11 12:30:57 +01:00
parent 80ac51c804
commit 4984840de1
22 changed files with 170 additions and 85 deletions

View File

@ -4,7 +4,6 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// activateCmd represents the activate command
@ -16,7 +15,7 @@ var activateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
intName := args[0]
integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()
// Check if the integation exists
err := integration.Activate(intName, namespace)
if err != nil {
@ -26,7 +25,7 @@ var activateCmd = &cobra.Command{
color.Yellow("Activating analyzer for integration %s", intName)
// TODO:
// Write the integration to the config file
color.Green("Activate integration %s", intName)
},

View File

@ -7,7 +7,6 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// deactivateCmd represents the deactivate command
@ -19,8 +18,7 @@ var deactivateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
intName := args[0]
// Check if the integation exists
integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()
if err := integration.Deactivate(intName, namespace); err != nil {
color.Red("Error: %v", err)

View File

@ -6,7 +6,6 @@ import (
var (
namespace string
name string
)
// IntegrationCmd represents the integrate command
@ -14,7 +13,8 @@ var IntegrationCmd = &cobra.Command{
Use: "integration",
Short: "Intergrate another tool into K8sGPT",
Long: `Intergrate another tool into K8sGPT. For example:
k8sgpt integration add trivy
k8sgpt integration activate trivy
This would allow you to deploy trivy into your cluster and use a K8sGPT analyzer to parse trivy results.`,
Run: func(cmd *cobra.Command, args []string) {
@ -23,6 +23,5 @@ var IntegrationCmd = &cobra.Command{
}
func init() {
IntegrationCmd.PersistentFlags().StringVarP(&name, "name", "a", "", "The name of the integration")
IntegrationCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "The namespace to use for the integration")
}

View File

@ -2,11 +2,11 @@ package integration
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// listCmd represents the list command
@ -15,12 +15,31 @@ var listCmd = &cobra.Command{
Short: "Lists built-in integrations",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()
integrations := integration.List()
for _, integration := range integrations {
fmt.Printf("> %s\n", color.GreenString(integration))
fmt.Println(color.YellowString("Active:"))
for _, i := range integrations {
b, err := integration.IsActivate(i)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if b {
fmt.Printf("> %s\n", color.GreenString(i))
}
}
fmt.Println(color.YellowString("Unused: "))
for _, i := range integrations {
b, err := integration.IsActivate(i)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !b {
fmt.Printf("> %s\n", color.GreenString(i))
}
}
},
}

View File

@ -10,7 +10,6 @@ import (
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"github.com/k8sgpt-ai/k8sgpt/cmd/integration"
in "github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -59,10 +58,6 @@ func init() {
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.")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", kubeconfigPath, "Path to a kubeconfig. Only required if out-of-cluster.")
// Integration start up
integration := in.NewIntegration()
viper.Set("integration", integration)
}
// initConfig reads in config file and ENV variables if set.

2
go.mod
View File

@ -17,6 +17,7 @@ require (
k8s.io/api v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.26.3
k8s.io/kubectl v0.26.3
)
require (
@ -154,7 +155,6 @@ require (
k8s.io/component-base v0.26.3 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230327201221-f5883ff37f0c // indirect
k8s.io/kubectl v0.26.3 // indirect
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749 // indirect
oras.land/oras-go v1.2.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect

View File

@ -10,6 +10,7 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/schollz/progressbar/v3"
"github.com/spf13/viper"
@ -20,7 +21,7 @@ type Analysis struct {
Filters []string
Client *kubernetes.Client
AIClient ai.IAI
Results []analyzer.Result
Results []common.Result
Namespace string
NoCache bool
Explain bool
@ -34,9 +35,9 @@ const (
)
type JsonOutput struct {
Status AnalysisStatus `json:"status"`
Problems int `json:"problems"`
Results []analyzer.Result `json:"results"`
Status AnalysisStatus `json:"status"`
Problems int `json:"problems"`
Results []common.Result `json:"results"`
}
func (a *Analysis) RunAnalysis() error {
@ -45,7 +46,7 @@ func (a *Analysis) RunAnalysis() error {
analyzerMap := analyzer.GetAnalyzerMap()
analyzerConfig := analyzer.Analyzer{
analyzerConfig := common.Analyzer{
Client: a.Client,
Context: a.Context,
Namespace: a.Namespace,

View File

@ -3,22 +3,23 @@ package analysis
import (
"encoding/json"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/stretchr/testify/require"
"testing"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/stretchr/testify/require"
)
func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{},
Results: []common.Result{},
Namespace: "default",
}
expected := JsonOutput{
Status: StateOK,
Problems: 0,
Results: []analyzer.Result{},
Results: []common.Result{},
}
gotJson, err := analysis.JsonOutput()
@ -40,7 +41,7 @@ func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
func TestAnalysis_ProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
Results: []common.Result{
{
"Deployment",
"test-deployment",
@ -54,7 +55,7 @@ func TestAnalysis_ProblemJsonOutput(t *testing.T) {
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 1,
Results: []analyzer.Result{
Results: []common.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem"},
@ -82,7 +83,7 @@ func TestAnalysis_ProblemJsonOutput(t *testing.T) {
func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
Results: []common.Result{
{
"Deployment",
"test-deployment",
@ -96,7 +97,7 @@ func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 2,
Results: []analyzer.Result{
Results: []common.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem", "another-test-problem"},

View File

@ -1,10 +1,15 @@
package analyzer
type IAnalyzer interface {
Analyze(analysis Analyzer) ([]Result, error)
}
import (
"fmt"
"os"
var coreAnalyzerMap = map[string]IAnalyzer{
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
)
var coreAnalyzerMap = map[string]common.IAnalyzer{
"Pod": PodAnalyzer{},
"ReplicaSet": ReplicaSetAnalyzer{},
"PersistentVolumeClaim": PvcAnalyzer{},
@ -13,7 +18,7 @@ var coreAnalyzerMap = map[string]IAnalyzer{
"StatefulSet": StatefulSetAnalyzer{},
}
var additionalAnalyzerMap = map[string]IAnalyzer{
var additionalAnalyzerMap = map[string]common.IAnalyzer{
"HorizontalPodAutoScaler": HpaAnalyzer{},
"PodDisruptionBudget": PdbAnalyzer{},
}
@ -31,9 +36,9 @@ func ListFilters() ([]string, []string) {
return coreKeys, additionalKeys
}
func GetAnalyzerMap() map[string]IAnalyzer {
func GetAnalyzerMap() map[string]common.IAnalyzer {
mergedMap := make(map[string]IAnalyzer)
mergedMap := make(map[string]common.IAnalyzer)
// add core analyzer
for key, value := range coreAnalyzerMap {
@ -45,5 +50,23 @@ func GetAnalyzerMap() map[string]IAnalyzer {
mergedMap[key] = value
}
integrationProvider := integration.NewIntegration()
for _, i := range integrationProvider.List() {
b, err := integrationProvider.IsActivate(i)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
if b {
in, err := integrationProvider.Get(i)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
in.AddAnalyzer(&mergedMap)
}
}
return mergedMap
}

View File

@ -2,6 +2,7 @@ package analyzer
import (
"context"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

View File

@ -2,20 +2,22 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type HpaAnalyzer struct{}
func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
list, err := a.Client.GetClient().AutoscalingV1().HorizontalPodAutoscalers(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, hpa := range list.Items {
var failures []string
@ -54,7 +56,7 @@ func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", hpa.Namespace, hpa.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", hpa.Namespace, hpa.Name)] = common.PreAnalysis{
HorizontalPodAutoscalers: hpa,
FailureDetails: failures,
}
@ -63,7 +65,7 @@ func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "HorizontalPodAutoscaler",
Name: key,
Error: value.FailureDetails,

View File

@ -2,20 +2,22 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type IngressAnalyzer struct{}
func (IngressAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (IngressAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
list, err := a.Client.GetClient().NetworkingV1().Ingresses(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, ing := range list.Items {
var failures []string
@ -57,7 +59,7 @@ func (IngressAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", ing.Namespace, ing.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", ing.Namespace, ing.Name)] = common.PreAnalysis{
Ingress: ing,
FailureDetails: failures,
}
@ -66,7 +68,7 @@ func (IngressAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "Ingress",
Name: key,
Error: value.FailureDetails,

View File

@ -2,20 +2,22 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PdbAnalyzer struct{}
func (PdbAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (PdbAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
list, err := a.Client.GetClient().PolicyV1().PodDisruptionBudgets(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, pdb := range list.Items {
var failures []string
@ -39,7 +41,7 @@ func (PdbAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", pdb.Namespace, pdb.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", pdb.Namespace, pdb.Name)] = common.PreAnalysis{
PodDisruptionBudget: pdb,
FailureDetails: failures,
}
@ -47,7 +49,7 @@ func (PdbAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "PodDisruptionBudget",
Name: key,
Error: value.FailureDetails,

View File

@ -2,6 +2,8 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@ -9,13 +11,13 @@ import (
type PodAnalyzer struct {
}
func (PodAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (PodAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// search all namespaces for pods that are not running
list, err := a.Client.GetClient().CoreV1().Pods(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, pod := range list.Items {
var failures []string
@ -55,7 +57,7 @@ func (PodAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)] = common.PreAnalysis{
Pod: pod,
FailureDetails: failures,
}
@ -63,7 +65,7 @@ func (PodAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "Pod",
Name: key,
Error: value.FailureDetails,

View File

@ -2,13 +2,15 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PvcAnalyzer struct{}
func (PvcAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (PvcAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// search all namespaces for pods that are not running
list, err := a.Client.GetClient().CoreV1().PersistentVolumeClaims(a.Namespace).List(a.Context, metav1.ListOptions{})
@ -16,7 +18,7 @@ func (PvcAnalyzer) Analyze(a Analyzer) ([]Result, error) {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, pvc := range list.Items {
var failures []string
@ -34,7 +36,7 @@ func (PvcAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name)] = common.PreAnalysis{
PersistentVolumeClaim: pvc,
FailureDetails: failures,
}
@ -42,7 +44,7 @@ func (PvcAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "PersistentVolumeClaim",
Name: key,
Error: value.FailureDetails,

View File

@ -2,13 +2,15 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ReplicaSetAnalyzer struct{}
func (ReplicaSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (ReplicaSetAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// search all namespaces for pods that are not running
list, err := a.Client.GetClient().AppsV1().ReplicaSets(a.Namespace).List(a.Context, metav1.ListOptions{})
@ -16,7 +18,7 @@ func (ReplicaSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, rs := range list.Items {
var failures []string
@ -32,7 +34,7 @@ func (ReplicaSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", rs.Namespace, rs.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", rs.Namespace, rs.Name)] = common.PreAnalysis{
ReplicaSet: rs,
FailureDetails: failures,
}
@ -40,7 +42,7 @@ func (ReplicaSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "ReplicaSet",
Name: key,
Error: value.FailureDetails,

View File

@ -2,14 +2,16 @@ package analyzer
import (
"fmt"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ServiceAnalyzer struct{}
func (ServiceAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (ServiceAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// search all namespaces for pods that are not running
list, err := a.Client.GetClient().CoreV1().Endpoints(a.Namespace).List(a.Context, metav1.ListOptions{})
@ -17,7 +19,7 @@ func (ServiceAnalyzer) Analyze(a Analyzer) ([]Result, error) {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, ep := range list.Items {
var failures []string
@ -50,7 +52,7 @@ func (ServiceAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", ep.Namespace, ep.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", ep.Namespace, ep.Name)] = common.PreAnalysis{
Endpoint: ep,
FailureDetails: failures,
}
@ -58,7 +60,7 @@ func (ServiceAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "Service",
Name: key,
Error: value.FailureDetails,

View File

@ -3,18 +3,19 @@ package analyzer
import (
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type StatefulSetAnalyzer struct{}
func (StatefulSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (StatefulSetAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
list, err := a.Client.GetClient().AppsV1().StatefulSets(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}
var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, sts := range list.Items {
var failures []string
@ -36,7 +37,7 @@ func (StatefulSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", sts.Namespace, sts.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", sts.Namespace, sts.Name)] = common.PreAnalysis{
StatefulSet: sts,
FailureDetails: failures,
}
@ -44,7 +45,7 @@ func (StatefulSetAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "StatefulSet",
Name: key,
Error: value.FailureDetails,

View File

@ -1,4 +1,4 @@
package analyzer
package common
import (
"context"
@ -13,6 +13,10 @@ import (
policyv1 "k8s.io/api/policy/v1"
)
type IAnalyzer interface {
Analyze(analysis Analyzer) ([]Result, error)
}
type Analyzer struct {
Client *kubernetes.Client
Context context.Context

View File

@ -3,7 +3,7 @@ package integration
import (
"errors"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy"
)
@ -13,7 +13,7 @@ type IIntegration interface {
// Remove removes an integration from the cluster
UnDeploy(namespace string) error
//
AddAnalyzer() (string, analyzer.IAnalyzer, error)
AddAnalyzer(*map[string]common.IAnalyzer)
// RemoveAnalyzer removes an analyzer from the cluster
RemoveAnalyzer() error
@ -23,6 +23,10 @@ type IIntegration interface {
type Integration struct {
}
type IntegrationProvider struct {
Active []string `mapstructure:"active"`
}
var integrations = map[string]IIntegration{
"trivy": trivy.NewTrivy(),
}
@ -39,16 +43,40 @@ func (*Integration) List() []string {
return keys
}
func (*Integration) Get(name string) (IIntegration, error) {
if _, ok := integrations[name]; !ok {
return nil, errors.New("integration not found")
}
return integrations[name], nil
}
func (*Integration) Activate(name string, namespace string) error {
if _, ok := integrations[name]; !ok {
return errors.New("integration not found")
}
return integrations[name].Deploy(namespace)
if err := integrations[name].Deploy(namespace); err != nil {
return err
}
return nil
}
func (*Integration) Deactivate(name string, namespace string) error {
if _, ok := integrations[name]; !ok {
return errors.New("integration not found")
}
return integrations[name].UnDeploy(namespace)
if err := integrations[name].UnDeploy(namespace); err != nil {
return err
}
return nil
}
func (*Integration) IsActivate(name string) (bool, error) {
if _, ok := integrations[name]; !ok {
return false, errors.New("integration not found")
}
return integrations[name].IsActivate(), nil
}

View File

@ -4,14 +4,14 @@ import (
"fmt"
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
)
type TrivyAnalyzer struct {
}
func (TrivyAnalyzer) Analyze(a analyzer.Analyzer) ([]analyzer.Result, error) {
func (TrivyAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// Get all trivy VulnerabilityReports
result := &v1alpha1.VulnerabilityReportList{}
@ -22,7 +22,7 @@ func (TrivyAnalyzer) Analyze(a analyzer.Analyzer) ([]analyzer.Result, error) {
}
// Find criticals and get CVE
var preAnalysis = map[string]analyzer.PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}
for _, report := range result.Items {
@ -37,7 +37,7 @@ func (TrivyAnalyzer) Analyze(a analyzer.Analyzer) ([]analyzer.Result, error) {
}
if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", report.Labels["trivy-operator.resource.namespace"],
report.Labels["trivy-operator.resource.name"])] = analyzer.PreAnalysis{
report.Labels["trivy-operator.resource.name"])] = common.PreAnalysis{
TrivyVulnerabilityReport: report,
FailureDetails: failures,
}
@ -45,7 +45,7 @@ func (TrivyAnalyzer) Analyze(a analyzer.Analyzer) ([]analyzer.Result, error) {
}
for key, value := range preAnalysis {
var currentAnalysis = analyzer.Result{
var currentAnalysis = common.Result{
Kind: "VulnerabilityReport",
Name: key,
Error: value.FailureDetails,

View File

@ -4,7 +4,7 @@ import (
"context"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
helmclient "github.com/mittwald/go-helm-client"
"helm.sh/helm/v3/pkg/repo"
)
@ -88,8 +88,10 @@ func (t *Trivy) IsActivate() bool {
return true
}
func (t *Trivy) AddAnalyzer() (string, analyzer.IAnalyzer, error) {
return "trivy", TrivyAnalyzer{}, nil
func (t *Trivy) AddAnalyzer(mergedMap *map[string]common.IAnalyzer) {
(*mergedMap)["trivy"] = &TrivyAnalyzer{}
}
func (t *Trivy) RemoveAnalyzer() error {