Skip to content

Commit

Permalink
Change schema of OperatorBundle to include field 'bundlepath'.
Browse files Browse the repository at this point in the history
Migrate older database to new schema.
Extend API to allow querying of the new `bundlepath` field.
  • Loading branch information
anik120 committed Oct 22, 2019
1 parent 3aa6ddd commit 9b42083
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 101 deletions.
7 changes: 7 additions & 0 deletions pkg/api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ func BundleStringToAPIBundle(bundleString string, entry *registry.ChannelEntry)
out.PackageName = entry.PackageName
return out, nil
}

func BundlePathStringToAPIBundlePath(bundlePath string) *BundlePath {
out := &BundlePath{
Path: bundlePath,
}
return out
}
368 changes: 275 additions & 93 deletions pkg/api/registry.pb.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions pkg/api/registry.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ service Registry {
rpc ListPackages(ListPackageRequest) returns (stream PackageName) {}
rpc GetPackage(GetPackageRequest) returns (Package) {}
rpc GetBundle(GetBundleRequest) returns (Bundle) {}
rpc GetBundlePath(GetBundlePathRequest) returns (BundlePath) {}
rpc GetBundleForChannel(GetBundleInChannelRequest) returns (Bundle) {}
rpc GetChannelEntriesThatReplace(GetAllReplacementsRequest) returns (stream ChannelEntry) {}
rpc GetBundleThatReplaces(GetReplacementRequest) returns (Bundle) {}
Expand Down Expand Up @@ -38,6 +39,10 @@ message Bundle{
repeated string object = 5;
}

message BundlePath{
string path = 1;
}

message ChannelEntry{
string packageName = 1;
string channelName = 2;
Expand All @@ -57,6 +62,12 @@ message GetBundleRequest{
string csvName = 3;
}

message GetBundlePathRequest{
string pkgName = 1;
string channelName = 2;
string csvName = 3;
}

message GetBundleInChannelRequest{
string pkgName = 1;
string channelName = 2;
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type Interface interface {
GetBundle(ctx context.Context, packageName, channelName, csvName string) (*registry.Bundle, error)
GetBundlePath(ctx context.Context, packageName, channelName, csvName string) (string, error)
GetBundleInPackageChannel(ctx context.Context, packageName, channelName string) (*registry.Bundle, error)
GetReplacementBundleInPackageChannel(ctx context.Context, currentName, packageName, channelName string) (*registry.Bundle, error)
GetBundleThatProvides(ctx context.Context, group, version, kind string) (*registry.Bundle, error)
Expand All @@ -37,6 +38,14 @@ func (c *Client) GetBundle(ctx context.Context, packageName, channelName, csvNam
return registry.NewBundleFromStrings(bundle.CsvName, bundle.PackageName, bundle.ChannelName, bundle.Object)
}

func (c *Client) GetBundlePath(ctx context.Context, packageName, channelName, csvName string) (string, error) {
bundlePath, err := c.Registry.GetBundlePath(ctx, &api.GetBundlePathRequest{PkgName: packageName, ChannelName: channelName, CsvName: csvName})
if err != nil {
return "", err
}
return bundlePath.Path, nil
}

func (c *Client) GetBundleInPackageChannel(ctx context.Context, packageName, channelName string) (*registry.Bundle, error) {
bundle, err := c.Registry.GetBundleForChannel(ctx, &api.GetBundleInChannelRequest{PkgName: packageName, ChannelName: channelName})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/registry/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (EmptyQuery) GetBundle(ctx context.Context, pkgName, channelName, csvName s
return "", errors.New("empty querier: cannot get bundle")
}

func (EmptyQuery) GetBundlePath(ctx context.Context, pkgName, channelName, csvName string) (string, error) {
return "", errors.New("empty querier: cannot get bundle path")
}

func (EmptyQuery) GetBundleForChannel(ctx context.Context, pkgName string, channelName string) (string, error) {
return "", errors.New("empty querier: cannot get bundle for channel")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/registry/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Query interface {
ListPackages(ctx context.Context) ([]string, error)
GetPackage(ctx context.Context, name string) (*PackageManifest, error)
GetBundle(ctx context.Context, pkgName, channelName, csvName string) (string, error)
GetBundlePath(ctx context.Context, pkgName, channelName, csvName string) (string, error)
GetBundleForChannel(ctx context.Context, pkgName string, channelName string) (string, error)
// Get all channel entries that say they replace this one
GetChannelEntriesThatReplace(ctx context.Context, name string) (entries []*ChannelEntry, err error)
Expand Down
9 changes: 9 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func (s *RegistryServer) GetBundle(ctx context.Context, req *api.GetBundleReques
return api.BundleStringToAPIBundle(bundleString, entry)
}

func (s *RegistryServer) GetBundlePath(ctx context.Context, req *api.GetBundlePathRequest) (*api.BundlePath, error) {
bundlePath, err := s.store.GetBundlePath(ctx, req.GetPkgName(), req.GetChannelName(), req.GetCsvName())
if err != nil {
return nil, err
}

return api.BundlePathStringToAPIBundlePath(bundlePath), nil
}

func (s *RegistryServer) GetBundleForChannel(ctx context.Context, req *api.GetBundleInChannelRequest) (*api.Bundle, error) {
bundleString, err := s.store.GetBundleForChannel(ctx, req.GetPkgName(), req.GetChannelName())
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* TODO: Make this idempotent */
ALTER TABLE operatorbundle
ADD COLUMN bundlepath TEXT


10 changes: 10 additions & 0 deletions pkg/sqlite/db_migrations/20191021125829_add_bundle_path.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BEGIN TRANSACTION;
PRAGMA foreign_keys = 0;
CREATE TEMPORARY TABLE operatorbundle_backup (name TEXT,csv TEXT,bundle TEXT);
INSERT INTO operatorbundle_backup SELECT name,csv,bundle FROM operatorbundle;
DROP TABLE operatorbundle;
CREATE TABLE operatorbundle(name TEXT,csv TEXT,bundle TEXT);
INSERT INTO operatorbundle SELECT name,csv,bundle FROM operatorbundle_backup;
DROP TABLE operatorbundle_backup;
PRAGMA foreign_keys = 1;
COMMIT;
7 changes: 4 additions & 3 deletions pkg/sqlite/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func NewSQLLiteLoader(opts ...DbOption) (*SQLLoader, error) {
CREATE TABLE IF NOT EXISTS operatorbundle (
name TEXT PRIMARY KEY,
csv TEXT UNIQUE,
bundle TEXT
bundle TEXT,
bundlepath TEXT
);
CREATE TABLE IF NOT EXISTS package (
name TEXT PRIMARY KEY,
Expand Down Expand Up @@ -109,7 +110,7 @@ func (s *SQLLoader) AddOperatorBundle(bundle *registry.Bundle) error {
tx.Rollback()
}()

stmt, err := tx.Prepare("insert into operatorbundle(name, csv, bundle) values(?, ?, ?)")
stmt, err := tx.Prepare("insert into operatorbundle(name, csv, bundle, bundlepath) values(?, ?, ?, ?)")
if err != nil {
return err
}
Expand All @@ -124,7 +125,7 @@ func (s *SQLLoader) AddOperatorBundle(bundle *registry.Bundle) error {
return fmt.Errorf("csv name not found")
}

if _, err := stmt.Exec(csvName, csvBytes, bundleBytes); err != nil {
if _, err := stmt.Exec(csvName, csvBytes, bundleBytes, nil); err != nil {
return err
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/sqlite/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type SQLMigrator struct {
db *sql.DB
migrationsPath string
generated bool
generated bool
}

// NewSQLLiteMigrator returns a SQLMigrator. The SQLMigrator takes a sql database and directory for migrations
Expand All @@ -44,7 +44,7 @@ func NewSQLLiteMigrator(db *sql.DB, migrationsPath string) (*SQLMigrator, error)
if err != nil {
return nil, err
}

f, err := os.Create(fmt.Sprintf("%s/%s", tempDir, file))
if err != nil {
return nil, err
Expand All @@ -60,18 +60,18 @@ func NewSQLLiteMigrator(db *sql.DB, migrationsPath string) (*SQLMigrator, error)
return &SQLMigrator{
db: db,
migrationsPath: tempDir,
generated: true,
generated: true,
}, nil
}

return &SQLMigrator{
db: db,
migrationsPath: migrationsPath,
generated: false,
generated: false,
}, nil
}

// CleanUpMigrator deletes any unnecessary data generated just for the scope of the migrator.
// CleanUpMigrator deletes any unnecessary data generated just for the scope of the migrator.
// Call this function once the scope of the Migrator is no longer required
func (m *SQLMigrator) CleanUpMigrator() {
if m.generated {
Expand Down
19 changes: 19 additions & 0 deletions pkg/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ func (s *SQLQuerier) GetBundle(ctx context.Context, pkgName, channelName, csvNam
return bundleStringSQL.String, nil
}

func (s *SQLQuerier) GetBundlePath(ctx context.Context, pkgName, channelName, csvName string) (string, error) {
query := `SELECT DISTINCT operatorbundle.bundlepath
FROM operatorbundle INNER JOIN channel_entry ON operatorbundle.name=channel_entry.operatorbundle_name
WHERE channel_entry.package_name=? AND channel_entry.channel_name=? AND operatorbundle.name=? LIMIT 1`
rows, err := s.db.QueryContext(ctx, query, pkgName, channelName, csvName)
if err != nil {
return "", err
}

if !rows.Next() {
return "", fmt.Errorf("no bundle path found for csv %s", csvName)
}
var bundlePathStringSQL sql.NullString
if err := rows.Scan(&bundlePathStringSQL); err != nil {
return "", err
}
return bundlePathStringSQL.String, nil
}

func (s *SQLQuerier) GetBundleForChannel(ctx context.Context, pkgName string, channelName string) (string, error) {
query := `SELECT DISTINCT operatorbundle.bundle
FROM channel INNER JOIN operatorbundle ON channel.head_operatorbundle_name=operatorbundle.name
Expand Down

0 comments on commit 9b42083

Please sign in to comment.