Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
grokspawn committed Apr 22, 2024
1 parent 3fe3905 commit e9698fe
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 35 deletions.
17 changes: 17 additions & 0 deletions alpha/template/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ import (
"github.com/operator-framework/operator-registry/alpha/action"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/pkg/image"
"gopkg.in/yaml.v2"
)

const schema string = "olm.template.basic"

// basic template is a schema and any kind of valid FBC except that `olm.bundle` objects have only image references
type basicTemplate struct {
Schema string `json:"schema"`
}

type Template struct {
Registry image.Registry
}

func (t Template) Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) {
bt := basicTemplate{}
err := yaml.NewDecoder(reader).Decode(&bt)
if err != nil {
return nil, err
}
if bt.Schema != schema {
return nil, fmt.Errorf("invalid schema. basic template schema must be %q instead of %q", schema, bt.Schema)
}

cfg, err := declcfg.LoadReader(reader)
if err != nil {
return cfg, err
Expand Down
2 changes: 2 additions & 0 deletions alpha/template/composite/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ func TestBasicBuilder(t *testing.T) {
}

const basicYaml = `---
schema: olm.template.basic
---
defaultChannel: preview
name: webhook-operator
schema: olm.package
Expand Down
7 changes: 7 additions & 0 deletions alpha/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package template

type Template interface {
GetSchemas() []string

}

6 changes: 4 additions & 2 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 @@ -37,6 +36,10 @@ When FILE is '-' or not provided, the template is read from standard input`,
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
}
18 changes: 15 additions & 3 deletions cmd/opm/alpha/template/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ import (
)

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())
runCmd.AddCommand(newCompositeTemplateCmd())
bc := newBasicTemplateCmd()
// bc.Hidden = true
runCmd.AddCommand(bc)

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

cc := newCompositeTemplateCmd()
// cc.Hidden = true
runCmd.AddCommand(cc)

runCmd.PersistentFlags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")

return runCmd
}
Expand Down
58 changes: 30 additions & 28 deletions cmd/opm/alpha/template/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (

func newCompositeTemplateCmd() *cobra.Command {
var (
output string
validate bool
compositeFile string
catalogFile string
validate bool
catalogFile string
)
cmd := &cobra.Command{
Use: "composite",
Use: "composite [FILE]",
Short: `Generate file-based catalogs from a catalog configuration file
and a 'composite template' file`,
Long: `Generate file-based catalogs from a catalog configuration file
and a 'composite template' file`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

output, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatalf("unable to determine output format")
}
switch output {
case "yaml":
// do nothing
Expand All @@ -43,41 +45,41 @@ and a 'composite template' file`,
}
defer reg.Destroy()

// operator author's 'composite.yaml' file
compositeReader, err := os.Open(compositeFile)
if err != nil {
log.Fatalf("opening composite config file %q: %v", compositeFile, err)
}
defer compositeReader.Close()

compositePath, err := filepath.Abs(filepath.Dir(compositeFile))
if err != nil {
log.Fatalf("getting absolute path of composite config file %q: %v", compositeFile, err)
}

// catalog maintainer's 'catalogs.yaml' file
tempCatalog, err := composite.FetchCatalogConfig(catalogFile, http.DefaultClient)
if err != nil {
log.Fatalf(err.Error())
}
defer tempCatalog.Close()

template := composite.NewTemplate(
composite.WithCatalogFile(tempCatalog),
composite.WithContributionFile(compositeReader, compositePath),
composite.WithOutputType(output),
composite.WithRegistry(reg),
)
for _, contribution := range args {
// operator author's 'composite.yaml' file
compositeReader, err := os.Open(contribution)
if err != nil {
log.Fatalf("opening composite config file %q: %v", contribution, err)
}
defer compositeReader.Close()

err = template.Render(cmd.Context(), validate)
if err != nil {
log.Fatalf("rendering the composite template: %v", err)
compositePath, err := filepath.Abs(filepath.Dir(contribution))
if err != nil {
log.Fatalf("getting absolute path of composite config file %q: %v", contribution, err)
}

template := composite.NewTemplate(
composite.WithCatalogFile(tempCatalog),
composite.WithContributionFile(compositeReader, compositePath),
composite.WithOutputType(output),
composite.WithRegistry(reg),
)

err = template.Render(cmd.Context(), validate)
if err != nil {
log.Fatalf("rendering the composite template: %v", err)
}
}
},
}
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")
cmd.Flags().BoolVar(&validate, "validate", true, "whether or not the created FBC should be validated (i.e 'opm validate')")
cmd.Flags().StringVarP(&compositeFile, "composite-config", "c", "composite.yaml", "File to use as the composite configuration file")
cmd.Flags().StringVarP(&catalogFile, "catalog-config", "f", "catalogs.yaml", "File to use as the catalog configuration file")
return cmd
}
6 changes: 4 additions & 2 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 @@ -35,6 +34,10 @@ When FILE is '-' or not provided, the template is read from standard input`,
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
}

0 comments on commit e9698fe

Please sign in to comment.