Skip to content

Commit

Permalink
throw error instead of panic when replacements source.fieldPath doesn…
Browse files Browse the repository at this point in the history
…'t exist (#4166)

* test for missing source.fieldPath in replacements

* throw error instead of panic when replacements source.fieldPath doesn't exist
  • Loading branch information
natasha41575 committed Sep 3, 2021
1 parent f122fb1 commit 2bfc7cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions api/filters/replacement/replacement.go
Expand Up @@ -131,10 +131,11 @@ func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, err
if err != nil {
return nil, err
}
if !rn.IsNilOrEmpty() {
return getRefinedValue(r.Source.Options, rn)
if rn.IsNilOrEmpty() {
return nil, fmt.Errorf("fieldPath `%s` is missing for replacement source %s", r.Source.FieldPath, r.Source)
}
return rn, nil

return getRefinedValue(r.Source.Options, rn)
}

func getRefinedValue(options *types.FieldOptions, rn *yaml.RNode) (*yaml.RNode, error) {
Expand Down
32 changes: 32 additions & 0 deletions api/filters/replacement/replacement_test.go
Expand Up @@ -1556,6 +1556,38 @@ spec:
name: postgresdb
`,
},

"replacement source.fieldPath does not exist": {
input: `apiVersion: v1
kind: ConfigMap
metadata:
name: ports-from
data:
grpcPort: 8080
---
apiVersion: v1
kind: ConfigMap
metadata:
name: ports-to
data:
grpcPort: 8081
`,
replacements: `replacements:
- source:
kind: ConfigMap
name: ports-from
fieldPath: data.httpPort
targets:
- select:
kind: ConfigMap
name: ports-to
fieldPaths:
- data.grpcPort
options:
create: true
`,
expectedErr: "fieldPath `data.httpPort` is missing for replacement source ~G_~V_ConfigMap|~X|ports-from:data.httpPort",
},
}

for tn, tc := range testCases {
Expand Down

0 comments on commit 2bfc7cc

Please sign in to comment.