refactor: replace custom decode function with runtime.DecodeInto for improved error handling in fileStorage (#2822)

- Updated Create, Get, and GuaranteedUpdate methods to use runtime.DecodeInto instead of a custom decode function.
- Enhanced error messages for better debugging.
- Commented out the old decode function for potential future reference.

Signed-off-by: redscholar <blacktiledhouse@gmail.com>
This commit is contained in:
liujian 2025-10-24 14:33:29 +08:00 committed by redscholar
parent 465f7cd7a7
commit 204fb6c525
No known key found for this signature in database
GPG Key ID: 5A4D7C7DB5D38D49
2 changed files with 8 additions and 21 deletions

View File

@ -84,7 +84,7 @@ GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT := $(abspath $(OUTPUT_TOOLS_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)) GOLANGCI_LINT := $(abspath $(OUTPUT_TOOLS_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER))
GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
GORELEASER_VER := $(shell cat .github/workflows/releaser.yaml | grep [[:space:]]version | sed 's/.*version: //') GORELEASER_VER := v2.5.1
GORELEASER_BIN := goreleaser GORELEASER_BIN := goreleaser
GORELEASER := $(abspath $(OUTPUT_TOOLS_DIR)/$(GORELEASER_BIN)-$(GORELEASER_VER)) GORELEASER := $(abspath $(OUTPUT_TOOLS_DIR)/$(GORELEASER_BIN)-$(GORELEASER_VER))
GORELEASER_PKG := github.com/goreleaser/goreleaser/v2 GORELEASER_PKG := github.com/goreleaser/goreleaser/v2

View File

@ -103,8 +103,10 @@ func (s fileStorage) Create(_ context.Context, key string, obj, out runtime.Obje
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to encode object %q", key) return errors.Wrapf(err, "failed to encode object %q", key)
} }
if err := decode(s.codec, data, out); err != nil { if out != nil {
return err if err := runtime.DecodeInto(s.codec, data, out); err != nil {
return errors.Wrapf(err, "unable to decode the created object data for %q", key)
}
} }
// render to file // render to file
if err := os.WriteFile(key+yamlSuffix, data, os.ModePerm); err != nil { if err := os.WriteFile(key+yamlSuffix, data, os.ModePerm); err != nil {
@ -156,7 +158,7 @@ func (s fileStorage) Get(_ context.Context, key string, _ apistorage.GetOptions,
return err return err
} }
return decode(s.codec, data, out) return runtime.DecodeInto(s.codec, data, out)
} }
// GetList local resource files. // GetList local resource files.
@ -416,9 +418,8 @@ func (s fileStorage) GuaranteedUpdate(ctx context.Context, key string, destinati
} }
// render to destination // render to destination
if destination != nil { if destination != nil {
err = decode(s.codec, data, destination) if err := runtime.DecodeInto(s.codec, data, destination); err != nil {
if err != nil { return errors.Wrapf(err, "unable to decode the updated object data for %q", key)
return err
} }
} }
// render to file // render to file
@ -481,20 +482,6 @@ func (s fileStorage) RequestWatchProgress(context.Context) error {
return nil return nil
} }
// decode decodes value of bytes into object. It will also set the object resource version to rev.
// On success, objPtr would be set to the object.
func decode(codec runtime.Codec, value []byte, objPtr runtime.Object) error {
if _, err := conversion.EnforcePtr(objPtr); err != nil {
return errors.Wrap(err, "failed to convert output object to pointer")
}
_, _, err := codec.Decode(value, nil, objPtr)
if err != nil {
return errors.Wrap(err, "failed to decode output object")
}
return nil
}
func getNewItem(listObj runtime.Object, v reflect.Value) runtime.Object { func getNewItem(listObj runtime.Object, v reflect.Value) runtime.Object {
// For unstructured lists with a target group/version, preserve the group/version in the instantiated list items // For unstructured lists with a target group/version, preserve the group/version in the instantiated list items
if unstructuredList, isUnstructured := listObj.(*unstructured.UnstructuredList); isUnstructured { if unstructuredList, isUnstructured := listObj.(*unstructured.UnstructuredList); isUnstructured {