Skip to content

Commit fa40846

Browse files
committed
Return a typed error in case DecodePluginConfig was unable to find the provided key
Signed-off-by: Adrian Orive Oneca <[email protected]>
1 parent 479367b commit fa40846

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

pkg/config/errors.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,38 @@ func (e UnsupportedVersionError) Error() string {
3232
return fmt.Sprintf("version %s is not supported", e.Version)
3333
}
3434

35-
// UnsupportedField is returned when a project configuration version does not support
35+
// UnsupportedFieldError is returned when a project configuration version does not support
3636
// one of the fields as interface must be common for all the versions
37-
type UnsupportedField struct {
37+
type UnsupportedFieldError struct {
3838
Version Version
3939
Field string
4040
}
4141

4242
// Error implements error interface
43-
func (e UnsupportedField) Error() string {
43+
func (e UnsupportedFieldError) Error() string {
4444
return fmt.Sprintf("version %s does not support the %s field", e.Version, e.Field)
4545
}
4646

47-
// UnknownResource is returned by Config.GetResource when the provided GVK cannot be found
48-
type UnknownResource struct {
47+
// ResourceNotFoundError is returned by Config.GetResource when the provided GVK cannot be found
48+
type ResourceNotFoundError struct {
4949
GVK resource.GVK
5050
}
5151

5252
// Error implements error interface
53-
func (e UnknownResource) Error() string {
53+
func (e ResourceNotFoundError) Error() string {
5454
return fmt.Sprintf("resource %v could not be found", e.GVK)
5555
}
5656

57+
// PluginKeyNotFoundError is returned by Config.DecodePluginConfig when the provided key cannot be found
58+
type PluginKeyNotFoundError struct {
59+
Key string
60+
}
61+
62+
// Error implements error interface
63+
func (e PluginKeyNotFoundError) Error() string {
64+
return fmt.Sprintf("plugin key %q could not be found", e.Key)
65+
}
66+
5767
// MarshalError is returned by Config.Marshal when something went wrong while marshalling to YAML
5868
type MarshalError struct {
5969
Err error

pkg/config/errors_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ var _ = Describe("UnsupportedVersionError", func() {
3737
})
3838
})
3939

40-
var _ = Describe("UnsupportedField", func() {
41-
var err = UnsupportedField{
40+
var _ = Describe("UnsupportedFieldError", func() {
41+
var err = UnsupportedFieldError{
4242
Version: Version{Number: 1},
4343
Field: "name",
4444
}
@@ -50,8 +50,8 @@ var _ = Describe("UnsupportedField", func() {
5050
})
5151
})
5252

53-
var _ = Describe("UnknownResource", func() {
54-
var err = UnknownResource{
53+
var _ = Describe("ResourceNotFoundError", func() {
54+
var err = ResourceNotFoundError{
5555
GVK: resource.GVK{
5656
Group: "group",
5757
Domain: "my.domain",
@@ -67,6 +67,18 @@ var _ = Describe("UnknownResource", func() {
6767
})
6868
})
6969

70+
var _ = Describe("PluginKeyNotFoundError", func() {
71+
var err = PluginKeyNotFoundError{
72+
Key: "go.kubebuilder.io/v1",
73+
}
74+
75+
Context("Error", func() {
76+
It("should return the correct error message", func() {
77+
Expect(err.Error()).To(Equal("plugin key \"go.kubebuilder.io/v1\" could not be found"))
78+
})
79+
})
80+
})
81+
7082
var _ = Describe("MarshalError", func() {
7183
var (
7284
wrapped = fmt.Errorf("error message")

pkg/config/v2/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (c cfg) GetProjectName() string {
8888

8989
// SetProjectName implements config.Config
9090
func (c *cfg) SetProjectName(string) error {
91-
return config.UnsupportedField{
91+
return config.UnsupportedFieldError{
9292
Version: Version,
9393
Field: "project name",
9494
}
@@ -101,7 +101,7 @@ func (c cfg) GetLayout() string {
101101

102102
// SetLayout implements config.Config
103103
func (c *cfg) SetLayout(string) error {
104-
return config.UnsupportedField{
104+
return config.UnsupportedFieldError{
105105
Version: Version,
106106
Field: "layout",
107107
}
@@ -131,15 +131,15 @@ func (c cfg) IsComponentConfig() bool {
131131

132132
// SetComponentConfig implements config.Config
133133
func (c *cfg) SetComponentConfig() error {
134-
return config.UnsupportedField{
134+
return config.UnsupportedFieldError{
135135
Version: Version,
136136
Field: "component config",
137137
}
138138
}
139139

140140
// ClearComponentConfig implements config.Config
141141
func (c *cfg) ClearComponentConfig() error {
142-
return config.UnsupportedField{
142+
return config.UnsupportedFieldError{
143143
Version: Version,
144144
Field: "component config",
145145
}
@@ -175,7 +175,7 @@ func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
175175
}
176176
}
177177

178-
return resource.Resource{}, config.UnknownResource{GVK: gvk}
178+
return resource.Resource{}, config.ResourceNotFoundError{GVK: gvk}
179179
}
180180

181181
// GetResources implements config.Config
@@ -234,15 +234,15 @@ func (c cfg) IsWebhookVersionCompatible(webhookVersion string) bool {
234234

235235
// DecodePluginConfig implements config.Config
236236
func (c cfg) DecodePluginConfig(string, interface{}) error {
237-
return config.UnsupportedField{
237+
return config.UnsupportedFieldError{
238238
Version: Version,
239239
Field: "plugins",
240240
}
241241
}
242242

243243
// EncodePluginConfig implements config.Config
244244
func (c cfg) EncodePluginConfig(string, interface{}) error {
245-
return config.UnsupportedField{
245+
return config.UnsupportedFieldError{
246246
Version: Version,
247247
Field: "plugins",
248248
}

pkg/config/v3/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
180180
}
181181
}
182182

183-
return resource.Resource{}, config.UnknownResource{GVK: gvk}
183+
return resource.Resource{}, config.ResourceNotFoundError{GVK: gvk}
184184
}
185185

186186
// GetResources implements config.Config
@@ -283,7 +283,7 @@ func (c cfg) resourceAPIVersionCompatible(verType, version string) bool {
283283
// DecodePluginConfig implements config.Config
284284
func (c cfg) DecodePluginConfig(key string, configObj interface{}) error {
285285
if len(c.Plugins) == 0 {
286-
return nil
286+
return config.PluginKeyNotFoundError{Key: key}
287287
}
288288

289289
// Get the object blob by key and unmarshal into the object.
@@ -295,9 +295,10 @@ func (c cfg) DecodePluginConfig(key string, configObj interface{}) error {
295295
if err := yaml.Unmarshal(b, configObj); err != nil {
296296
return fmt.Errorf("failed to unmarshal extra fields object: %w", err)
297297
}
298+
return nil
298299
}
299300

300-
return nil
301+
return config.PluginKeyNotFoundError{Key: key}
301302
}
302303

303304
// EncodePluginConfig will return an error if used on any project version < v3.

pkg/config/v3/config_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ limitations under the License.
1717
package v3
1818

1919
import (
20+
"errors"
2021
"testing"
2122

2223
. "github.com/onsi/ginkgo"
2324
. "github.com/onsi/ginkgo/extensions/table"
2425
. "github.com/onsi/gomega"
2526

27+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2628
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2729
)
2830

@@ -390,13 +392,19 @@ var _ = Describe("cfg", func() {
390392
}
391393
)
392394

395+
It("DecodePluginConfig should fail for no plugin config object", func() {
396+
var pluginConfig PluginConfig
397+
err := c0.DecodePluginConfig(key, &pluginConfig)
398+
Expect(err).To(HaveOccurred())
399+
Expect(errors.As(err, &config.PluginKeyNotFoundError{})).To(BeTrue())
400+
})
401+
393402
DescribeTable("DecodePluginConfig should retrieve the plugin data correctly",
394403
func(inputConfig cfg, expectedPluginConfig PluginConfig) {
395404
var pluginConfig PluginConfig
396405
Expect(inputConfig.DecodePluginConfig(key, &pluginConfig)).To(Succeed())
397406
Expect(pluginConfig).To(Equal(expectedPluginConfig))
398407
},
399-
Entry("for no plugin config object", c0, nil),
400408
Entry("for an empty plugin config object", c1, PluginConfig{}),
401409
Entry("for a full plugin config object", c2, pluginConfig),
402410
// TODO (coverage): add cases where yaml.Marshal returns an error

0 commit comments

Comments
 (0)