Skip to content

Commit 5335b3c

Browse files
committed
Add label and annotation selector code.
Closes kubernetes-sigs#2893
1 parent 4af4483 commit 5335b3c

File tree

21 files changed

+2847
-43
lines changed

21 files changed

+2847
-43
lines changed

Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,6 @@ build-kustomize-api: $(builtinplugins)
222222
generate-kustomize-api:
223223
cd api; go generate ./...
224224

225-
.PHONY: clean-kustomize-api
226-
clean-kustomize-api:
227-
228225
.PHONY: test-unit-kustomize-api
229226
test-unit-kustomize-api: build-kustomize-api
230227
cd api; go test ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222"

api/internal/wrappy/wnode.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ func (wn *WNode) MarshalJSON() ([]byte, error) {
163163
}
164164

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

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

175175
// SetAnnotations implements ifc.Kunstructured.

api/resmap/selector_test.go

+28-30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package resmap_test
66
import (
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
910
"sigs.k8s.io/kustomize/api/resid"
1011
. "sigs.k8s.io/kustomize/api/resmap"
1112
"sigs.k8s.io/kustomize/api/types"
@@ -48,38 +49,36 @@ metadata:
4849
name: x-name1
4950
namespace: x-default
5051
`))
51-
if err != nil {
52-
t.Fatalf("unexpected error %v", err)
53-
}
52+
assert.NoError(t, err)
5453
return result
5554
}
5655

5756
func TestFindPatchTargets(t *testing.T) {
5857
rm := setupRMForPatchTargets(t)
59-
testcases := []struct {
58+
testcases := map[string]struct {
6059
target types.Selector
6160
count int
6261
}{
63-
{
62+
"hey01": {
6463
target: types.Selector{
6564
Name: "name.*",
6665
},
6766
count: 3,
6867
},
69-
{
68+
"hey02": {
7069
target: types.Selector{
7170
Name: "name.*",
7271
AnnotationSelector: "foo=bar",
7372
},
7473
count: 2,
7574
},
76-
{
75+
"hey03": {
7776
target: types.Selector{
7877
LabelSelector: "app=name1",
7978
},
8079
count: 1,
8180
},
82-
{
81+
"hey04": {
8382
target: types.Selector{
8483
Gvk: resid.Gvk{
8584
Kind: "Kind1",
@@ -88,31 +87,31 @@ func TestFindPatchTargets(t *testing.T) {
8887
},
8988
count: 2,
9089
},
91-
{
90+
"hey05": {
9291
target: types.Selector{
9392
Name: "NotMatched",
9493
},
9594
count: 0,
9695
},
97-
{
96+
"hey06": {
9897
target: types.Selector{
9998
Name: "",
10099
},
101100
count: 4,
102101
},
103-
{
102+
"hey07": {
104103
target: types.Selector{
105104
Namespace: "default",
106105
},
107106
count: 2,
108107
},
109-
{
108+
"hey08": {
110109
target: types.Selector{
111110
Namespace: "",
112111
},
113112
count: 4,
114113
},
115-
{
114+
"hey09": {
116115
target: types.Selector{
117116
Namespace: "default",
118117
Name: "name.*",
@@ -122,69 +121,68 @@ func TestFindPatchTargets(t *testing.T) {
122121
},
123122
count: 1,
124123
},
125-
{
124+
"hey10": {
126125
target: types.Selector{
127126
Name: "^name.*",
128127
},
129128
count: 3,
130129
},
131-
{
130+
"hey11": {
132131
target: types.Selector{
133132
Name: "name.*$",
134133
},
135134
count: 3,
136135
},
137-
{
136+
"hey12": {
138137
target: types.Selector{
139138
Name: "^name.*$",
140139
},
141140
count: 3,
142141
},
143-
{
142+
"hey13": {
144143
target: types.Selector{
145144
Namespace: "^def.*",
146145
},
147146
count: 2,
148147
},
149-
{
148+
"hey14": {
150149
target: types.Selector{
151150
Namespace: "def.*$",
152151
},
153152
count: 2,
154153
},
155-
{
154+
"hey15": {
156155
target: types.Selector{
157156
Namespace: "^def.*$",
158157
},
159158
count: 2,
160159
},
161-
{
160+
"hey16": {
162161
target: types.Selector{
163162
Namespace: "default",
164163
},
165164
count: 2,
166165
},
167-
{
166+
"hey17": {
168167
target: types.Selector{
169168
Namespace: "NotMatched",
170169
},
171170
count: 0,
172171
},
173-
{
172+
"hey18": {
174173
target: types.Selector{
175174
Namespace: "ns1",
176175
},
177176
count: 1,
178177
},
179178
}
180-
for _, testcase := range testcases {
181-
actual, err := rm.Select(testcase.target)
182-
if err != nil {
183-
t.Errorf("unexpected error %v", err)
184-
}
185-
if len(actual) != testcase.count {
186-
t.Errorf("expected %d objects, but got %d:\n%v", testcase.count, len(actual), actual)
179+
for n, testcase := range testcases {
180+
if n !="hey03" {
181+
continue
187182
}
183+
actual, err := rm.Select(testcase.target)
184+
assert.NoError(t, err)
185+
assert.Equalf(
186+
t, testcase.count, len(actual), "test=%s target=%v", n, testcase.target)
188187
}
189-
190188
}

kyaml/Makefile

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
MYGOBIN := $(shell go env GOPATH)/bin
55
export PATH := $(MYGOBIN):$(PATH)
66

7-
.PHONY: generate license fix vet fmt test lint tidy
7+
.PHONY: generate license fix vet fmt test lint tidy clean
88
all: generate license fix vet fmt test lint tidy
99

1010
fix:
@@ -25,11 +25,17 @@ tidy:
2525
go mod tidy
2626

2727
lint:
28-
(which $(MYGOBIN)/golangci-lint || go get github.com/golangci/golangci-lint/cmd/[email protected])
29-
$(MYGOBIN)/golangci-lint run ./...
28+
(which $(MYGOBIN)/golangci-lint || \
29+
go get github.com/golangci/golangci-lint/cmd/[email protected])
30+
$(MYGOBIN)/golangci-lint \
31+
--skip-dirs yaml/internal/k8sgen/pkg \
32+
run ./...
3033

3134
test:
3235
go test -cover ./...
3336

3437
vet:
3538
go vet ./...
39+
40+
clean:
41+
rm -rf yaml/internal/k8sgen/pkg

kyaml/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ require (
1919
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
2020
gopkg.in/yaml.v2 v2.3.0
2121
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
22+
k8s.io/apimachinery v0.17.0 // indirect
2223
)

0 commit comments

Comments
 (0)