Skip to content

Commit

Permalink
intro new migrations package
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford authored and grokspawn committed Jul 23, 2024
1 parent 0e5fde0 commit 66abaaa
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package migrations

import (
"encoding/json"

"github.com/operator-framework/api/pkg/operators/v1alpha1"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
)

var BundleObjectToCSVMetadata = NewMigration("bundle-object-to-csv-metadata", func(cfg *declcfg.DeclarativeConfig) error {
convertBundleObjectToCSVMetadata := func(b *declcfg.Bundle) error {
if b.Image == "" || b.CsvJSON == "" {
return nil
}

var csv v1alpha1.ClusterServiceVersion
if err := json.Unmarshal([]byte(b.CsvJSON), &csv); err != nil {
return err
}

props := b.Properties[:0]
for _, p := range b.Properties {
switch p.Type {
case property.TypeBundleObject:
// Get rid of the bundle objects
case property.TypeCSVMetadata:
// If this bundle already has a CSV metadata
// property, we won't mutate the bundle at all.
return nil
default:
// Keep all of the other properties
props = append(props, p)
}
}
b.Properties = append(props, property.MustBuildCSVMetadata(csv))
return nil
}

for bi := range cfg.Bundles {
if err := convertBundleObjectToCSVMetadata(&cfg.Bundles[bi]); err != nil {
return err
}
}
return nil
})
84 changes: 84 additions & 0 deletions alpha/action/internal/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package migrations

import (
"fmt"
"slices"
"strings"

"github.com/operator-framework/operator-registry/alpha/declcfg"
)

type Migration interface {
Name() string
Migrate(*declcfg.DeclarativeConfig) error
}

func NewMigration(name string, fn func(config *declcfg.DeclarativeConfig) error) Migration {
return &simpleMigration{name: name, fn: fn}
}

type simpleMigration struct {
name string
fn func(*declcfg.DeclarativeConfig) error
}

func (s simpleMigration) Name() string {
return s.name
}

func (s simpleMigration) Migrate(config *declcfg.DeclarativeConfig) error {
return s.fn(config)
}

type Migrations struct {
migrations []Migration
}

var allMigrations = []Migration{
BundleObjectToCSVMetadata,
}

func NewMigrations(level string) (*Migrations, error) {
migrations := slices.Clone(allMigrations)
if level == "" {
return &Migrations{migrations: migrations}, nil
}

found := false
keep := migrations[:0]
for _, migration := range migrations {
keep = append(keep, migration)
if migration.Name() == level {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("unknown migration level %q", level)
}
return &Migrations{migrations: keep}, nil
}

func (m *Migrations) HelpText() string {
var help strings.Builder
help.WriteString("-- Migrations --\n")
help.WriteString(" To run a migration, use the --level flag with the migration name.\n")
help.WriteString(" The migrator will run all migrations up to and including the selected level.\n\n")
help.WriteString(" Available migration levels:\n")
if len(m.migrations) == 0 {
help.WriteString(" (no migrations available in this version)\n")
}
for i, migration := range m.migrations {
help.WriteString(fmt.Sprintf(" - %s\n", i+1, migration.Name()))
}
return help.String()
}

func (m *Migrations) Migrate(config *declcfg.DeclarativeConfig) error {
for _, migration := range m.migrations {
if err := migration.Migrate(config); err != nil {
return err
}
}
return nil
}

0 comments on commit 66abaaa

Please sign in to comment.