Skip to content

Commit

Permalink
Add label and annotation selector code.
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Nov 26, 2020
1 parent 4af4483 commit 47d362b
Show file tree
Hide file tree
Showing 21 changed files with 2,848 additions and 43 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,6 @@ build-kustomize-api: $(builtinplugins)
generate-kustomize-api:
cd api; go generate ./...

.PHONY: clean-kustomize-api
clean-kustomize-api:

.PHONY: test-unit-kustomize-api
test-unit-kustomize-api: build-kustomize-api
cd api; go test ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222"
Expand Down
8 changes: 4 additions & 4 deletions api/internal/wrappy/wnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (wn *WNode) MarshalJSON() ([]byte, error) {
}

// MatchesAnnotationSelector implements ifc.Kunstructured.
func (wn *WNode) MatchesAnnotationSelector(string) (bool, error) {
panic("TODO(#WNode) MatchesAnnotationSelector; implement or drop from API")
func (wn *WNode) MatchesAnnotationSelector(selector string) (bool, error) {
return wn.node.MatchesAnnotationSelector(selector)
}

// MatchesLabelSelector implements ifc.Kunstructured.
func (wn *WNode) MatchesLabelSelector(string) (bool, error) {
panic("TODO(#WNode) MatchesLabelSelector; implement or drop from API")
func (wn *WNode) MatchesLabelSelector(selector string) (bool, error) {
return wn.node.MatchesLabelSelector(selector)
}

// SetAnnotations implements ifc.Kunstructured.
Expand Down
58 changes: 28 additions & 30 deletions api/resmap/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resmap_test
import (
"testing"

"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/api/resid"
. "sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
Expand Down Expand Up @@ -48,38 +49,36 @@ metadata:
name: x-name1
namespace: x-default
`))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
assert.NoError(t, err)
return result
}

func TestFindPatchTargets(t *testing.T) {
rm := setupRMForPatchTargets(t)
testcases := []struct {
testcases := map[string]struct {
target types.Selector
count int
}{
{
"hey01": {
target: types.Selector{
Name: "name.*",
},
count: 3,
},
{
"hey02": {
target: types.Selector{
Name: "name.*",
AnnotationSelector: "foo=bar",
},
count: 2,
},
{
"hey03": {
target: types.Selector{
LabelSelector: "app=name1",
},
count: 1,
},
{
"hey04": {
target: types.Selector{
Gvk: resid.Gvk{
Kind: "Kind1",
Expand All @@ -88,31 +87,31 @@ func TestFindPatchTargets(t *testing.T) {
},
count: 2,
},
{
"hey05": {
target: types.Selector{
Name: "NotMatched",
},
count: 0,
},
{
"hey06": {
target: types.Selector{
Name: "",
},
count: 4,
},
{
"hey07": {
target: types.Selector{
Namespace: "default",
},
count: 2,
},
{
"hey08": {
target: types.Selector{
Namespace: "",
},
count: 4,
},
{
"hey09": {
target: types.Selector{
Namespace: "default",
Name: "name.*",
Expand All @@ -122,69 +121,68 @@ func TestFindPatchTargets(t *testing.T) {
},
count: 1,
},
{
"hey10": {
target: types.Selector{
Name: "^name.*",
},
count: 3,
},
{
"hey11": {
target: types.Selector{
Name: "name.*$",
},
count: 3,
},
{
"hey12": {
target: types.Selector{
Name: "^name.*$",
},
count: 3,
},
{
"hey13": {
target: types.Selector{
Namespace: "^def.*",
},
count: 2,
},
{
"hey14": {
target: types.Selector{
Namespace: "def.*$",
},
count: 2,
},
{
"hey15": {
target: types.Selector{
Namespace: "^def.*$",
},
count: 2,
},
{
"hey16": {
target: types.Selector{
Namespace: "default",
},
count: 2,
},
{
"hey17": {
target: types.Selector{
Namespace: "NotMatched",
},
count: 0,
},
{
"hey18": {
target: types.Selector{
Namespace: "ns1",
},
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)
for n, testcase := range testcases {
if n !="hey03" {
continue
}
actual, err := rm.Select(testcase.target)
assert.NoError(t, err)
assert.Equalf(
t, testcase.count, len(actual), "test=%s target=%v", n, testcase.target)
}

}
12 changes: 9 additions & 3 deletions kyaml/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MYGOBIN := $(shell go env GOPATH)/bin
export PATH := $(MYGOBIN):$(PATH)

.PHONY: generate license fix vet fmt test lint tidy
.PHONY: generate license fix vet fmt test lint tidy clean
all: generate license fix vet fmt test lint tidy

fix:
Expand All @@ -25,11 +25,17 @@ tidy:
go mod tidy

lint:
(which $(MYGOBIN)/golangci-lint || go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.19.1)
$(MYGOBIN)/golangci-lint run ./...
(which $(MYGOBIN)/golangci-lint || \
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.19.1)
$(MYGOBIN)/golangci-lint \
--skip-dirs yaml/internal/k8sgen/pkg \
run ./...

test:
go test -cover ./...

vet:
go vet ./...

clean:
rm -rf yaml/internal/k8sgen/pkg
1 change: 1 addition & 0 deletions kyaml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ require (
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
k8s.io/apimachinery v0.17.0 // indirect
)
Loading

0 comments on commit 47d362b

Please sign in to comment.