Skip to content

Commit

Permalink
Merge pull request #157 from Nordix/callback-draft
Browse files Browse the repository at this point in the history
Refactor Porch code to remove callback from PackageRevisionDraft to repository for Draft closing
  • Loading branch information
nephio-prow[bot] authored Dec 12, 2024
2 parents 016eaf1 + 6b6afe2 commit 0b92c55
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 122 deletions.
4 changes: 2 additions & 2 deletions pkg/cache/memory/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
71 changes: 0 additions & 71 deletions pkg/cache/memory/draft.go

This file was deleted.

55 changes: 41 additions & 14 deletions pkg/cache/memory/repository.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022,2024 The kpt and Nephio Authors
// Copyright 2022, 2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -168,29 +168,56 @@ func (r *cachedRepository) getCachedPackages(ctx context.Context, forceRefresh b
}

func (r *cachedRepository) CreatePackageRevision(ctx context.Context, obj *v1alpha1.PackageRevision) (repository.PackageRevisionDraft, error) {
created, err := r.repo.CreatePackageRevision(ctx, obj)
return r.repo.CreatePackageRevision(ctx, obj)
}

func (r *cachedRepository) ClosePackageRevisionDraft(ctx context.Context, prd repository.PackageRevisionDraft, version string) (repository.PackageRevision, error) {
ctx, span := tracer.Start(ctx, "cachedRepository::ClosePackageRevisionDraft", trace.WithAttributes())
defer span.End()

v, err := r.Version(ctx)
if err != nil {
return nil, err
}

if v != r.lastVersion {
_, _, err = r.refreshAllCachedPackages(ctx)
if err != nil {
return nil, err
}
}

revisions, err := r.ListPackageRevisions(ctx, repository.ListPackageRevisionFilter{
Package: prd.GetName(),
})
if err != nil {
return nil, err
}

return &cachedDraft{
PackageRevisionDraft: created,
cache: r,
}, nil
var publishedRevisions []string
for _, rev := range revisions {
if v1alpha1.LifecycleIsPublished(rev.Lifecycle()) {
publishedRevisions = append(publishedRevisions, rev.Key().Revision)
}
}

nextVersion, err := repository.NextRevisionNumber(publishedRevisions)
if err != nil {
return nil, err
}

if closed, err := r.repo.ClosePackageRevisionDraft(ctx, prd, nextVersion); err != nil {
return nil, err
} else {
return r.update(ctx, closed)
}
}

func (r *cachedRepository) UpdatePackageRevision(ctx context.Context, old repository.PackageRevision) (repository.PackageRevisionDraft, error) {
// Unwrap
unwrapped := old.(*cachedPackageRevision).PackageRevision
created, err := r.repo.UpdatePackageRevision(ctx, unwrapped)
if err != nil {
return nil, err
}

return &cachedDraft{
PackageRevisionDraft: created,
cache: r,
}, nil
return r.repo.UpdatePackageRevision(ctx, unwrapped)
}

func (r *cachedRepository) update(ctx context.Context, updated repository.PackageRevision) (*cachedPackageRevision, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022,2024 The kpt and Nephio Authors
// Copyright 2022, 2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -184,7 +184,7 @@ func (cad *cadEngine) CreatePackageRevision(ctx context.Context, repositoryObj *
}

// Updates are done.
repoPkgRev, err := draft.Close(ctx, "")
repoPkgRev, err := repo.ClosePackageRevisionDraft(ctx, draft, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func (cad *cadEngine) UpdatePackageRevision(ctx context.Context, version string,
}

// Updates are done.
repoPkgRev, err = draft.Close(ctx, version)
repoPkgRev, err = repo.ClosePackageRevisionDraft(ctx, draft, version)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -509,7 +509,7 @@ func (cad *cadEngine) UpdatePackageResources(ctx context.Context, repositoryObj
return nil, renderStatus, err
}
// No lifecycle change when updating package resources; updates are done.
repoPkgRev, err := draft.Close(ctx, "")
repoPkgRev, err := repo.ClosePackageRevisionDraft(ctx, draft, "")
if err != nil {
return nil, renderStatus, err
}
Expand Down Expand Up @@ -562,7 +562,7 @@ func (cad *cadEngine) RecloneAndReplay(ctx context.Context, parentPR repository.
return nil, err
}

repoPkgRev, err := draft.Close(ctx, version)
repoPkgRev, err := repo.ClosePackageRevisionDraft(ctx, draft, version)

if err != nil {
return nil, err
Expand Down
10 changes: 1 addition & 9 deletions pkg/git/draft.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022, 2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,14 +63,6 @@ func (d *gitPackageRevisionDraft) UpdateLifecycle(ctx context.Context, new v1alp
return nil
}

// Finish round of updates.
func (d *gitPackageRevisionDraft) Close(ctx context.Context, version string) (repository.PackageRevision, error) {
ctx, span := tracer.Start(ctx, "gitPackageDraft::Close", trace.WithAttributes())
defer span.End()

return d.parent.CloseDraft(ctx, version, d)
}

func (d *gitPackageRevisionDraft) GetName() string {
packageDirectory := d.parent.directory
packageName := strings.TrimPrefix(d.path, packageDirectory+"/")
Expand Down
7 changes: 6 additions & 1 deletion pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,10 +1480,15 @@ func (r *gitRepository) UpdateDraftResources(ctx context.Context, draft *gitPack
return nil
}

func (r *gitRepository) CloseDraft(ctx context.Context, version string, d *gitPackageRevisionDraft) (*gitPackageRevision, error) {
func (r *gitRepository) ClosePackageRevisionDraft(ctx context.Context, prd repository.PackageRevisionDraft, version string) (repository.PackageRevision, error) {
ctx, span := tracer.Start(ctx, "GitRepository::UpdateLifecycle", trace.WithAttributes())
defer span.End()

r.mutex.Lock()
defer r.mutex.Unlock()

d := prd.(*gitPackageRevisionDraft)

refSpecs := newPushRefSpecBuilder()
draftBranch := createDraftName(d.path, d.workspaceName)
proposedBranch := createProposedName(d.path, d.workspaceName)
Expand Down
14 changes: 7 additions & 7 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022, 2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -180,7 +180,7 @@ func (g GitSuite) TestGitPackageRoundTrip(t *testing.T) {
t.Fatalf("draft.UpdateResources(%#v, %#v) failed: %v", newResources, task, err)
}

revision, err := draft.Close(ctx, "v1")
revision, err := repo.ClosePackageRevisionDraft(ctx, draft, "v1")
if err != nil {
t.Fatalf("draft.Close() failed: %v", err)
}
Expand All @@ -207,7 +207,7 @@ func (g GitSuite) TestGitPackageRoundTrip(t *testing.T) {
if err := update.UpdateLifecycle(ctx, v1alpha1.PackageRevisionLifecyclePublished); err != nil {
t.Fatalf("UpdateLifecycle failed: %v", err)
}
approved, err := update.Close(ctx, "v1")
approved, err := repo.ClosePackageRevisionDraft(ctx, update, "v1")
if err != nil {
t.Fatalf("Close() of %q, %q failed: %v", packageName, workspace, err)
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func (g GitSuite) TestListPackagesTrivial(t *testing.T) {
}); err != nil {
t.Fatalf("UpdateResources() failed: %v", err)
}
newRevision, err := draft.Close(ctx, "")
newRevision, err := git.ClosePackageRevisionDraft(ctx, draft, "")
if err != nil {
t.Fatalf("draft.Close() failed: %v", err)
}
Expand Down Expand Up @@ -506,7 +506,7 @@ func (g GitSuite) TestCreatePackageInTrivialRepository(t *testing.T) {
}); err != nil {
t.Fatalf("UpdateResources() failed: %v", err)
}
newRevision, err := draft.Close(ctx, "")
newRevision, err := git.ClosePackageRevisionDraft(ctx, draft, "")
if err != nil {
t.Fatalf("draft.Close() failed: %v", err)
}
Expand Down Expand Up @@ -688,7 +688,7 @@ func (g GitSuite) TestApproveDraft(t *testing.T) {

update.UpdateLifecycle(ctx, v1alpha1.PackageRevisionLifecyclePublished)

new, err := update.Close(ctx, "v1")
new, err := git.ClosePackageRevisionDraft(ctx, update, "v1")
if err != nil {
t.Fatalf("Close failed: %v", err)
}
Expand Down Expand Up @@ -750,7 +750,7 @@ func (g GitSuite) TestApproveDraftWithHistory(t *testing.T) {

update.UpdateLifecycle(ctx, v1alpha1.PackageRevisionLifecyclePublished)

new, err := update.Close(ctx, "v1")
new, err := git.ClosePackageRevisionDraft(ctx, update, "v1")
if err != nil {
t.Fatalf("Close failed: %v", err)
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/oci/mutate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022, 2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,7 @@ func (r *ociRepository) CreatePackageRevision(ctx context.Context, obj *v1alpha1
}

// digestName := ImageDigestName{}
return &ociPackageDraft{
return &ociPackageRevisionDraft{
packageName: packageName,
parent: r,
tasks: []v1alpha1.Task{},
Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *ociRepository) UpdatePackageRevision(ctx context.Context, old repositor
return nil, fmt.Errorf("error fetching image %q: %w", ref, err)
}

return &ociPackageDraft{
return &ociPackageRevisionDraft{
packageName: packageName,
parent: r,
tasks: []v1alpha1.Task{},
Expand All @@ -102,7 +102,7 @@ func (r *ociRepository) UpdatePackageRevision(ctx context.Context, old repositor
}, nil
}

type ociPackageDraft struct {
type ociPackageRevisionDraft struct {
packageName string

created time.Time
Expand All @@ -118,10 +118,10 @@ type ociPackageDraft struct {
lifecycle v1alpha1.PackageRevisionLifecycle // New value of the package revision lifecycle
}

var _ repository.PackageRevisionDraft = (*ociPackageDraft)(nil)
var _ repository.PackageRevisionDraft = (*ociPackageRevisionDraft)(nil)

func (p *ociPackageDraft) UpdateResources(ctx context.Context, new *v1alpha1.PackageRevisionResources, task *v1alpha1.Task) error {
_, span := tracer.Start(ctx, "ociPackageDraft::UpdateResources", trace.WithAttributes())
func (p *ociPackageRevisionDraft) UpdateResources(ctx context.Context, new *v1alpha1.PackageRevisionResources, task *v1alpha1.Task) error {
_, span := tracer.Start(ctx, "ociPackageRevisionDraft::UpdateResources", trace.WithAttributes())
defer span.End()

buf := bytes.NewBuffer(nil)
Expand Down Expand Up @@ -190,20 +190,22 @@ func (p *ociPackageDraft) UpdateResources(ctx context.Context, new *v1alpha1.Pac
return nil
}

func (p *ociPackageDraft) UpdateLifecycle(ctx context.Context, new v1alpha1.PackageRevisionLifecycle) error {
func (p *ociPackageRevisionDraft) UpdateLifecycle(ctx context.Context, new v1alpha1.PackageRevisionLifecycle) error {
p.lifecycle = new
return nil
}

func (p *ociPackageDraft) GetName() string {
func (p *ociPackageRevisionDraft) GetName() string {
return p.packageName
}

// Finish round of updates.
func (p *ociPackageDraft) Close(ctx context.Context, version string) (repository.PackageRevision, error) {
ctx, span := tracer.Start(ctx, "ociPackageDraft::Close", trace.WithAttributes())
func (r *ociRepository) ClosePackageRevisionDraft(ctx context.Context, prd repository.PackageRevisionDraft, version string) (repository.PackageRevision, error) {
ctx, span := tracer.Start(ctx, "ociRepository::ClosePackageRevisionDraft", trace.WithAttributes())
defer span.End()

p := prd.(*ociPackageRevisionDraft)

ref := p.tag
option := remote.WithAuthFromKeychain(gcrane.Keychain)

Expand Down
4 changes: 4 additions & 0 deletions pkg/repository/fake/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (r *Repository) CreatePackageRevision(_ context.Context, pr *v1alpha1.Packa
return nil, nil
}

func (r *Repository) ClosePackageRevisionDraft(ctx context.Context, prd repository.PackageRevisionDraft, version string) (repository.PackageRevision, error) {
return nil, nil
}

func (r *Repository) DeletePackageRevision(context.Context, repository.PackageRevision) error {
return nil
}
Expand Down
Loading

0 comments on commit 0b92c55

Please sign in to comment.