Skip to content

Commit

Permalink
fix: resolve issue with duplicated integration filters.
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Apr 15, 2023
1 parent 51b1b35 commit 960ba56
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package integration
import (
"errors"
"os"
"strings"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -56,16 +58,24 @@ func (*Integration) Activate(name string, namespace string) error {
return errors.New("integration not found")
}

if err := integrations[name].Deploy(namespace); err != nil {
return err
}

// Update filters
activeFilters := viper.GetStringSlice("active_filters")

activeFilters = append(activeFilters, integrations[name].GetAnalyzerName())
mergedFilters := append(activeFilters, integrations[name].GetAnalyzerName())

viper.Set("active_filters", activeFilters)
uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(mergedFilters)

// Verify dupplicate
if len(dupplicatedFilters) != 0 {
color.Red("Integration already activated : %s", strings.Join(dupplicatedFilters, ", "))
os.Exit(1)
}

viper.Set("active_filters", uniqueFilters)

if err := integrations[name].Deploy(namespace); err != nil {
return err
}

if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error())
Expand All @@ -80,21 +90,27 @@ func (*Integration) Deactivate(name string, namespace string) error {
return errors.New("integration not found")
}

if err := integrations[name].UnDeploy(namespace); err != nil {
return err
}
activeFilters := viper.GetStringSlice("active_filters")

// Update filters
// This might be a bad idea, but we cannot reference analyzer here
activeFilters := viper.GetStringSlice("active_filters")

// Remove filter
foundFilter := false
for i, v := range activeFilters {
if v == integrations[name].GetAnalyzerName() {
foundFilter = true
activeFilters = append(activeFilters[:i], activeFilters[i+1:]...)
break
}
}
if !foundFilter {
color.Red("Ingregation %s does not exist in configuration file. Please use k8sgpt integration add.", name)
os.Exit(1)
}

if err := integrations[name].UnDeploy(namespace); err != nil {
return err
}

viper.Set("active_filters", activeFilters)

if err := viper.WriteConfig(); err != nil {
Expand Down

0 comments on commit 960ba56

Please sign in to comment.