Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: return error instead of log at FromMapAndOption #5381

Merged
merged 10 commits into from
Oct 27, 2023
5 changes: 3 additions & 2 deletions api/internal/plugins/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func makeConfigMap(rf *resource.Factory, name, behavior string, hashValue *strin
return r
}

func makeConfigMapOptions(rf *resource.Factory, name, behavior string, disableHash bool) *resource.Resource {
func makeConfigMapOptions(rf *resource.Factory, name, behavior string, disableHash bool) (*resource.Resource, error) {
return rf.FromMapAndOption(map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
Expand Down Expand Up @@ -89,7 +89,8 @@ func TestUpdateResourceOptions(t *testing.T) {
name := fmt.Sprintf("test%d", i)
err := in.Append(makeConfigMap(rf, name, c.behavior, c.hashValue))
require.NoError(t, err)
err = expected.Append(makeConfigMapOptions(rf, name, c.behavior, !c.needsHash))
config := makeConfigMap(rf, name, c.behavior, c.hashValue)
err = expected.Append(config)
require.NoError(t, err)
}
actual, err := UpdateResourceOptions(in)
Expand Down
47 changes: 30 additions & 17 deletions api/resmap/reswrangler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ func TestAppendAll(t *testing.T) {
}
}

func makeMap1() ResMap {
return rmF.FromResource(rf.FromMapAndOption(
func makeMap1(t *testing.T) ResMap {
r, err := rf.FromMapAndOption(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "ConfigMap",
Expand All @@ -836,11 +836,15 @@ func makeMap1() ResMap {
},
}, &types.GeneratorArgs{
Behavior: "create",
}))
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
return rmF.FromResource(r)
}

func makeMap2(b types.GenerationBehavior) ResMap {
return rmF.FromResource(rf.FromMapAndOption(
func makeMap2(b types.GenerationBehavior, t *testing.T) ResMap {
r, err := rf.FromMapAndOption(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "ConfigMap",
Expand All @@ -854,14 +858,19 @@ func makeMap2(b types.GenerationBehavior) ResMap {
},
}, &types.GeneratorArgs{
Behavior: b.String(),
}))
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same too

}
return rmF.FromResource(r)
}

func TestAbsorbAll(t *testing.T) {
metadata := map[string]interface{}{
"name": "cmap",
}
expected := rmF.FromResource(rf.FromMapAndOption(

r, err := rf.FromMapAndOption(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "ConfigMap",
Expand All @@ -874,24 +883,28 @@ func TestAbsorbAll(t *testing.T) {
},
&types.GeneratorArgs{
Behavior: "create",
}))
w := makeMap1()
assert.NoError(t, w.AbsorbAll(makeMap2(types.BehaviorMerge)))
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same too

}
expected := rmF.FromResource(r)
w := makeMap1(t)
assert.NoError(t, w.AbsorbAll(makeMap2(types.BehaviorMerge, t)))
expected.RemoveBuildAnnotations()
w.RemoveBuildAnnotations()
assert.NoError(t, expected.ErrorIfNotEqualLists(w))
w = makeMap1()
w = makeMap1(t)
assert.NoError(t, w.AbsorbAll(nil))
assert.NoError(t, w.ErrorIfNotEqualLists(makeMap1()))
assert.NoError(t, w.ErrorIfNotEqualLists(makeMap1(t)))

w = makeMap1()
w2 := makeMap2(types.BehaviorReplace)
w = makeMap1(t)
w2 := makeMap2(types.BehaviorReplace, t)
assert.NoError(t, w.AbsorbAll(w2))
w2.RemoveBuildAnnotations()
assert.NoError(t, w2.ErrorIfNotEqualLists(w))
w = makeMap1()
w2 = makeMap2(types.BehaviorUnspecified)
err := w.AbsorbAll(w2)
w = makeMap1(t)
w2 = makeMap2(types.BehaviorUnspecified, t)
err = w.AbsorbAll(w2)
assert.Error(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can, Could you summarize it in one line?

assert.True(
t, strings.Contains(err.Error(), "behavior must be merge or replace"))
Expand Down
18 changes: 12 additions & 6 deletions api/resource/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func (rf *Factory) Hasher() ifc.KustHasher {

// FromMap returns a new instance of Resource.
func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
return rf.FromMapAndOption(m, nil)
res, err := rf.FromMapAndOption(m, nil)
if err != nil {
log.Fatal("Option must not be null")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this line abandoned a non-nil error.
Could you make a change to print the substance of the returned error?

}
return res
}

// FromMapWithName returns a new instance with the given "original" name.
Expand All @@ -52,19 +56,21 @@ func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource

// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
r := rf.FromMapAndOption(m, nil)
r, err := rf.FromMapAndOption(m, nil)
if err != nil {
log.Fatal("Option must not be null")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
return r.setPreviousId(ns, n, r.GetKind())
}

// FromMapAndOption returns a new instance of Resource with given options.
func (rf *Factory) FromMapAndOption(
m map[string]interface{}, args *types.GeneratorArgs) *Resource {
m map[string]interface{}, args *types.GeneratorArgs) (*Resource, error) {
n, err := yaml.FromMap(m)
if err != nil {
// TODO: return err instead of log.
log.Fatal(err)
return nil, err
}
return rf.makeOne(n, args)
return rf.makeOne(n, args), nil
}

// makeOne returns a new instance of Resource.
Expand Down