Skip to content

Commit

Permalink
dedup search rules
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Mar 27, 2020
1 parent 924e34c commit 302456c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 20 additions & 1 deletion pkg/kbld/config/conf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"reflect"

ctlres "github.com/k14s/kbld/pkg/kbld/resources"
)

Expand Down Expand Up @@ -71,7 +73,7 @@ func (c Conf) SearchRules() []SearchRule {
defaultRule := SearchRule{
KeyMatcher: &SearchRuleKeyMatcher{Name: "image"},
}
return append([]SearchRule{defaultRule}, c.SearchRulesWithoutDefaults()...)
return c.dedupSearchRules(append([]SearchRule{defaultRule}, c.SearchRulesWithoutDefaults()...))
}

func (c Conf) SearchRulesWithoutDefaults() []SearchRule {
Expand All @@ -86,5 +88,22 @@ func (c Conf) SearchRulesWithoutDefaults() []SearchRule {
for _, config := range c.configs {
result = append(result, config.SearchRules...)
}
return c.dedupSearchRules(result)
}

func (c Conf) dedupSearchRules(rules []SearchRule) []SearchRule {
var result []SearchRule
for _, rule := range rules {
var alreadySaved bool
for _, savedRule := range result {
if reflect.DeepEqual(rule, savedRule) {
alreadySaved = true
break
}
}
if !alreadySaved {
result = append(result, rule)
}
}
return result
}
5 changes: 1 addition & 4 deletions test/e2e/lock_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ images:
- image: nginx:1.14.2
- image: sample-app
- sidecarImage: sample-app
`

inputConfig := `
---
apiVersion: kbld.k14s.io/v1alpha1
kind: ImageOverrides
Expand All @@ -36,7 +33,7 @@ keys:
path := "/tmp/kbld-test-lock-output-successful"

out, _ := kbld.RunWithOpts([]string{"-f", "-", "--images-annotation=false", "--lock-output=" + path}, RunOpts{
StdinReader: strings.NewReader(input + inputConfig),
StdinReader: strings.NewReader(input),
})

expectedOut := `---
Expand Down

0 comments on commit 302456c

Please sign in to comment.