Skip to content

Commit

Permalink
Use viper to return slice of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi-kapoor committed Dec 9, 2024
1 parent be75f85 commit 65dffee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions localdev/kubechecks/values.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
configMap:
create: true
env:
GRPC_ENFORCE_ALPN_ENABLED: false
KUBECHECKS_LOG_LEVEL: debug
KUBECHECKS_ENABLE_WEBHOOK_CONTROLLER: "false"
KUBECHECKS_ARGOCD_API_INSECURE: "true"
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"reflect"
"strings"
"time"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -105,6 +106,12 @@ func NewWithViper(v *viper.Viper) (ServerConfig, error) {
return time.ParseDuration(input)
}

if in.String() == "string" && out.String() == "[]string" {
input := value.(string)
ns := strings.Split(input, ",")
return ns, nil
}

return value, nil
}
}); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestNew(t *testing.T) {
v.Set("argocd-api-plaintext", "true")
v.Set("worst-conftest-state", "warning")
v.Set("repo-refresh-interval", "10m")
v.Set("allowed-namespaces", "default,kube-system")

cfg, err := NewWithViper(v)
require.NoError(t, err)
Expand All @@ -27,4 +28,5 @@ func TestNew(t *testing.T) {
assert.Equal(t, true, cfg.ArgoCDPlainText)
assert.Equal(t, pkg.StateWarning, cfg.WorstConfTestState, "worst states can be overridden")
assert.Equal(t, time.Minute*10, cfg.RepoRefreshInterval)
assert.Equal(t, []string{"default", "kube-system"}, cfg.AllowedNamespaces)
}
13 changes: 4 additions & 9 deletions pkg/events/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,11 @@ func (ce *CheckEvent) Process(ctx context.Context) error {
ce.logger.Info().Msgf("adding %d apps to the queue", len(ce.affectedItems.Applications))
// Produce apps onto channel
for _, app := range ce.affectedItems.Applications {
if len(ce.ctr.Config.AllowedNamespaces) > 0 {
ns := strings.Split(ce.ctr.Config.AllowedNamespaces[0], ",")
if slices.Contains(ns, app.Spec.Destination.Namespace) {
ce.queueApp(app)
} else {
ce.logger.Info().Msgf("skipping app %s, namespace %s not allowed", app.Name, app.Spec.Destination.Namespace)
}
} else {
ce.queueApp(app)
if len(ce.ctr.Config.AllowedNamespaces) > 0 && !slices.Contains(ce.ctr.Config.AllowedNamespaces, app.Spec.Destination.Namespace) {
ce.logger.Info().Msgf("skipping app %s, namespace %s not allowed", app.Name, app.Spec.Destination.Namespace)
continue
}
ce.queueApp(app)
}

ce.wg.Wait()
Expand Down

0 comments on commit 65dffee

Please sign in to comment.