From d02b0b68acab6bb218b431bed70b3703ffc168ab Mon Sep 17 00:00:00 2001 From: Jordan Keister Date: Tue, 5 Sep 2023 15:14:34 -0500 Subject: [PATCH] export parsed composite specs (#1139) Signed-off-by: Jordan Keister --- alpha/template/composite/composite.go | 49 +++++++++++---------------- alpha/template/composite/types.go | 34 ++++++++++++++++++- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/alpha/template/composite/composite.go b/alpha/template/composite/composite.go index ce2cd0f4d..41a287c08 100644 --- a/alpha/template/composite/composite.go +++ b/alpha/template/composite/composite.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "net/http" "net/url" "os" "path/filepath" @@ -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 @@ -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. @@ -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) @@ -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 diff --git a/alpha/template/composite/types.go b/alpha/template/composite/types.go index 5295a5ddf..93b53883c 100644 --- a/alpha/template/composite/types.go +++ b/alpha/template/composite/types.go @@ -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 @@ -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) +}