mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-06 09:42:02 +00:00
chore: fixing filters
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
|
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@@ -27,18 +26,11 @@ var listCmd = &cobra.Command{
|
|||||||
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
|
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
|
||||||
fmt.Printf(color.YellowString("Active: \n"))
|
fmt.Printf(color.YellowString("Active: \n"))
|
||||||
for _, filter := range activeFilters {
|
for _, filter := range activeFilters {
|
||||||
fmt.Printf("> %s\n", color.GreenString(filter))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add integrations ( which are dynamic ) to active filters
|
// if the filter is an integration, mark this differently
|
||||||
integrationProvider := integration.NewIntegration()
|
if util.SliceContainsString(integrationFilters, filter) {
|
||||||
fmt.Printf(color.BlueString("Active Integrations: \n"))
|
fmt.Printf("> %s\n", color.BlueString("%s (integration)\n", filter))
|
||||||
for _, filter := range integrationFilters {
|
} else {
|
||||||
b, err := integrationProvider.IsActivate(filter)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf(color.RedString("Error: %s", err))
|
|
||||||
}
|
|
||||||
if b {
|
|
||||||
fmt.Printf("> %s\n", color.GreenString(filter))
|
fmt.Printf("> %s\n", color.GreenString(filter))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,11 +23,7 @@ var activateCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Yellow("Activating analyzer for integration %s", intName)
|
color.Green("Activated integration %s", intName)
|
||||||
|
|
||||||
// Write the integration to the config file
|
|
||||||
|
|
||||||
color.Green("Activate integration %s", intName)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,7 +25,7 @@ var deactivateCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Green("Deactivate integration %s", intName)
|
color.Green("Deactivated integration %s", intName)
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -34,13 +34,22 @@ func ListFilters() ([]string, []string, []string) {
|
|||||||
additionalKeys = append(additionalKeys, k)
|
additionalKeys = append(additionalKeys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
intList := integration.NewIntegration().List()
|
integrationProvider := integration.NewIntegration()
|
||||||
integrationKeys := make([]string, 0, len(intList))
|
var integrationAnalyzers []string
|
||||||
for _, k := range integration.NewIntegration().List() {
|
|
||||||
integrationKeys = append(integrationKeys, k)
|
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 {
|
func GetAnalyzerMap() map[string]common.IAnalyzer {
|
||||||
|
@@ -2,9 +2,12 @@ package integration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"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/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IIntegration interface {
|
type IIntegration interface {
|
||||||
@@ -17,6 +20,8 @@ type IIntegration interface {
|
|||||||
// RemoveAnalyzer removes an analyzer from the cluster
|
// RemoveAnalyzer removes an analyzer from the cluster
|
||||||
RemoveAnalyzer() error
|
RemoveAnalyzer() error
|
||||||
|
|
||||||
|
GetAnalyzerName() string
|
||||||
|
|
||||||
IsActivate() bool
|
IsActivate() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +29,7 @@ type Integration struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var integrations = map[string]IIntegration{
|
var integrations = map[string]IIntegration{
|
||||||
"VulnerabilityReport": trivy.NewTrivy(),
|
"trivy": trivy.NewTrivy(),
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIntegration() *Integration {
|
func NewIntegration() *Integration {
|
||||||
@@ -55,6 +60,18 @@ func (*Integration) Activate(name string, namespace string) error {
|
|||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +84,24 @@ func (*Integration) Deactivate(name string, namespace string) error {
|
|||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -31,6 +31,10 @@ func NewTrivy() *Trivy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Trivy) GetAnalyzerName() string {
|
||||||
|
return "VulnerabilityReport"
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Trivy) Deploy(namespace string) error {
|
func (t *Trivy) Deploy(namespace string) error {
|
||||||
|
|
||||||
// Add the repository
|
// Add the repository
|
||||||
|
@@ -7,6 +7,15 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
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) {
|
func GetParent(client *kubernetes.Client, meta metav1.ObjectMeta) (string, bool) {
|
||||||
if meta.OwnerReferences != nil {
|
if meta.OwnerReferences != nil {
|
||||||
for _, owner := range meta.OwnerReferences {
|
for _, owner := range meta.OwnerReferences {
|
||||||
|
Reference in New Issue
Block a user