Skip to content

Commit

Permalink
template/*: allow passing custom bundle renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanford committed Aug 22, 2024
1 parent e787595 commit 4693c71
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
8 changes: 4 additions & 4 deletions alpha/action/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ func (r Render) renderReference(ctx context.Context, ref string) (*declcfg.Decla
func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.DeclarativeConfig, error) {
ref := image.SimpleReference(imageRef)
if err := r.Registry.Pull(ctx, ref); err != nil {
return nil, err
return nil, fmt.Errorf("failed to pull image %q: %v", ref, err)
}
labels, err := r.Registry.Labels(ctx, ref)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get labels for image %q: %v", ref, err)
}
tmpDir, err := os.MkdirTemp("", "render-unpack-")
if err != nil {
return nil, err
return nil, fmt.Errorf("create tempdir: %v", err)
}
defer os.RemoveAll(tmpDir)
if err := r.Registry.Unpack(ctx, ref, tmpDir); err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack image %q: %v", ref, err)
}

var cfg *declcfg.DeclarativeConfig
Expand Down
16 changes: 2 additions & 14 deletions alpha/template/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import (

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

"github.com/operator-framework/operator-registry/alpha/action"
"github.com/operator-framework/operator-registry/alpha/action/migrations"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/pkg/image"
)

const schema string = "olm.template.basic"

type Template struct {
Registry image.Registry
Migrations *migrations.Migrations
RenderBundle func(context.Context, string) (*declcfg.DeclarativeConfig, error)
}

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

outb := cfg.Bundles[:0]
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
r := action.Render{
Registry: t.Registry,
AllowedRefMask: action.RefBundleImage,
Migrations: t.Migrations,
}

for _, b := range cfg.Bundles {
if !isBundleTemplate(&b) {
return nil, fmt.Errorf("unexpected fields present in basic template bundle")
}
r.Refs = []string{b.Image}
contributor, err := r.Run(ctx)
contributor, err := t.RenderBundle(ctx, b.Image)
if err != nil {
return nil, err
}
Expand Down
9 changes: 1 addition & 8 deletions alpha/template/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -31,13 +30,7 @@ func (t Template) Render(ctx context.Context) (*declcfg.DeclarativeConfig, error
buildBundleList(&sv.Stable.Bundles, &bundleDict)

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
}
Expand Down
9 changes: 4 additions & 5 deletions alpha/template/semver/types.go
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 {
Data io.Reader
Registry image.Registry
Migrations *migrations.Migrations
Data io.Reader
RenderBundle func(context.Context, string) (*declcfg.DeclarativeConfig, error)
}

// IO structs -- BEGIN
Expand Down
19 changes: 15 additions & 4 deletions cmd/opm/alpha/template/basic.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package template

import (
"context"
"io"
"log"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

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

template.Registry = reg

var m *migrations.Migrations
if migrateLevel != "" {
m, err := migrations.NewMigrations(migrateLevel)
m, err = migrations.NewMigrations(migrateLevel)
if err != nil {
log.Fatal(err)
}
template.Migrations = m
}

template.RenderBundle = func(ctx context.Context, image string) (*declcfg.DeclarativeConfig, error) {
// populate registry, incl any flags from CLI, and enforce only rendering bundle images
r := action.Render{
Refs: []string{image},
Registry: reg,
AllowedRefMask: action.RefBundleImage,
Migrations: m,
}
return r.Run(ctx)
}

// only taking first file argument
Expand Down
24 changes: 18 additions & 6 deletions cmd/opm/alpha/template/semver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package template

import (
"context"
"fmt"
"io"
"log"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

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

template := semver.Template{
Data: data,
Registry: reg,
}
var m *migrations.Migrations
if migrateLevel != "" {
m, err := migrations.NewMigrations(migrateLevel)
m, err = migrations.NewMigrations(migrateLevel)
if err != nil {
log.Fatal(err)
}
template.Migrations = m
}

template := semver.Template{
Data: data,
RenderBundle: func(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) {
renderer := action.Render{
Refs: []string{ref},
Registry: reg,
AllowedRefMask: action.RefBundleImage,
Migrations: m,
}
return renderer.Run(ctx)
},
}

out, err := template.Render(cmd.Context())
if err != nil {
log.Fatalf("semver %q: %v", source, err)
Expand Down

0 comments on commit 4693c71

Please sign in to comment.