add function to find all matched patch targets

This commit is contained in:
jingfangliu
2019-06-27 11:41:25 -07:00
parent b9b9fb1dd2
commit 349cfff1cb
5 changed files with 222 additions and 3 deletions

View File

@@ -8,6 +8,8 @@ package resmap
import (
"bytes"
"fmt"
"regexp"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/resid"
@@ -158,6 +160,10 @@ type ResMap interface {
// Debug prints the ResMap.
Debug(title string)
// Select returns a list of resources that
// are selected by a Selector
Select(types.Selector) ([]*resource.Resource, error)
}
// resWrangler holds the content manipulated by kustomize.
@@ -587,3 +593,66 @@ func (m *resWrangler) appendReplaceOrMerge(
}
return nil
}
// 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)
var result []*resource.Resource
for _, r := range m.Resources() {
curId := r.CurId()
orgId := r.OrgId()
// matches the namespace when namespace is not empty in the selector
// It first tries to match with the original namespace
// then matches with the current namespace
if r.GetNamespace() != "" {
matched := ns.MatchString(orgId.EffectiveNamespace())
if !matched {
matched = ns.MatchString(curId.EffectiveNamespace())
if !matched {
continue
}
}
}
// matches the name when name is not empty in the selector
// It first tries to match with the original name
// then matches with the current name
if r.GetName() != "" {
matched := nm.MatchString(orgId.Name)
if !matched {
matched = nm.MatchString(curId.Name)
if !matched {
continue
}
}
}
// matches the GVK
if !r.GetGvk().IsSelected(&s.Gvk) {
continue
}
// matches the label selector
matched, err := r.MatchesLabelSelector(s.LabelSelector)
if err != nil {
return nil, err
}
if !matched {
continue
}
// matches the annotation selector
matched, err = r.MatchesAnnotationSelector(s.AnnotationSelector)
if err != nil {
return nil, err
}
if !matched {
continue
}
result = append(result, r)
}
return result, nil
}

130
pkg/resmap/selector_test.go Normal file
View File

@@ -0,0 +1,130 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.
package resmap_test
import (
"testing"
"sigs.k8s.io/kustomize/v3/pkg/gvk"
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/types"
)
func setupRMForPatchTargets(t *testing.T) resmap.ResMap {
result, err := rmF.NewResMapFromBytes([]byte(`
apiVersion: group1/v1
kind: Kind1
metadata:
name: name1
namespace: ns1
labels:
app: name1
annotations:
foo: bar
---
apiVersion: group1/v1
kind: Kind1
metadata:
name: name2
namespace: default
labels:
app: name2
annotations:
foo: bar
---
apiVersion: group1/v1
kind: Kind2
metadata:
name: name3
labels:
app: name3
annotations:
bar: baz
`))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
return result
}
func TestFindPatchTargets(t *testing.T) {
rm := setupRMForPatchTargets(t)
testcases := []struct {
target types.Selector
count int
}{
{
target: types.Selector{
Name: "name*",
},
count: 3,
},
{
target: types.Selector{
Name: "name*",
AnnotationSelector: "foo=bar",
},
count: 2,
},
{
target: types.Selector{
LabelSelector: "app=name1",
},
count: 1,
},
{
target: types.Selector{
Gvk: gvk.Gvk{
Kind: "Kind1",
},
Name: "name*",
},
count: 2,
},
{
target: types.Selector{
Name: "NotMatched",
},
count: 0,
},
{
target: types.Selector{
Name: "",
},
count: 3,
},
{
target: types.Selector{
Namespace: "default",
},
count: 2,
},
{
target: types.Selector{
Namespace: "",
},
count: 3,
},
{
target: types.Selector{
Namespace: "default",
Name: "name*",
Gvk: gvk.Gvk{
Kind: "Kind1",
},
},
count: 1,
},
}
for _, testcase := range testcases {
actual, err := rm.Select(testcase.target)
if err != nil {
t.Errorf("unexpected error %v", err)
}
if len(actual) != testcase.count {
t.Errorf("expected %d objects, but got %d:\n%v", testcase.count, len(actual), actual)
}
}
}