Skip to content

provide required schema for basic template #1335

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
4 changes: 2 additions & 2 deletions alpha/declcfg/declcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ type ChannelEntry struct {
// evaluation in bundlesEqual().
type Bundle struct {
Schema string `json:"schema"`
Name string `json:"name"`
Package string `json:"package"`
Name string `json:"name,omitempty"`
Package string `json:"package,omitempty"`
Image string `json:"image"`
Properties []property.Property `json:"properties,omitempty" hash:"set"`
RelatedImages []RelatedImage `json:"relatedImages,omitempty" hash:"set"`
Expand Down
11 changes: 11 additions & 0 deletions alpha/declcfg/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ func LoadFile(root fs.FS, path string) (*DeclarativeConfig, error) {
return cfg, nil
}

// LoadSlice will compose declarative config components from a slice of Meta objects
func LoadSlice(metas []*Meta) (*DeclarativeConfig, error) {
builder := fbcBuilder{}
for _, meta := range metas {
if err := builder.addMeta(meta); err != nil {
return nil, err
}
}
return &builder.cfg, nil
}

type fbcBuilder struct {
cfg DeclarativeConfig

Expand Down
72 changes: 70 additions & 2 deletions alpha/template/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,58 @@ package basic

import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/operator-framework/operator-registry/alpha/action"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/pkg/image"
"k8s.io/apimachinery/pkg/util/yaml"
)

const schema string = "olm.template.basic"

type Template struct {
Registry image.Registry
}

type BasicTemplate struct {
Schema string `json:"schema"`
Entries []*declcfg.Meta `json:"entries"`
}

func parseSpec(reader io.Reader) (*BasicTemplate, error) {
bt := &BasicTemplate{}
btDoc := json.RawMessage{}
btDecoder := yaml.NewYAMLOrJSONDecoder(reader, 4096)
err := btDecoder.Decode(&btDoc)
if err != nil {
return nil, fmt.Errorf("decoding template schema: %v", err)
}
err = json.Unmarshal(btDoc, bt)
if err != nil {
return nil, fmt.Errorf("unmarshalling template: %v", err)
}

if bt.Schema != schema {
return nil, fmt.Errorf("template has unknown schema (%q), should be %q", bt.Schema, schema)
}

return bt, nil
}

func (t Template) Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) {
cfg, err := declcfg.LoadReader(reader)
bt, err := parseSpec(reader)
if err != nil {
return nil, err
}
cfg, err := declcfg.LoadSlice(bt.Entries)
if err != nil {
return cfg, err
}

outb := cfg.Bundles[:0] // allocate based on max size of input, but empty slice
outb := cfg.Bundles[:0]
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
r := action.Render{
Registry: t.Registry,
Expand Down Expand Up @@ -48,3 +81,38 @@ func (t Template) Render(ctx context.Context, reader io.Reader) (*declcfg.Declar
func isBundleTemplate(b *declcfg.Bundle) bool {
return b.Schema != "" && b.Image != "" && b.Package == "" && len(b.Properties) == 0 && len(b.RelatedImages) == 0
}

// FromReader reads FBC from a reader and generates a BasicTemplate from it
func FromReader(r io.Reader) (*BasicTemplate, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry if this lacks context, or has already been discussed and discarded, but would it make sense to move this, and maybe other generally useful, FBC functions to the O-F API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once we get some more use/feedback/maturity in the interface, the plan is to eventually promote the catalog templates out of alpha and into pkg.

var entries []*declcfg.Meta
if err := declcfg.WalkMetasReader(r, func(meta *declcfg.Meta, err error) error {
if err != nil {
return err
}
if meta.Schema == declcfg.SchemaBundle {
var b declcfg.Bundle
if err := json.Unmarshal(meta.Blob, &b); err != nil {
return fmt.Errorf("parse bundle: %v", err)
}
b2 := declcfg.Bundle{
Schema: b.Schema,
Image: b.Image,
}
meta.Blob, err = json.Marshal(b2)
if err != nil {
return fmt.Errorf("re-serialize bundle: %v", err)
}
}
entries = append(entries, meta)
return nil
}); err != nil {
return nil, err
}

bt := &BasicTemplate{
Schema: schema,
Entries: entries,
}

return bt, nil
}
39 changes: 39 additions & 0 deletions alpha/template/converter/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package converter

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/operator-framework/operator-registry/alpha/template/basic"
"github.com/operator-framework/operator-registry/pkg/image"
"sigs.k8s.io/yaml"
)

type Converter struct {
FbcReader io.Reader
OutputFormat string
Registry image.Registry
}

func (c *Converter) Convert() error {
bt, err := basic.FromReader(c.FbcReader)
if err != nil {
return err
}

b, _ := json.MarshalIndent(bt, "", " ")
if c.OutputFormat == "json" {
fmt.Fprintln(os.Stdout, string(b))
} else {
y, err := yaml.JSONToYAML(b)
if err != nil {
return err
}
y = append([]byte("---\n"), y...)
fmt.Fprintln(os.Stdout, string(y))
}

return nil
}
2 changes: 2 additions & 0 deletions cmd/opm/alpha/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/operator-framework/operator-registry/cmd/opm/alpha/bundle"
converttemplate "github.com/operator-framework/operator-registry/cmd/opm/alpha/convert-template"
"github.com/operator-framework/operator-registry/cmd/opm/alpha/list"
rendergraph "github.com/operator-framework/operator-registry/cmd/opm/alpha/render-graph"
"github.com/operator-framework/operator-registry/cmd/opm/alpha/template"
Expand All @@ -26,6 +27,7 @@ func NewCmd(showAlphaHelp bool) *cobra.Command {
list.NewCmd(),
rendergraph.NewCmd(),
template.NewCmd(),
converttemplate.NewCmd(),
)
return runCmd
}
64 changes: 64 additions & 0 deletions cmd/opm/alpha/convert-template/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package converttemplate

import (
"fmt"
"log"

"github.com/spf13/cobra"

"github.com/operator-framework/operator-registry/alpha/template/converter"
"github.com/operator-framework/operator-registry/cmd/opm/internal/util"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert-template",
Short: "Convert existing FBC to a supported template type",
}
cmd.AddCommand(
newBasicConvertCmd(),
)
return cmd
}

func newBasicConvertCmd() *cobra.Command {
var (
converter converter.Converter
output string
)
cmd := &cobra.Command{
Use: "basic [<fbc-file> | -]",
Args: cobra.MaximumNArgs(1),
Short: "Generate a basic template from existing FBC",
Long: `Generate a basic template from existing FBC.

This command outputs a basic catalog template to STDOUT from input FBC.
If no argument is specified or is '-' input is assumed from STDIN.
`,
RunE: func(c *cobra.Command, args []string) error {

switch output {
case "yaml", "json":
converter.OutputFormat = output
default:
log.Fatalf("invalid --output value %q, expected (json|yaml)", output)
}

reader, name, err := util.OpenFileOrStdin(c, args)
if err != nil {
return fmt.Errorf("unable to open input: %q", name)
}

converter.FbcReader = reader
err = converter.Convert()
if err != nil {
return fmt.Errorf("converting: %v", err)
}

return nil
},
}
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")

return cmd
}
8 changes: 5 additions & 3 deletions cmd/opm/alpha/template/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
func newBasicTemplateCmd() *cobra.Command {
var (
template basic.Template
output string
)
cmd := &cobra.Command{
Use: "basic basic-template-file",
Expand All @@ -30,13 +29,17 @@ When FILE is '-' or not provided, the template is read from standard input`,
// When no arguments or "-" is passed to the command,
// assume input is coming from stdin
// Otherwise open the file passed to the command
data, source, err := openFileOrStdin(cmd, args)
data, source, err := util.OpenFileOrStdin(cmd, args)
if err != nil {
log.Fatalf("unable to open %q: %v", source, err)
}
defer data.Close()

var write func(declcfg.DeclarativeConfig, io.Writer) error
output, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatalf("unable to determine output format")
}
switch output {
case "yaml":
write = declcfg.WriteYAML
Expand Down Expand Up @@ -70,6 +73,5 @@ When FILE is '-' or not provided, the template is read from standard input`,
}
},
}
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")
return cmd
}
24 changes: 11 additions & 13 deletions cmd/opm/alpha/template/cmd.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package template

import (
"io"
"os"

"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
var output string

runCmd := &cobra.Command{
Use: "render-template",
Short: "Render a catalog template type",
Args: cobra.NoArgs,
}

runCmd.AddCommand(newBasicTemplateCmd())
runCmd.AddCommand(newSemverTemplateCmd())
bc := newBasicTemplateCmd()
// bc.Hidden = true
runCmd.AddCommand(bc)

return runCmd
}
sc := newSemverTemplateCmd()
// sc.Hidden = true
runCmd.AddCommand(sc)

func openFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) {
if len(args) == 0 || args[0] == "-" {
return io.NopCloser(cmd.InOrStdin()), "stdin", nil
}
reader, err := os.Open(args[0])
return reader, args[0], err
runCmd.PersistentFlags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")

return runCmd
}
8 changes: 5 additions & 3 deletions cmd/opm/alpha/template/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func newSemverTemplateCmd() *cobra.Command {
output := ""
cmd := &cobra.Command{
Use: "semver [FILE]",
Short: `Generate a file-based catalog from a single 'semver template' file
Expand All @@ -28,13 +27,17 @@ When FILE is '-' or not provided, the template is read from standard input`,
// When no arguments or "-" is passed to the command,
// assume input is coming from stdin
// Otherwise open the file passed to the command
data, source, err := openFileOrStdin(cmd, args)
data, source, err := util.OpenFileOrStdin(cmd, args)
if err != nil {
return err
}
defer data.Close()

var write func(declcfg.DeclarativeConfig, io.Writer) error
output, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatalf("unable to determine output format")
}
switch output {
case "json":
write = declcfg.WriteJSON
Expand Down Expand Up @@ -79,6 +82,5 @@ When FILE is '-' or not provided, the template is read from standard input`,
},
}

cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml|mermaid)")
return cmd
}
9 changes: 9 additions & 0 deletions cmd/opm/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"errors"
"io"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,3 +67,11 @@ func CreateCLIRegistry(cmd *cobra.Command) (*containerdregistry.Registry, error)
}
return reg, nil
}

func OpenFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This is an internal function, and I know it was mostly a move, but the implementation only ever looks at the first argument. It seems reasonable to just make this a single string argument instead of a slice of strings

Suggested change
func OpenFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) {
func OpenFileOrStdin(cmd *cobra.Command, path string) (io.ReadCloser, string, error) {

Copy link
Contributor Author

@grokspawn grokspawn Jun 25, 2024

Choose a reason for hiding this comment

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

This has bugged me too, but it does actually check for empty args as a condition for reading from stdin instead.
We could make it pass just the input path and have each caller test for empty args (and pass in an empty string) but then the function's utility is significantly reduced.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't feel super strongly one way or the other, and saw the empty args check but figured an empty string would work equivalently. This shouldn't block this PR if you'd rather keep it as is.

if len(args) == 0 || args[0] == "-" {
return io.NopCloser(cmd.InOrStdin()), "stdin", nil
}
reader, err := os.Open(args[0])
return reader, args[0], err
}
Loading