Skip to content

Commit

Permalink
Latest update
Browse files Browse the repository at this point in the history
  • Loading branch information
liamfallon committed Dec 16, 2024
1 parent ffb078f commit 7ef4ebe
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 156 deletions.
1 change: 1 addition & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ import (

type Cache interface {
OpenRepository(ctx context.Context, repositorySpec *configapi.Repository) (repository.Repository, error)
UpdateRepository(ctx context.Context, repositorySpec *configapi.Repository) error
CloseRepository(ctx context.Context, repositorySpec *configapi.Repository, allRepos []configapi.Repository) error
}
8 changes: 4 additions & 4 deletions pkg/cache/dbcache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestPublishedLatest(t *testing.T) {
if err := update.UpdateLifecycle(ctx, api.PackageRevisionLifecyclePublished); err != nil {
t.Fatalf("UpdateLifecycle failed; %v", err)
}
closed, err := update.Close(ctx, "")
closed, err := cachedRepo.ClosePackageRevisionDraft(ctx, update, "")
if err != nil {
t.Fatalf("Close failed: %v", err)
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestDeletePublishedMain(t *testing.T) {
if err := update.UpdateLifecycle(ctx, api.PackageRevisionLifecyclePublished); err != nil {
t.Fatalf("UpdateLifecycle failed; %v", err)
}
closed, err := update.Close(ctx, "")
closed, err := cachedRepo.ClosePackageRevisionDraft(ctx, update, "")
if err != nil {
t.Fatalf("Close failed: %v", err)
}
Expand Down Expand Up @@ -274,11 +274,11 @@ func createMetadataStoreFromArchive(t *testing.T, testPath, name string) meta.Me
}
if os.IsNotExist(err) {
return &fakemeta.MemoryMetadataStore{
Metas: []meta.PackageRevisionMeta{},
Metas: []metav1.ObjectMeta{},
}
}

var metas []meta.PackageRevisionMeta
var metas []metav1.ObjectMeta
if err := yaml.Unmarshal(c, &metas); err != nil {
t.Fatalf("Error unmarshalling metadata file for repository %s", name)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/cache/dbcache/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ func (c *dbCache) OpenRepository(ctx context.Context, repositorySpec *configapi.
return dbRepo.OpenRepository()
}

func (c *dbCache) UpdateRepository(ctx context.Context, repositorySpec *configapi.Repository) error {
_, span := tracer.Start(ctx, "dbCache::OpenRepository", trace.WithAttributes())
defer span.End()

dbRepo := dbRepository{
repoKey: repository.RepositoryKey{
Namespace: repositorySpec.Namespace,
Repository: repositorySpec.Name,
},
meta: &repositorySpec.ObjectMeta,
spec: &repositorySpec.Spec,
updated: time.Now(),
updatedBy: getCurrentUser(),
deployment: repositorySpec.Spec.Deployment,
}

return repoUpdateDB(&dbRepo)
}

func (c *dbCache) CloseRepository(ctx context.Context, repositorySpec *configapi.Repository, allRepos []configapi.Repository) error {
_, span := tracer.Start(ctx, "dbCache::OpenRepository", trace.WithAttributes())
defer span.End()
Expand Down
56 changes: 27 additions & 29 deletions pkg/cache/dbcache/dbpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,50 @@ var _ repository.Package = &dbPackage{}

type dbPackage struct {
pkgKey repository.PackageKey
meta metav1.ObjectMeta
spec v1alpha1.PackageSpec
updated time.Time
updatedBy string
}

func (p dbPackage) KubeObjectName() string {
func (p *dbPackage) KubeObjectName() string {
return p.Key().NonNSString()
}

func (p dbPackage) KubeObjectNamespace() string {
func (p *dbPackage) KubeObjectNamespace() string {
return p.Key().Namespace
}

func (p dbPackage) UID() types.UID {
func (p *dbPackage) UID() types.UID {
return generateUid("packages.", p.KubeObjectNamespace(), p.KubeObjectName())
}

func (p dbPackage) Key() repository.PackageKey {
func (p *dbPackage) Key() repository.PackageKey {
return p.pkgKey
}

func (p dbPackage) createPackage() (*dbPackage, error) {
func (p *dbPackage) createPackage() (*dbPackage, error) {
_, err := pkgReadFromDB(p.Key())
if err == nil {
return &p, pkgUpdateDB(p)
return p, pkgUpdateDB(p)
} else if err != sql.ErrNoRows {
return &p, err
return p, err
}

return &p, pkgWriteToDB(p)
p.meta = metav1.ObjectMeta{
Name: p.KubeObjectName(),
Namespace: p.KubeObjectNamespace(),
}

p.spec = v1alpha1.PackageSpec{
PackageName: p.pkgKey.Package,
RepositoryName: p.Key().Repository,
}

return p, pkgWriteToDB(p)
}

func (p dbPackage) GetPackage() *v1alpha1.PorchPackage {
func (p *dbPackage) GetPackage() *v1alpha1.PorchPackage {
key := p.Key()

return &v1alpha1.PorchPackage{
Expand All @@ -87,25 +99,11 @@ func (p dbPackage) GetPackage() *v1alpha1.PorchPackage {
}
}

func (p dbPackage) createPackageRevision(d *dbPackageRevisionDraft) (*dbPackageRevision, error) {
dbPkgRev := dbPackageRevision{
pkgRevKey: repository.PackageRevisionKey{
Namespace: p.Key().Namespace,
Repository: p.Key().Repository,
Package: p.Key().Package,
Revision: d.revision,
WorkspaceName: d.workspaceName,
},
lifecycle: d.lifecycle,
resources: d.resources,
updated: d.updated,
updatedBy: d.updatedBy,
}

return dbPkgRev.createPackageRevision()
func (p *dbPackage) createPackageRevision(d *dbPackageRevision) (*dbPackageRevision, error) {
return d.createPackageRevision()
}

func (p dbPackage) DeletePackageRevision(ctx context.Context, old repository.PackageRevision) error {
func (p *dbPackage) DeletePackageRevision(ctx context.Context, old repository.PackageRevision) error {
if err := pkgRevDeleteFromDB(old.Key()); err != nil && err != sql.ErrNoRows {
return err
}
Expand All @@ -122,18 +120,18 @@ func (p dbPackage) DeletePackageRevision(ctx context.Context, old repository.Pac
return nil
}

func (p dbPackage) GetLatestRevision() string {
func (p *dbPackage) GetLatestRevision() string {
return pkgRevReadLatestPRFromDB(p.Key()).ResourceVersion()
}

func (p dbPackage) Close() error {
func (p *dbPackage) Delete() error {
dbPkgRevs, err := pkgRevReadPRsFromDB(p.Key())
if err != nil {
return err
}

for _, pkgRev := range dbPkgRevs {
if err := pkgRev.Close(); err != nil {
if err := pkgRev.Delete(); err != nil {
return err
}
}
Expand Down
62 changes: 0 additions & 62 deletions pkg/cache/dbcache/dbpackagedraft.go

This file was deleted.

Loading

0 comments on commit 7ef4ebe

Please sign in to comment.