Skip to content

Commit

Permalink
chore: fixing filters
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Apr 11, 2023
1 parent 1b7f4ce commit 4d20f70
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
16 changes: 4 additions & 12 deletions cmd/filters/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/integration/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/integration/deactivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var deactivateCmd = &cobra.Command{
return
}

color.Green("Deactivate integration %s", intName)
color.Green("Deactivated integration %s", intName)

},
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 36 additions & 1 deletion pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,14 +20,16 @@ type IIntegration interface {
// RemoveAnalyzer removes an analyzer from the cluster
RemoveAnalyzer() error

GetAnalyzerName() string

IsActivate() bool
}

type Integration struct {
}

var integrations = map[string]IIntegration{
"VulnerabilityReport": trivy.NewTrivy(),
"trivy": trivy.NewTrivy(),
}

func NewIntegration() *Integration {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func NewTrivy() *Trivy {
}
}

func (t *Trivy) GetAnalyzerName() string {
return "VulnerabilityReport"
}

func (t *Trivy) Deploy(namespace string) error {

// Add the repository
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4d20f70

Please sign in to comment.