Skip to content

Commit

Permalink
Merge pull request #51 from anchore/found-it/backwards-compatible-config
Browse files Browse the repository at this point in the history
Add backwards compatibility for kai configuration
  • Loading branch information
zhill authored Nov 18, 2021
2 parents 5f915aa + d97b992 commit 69d07da
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 28 deletions.
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Configure which namespaces kai should search.
* Example:

```yaml
namespaces:
namespace-selectors:
include:
- default
- kube-system
Expand All @@ -256,7 +256,7 @@ namespaces:
* Example:

```yaml
namespaces:
namespace-selectors:
exclude:
- default
- ^kube-*
Expand All @@ -265,7 +265,7 @@ namespaces:

```yaml
# Which namespaces to search or exclude.
namespaces:
namespace-selectors:
# Namespaces to include as explicit strings, not regex
# NOTE: Will search ALL namespaces if left as an empty array
include: []
Expand Down Expand Up @@ -379,6 +379,51 @@ anchore:
timeout-seconds: 10
```

## Configuration Changes (v0.2.2 -> v0.3.0)

There are a few configurations that were changed from v0.2.2 to v0.3.0

#### `kubernetes-request-timeout-seconds`

The request timeout for the kubernetes API was changed from

```yaml
kubernetes-request-timeout-seconds: 60
```

to

```yaml
kubernetes:
request-timeout-seconds: 60
```

KAI will still honor the old configuration. It will prefer the old configuration
parameter until it is removed from the config entirely. It is safe to remove the
old configuration in favor of the new config.

#### `namespaces`

The namespace configuration was changed from

```yaml
namespaces:
- all
```

to

```yaml
namespace-selectors:
include: []
exclude: []
```

`namespace-selectors` was added to eventually replace `namespaces` to allow for both
include and exclude configs. The old `namespaces` array will be honored if
`namespace-selectors.include` is empty. It is safe to remove `namespaces` entirely
in favor of `namespace-selectors`

## Developing
### Build
**Note:** This will drop the binary in the `./snapshot/` directory
Expand Down
65 changes: 47 additions & 18 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ type CliOnlyOptions struct {

// All Application configurations
type Application struct {
ConfigPath string
PresenterOpt presenter.Option
Output string `mapstructure:"output"`
Quiet bool `mapstructure:"quiet"`
Log Logging `mapstructure:"log"`
CliOptions CliOnlyOptions
Dev Development `mapstructure:"dev"`
KubeConfig KubeConf `mapstructure:"kubeconfig"`
Kubernetes KubernetesAPI `mapstructure:"kubernetes"`
Namespaces NamespacesConf `mapstructure:"namespaces"`
MissingTagPolicy MissingTagConf `mapstructure:"missing-tag-policy"`
RunMode mode.Mode
Mode string `mapstructure:"mode"`
IgnoreNotRunning bool `mapstructure:"ignore-not-running"`
PollingIntervalSeconds int `mapstructure:"polling-interval-seconds"`
AnchoreDetails AnchoreInfo `mapstructure:"anchore"`
ConfigPath string
PresenterOpt presenter.Option
Output string `mapstructure:"output"`
Quiet bool `mapstructure:"quiet"`
Log Logging `mapstructure:"log"`
CliOptions CliOnlyOptions
Dev Development `mapstructure:"dev"`
KubeConfig KubeConf `mapstructure:"kubeconfig"`
Kubernetes KubernetesAPI `mapstructure:"kubernetes"`
Namespaces []string `mapstructure:"namespaces"`
KubernetesRequestTimeoutSeconds int64 `mapstructure:"kubernetes-request-timeout-seconds"`
NamespaceSelectors NamespaceSelector `mapstructure:"namespace-selectors"`
MissingTagPolicy MissingTagConf `mapstructure:"missing-tag-policy"`
RunMode mode.Mode
Mode string `mapstructure:"mode"`
IgnoreNotRunning bool `mapstructure:"ignore-not-running"`
PollingIntervalSeconds int `mapstructure:"polling-interval-seconds"`
AnchoreDetails AnchoreInfo `mapstructure:"anchore"`
}

// MissingTagConf details the policy for handling missing tags when reporting images
Expand All @@ -62,8 +64,8 @@ type MissingTagConf struct {
Tag string `mapstructure:"tag,omitempty"`
}

// NamespacesConf details the inclusion/exclusion rules for namespaces
type NamespacesConf struct {
// NamespaceSelector details the inclusion/exclusion rules for namespaces
type NamespaceSelector struct {
Include []string `mapstructure:"include"`
Exclude []string `mapstructure:"exclude"`
}
Expand Down Expand Up @@ -119,12 +121,16 @@ func setNonCliDefaultValues(v *viper.Viper) {
v.SetDefault("kubeconfig.anchore.account", "admin")
v.SetDefault("anchore.http.insecure", false)
v.SetDefault("anchore.http.timeout-seconds", 10)
v.SetDefault("kubernetes-request-timeout-seconds", -1)
v.SetDefault("kubernetes.request-timeout-seconds", 60)
v.SetDefault("kubernetes.request-batch-size", 100)
v.SetDefault("kubernetes.worker-pool-size", 100)
v.SetDefault("ignore-not-running", true)
v.SetDefault("missing-tag-policy.policy", "digest")
v.SetDefault("missing-tag-policy.tag", "UNKNOWN")
v.SetDefault("namespaces", []string{})
v.SetDefault("namespace-selectors.include", []string{})
v.SetDefault("namespace-selectors.exclude", []string{})
}

// Load the Application Configuration from the Viper specifications
Expand Down Expand Up @@ -214,9 +220,32 @@ func (cfg *Application) Build() error {
return fmt.Errorf("missing-tag-policy.policy must be one of %v", policies)
}

cfg.handleBackwardsCompatibility()

return nil
}

func (cfg *Application) handleBackwardsCompatibility() {
// BACKWARDS COMPATIBILITY - Translate namespaces into the new selector config
// Only trigger if there is nothing in the include selector.
if len(cfg.NamespaceSelectors.Include) == 0 && len(cfg.Namespaces) > 0 {
for _, ns := range cfg.Namespaces {
if ns == "all" {
// set the include namespaces to an empty array if namespaces indicates collect "all"
cfg.NamespaceSelectors.Include = []string{}
break
}
// otherwise add the namespaces list to the include namespaces
cfg.NamespaceSelectors.Include = append(cfg.NamespaceSelectors.Include, ns)
}
}

// defer to the old config parameter if it is still present
if cfg.KubernetesRequestTimeoutSeconds > 0 {
cfg.Kubernetes.RequestTimeoutSeconds = cfg.KubernetesRequestTimeoutSeconds
}
}

func readConfig(v *viper.Viper, configPath string) error {
v.AutomaticEnv()
v.SetEnvPrefix(internal.ApplicationName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ kubernetes:
requesttimeoutseconds: 60
requestbatchsize: 100
workerpoolsize: 100
namespaces:
namespaces: []
kubernetesrequesttimeoutseconds: -1
namespaceselectors:
include: []
exclude: []
missingtagpolicy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ kubernetes:
requesttimeoutseconds: 0
requestbatchsize: 0
workerpoolsize: 0
namespaces:
namespaces: []
kubernetesrequesttimeoutseconds: 0
namespaceselectors:
include: []
exclude: []
missingtagpolicy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ kubernetes:
requesttimeoutseconds: 60
requestbatchsize: 100
workerpoolsize: 100
namespaces:
namespaces: []
kubernetesrequesttimeoutseconds: -1
namespaceselectors:
include: []
exclude: []
missingtagpolicy:
Expand Down
2 changes: 1 addition & 1 deletion kai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kubeconfig:
token:

# Which namespaces to search or exclude.
namespaces:
namespace-selectors:
# Namespaces to include as explicit strings, not regex
# NOTE: Will search ALL namespaces if left as an empty array
include: []
Expand Down
6 changes: 3 additions & 3 deletions kai/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func excludeNamespace(checks []excludeCheck, namespace string) bool {
func fetchNamespaces(kubeconfig *rest.Config, cfg *config.Application) ([]string, error) {
namespaces := make([]string, 0)

exclusionChecklist := buildExclusionChecklist(cfg.Namespaces.Exclude)
exclusionChecklist := buildExclusionChecklist(cfg.NamespaceSelectors.Exclude)

// Return list of namespaces if there are any present
if len(cfg.Namespaces.Include) > 0 {
for _, ns := range cfg.Namespaces.Include {
if len(cfg.NamespaceSelectors.Include) > 0 {
for _, ns := range cfg.NamespaceSelectors.Include {
if !excludeNamespace(exclusionChecklist, ns) {
namespaces = append(namespaces, ns)
}
Expand Down

0 comments on commit 69d07da

Please sign in to comment.