Skip to content

Commit 0ff26f0

Browse files
joelanfordci-robot
authored andcommitted
template/*: allow passing custom bundle renderer (#1423)
Upstream-repository: operator-registry Upstream-commit: 1adde98228cd2a2e6f71d38a1be97907c11d9a14
1 parent f5d06d1 commit 0ff26f0

File tree

14 files changed

+133
-119
lines changed

14 files changed

+133
-119
lines changed

staging/operator-registry/alpha/action/list_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ foo Foo Operator beta
2828
{
2929
name: "Error/UnknownIndex",
3030
list: ListPackages{IndexReference: "unknown-index"},
31-
expectedErr: `render reference "unknown-index": repository name must be canonical`,
31+
expectedErr: `render reference "unknown-index": failed to pull image "unknown-index": repository name must be canonical`,
3232
},
3333
}
3434
for _, s := range specs {
@@ -79,7 +79,7 @@ foo stable foo.v0.2.0
7979
{
8080
name: "Error/UnknownIndex",
8181
list: ListChannels{IndexReference: "unknown-index"},
82-
expectedErr: `render reference "unknown-index": repository name must be canonical`,
82+
expectedErr: `render reference "unknown-index": failed to pull image "unknown-index": repository name must be canonical`,
8383
},
8484
{
8585
name: "Error/UnknownPackage",
@@ -138,7 +138,7 @@ foo stable foo.v0.2.0 foo.v0.1.0 foo.v0.1.1,foo.v0.1.2 <0.2.0 tes
138138
{
139139
name: "Error/UnknownIndex",
140140
list: ListBundles{IndexReference: "unknown-index"},
141-
expectedErr: `render reference "unknown-index": repository name must be canonical`,
141+
expectedErr: `render reference "unknown-index": failed to pull image "unknown-index": repository name must be canonical`,
142142
},
143143
{
144144
name: "Error/UnknownPackage",

staging/operator-registry/alpha/action/render.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,19 @@ func (r Render) renderReference(ctx context.Context, ref string) (*declcfg.Decla
162162
func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.DeclarativeConfig, error) {
163163
ref := image.SimpleReference(imageRef)
164164
if err := r.Registry.Pull(ctx, ref); err != nil {
165-
return nil, err
165+
return nil, fmt.Errorf("failed to pull image %q: %v", ref, err)
166166
}
167167
labels, err := r.Registry.Labels(ctx, ref)
168168
if err != nil {
169-
return nil, err
169+
return nil, fmt.Errorf("failed to get labels for image %q: %v", ref, err)
170170
}
171171
tmpDir, err := os.MkdirTemp("", "render-unpack-")
172172
if err != nil {
173-
return nil, err
173+
return nil, fmt.Errorf("create tempdir: %v", err)
174174
}
175175
defer os.RemoveAll(tmpDir)
176176
if err := r.Registry.Unpack(ctx, ref, tmpDir); err != nil {
177-
return nil, err
177+
return nil, fmt.Errorf("failed to unpack image %q: %v", ref, err)
178178
}
179179

180180
var cfg *declcfg.DeclarativeConfig

staging/operator-registry/alpha/template/basic/basic.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ import (
88

99
"k8s.io/apimachinery/pkg/util/yaml"
1010

11-
"github.com/operator-framework/operator-registry/alpha/action"
12-
"github.com/operator-framework/operator-registry/alpha/action/migrations"
1311
"github.com/operator-framework/operator-registry/alpha/declcfg"
14-
"github.com/operator-framework/operator-registry/pkg/image"
1512
)
1613

1714
const schema string = "olm.template.basic"
1815

1916
type Template struct {
20-
Registry image.Registry
21-
Migrations *migrations.Migrations
17+
RenderBundle func(context.Context, string) (*declcfg.DeclarativeConfig, error)
2218
}
2319

2420
type BasicTemplate struct {
@@ -57,19 +53,11 @@ func (t Template) Render(ctx context.Context, reader io.Reader) (*declcfg.Declar
5753
}
5854

5955
outb := cfg.Bundles[:0]
60-
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
61-
r := action.Render{
62-
Registry: t.Registry,
63-
AllowedRefMask: action.RefBundleImage,
64-
Migrations: t.Migrations,
65-
}
66-
6756
for _, b := range cfg.Bundles {
6857
if !isBundleTemplate(&b) {
6958
return nil, fmt.Errorf("unexpected fields present in basic template bundle")
7059
}
71-
r.Refs = []string{b.Image}
72-
contributor, err := r.Run(ctx)
60+
contributor, err := t.RenderBundle(ctx, b.Image)
7361
if err != nil {
7462
return nil, err
7563
}

staging/operator-registry/alpha/template/semver/semver.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"k8s.io/apimachinery/pkg/util/errors"
1111
"sigs.k8s.io/yaml"
1212

13-
"github.com/operator-framework/operator-registry/alpha/action"
1413
"github.com/operator-framework/operator-registry/alpha/declcfg"
1514
"github.com/operator-framework/operator-registry/alpha/property"
1615
)
@@ -25,22 +24,16 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error
2524

2625
var cfgs []declcfg.DeclarativeConfig
2726

28-
bundleDict := make(map[string]struct{})
29-
buildBundleList(&sv.Candidate.Bundles, &bundleDict)
30-
buildBundleList(&sv.Fast.Bundles, &bundleDict)
31-
buildBundleList(&sv.Stable.Bundles, &bundleDict)
32-
27+
bundleDict := buildBundleList(*sv)
3328
for b := range bundleDict {
34-
r := action.Render{
35-
AllowedRefMask: action.RefBundleImage,
36-
Refs: []string{b},
37-
Registry: t.Registry,
38-
Migrations: t.Migrations,
39-
}
40-
c, err := r.Run(ctx)
29+
c, err := t.RenderBundle(ctx, b)
4130
if err != nil {
4231
return nil, err
4332
}
33+
if len(c.Bundles) != 1 {
34+
return nil, fmt.Errorf("bundle reference %q resulted in %d bundles, expected 1", b, len(c.Bundles))
35+
}
36+
bundleDict[b] = c.Bundles[0].Image
4437
cfgs = append(cfgs, *c)
4538
}
4639
out = *combineConfigs(cfgs)
@@ -49,7 +42,7 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error
4942
return nil, fmt.Errorf("render: no bundles specified or no bundles could be rendered")
5043
}
5144

52-
channelBundleVersions, err := sv.getVersionsFromStandardChannels(&out)
45+
channelBundleVersions, err := sv.getVersionsFromStandardChannels(&out, bundleDict)
5346
if err != nil {
5447
return nil, fmt.Errorf("render: unable to post-process bundle info: %v", err)
5548
}
@@ -61,12 +54,16 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error
6154
return &out, nil
6255
}
6356

64-
func buildBundleList(bundles *[]semverTemplateBundleEntry, dict *map[string]struct{}) {
65-
for _, b := range *bundles {
66-
if _, ok := (*dict)[b.Image]; !ok {
67-
(*dict)[b.Image] = struct{}{}
57+
func buildBundleList(t semverTemplate) map[string]string {
58+
dict := make(map[string]string)
59+
for _, bl := range []semverTemplateChannelBundles{t.Candidate, t.Fast, t.Stable} {
60+
for _, b := range bl.Bundles {
61+
if _, ok := dict[b.Image]; !ok {
62+
dict[b.Image] = b.Image
63+
}
6864
}
6965
}
66+
return dict
7067
}
7168

7269
func readFile(reader io.Reader) (*semverTemplate, error) {
@@ -114,10 +111,10 @@ func readFile(reader io.Reader) (*semverTemplate, error) {
114111
return &sv, nil
115112
}
116113

117-
func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.DeclarativeConfig) (*bundleVersions, error) {
114+
func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.DeclarativeConfig, bundleDict map[string]string) (*bundleVersions, error) {
118115
versions := bundleVersions{}
119116

120-
bdm, err := sv.getVersionsFromChannel(sv.Candidate.Bundles, cfg)
117+
bdm, err := sv.getVersionsFromChannel(sv.Candidate.Bundles, bundleDict, cfg)
121118
if err != nil {
122119
return nil, err
123120
}
@@ -126,7 +123,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati
126123
}
127124
versions[candidateChannelArchetype] = bdm
128125

129-
bdm, err = sv.getVersionsFromChannel(sv.Fast.Bundles, cfg)
126+
bdm, err = sv.getVersionsFromChannel(sv.Fast.Bundles, bundleDict, cfg)
130127
if err != nil {
131128
return nil, err
132129
}
@@ -135,7 +132,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati
135132
}
136133
versions[fastChannelArchetype] = bdm
137134

138-
bdm, err = sv.getVersionsFromChannel(sv.Stable.Bundles, cfg)
135+
bdm, err = sv.getVersionsFromChannel(sv.Stable.Bundles, bundleDict, cfg)
139136
if err != nil {
140137
return nil, err
141138
}
@@ -147,7 +144,7 @@ func (sv *semverTemplate) getVersionsFromStandardChannels(cfg *declcfg.Declarati
147144
return &versions, nil
148145
}
149146

150-
func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateBundleEntry, cfg *declcfg.DeclarativeConfig) (map[string]semver.Version, error) {
147+
func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateBundleEntry, bundleDict map[string]string, cfg *declcfg.DeclarativeConfig) (map[string]semver.Version, error) {
151148
entries := make(map[string]semver.Version)
152149

153150
// we iterate over the channel bundles from the template, to:
@@ -158,7 +155,7 @@ func (sv *semverTemplate) getVersionsFromChannel(semverBundles []semverTemplateB
158155
// test if the bundle specified in the template is present in the successfully-rendered bundles
159156
index := 0
160157
for index < len(cfg.Bundles) {
161-
if cfg.Bundles[index].Image == semverBundle.Image {
158+
if cfg.Bundles[index].Image == bundleDict[semverBundle.Image] {
162159
break
163160
}
164161
index++

staging/operator-registry/alpha/template/semver/semver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ func TestGetVersionsFromStandardChannel(t *testing.T) {
538538
for _, tt := range tests {
539539
t.Run(tt.name, func(t *testing.T) {
540540
iosv := tt.sv
541-
versions, err := iosv.getVersionsFromStandardChannels(&tt.dc)
541+
versions, err := iosv.getVersionsFromStandardChannels(&tt.dc, buildBundleList(tt.sv))
542542
require.NoError(t, err)
543543
require.EqualValues(t, tt.outVersions, *versions)
544544
require.EqualValues(t, "a", iosv.pkg) // verify that we learned the package name and stashed it in the receiver
@@ -591,7 +591,7 @@ func TestBailOnVersionBuildMetadata(t *testing.T) {
591591
}
592592

593593
t.Run("Abort on unorderable build metadata version data", func(t *testing.T) {
594-
_, err := sv.getVersionsFromStandardChannels(&dc)
594+
_, err := sv.getVersionsFromStandardChannels(&dc, buildBundleList(sv))
595595
require.Error(t, err)
596596
})
597597
}

staging/operator-registry/alpha/template/semver/types.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package semver
22

33
import (
4+
"context"
45
"io"
56

67
"github.com/blang/semver/v4"
78

8-
"github.com/operator-framework/operator-registry/alpha/action/migrations"
9-
"github.com/operator-framework/operator-registry/pkg/image"
9+
"github.com/operator-framework/operator-registry/alpha/declcfg"
1010
)
1111

1212
// data passed into this module externally
1313
type Template struct {
14-
Data io.Reader
15-
Registry image.Registry
16-
Migrations *migrations.Migrations
14+
Data io.Reader
15+
RenderBundle func(context.Context, string) (*declcfg.DeclarativeConfig, error)
1716
}
1817

1918
// IO structs -- BEGIN

staging/operator-registry/cmd/opm/alpha/template/basic.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package template
22

33
import (
4+
"context"
45
"io"
56
"log"
67
"os"
78

89
"github.com/sirupsen/logrus"
910
"github.com/spf13/cobra"
1011

12+
"github.com/operator-framework/operator-registry/alpha/action"
1113
"github.com/operator-framework/operator-registry/alpha/action/migrations"
1214
"github.com/operator-framework/operator-registry/alpha/declcfg"
1315
"github.com/operator-framework/operator-registry/alpha/template/basic"
@@ -62,14 +64,23 @@ When FILE is '-' or not provided, the template is read from standard input`,
6264
}
6365
defer reg.Destroy()
6466

65-
template.Registry = reg
66-
67+
var m *migrations.Migrations
6768
if migrateLevel != "" {
68-
m, err := migrations.NewMigrations(migrateLevel)
69+
m, err = migrations.NewMigrations(migrateLevel)
6970
if err != nil {
7071
log.Fatal(err)
7172
}
72-
template.Migrations = m
73+
}
74+
75+
template.RenderBundle = func(ctx context.Context, image string) (*declcfg.DeclarativeConfig, error) {
76+
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
77+
r := action.Render{
78+
Refs: []string{image},
79+
Registry: reg,
80+
AllowedRefMask: action.RefBundleImage,
81+
Migrations: m,
82+
}
83+
return r.Run(ctx)
7384
}
7485

7586
// only taking first file argument

staging/operator-registry/cmd/opm/alpha/template/semver.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package template
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"log"
@@ -9,6 +10,7 @@ import (
910
"github.com/sirupsen/logrus"
1011
"github.com/spf13/cobra"
1112

13+
"github.com/operator-framework/operator-registry/alpha/action"
1214
"github.com/operator-framework/operator-registry/alpha/action/migrations"
1315
"github.com/operator-framework/operator-registry/alpha/declcfg"
1416
"github.com/operator-framework/operator-registry/alpha/template/semver"
@@ -68,17 +70,27 @@ When FILE is '-' or not provided, the template is read from standard input`,
6870
}
6971
defer reg.Destroy()
7072

71-
template := semver.Template{
72-
Data: data,
73-
Registry: reg,
74-
}
73+
var m *migrations.Migrations
7574
if migrateLevel != "" {
76-
m, err := migrations.NewMigrations(migrateLevel)
75+
m, err = migrations.NewMigrations(migrateLevel)
7776
if err != nil {
7877
log.Fatal(err)
7978
}
80-
template.Migrations = m
8179
}
80+
81+
template := semver.Template{
82+
Data: data,
83+
RenderBundle: func(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) {
84+
renderer := action.Render{
85+
Refs: []string{ref},
86+
Registry: reg,
87+
AllowedRefMask: action.RefBundleImage,
88+
Migrations: m,
89+
}
90+
return renderer.Run(ctx)
91+
},
92+
}
93+
8294
out, err := template.Render(cmd.Context())
8395
if err != nil {
8496
log.Fatalf("semver %q: %v", source, err)

vendor/github.com/operator-framework/operator-registry/alpha/action/render.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)