Skip to content

Commit

Permalink
Fix the metadata returned to the API on packagerevisions
Browse files Browse the repository at this point in the history
  • Loading branch information
liamfallon committed Nov 13, 2024
1 parent a266712 commit ae16c3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
23 changes: 15 additions & 8 deletions pkg/cache/memory/packagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package memory
import (
"context"

"github.com/nephio-project/porch/api/porch/v1alpha1"
api "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/pkg/repository"
)
Expand All @@ -35,21 +34,29 @@ type cachedPackageRevision struct {
isLatestRevision bool
}

func (c *cachedPackageRevision) GetPackageRevision(ctx context.Context) (*v1alpha1.PackageRevision, error) {
rev, err := c.PackageRevision.GetPackageRevision(ctx)
func (c *cachedPackageRevision) GetPackageRevision(ctx context.Context) (*api.PackageRevision, error) {
apiPR, err := c.PackageRevision.GetPackageRevision(ctx)
if err != nil {
return nil, err
}

apiPR.Annotations = c.GetMeta().Annotations
apiPR.Finalizers = c.GetMeta().Finalizers
apiPR.OwnerReferences = c.GetMeta().OwnerReferences
apiPR.DeletionTimestamp = c.GetMeta().DeletionTimestamp
apiPR.Labels = c.GetMeta().Labels

if c.isLatestRevision {
// copy the labels in case the cached object is being read by another go routine
labels := make(map[string]string, len(rev.Labels))
for k, v := range rev.Labels {
labels := make(map[string]string, len(apiPR.Labels))
for k, v := range apiPR.Labels {
labels[k] = v
}
labels[v1alpha1.LatestPackageRevisionKey] = v1alpha1.LatestPackageRevisionValue
rev.Labels = labels
labels[api.LatestPackageRevisionKey] = api.LatestPackageRevisionValue
apiPR.Labels = labels
}
return rev, nil

return apiPR, nil
}

func (c *cachedPackageRevision) GetCachedPackageRevision(ctx context.Context) (*api.PackageRevision, error) {
Expand Down
7 changes: 2 additions & 5 deletions pkg/cache/memory/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -212,9 +211,7 @@ func (r *cachedRepository) update(ctx context.Context, updated repository.Packag
Package: k.Package,
WorkspaceName: k.WorkspaceName,
}
if _, ok := r.cachedPackageRevisions[oldKey]; ok {
delete(r.cachedPackageRevisions, oldKey)
}
delete(r.cachedPackageRevisions, oldKey)
}

cached := &cachedPackageRevision{PackageRevision: updated}
Expand Down Expand Up @@ -471,7 +468,7 @@ func (r *cachedRepository) refreshAllCachedPackages(ctx context.Context) (map[re
Name: prm.Name,
Namespace: prm.Namespace,
}, true); err != nil {
if !apierrors.IsNotFound(err) {
if !errors.IsNotFound(err) {
// This will be retried the next time the sync runs.
klog.Warningf("repo %s: unable to delete PackageRev CR for %s/%s: %v",
r.id, prm.Name, prm.Namespace, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cache/memory/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ func identifyLatestRevisions(result map[repository.PackageRevisionKey]*cachedPac
latest[currentKey.Package] = current
}
}

// Mark the winners as latest
for _, v := range latest {
v.isLatestRevision = true
}

// If the
}

func toPackageRevisionSlice(cached map[repository.PackageRevisionKey]*cachedPackageRevision, filter repository.ListPackageRevisionFilter) []repository.PackageRevision {
Expand Down

0 comments on commit ae16c3a

Please sign in to comment.