Skip to content

Commit

Permalink
Merge pull request openshift#461 from grokspawn/veneer-template-rename
Browse files Browse the repository at this point in the history
OPECO-2737: Veneer template rename
  • Loading branch information
openshift-merge-robot authored Mar 22, 2023
2 parents 88f2e1e + bd18612 commit 3a82ea6
Show file tree
Hide file tree
Showing 45 changed files with 3,655 additions and 175 deletions.
2 changes: 2 additions & 0 deletions operator-registry.blacklist
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
0b2f43f426f9d621cab7a5224fc260ae3265be1c
eb2e6393796f397e93618c4aa5038f6a366cdb31
6e014a3aa10f1754f7ac89ee7c0dd435f85b9d07
25a478d9040b0f1fd50c5cbae570b013af01c73f
6 changes: 3 additions & 3 deletions staging/operator-registry/alpha/action/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ foo Foo Operator beta
{
name: "Error/UnknownIndex",
list: ListPackages{IndexReference: "unknown-index"},
expectedErr: `render reference "unknown-index": error resolving name : object required`,
expectedErr: `render reference "unknown-index": error resolving name for image ref unknown-index: object required`,
},
}
for _, s := range specs {
Expand Down Expand Up @@ -79,7 +79,7 @@ foo stable foo.v0.2.0
{
name: "Error/UnknownIndex",
list: ListChannels{IndexReference: "unknown-index"},
expectedErr: `render reference "unknown-index": error resolving name : object required`,
expectedErr: `render reference "unknown-index": error resolving name for image ref unknown-index: object required`,
},
{
name: "Error/UnknownPackage",
Expand Down Expand Up @@ -138,7 +138,7 @@ foo stable foo.v0.2.0 foo.v0.1.0 foo.v0.1.1,foo.v0.1.2 <0.2.0 tes
{
name: "Error/UnknownIndex",
list: ListBundles{IndexReference: "unknown-index"},
expectedErr: `render reference "unknown-index": error resolving name : object required`,
expectedErr: `render reference "unknown-index": error resolving name for image ref unknown-index: object required`,
},
{
name: "Error/UnknownPackage",
Expand Down
47 changes: 46 additions & 1 deletion staging/operator-registry/alpha/declcfg/load.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package declcfg

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -135,7 +136,7 @@ func LoadReader(r io.Reader) (*DeclarativeConfig, error) {

var in Meta
if err := json.Unmarshal(doc, &in); err != nil {
return nil, err
return nil, fmt.Errorf("unmarshal error: %s", resolveUnmarshalErr(doc, err))
}

switch in.Schema {
Expand Down Expand Up @@ -186,3 +187,47 @@ func LoadFile(root fs.FS, path string) (*DeclarativeConfig, error) {

return cfg, nil
}

func resolveUnmarshalErr(data []byte, err error) string {
var te *json.UnmarshalTypeError
if errors.As(err, &te) {
return formatUnmarshallErrorString(data, te.Error(), te.Offset)
}
var se *json.SyntaxError
if errors.As(err, &se) {
return formatUnmarshallErrorString(data, se.Error(), se.Offset)
}
return err.Error()
}

func formatUnmarshallErrorString(data []byte, errmsg string, offset int64) string {
sb := new(strings.Builder)
_, _ = sb.WriteString(fmt.Sprintf("%s at offset %d (indicated by <==)\n ", errmsg, offset))
// attempt to present the erroneous JSON in indented, human-readable format
// errors result in presenting the original, unformatted output
var pretty bytes.Buffer
err := json.Indent(&pretty, data, "", " ")
if err == nil {
pString := pretty.String()
// calc the prettified string offset which correlates to the original string offset
var pOffset, origOffset int64
origOffset = 0
for origOffset = 0; origOffset < offset; {
pOffset++
if pString[pOffset] != '\n' && pString[pOffset] != ' ' {
origOffset++
}
}
_, _ = sb.WriteString(pString[:pOffset])
_, _ = sb.WriteString(" <== ")
_, _ = sb.WriteString(pString[pOffset:])
} else {
for i := int64(0); i < offset; i++ {
_ = sb.WriteByte(data[i])
}
_, _ = sb.WriteString(" <== ")
_, _ = sb.Write(data[offset:])
}

return sb.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/operator-framework/operator-registry/pkg/image"
)

type Veneer struct {
type Template struct {
Registry image.Registry
}

func (v Veneer) Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) {
func (t Template) Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) {
cfg, err := declcfg.LoadReader(reader)
if err != nil {
return cfg, err
Expand All @@ -23,13 +23,13 @@ func (v Veneer) Render(ctx context.Context, reader io.Reader) (*declcfg.Declarat
outb := cfg.Bundles[:0] // allocate based on max size of input, but empty slice
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
r := action.Render{
Registry: v.Registry,
Registry: t.Registry,
AllowedRefMask: action.RefBundleImage,
}

for _, b := range cfg.Bundles {
if !isBundleVeneer(&b) {
return nil, fmt.Errorf("unexpected fields present in basic veneer bundle")
if !isBundleTemplate(&b) {
return nil, fmt.Errorf("unexpected fields present in basic template bundle")
}
r.Refs = []string{b.Image}
contributor, err := r.Run(ctx)
Expand All @@ -43,8 +43,8 @@ func (v Veneer) Render(ctx context.Context, reader io.Reader) (*declcfg.Declarat
return cfg, nil
}

// isBundleVeneer identifies a Bundle veneer source as having a Schema and Image defined
// isBundleTemplate identifies a Bundle template source as having a Schema and Image defined
// but no Properties, RelatedImages or Package defined
func isBundleVeneer(b *declcfg.Bundle) bool {
func isBundleTemplate(b *declcfg.Bundle) bool {
return b.Schema != "" && b.Image != "" && b.Package == "" && len(b.Properties) == 0 && len(b.RelatedImages) == 0
}
Loading

0 comments on commit 3a82ea6

Please sign in to comment.