Skip to content

Commit

Permalink
Automatically anchor resource selector patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmarshall committed Jul 30, 2019
1 parent 4690558 commit 963913f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
6 changes: 5 additions & 1 deletion docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ patches:
group: apps
version: v1
kind: Deployment
name: deploy
name: deploy.*
labelSelector: "env=dev"
annotationSelector: "zone=west"
- patch: |-
Expand All @@ -295,6 +295,10 @@ patches:
labelSelector: "env=dev"
```

The `name` and `namespace` fields of the patch target selector are
automatically anchored regular expressions. This means that the value `myapp`
is equivalent to `^myapp$`.

### patchesStrategicMerge

Each entry in this list should be either a relative
Expand Down
11 changes: 9 additions & 2 deletions pkg/resmap/resmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,18 @@ func (m *resWrangler) appendReplaceOrMerge(
return nil
}

func anchorRegex(pattern string) string {
if pattern == "" {
return pattern
}
return "^" + pattern + "$"
}

// Select returns a list of resources that
// are selected by a Selector
func (m *resWrangler) Select(s types.Selector) ([]*resource.Resource, error) {
ns := regexp.MustCompile(s.Namespace)
nm := regexp.MustCompile(s.Name)
ns := regexp.MustCompile(anchorRegex(s.Namespace))
nm := regexp.MustCompile(anchorRegex(s.Name))
var result []*resource.Resource
for _, r := range m.Resources() {
curId := r.CurId()
Expand Down
60 changes: 54 additions & 6 deletions pkg/resmap/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ metadata:
app: name3
annotations:
bar: baz
---
apiVersion: group1/v1
kind: Kind2
metadata:
name: x-name1
namespace: x-default
`))
if err != nil {
t.Fatalf("unexpected error %v", err)
Expand All @@ -56,13 +62,13 @@ func TestFindPatchTargets(t *testing.T) {
}{
{
target: types.Selector{
Name: "name*",
Name: "name.*",
},
count: 3,
},
{
target: types.Selector{
Name: "name*",
Name: "name.*",
AnnotationSelector: "foo=bar",
},
count: 2,
Expand All @@ -78,7 +84,7 @@ func TestFindPatchTargets(t *testing.T) {
Gvk: gvk.Gvk{
Kind: "Kind1",
},
Name: "name*",
Name: "name.*",
},
count: 2,
},
Expand All @@ -92,7 +98,7 @@ func TestFindPatchTargets(t *testing.T) {
target: types.Selector{
Name: "",
},
count: 3,
count: 4,
},
{
target: types.Selector{
Expand All @@ -104,18 +110,60 @@ func TestFindPatchTargets(t *testing.T) {
target: types.Selector{
Namespace: "",
},
count: 3,
count: 4,
},
{
target: types.Selector{
Namespace: "default",
Name: "name*",
Name: "name.*",
Gvk: gvk.Gvk{
Kind: "Kind1",
},
},
count: 1,
},
{
target: types.Selector{
Name: "^name.*",
},
count: 3,
},
{
target: types.Selector{
Name: "name.*$",
},
count: 3,
},
{
target: types.Selector{
Name: "^name.*$",
},
count: 3,
},
{
target: types.Selector{
Namespace: "^def.*",
},
count: 2,
},
{
target: types.Selector{
Namespace: "def.*$",
},
count: 2,
},
{
target: types.Selector{
Namespace: "^def.*$",
},
count: 2,
},
{
target: types.Selector{
Namespace: "default",
},
count: 2,
},
}
for _, testcase := range testcases {
actual, err := rm.Select(testcase.target)
Expand Down

0 comments on commit 963913f

Please sign in to comment.