Skip to content

fix CSV unmarshaling error with custom unmarshaler with pretty errors #1138

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GO := go
CMDS := $(addprefix bin/, $(shell ls ./cmd | grep -v opm))
OPM := $(addprefix bin/, opm)
SPECIFIC_UNIT_TEST := $(if $(TEST),-run $(TEST),)
SPECIFIC_SKIP_UNIT_TEST := $(if $(SKIP),-skip $(SKIP),)
extra_env := $(GOENV)
export PKG := github.com/operator-framework/operator-registry
export GIT_COMMIT := $(or $(SOURCE_GIT_COMMIT),$(shell git rev-parse --short HEAD))
Expand Down Expand Up @@ -60,7 +61,7 @@ static: build

.PHONY: unit
unit:
$(GO) test -coverprofile=coverage.out $(SPECIFIC_UNIT_TEST) $(TAGS) $(TEST_RACE) -count=1 ./pkg/... ./alpha/...
$(GO) test -coverprofile=coverage.out $(SPECIFIC_UNIT_TEST) $(SPECIFIC_SKIP_UNIT_TEST) $(TAGS) $(TEST_RACE) -count=1 ./pkg/... ./alpha/...

.PHONY: sanity-check
sanity-check:
Expand Down
4 changes: 3 additions & 1 deletion alpha/declcfg/declcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"

prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"

"golang.org/x/text/cases"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -107,7 +109,7 @@ func (m *Meta) UnmarshalJSON(blob []byte) error {
// that eat our error type and return a generic error, such that we lose the
// ability to errors.As to get this error on the other side. For now, just return
// a string error that includes the pretty printed message.
return errors.New(newJSONUnmarshalError(blob, err).Pretty())
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(blob, err).Pretty())
}

// TODO: this function ensures we do not break backwards compatibility with
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package declcfg
package prettyunmarshaler

import (
"bytes"
Expand All @@ -8,29 +8,29 @@ import (
"strings"
)

type jsonUnmarshalError struct {
type JsonUnmarshalError struct {
data []byte
offset int64
err error
}

func newJSONUnmarshalError(data []byte, err error) *jsonUnmarshalError {
func NewJSONUnmarshalError(data []byte, err error) *JsonUnmarshalError {
var te *json.UnmarshalTypeError
if errors.As(err, &te) {
return &jsonUnmarshalError{data: data, offset: te.Offset, err: te}
return &JsonUnmarshalError{data: data, offset: te.Offset, err: te}
}
var se *json.SyntaxError
if errors.As(err, &se) {
return &jsonUnmarshalError{data: data, offset: se.Offset, err: se}
return &JsonUnmarshalError{data: data, offset: se.Offset, err: se}
}
return &jsonUnmarshalError{data: data, offset: -1, err: err}
return &JsonUnmarshalError{data: data, offset: -1, err: err}
}

func (e *jsonUnmarshalError) Error() string {
func (e *JsonUnmarshalError) Error() string {
return e.err.Error()
}

func (e *jsonUnmarshalError) Pretty() string {
func (e *JsonUnmarshalError) Pretty() string {
if len(e.data) == 0 || e.offset < 0 || e.offset > int64(len(e.data)) {
return e.err.Error()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package declcfg
package prettyunmarshaler

import (
"encoding/json"
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestJsonUnmarshalError(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
actualErr := newJSONUnmarshalError(tc.data, tc.inErr)
actualErr := NewJSONUnmarshalError(tc.data, tc.inErr)
assert.Equal(t, tc.expectErrorString, actualErr.Error())
assert.Equal(t, tc.expectPrettyString, actualErr.Pretty())
})
Expand Down
59 changes: 57 additions & 2 deletions pkg/registry/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package registry

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"

prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"

v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -82,7 +84,7 @@ type ClusterServiceVersion struct {
// ReadCSVFromBundleDirectory tries to parse every YAML file in the directory without inspecting sub-directories and
// returns a CSV. According to the strict one CSV per bundle rule, func returns an error if more than one CSV is found.
func ReadCSVFromBundleDirectory(bundleDir string) (*ClusterServiceVersion, error) {
dirContent, err := ioutil.ReadDir(bundleDir)
dirContent, err := os.ReadDir(bundleDir)
if err != nil {
return nil, fmt.Errorf("error reading bundle directory %s, %v", bundleDir, err)
}
Expand Down Expand Up @@ -412,3 +414,56 @@ func (csv *ClusterServiceVersion) GetSubstitutesFor() string {
}
return substitutesFor
}

func (csv *ClusterServiceVersion) UnmarshalJSON(data []byte) error {

if err := csv.UnmarshalSpec(data); err != nil {
return err
}
if err := csv.UnmarshalTypeMeta(data); err != nil {
return err
}
if err := csv.UnmarshalObjectMeta(data); err != nil {
return err
}

return nil
}

func (csv *ClusterServiceVersion) UnmarshalSpec(data []byte) error {
var m map[string]json.RawMessage
if err := json.Unmarshal(data, &m); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}

spec, ok := m["spec"]
if !ok {
return nil
}
csv.Spec = spec

return nil
}

func (csv *ClusterServiceVersion) UnmarshalTypeMeta(data []byte) error {
var t metav1.TypeMeta
if err := json.Unmarshal(data, &t); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}
csv.TypeMeta = t

return nil
}

func (csv *ClusterServiceVersion) UnmarshalObjectMeta(data []byte) error {
var o struct {
Metadata metav1.ObjectMeta `json:"metadata"`
}
if err := json.Unmarshal(data, &o); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}

csv.ObjectMeta = o.Metadata

return nil
}