-
Notifications
You must be signed in to change notification settings - Fork 260
template/*: allow passing custom bundle renderer #1423
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ import ( | |
"k8s.io/apimachinery/pkg/util/errors" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/action" | ||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
) | ||
|
@@ -25,22 +24,16 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error | |
|
||
var cfgs []declcfg.DeclarativeConfig | ||
|
||
bundleDict := make(map[string]struct{}) | ||
buildBundleList(&sv.Candidate.Bundles, &bundleDict) | ||
buildBundleList(&sv.Fast.Bundles, &bundleDict) | ||
buildBundleList(&sv.Stable.Bundles, &bundleDict) | ||
|
||
bundleDict := buildBundleList(*sv) | ||
for b := range bundleDict { | ||
r := action.Render{ | ||
AllowedRefMask: action.RefBundleImage, | ||
Refs: []string{b}, | ||
Registry: t.Registry, | ||
Migrations: t.Migrations, | ||
} | ||
c, err := r.Run(ctx) | ||
c, err := t.RenderBundle(ctx, b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(c.Bundles) != 1 { | ||
return nil, fmt.Errorf("bundle reference %q resulted in %d bundles, expected 1", b, len(c.Bundles)) | ||
} | ||
bundleDict[b] = c.Bundles[0].Image | ||
cfgs = append(cfgs, *c) | ||
} | ||
out = *combineConfigs(cfgs) | ||
|
@@ -49,7 +42,7 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error | |
return nil, fmt.Errorf("render: no bundles specified or no bundles could be rendered") | ||
} | ||
|
||
channelBundleVersions, err := sv.getVersionsFromStandardChannels(&out) | ||
channelBundleVersions, err := sv.getVersionsFromStandardChannels(&out, bundleDict) | ||
if err != nil { | ||
return nil, fmt.Errorf("render: unable to post-process bundle info: %v", err) | ||
} | ||
|
@@ -61,12 +54,16 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error | |
return &out, nil | ||
} | ||
|
||
func buildBundleList(bundles *[]semverTemplateBundleEntry, dict *map[string]struct{}) { | ||
for _, b := range *bundles { | ||
if _, ok := (*dict)[b.Image]; !ok { | ||
(*dict)[b.Image] = struct{}{} | ||
func buildBundleList(t semverTemplate) map[string]string { | ||
dict := make(map[string]string) | ||
for _, bl := range []semverTemplateChannelBundles{t.Candidate, t.Fast, t.Stable} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: wdyt about making the making the semverTemplateChannelBundles list a parameter? Before we could see which channel templates were being used to create the bundle list at the top level. Might also make the method more flexible? (though given that it isn't exported - maybe not...XD). |
||
for _, b := range bl.Bundles { | ||
if _, ok := dict[b.Image]; !ok { | ||
dict[b.Image] = b.Image | ||
} | ||
} | ||
} | ||
return dict | ||
} | ||
|
||
func readFile(reader io.Reader) (*semverTemplate, error) { | ||
|
@@ -114,10 +111,10 @@ func readFile(reader io.Reader) (*semverTemplate, error) { | |
return &sv, nil | ||
} | ||
|
||
func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.DeclarativeConfig) (*bundleVersions, error) { | ||
func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.DeclarativeConfig, bundleDict map[string]string) (*bundleVersions, error) { | ||
versions := bundleVersions{} | ||
|
||
bdm, err := sv.getVersionsFromChannel(sv.Candidate.Bundles, cfg) | ||
bdm, err := sv.getVersionsFromChannel(sv.Candidate.Bundles, bundleDict, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -126,7 +123,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati | |
} | ||
versions[candidateChannelArchetype] = bdm | ||
|
||
bdm, err = sv.getVersionsFromChannel(sv.Fast.Bundles, cfg) | ||
bdm, err = sv.getVersionsFromChannel(sv.Fast.Bundles, bundleDict, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -135,7 +132,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati | |
} | ||
versions[fastChannelArchetype] = bdm | ||
|
||
bdm, err = sv.getVersionsFromChannel(sv.Stable.Bundles, cfg) | ||
bdm, err = sv.getVersionsFromChannel(sv.Stable.Bundles, bundleDict, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -147,7 +144,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati | |
return &versions, nil | ||
} | ||
|
||
func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateBundleEntry, cfg *declcfg.DeclarativeConfig) (map[string]semver.Version, error) { | ||
func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateBundleEntry, bundleDict map[string]string, cfg *declcfg.DeclarativeConfig) (map[string]semver.Version, error) { | ||
entries := make(map[string]semver.Version) | ||
|
||
// we iterate over the channel bundles from the template, to: | ||
|
@@ -158,7 +155,7 @@ func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateB | |
// test if the bundle specified in the template is present in the successfully-rendered bundles | ||
index := 0 | ||
for index < len(cfg.Bundles) { | ||
if cfg.Bundles[index].Image == semverBundle.Image { | ||
if cfg.Bundles[index].Image == bundleDict[semverBundle.Image] { | ||
break | ||
} | ||
index++ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
package semver | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/blang/semver/v4" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/action/migrations" | ||
"github.com/operator-framework/operator-registry/pkg/image" | ||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
) | ||
|
||
// data passed into this module externally | ||
type Template struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: should we just make Template (as defined in basic with just the RenderBundle method) a shared type that basic and semver sub-type and implement? The basic and semver packages could even export a factory functions to get new instances of templates? |
||
Data io.Reader | ||
Registry image.Registry | ||
Migrations *migrations.Migrations | ||
Data io.Reader | ||
RenderBundle func(context.Context, string) (*declcfg.DeclarativeConfig, error) | ||
} | ||
|
||
// IO structs -- BEGIN | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would it be worth renaming sv to make the code a bit more readable?