Skip to content

Commit 77b1af2

Browse files
authored
Merge pull request #4352 from natasha41575/FilterErrors
prevent internal annotations from showing up in the errors thrown by filters
2 parents bb0a520 + c659306 commit 77b1af2

File tree

7 files changed

+26
-73
lines changed

7 files changed

+26
-73
lines changed

api/filters/fieldspec/fieldspec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) {
5151
}
5252
fltr.path = utils.PathSplitter(fltr.FieldSpec.Path, "/")
5353
if err := fltr.filter(obj); err != nil {
54-
s, _ := obj.String()
5554
return nil, errors.WrapPrefixf(err,
56-
"considering field '%s' of object\n%v", fltr.FieldSpec.Path, s)
55+
"considering field '%s' of object %s", fltr.FieldSpec.Path, resid.FromRNode(obj))
5756
}
5857
return obj, nil
5958
}

api/filters/fieldspec/fieldspec_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ apiVersion: foo
5959
kind: Bar
6060
xxx:
6161
`,
62-
error: `considering field '' of object
63-
apiVersion: foo/v1
64-
kind: Bar
65-
xxx:
66-
metadata:
67-
annotations:
68-
internal.config.kubernetes.io/annotations-migration-resource-id: '0'
69-
: cannot set or create an empty field name`,
62+
error: `considering field '' of object Bar.v1.foo/[noName].[noNs]: cannot set or create an empty field name`,
7063
filter: fieldspec.Filter{
7164
SetValue: filtersutil.SetScalar("e"),
7265
},
@@ -219,14 +212,7 @@ kind: Bar
219212
a:
220213
b: a
221214
`,
222-
error: `considering field 'a/b/c' of object
223-
kind: Bar
224-
a:
225-
b: a
226-
metadata:
227-
annotations:
228-
internal.config.kubernetes.io/annotations-migration-resource-id: '0'
229-
: expected sequence or mapping node`,
215+
error: `considering field 'a/b/c' of object Bar.[noVer].[noGrp]/[noName].[noNs]: expected sequence or mapping node`,
230216
filter: fieldspec.Filter{
231217
SetValue: filtersutil.SetScalar("e"),
232218
},

api/filters/refvar/refvar_test.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,7 @@ metadata:
243243
data:
244244
slice:
245245
- false`,
246-
expectedError: `considering field 'data/slice' of object
247-
apiVersion: apps/v1
248-
kind: Deployment
249-
metadata:
250-
name: dep
251-
annotations:
252-
config.kubernetes.io/index: '0'
253-
internal.config.kubernetes.io/index: '0'
254-
internal.config.kubernetes.io/annotations-migration-resource-id: '0'
255-
data:
256-
slice:
257-
- false
258-
: invalid value type expect a string`,
246+
expectedError: `considering field 'data/slice' of object Deployment.v1.apps/dep.[noNs]: invalid value type expect a string`,
259247
filter: Filter{
260248
MappingFunc: makeMf(map[string]interface{}{
261249
"VAR": int64(5),
@@ -271,18 +259,7 @@ metadata:
271259
name: dep
272260
data:
273261
1: str`,
274-
expectedError: `considering field 'data' of object
275-
apiVersion: apps/v1
276-
kind: Deployment
277-
metadata:
278-
name: dep
279-
annotations:
280-
config.kubernetes.io/index: '0'
281-
internal.config.kubernetes.io/index: '0'
282-
internal.config.kubernetes.io/annotations-migration-resource-id: '0'
283-
data:
284-
1: str
285-
: invalid map key: value='1', tag='` + yaml.NodeTagInt + `'`,
262+
expectedError: `considering field 'data' of object Deployment.v1.apps/dep.[noNs]: invalid map key: value='1', tag='` + yaml.NodeTagInt + `'`,
286263
filter: Filter{
287264
MappingFunc: makeMf(map[string]interface{}{
288265
"VAR": int64(5),

api/internal/accumulator/namereferencetransformer_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,10 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
520520
},
521521
},
522522
}).ResMap(),
523-
expectedErr: `updating name reference in 'rules/resourceNames' field of ` +
524-
`'ClusterRole.v1.rbac.authorization.k8s.io/cr.[noNs]'` +
525-
`: considering field 'rules/resourceNames' of object
526-
apiVersion: rbac.authorization.k8s.io/v1
527-
kind: ClusterRole
528-
metadata:
529-
name: cr
530-
rules:
531-
- resourceNames:
532-
foo: bar
533-
resources:
534-
- secrets
535-
: visit traversal on path: [resourceNames]: path config error; no 'name' field in node`},
523+
expectedErr: `updating name reference in 'rules/resourceNames' field of 'ClusterRole.v1.rbac.authorization.k8s.io/cr.[noNs]': ` +
524+
`considering field 'rules/resourceNames' of object ClusterRole.v1.rbac.authorization.k8s.io/cr.[noNs]: visit traversal on ` +
525+
`path: [resourceNames]: path config error; no 'name' field in node`,
526+
},
536527
}
537528

538529
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference)

api/internal/accumulator/refvartransformer_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,7 @@ func TestRefVarTransformer(t *testing.T) {
120120
"slice": []interface{}{5}, // noticeably *not* a []string
121121
}}).ResMap(),
122122
},
123-
errMessage: `considering field 'data/slice' of object
124-
apiVersion: v1
125-
data:
126-
slice:
127-
- 5
128-
kind: ConfigMap
129-
metadata:
130-
name: cm1
131-
: invalid value type expect a string`,
123+
errMessage: `considering field 'data/slice' of object ConfigMap.v1.[noGrp]/cm1.[noNs]: invalid value type expect a string`,
132124
},
133125
"var replacement in nil": {
134126
given: given{

kyaml/resid/resid.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package resid
66
import (
77
"reflect"
88
"strings"
9+
10+
"sigs.k8s.io/kustomize/kyaml/yaml"
911
)
1012

1113
// ResId is an identifier of a k8s resource object.
@@ -63,7 +65,7 @@ func FromString(s string) ResId {
6365
gvk := GvkFromString(values[0])
6466

6567
values = strings.Split(values[1], fieldSep)
66-
last := len(values)-1
68+
last := len(values) - 1
6769

6870
ns := values[last]
6971
if ns == noNamespace {
@@ -80,6 +82,13 @@ func FromString(s string) ResId {
8082
}
8183
}
8284

85+
// FromRNode returns the ResId for the RNode
86+
func FromRNode(rn *yaml.RNode) ResId {
87+
group, version := ParseGroupVersion(rn.GetApiVersion())
88+
return NewResIdWithNamespace(
89+
Gvk{Group: group, Version: version, Kind: rn.GetKind()}, rn.GetName(), rn.GetNamespace())
90+
}
91+
8392
// GvknEquals returns true if the other id matches
8493
// Group/Version/Kind/name.
8594
func (id ResId) GvknEquals(o ResId) bool {

kyaml/resid/resid_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ var resIdStringTests = []struct {
6060
ResId{},
6161
"[noKind].[noVer].[noGrp]/[noName].[noNs]",
6262
},
63-
6463
}
6564

6665
func TestResIdString(t *testing.T) {
@@ -271,18 +270,18 @@ var ids = []ResId{
271270
},
272271
{
273272
Gvk: Gvk{
274-
Group: "rbac.authorization.k8s.io",
275-
Version: "v1",
276-
Kind: "ClusterRole",
273+
Group: "rbac.authorization.k8s.io",
274+
Version: "v1",
275+
Kind: "ClusterRole",
277276
isClusterScoped: true,
278277
},
279278
Name: "nm",
280279
},
281280
{
282281
Gvk: Gvk{
283-
Group: "rbac.authorization.k8s.io",
284-
Version: "v1",
285-
Kind: "ClusterRole",
282+
Group: "rbac.authorization.k8s.io",
283+
Version: "v1",
284+
Kind: "ClusterRole",
286285
isClusterScoped: true,
287286
},
288287
Name: "my.name",

0 commit comments

Comments
 (0)