Skip to content

Commit

Permalink
export parsed composite specs (#1139)
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <[email protected]>
  • Loading branch information
grokspawn authored Sep 5, 2023
1 parent 56771f7 commit d02b0b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
49 changes: 19 additions & 30 deletions alpha/template/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
Expand All @@ -14,23 +13,6 @@ import (
"k8s.io/apimachinery/pkg/util/yaml"
)

type BuilderMap map[string]Builder

type CatalogBuilderMap map[string]BuilderMap

type builderFunc func(BuilderConfig) Builder

type Template struct {
catalogFile io.Reader
contributionFile io.Reader
validate bool
outputType string
registry image.Registry
registeredBuilders map[string]builderFunc
}

type TemplateOption func(t *Template)

func WithCatalogFile(catalogFile io.Reader) TemplateOption {
return func(t *Template) {
t.catalogFile = catalogFile
Expand Down Expand Up @@ -79,10 +61,6 @@ func NewTemplate(opts ...TemplateOption) *Template {
return temp
}

type HttpGetter interface {
Get(url string) (*http.Response, error)
}

// FetchCatalogConfig will fetch the catalog configuration file from the given path.
// The path can be a local file path OR a URL that returns the raw contents of the catalog
// configuration file.
Expand All @@ -100,7 +78,7 @@ func FetchCatalogConfig(path string, httpGetter HttpGetter) (io.ReadCloser, erro
}
} else {
// Evalute remote catalog config
// If URi is valid, execute fetch
// If URI is valid, execute fetch
tempResp, err := httpGetter.Get(catalogURI.String())
if err != nil {
return nil, fmt.Errorf("fetching remote catalog config file %q: %v", path, err)
Expand All @@ -111,26 +89,37 @@ func FetchCatalogConfig(path string, httpGetter HttpGetter) (io.ReadCloser, erro
return tempCatalog, nil
}

// TODO(everettraven): do we need the context here? If so, how should it be used?
func (t *Template) Render(ctx context.Context, validate bool) error {
func (t *Template) Parse() (*Specs, error) {
var s Specs

catalogFile, err := t.parseCatalogsSpec()
catalogSpec, err := t.parseCatalogsSpec()
if err != nil {
return err
return nil, err
}
s.CatalogSpec = catalogSpec

contributionSpec, err := t.parseContributionSpec()
if err != nil {
return nil, err
}
s.ContributionSpec = contributionSpec

return &s, nil
}

contributionFile, err := t.parseContributionSpec()
func (t *Template) Render(ctx context.Context, validate bool) error {
specs, err := t.Parse()
if err != nil {
return err
}

catalogBuilderMap, err := t.newCatalogBuilderMap(catalogFile.Catalogs, t.outputType)
catalogBuilderMap, err := t.newCatalogBuilderMap(specs.CatalogSpec.Catalogs, t.outputType)
if err != nil {
return err
}

// TODO(everettraven): should we return aggregated errors?
for _, component := range contributionFile.Components {
for _, component := range specs.ContributionSpec.Components {
if builderMap, ok := (*catalogBuilderMap)[component.Name]; ok {
if builder, ok := builderMap[component.Strategy.Template.Schema]; ok {
// run the builder corresponding to the schema
Expand Down
34 changes: 33 additions & 1 deletion alpha/template/composite/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package composite

import "encoding/json"
import (
"encoding/json"
"io"
"net/http"

"github.com/operator-framework/operator-registry/pkg/image"
)

type TemplateDefinition struct {
Schema string
Expand All @@ -27,3 +33,29 @@ type CustomConfig struct {
Args []string
Output string
}

type BuilderMap map[string]Builder

type CatalogBuilderMap map[string]BuilderMap

type builderFunc func(BuilderConfig) Builder

type Template struct {
catalogFile io.Reader
contributionFile io.Reader
validate bool
outputType string
registry image.Registry
registeredBuilders map[string]builderFunc
}

type TemplateOption func(t *Template)

type Specs struct {
CatalogSpec *CatalogConfig
ContributionSpec *CompositeConfig
}

type HttpGetter interface {
Get(url string) (*http.Response, error)
}

0 comments on commit d02b0b6

Please sign in to comment.