Skip to content

Commit 4693c71

Browse files
committed
template/*: allow passing custom bundle renderer
1 parent e787595 commit 4693c71

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

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

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
}

alpha/template/semver/semver.go

Lines changed: 1 addition & 8 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
)
@@ -31,13 +30,7 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error
3130
buildBundleList(&sv.Stable.Bundles, &bundleDict)
3231

3332
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)
33+
c, err := t.RenderBundle(ctx, b)
4134
if err != nil {
4235
return nil, err
4336
}

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

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

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)

0 commit comments

Comments
 (0)