chore: fixing filters

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
Alex Jones
2023-04-11 15:07:36 +01:00
parent 1b7f4ce44a
commit 4d20f70fb4
7 changed files with 69 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -27,18 +26,11 @@ var listCmd = &cobra.Command{
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
fmt.Printf(color.YellowString("Active: \n"))
for _, filter := range activeFilters {
fmt.Printf("> %s\n", color.GreenString(filter))
}
// Add integrations ( which are dynamic ) to active filters
integrationProvider := integration.NewIntegration()
fmt.Printf(color.BlueString("Active Integrations: \n"))
for _, filter := range integrationFilters {
b, err := integrationProvider.IsActivate(filter)
if err != nil {
fmt.Printf(color.RedString("Error: %s", err))
}
if b {
// if the filter is an integration, mark this differently
if util.SliceContainsString(integrationFilters, filter) {
fmt.Printf("> %s\n", color.BlueString("%s (integration)\n", filter))
} else {
fmt.Printf("> %s\n", color.GreenString(filter))
}
}

View File

@@ -23,11 +23,7 @@ var activateCmd = &cobra.Command{
return
}
color.Yellow("Activating analyzer for integration %s", intName)
// Write the integration to the config file
color.Green("Activate integration %s", intName)
color.Green("Activated integration %s", intName)
},
}

View File

@@ -25,7 +25,7 @@ var deactivateCmd = &cobra.Command{
return
}
color.Green("Deactivate integration %s", intName)
color.Green("Deactivated integration %s", intName)
},
}

View File

@@ -34,13 +34,22 @@ func ListFilters() ([]string, []string, []string) {
additionalKeys = append(additionalKeys, k)
}
intList := integration.NewIntegration().List()
integrationKeys := make([]string, 0, len(intList))
for _, k := range integration.NewIntegration().List() {
integrationKeys = append(integrationKeys, k)
integrationProvider := integration.NewIntegration()
var integrationAnalyzers []string
for _, i := range integrationProvider.List() {
b, _ := integrationProvider.IsActivate(i)
if b {
in, err := integrationProvider.Get(i)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
integrationAnalyzers = append(integrationAnalyzers, in.GetAnalyzerName())
}
}
return coreKeys, additionalKeys, integrationKeys
return coreKeys, additionalKeys, integrationAnalyzers
}
func GetAnalyzerMap() map[string]common.IAnalyzer {

View File

@@ -2,9 +2,12 @@ package integration
import (
"errors"
"os"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy"
"github.com/spf13/viper"
)
type IIntegration interface {
@@ -17,6 +20,8 @@ type IIntegration interface {
// RemoveAnalyzer removes an analyzer from the cluster
RemoveAnalyzer() error
GetAnalyzerName() string
IsActivate() bool
}
@@ -24,7 +29,7 @@ type Integration struct {
}
var integrations = map[string]IIntegration{
"VulnerabilityReport": trivy.NewTrivy(),
"trivy": trivy.NewTrivy(),
}
func NewIntegration() *Integration {
@@ -55,6 +60,18 @@ func (*Integration) Activate(name string, namespace string) error {
return err
}
// Update filters
activeFilters := viper.GetStringSlice("active_filters")
activeFilters = append(activeFilters, integrations[name].GetAnalyzerName())
viper.Set("active_filters", activeFilters)
if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
}
return nil
}
@@ -67,6 +84,24 @@ func (*Integration) Deactivate(name string, namespace string) error {
return err
}
// Update filters
// This might be a bad idea, but we cannot reference analyzer here
activeFilters := viper.GetStringSlice("active_filters")
// Remove filter
for i, v := range activeFilters {
if v == integrations[name].GetAnalyzerName() {
activeFilters = append(activeFilters[:i], activeFilters[i+1:]...)
break
}
}
viper.Set("active_filters", activeFilters)
if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
}
return nil
}

View File

@@ -31,6 +31,10 @@ func NewTrivy() *Trivy {
}
}
func (t *Trivy) GetAnalyzerName() string {
return "VulnerabilityReport"
}
func (t *Trivy) Deploy(namespace string) error {
// Add the repository

View File

@@ -7,6 +7,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func SliceContainsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func GetParent(client *kubernetes.Client, meta metav1.ObjectMeta) (string, bool) {
if meta.OwnerReferences != nil {
for _, owner := range meta.OwnerReferences {