Skip to content

Commit 304dab0

Browse files
refactor: always use strict unmarshalling for LoadFunctionConfig and related processors
1 parent e963d62 commit 304dab0

File tree

2 files changed

+9
-38
lines changed

2 files changed

+9
-38
lines changed

kyaml/fn/framework/processors.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ type SimpleProcessor struct {
3232
// Config must be a struct capable of receiving the data from ResourceList.functionConfig.
3333
// Filter functions may close over this struct to access its data.
3434
Config interface{}
35-
// Strict indicates whether the processor should use strict unmarshalling when loading the Config.
36-
Strict bool
3735
}
3836

3937
// Process makes SimpleProcessor implement the ResourceListProcessor interface.
4038
// It loads the ResourceList.functionConfig into the provided Config type, applying
4139
// defaulting and validation if supported by Config. It then executes the processor's filter.
4240
func (p SimpleProcessor) Process(rl *ResourceList) error {
43-
if err := LoadFunctionConfig(rl.FunctionConfig, p.Config, p.Strict); err != nil {
41+
if err := LoadFunctionConfig(rl.FunctionConfig, p.Config); err != nil {
4442
return errors.WrapPrefixf(err, "loading function config")
4543
}
4644
return errors.WrapPrefixf(rl.Filter(p.Filter), "processing filter")
@@ -113,7 +111,7 @@ func (p *VersionedAPIProcessor) Process(rl *ResourceList) error {
113111
if err != nil {
114112
return errors.WrapPrefixf(err, "unable to identify provider for resource")
115113
}
116-
if err := LoadFunctionConfig(rl.FunctionConfig, api, false); err != nil {
114+
if err := LoadFunctionConfig(rl.FunctionConfig, api); err != nil {
117115
return errors.Wrap(err)
118116
}
119117
return errors.Wrap(rl.Filter(api))
@@ -141,7 +139,7 @@ func extractGVK(src *yaml.RNode) (apiVersion, kind string) {
141139
// LoadFunctionConfig reads a configuration resource from YAML into the provided data structure
142140
// and then prepares it for use by running defaulting and validation on it, if supported.
143141
// ResourceListProcessors should use this function to load ResourceList.functionConfig.
144-
func LoadFunctionConfig(src *yaml.RNode, api interface{}, strict bool) error {
142+
func LoadFunctionConfig(src *yaml.RNode, api interface{}) error {
145143
if api == nil {
146144
return nil
147145
}
@@ -159,12 +157,8 @@ func LoadFunctionConfig(src *yaml.RNode, api interface{}, strict bool) error {
159157
// using sigs.k8s.io/yaml here lets the custom types embed core types
160158
// that only have json tags, notably types from k8s.io/apimachinery/pkg/apis/meta/v1
161159

162-
var err error
163-
if strict {
164-
err = k8syaml.UnmarshalStrict([]byte(src.MustString()), api)
165-
} else {
166-
err = k8syaml.Unmarshal([]byte(src.MustString()), api)
167-
}
160+
err := k8syaml.UnmarshalStrict([]byte(src.MustString()), api)
161+
168162
if err != nil {
169163
if schemaValidationError != nil {
170164
// if we got a validation error, report it instead as it is likely a nicer version of the same message
@@ -304,7 +298,7 @@ func (tp TemplateProcessor) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
304298
// directly as a processor. As a Processor, it loads the ResourceList.functionConfig into the
305299
// TemplateData field, exposing it to all templates by default.
306300
func (tp TemplateProcessor) Process(rl *ResourceList) error {
307-
if err := LoadFunctionConfig(rl.FunctionConfig, tp.TemplateData, false); err != nil {
301+
if err := LoadFunctionConfig(rl.FunctionConfig, tp.TemplateData); err != nil {
308302
return errors.Wrap(err)
309303
}
310304
return errors.Wrap(rl.Filter(tp))

kyaml/fn/framework/processors_test.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,6 @@ func TestLoadFunctionConfig(t *testing.T) {
722722
name string
723723
src *yaml.RNode
724724
api interface{}
725-
strict bool
726725
want interface{}
727726
wantErrMsgs []string
728727
}{
@@ -826,16 +825,6 @@ test: true
826825
api: &jsonTagTest{},
827826
want: &jsonTagTest{Name: "tester", Test: true},
828827
},
829-
{
830-
name: "successfully loads types that include fields only tagged with json markers, strict mode",
831-
src: yaml.MustParse(`
832-
name: tester
833-
test: true
834-
`),
835-
api: &jsonTagTest{},
836-
strict: true,
837-
want: &jsonTagTest{Name: "tester", Test: true},
838-
},
839828
{
840829
name: "successfully loads types that include fields only tagged with yaml markers",
841830
src: yaml.MustParse(`
@@ -846,42 +835,30 @@ test: true
846835
want: &yamlTagTest{Name: "tester", Test: true},
847836
},
848837
{
849-
name: "successfully loads types that include fields only tagged with yaml markers, strict mode",
850-
src: yaml.MustParse(`
851-
name: tester
852-
test: true
853-
`),
854-
api: &yamlTagTest{},
855-
strict: true,
856-
want: &yamlTagTest{Name: "tester", Test: true},
857-
},
858-
{
859-
name: "strict mode fails on unknown fields on types with yaml tags",
838+
name: "fails on unknown fields on types with yaml tags",
860839
src: yaml.MustParse(`
861840
name: tester
862841
test: true
863842
unknown: field
864843
`),
865844
api: &yamlTagTest{},
866-
strict: true,
867845
wantErrMsgs: []string{`error unmarshaling JSON: while decoding JSON: json: unknown field "unknown"`},
868846
},
869847
{
870-
name: "strict mode fails on unknown fields on types with json tags",
848+
name: "fails on unknown fields on types with json tags",
871849
src: yaml.MustParse(`
872850
name: tester
873851
test: true
874852
unknown: field
875853
`),
876854
api: &jsonTagTest{},
877-
strict: true,
878855
wantErrMsgs: []string{`error unmarshaling JSON: while decoding JSON: json: unknown field "unknown"`},
879856
},
880857
}
881858

882859
for _, tt := range tests {
883860
t.Run(tt.name, func(t *testing.T) {
884-
err := framework.LoadFunctionConfig(tt.src, tt.api, tt.strict)
861+
err := framework.LoadFunctionConfig(tt.src, tt.api)
885862
if len(tt.wantErrMsgs) == 0 {
886863
require.NoError(t, err)
887864
require.Equal(t, tt.want, tt.api)

0 commit comments

Comments
 (0)