Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up repo cache when repository is removed #4049

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion porch/pkg/cache/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (r *cachedRepository) Close() error {
}
r.objectNotifier.NotifyPackageRevisionChange(watch.Deleted, pr, pkgRevMeta)
}
return nil
return r.repo.Close()
}

// pollForever will continue polling until signal channel is closed or ctx is done.
Expand Down
4 changes: 4 additions & 0 deletions porch/pkg/engine/fake/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type Repository struct {

var _ repository.Repository = &Repository{}

func (r *Repository) Close() error {
return nil
}

func (r *Repository) ListPackageRevisions(_ context.Context, filter repository.ListPackageRevisionFilter) ([]repository.PackageRevision, error) {
var revs []repository.PackageRevision
for _, rev := range r.PackageRevisions {
Expand Down
11 changes: 11 additions & 0 deletions porch/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func OpenRepository(ctx context.Context, name, namespace string, spec *configapi
secret: spec.SecretRef.Name,
credentialResolver: opts.CredentialResolver,
userInfoProvider: opts.UserInfoProvider,
cacheDir: dir,
deployment: deployment,
}

Expand All @@ -157,6 +158,9 @@ type gitRepository struct {
credentialResolver repository.CredentialResolver
userInfoProvider repository.UserInfoProvider

// Folder used for the local git cache.
cacheDir string

// deployment holds spec.deployment
// TODO: Better caching here, support repository spec changes
deployment bool
Expand All @@ -175,6 +179,13 @@ type gitRepository struct {

var _ GitRepository = &gitRepository{}

func (r *gitRepository) Close() error {
if err := os.RemoveAll(r.cacheDir); err != nil {
return fmt.Errorf("error cleaning up local git cache for repo %s: %v", r.name, err)
}
return nil
}

func (r *gitRepository) ListPackages(ctx context.Context, filter repository.ListPackageFilter) ([]repository.Package, error) {
ctx, span := tracer.Start(ctx, "gitRepository::ListPackages", trace.WithAttributes())
defer span.End()
Expand Down
4 changes: 4 additions & 0 deletions porch/pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type ociRepository struct {
var _ repository.Repository = &ociRepository{}
var _ repository.FunctionRepository = &ociRepository{}

func (r *ociRepository) Close() error {
return nil
}

func (r *ociRepository) ListPackageRevisions(ctx context.Context, filter repository.ListPackageRevisionFilter) ([]repository.PackageRevision, error) {
if r.content != configapi.RepositoryContentPackage {
return []repository.PackageRevision{}, nil
Expand Down
45 changes: 43 additions & 2 deletions porch/pkg/registry/porch/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ loop:
bookmark = repository.ResourceVersion
klog.Infof("Bookmark: %q", bookmark)
} else {
b.updateCache(ctx, event.Type, repository)
if err := b.updateCache(ctx, event.Type, repository); err != nil {
klog.Warningf("error updating cache: %v", err)
}
}
} else {
klog.V(5).Infof("Received unexpected watch event Object: %T", event.Object)
Expand Down Expand Up @@ -136,13 +138,52 @@ func (b *background) updateCache(ctx context.Context, event watch.EventType, rep
// TODO: implement
case watch.Deleted:
klog.Infof("Repository deleted: %s:%s", repository.ObjectMeta.Namespace, repository.ObjectMeta.Name)
return b.cache.CloseRepository(repository)
shared, err := b.isSharedRepository(ctx, repository)
if err != nil {
return err
}
// Only close the repository if no other k8s repository resources references
// the same underlying git/oci repo.
if !shared {
return b.cache.CloseRepository(repository)
}
return nil
default:
klog.Warning("Unhandled watch event type: %s", event)
}
return nil
}

// isSharedRepository checks if the underlying git/oci repo of the provided
// k8s repository is also used by another repository.
func (b *background) isSharedRepository(ctx context.Context, repo *configapi.Repository) (bool, error) {
var obj configapi.RepositoryList
if err := b.coreClient.List(ctx, &obj); err != nil {
return false, err
}
for _, r := range obj.Items {
if r.Name == repo.Name && r.Namespace == repo.Namespace {
continue
}
if r.Spec.Type != repo.Spec.Type {
continue
}
switch r.Spec.Type {
case configapi.RepositoryTypeOCI:
if r.Spec.Oci.Registry == repo.Spec.Oci.Registry {
return true, nil
}
case configapi.RepositoryTypeGit:
if r.Spec.Git.Repo == repo.Spec.Git.Repo {
return true, nil
}
default:
return false, fmt.Errorf("type %q not supported", r.Spec.Type)
}
}
return false, nil
}

func (b *background) runOnce(ctx context.Context) error {
klog.Infof("background-refreshing repositories")
var repositories configapi.RepositoryList
Expand Down
3 changes: 3 additions & 0 deletions porch/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ type Repository interface {

// DeletePackage deletes a package
DeletePackage(ctx context.Context, old Package) error

// Close cleans up any resources associated with the repository
Close() error
}

type FunctionRepository interface {
Expand Down
Loading