Skip to content

Commit

Permalink
Use Kubernetes metadata struct rather than Porch derived metadata struct
Browse files Browse the repository at this point in the history
  • Loading branch information
liamfallon committed Dec 9, 2024
1 parent 6d34cf8 commit 6c7ba83
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 60 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 @@ -272,11 +272,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
7 changes: 4 additions & 3 deletions pkg/cache/memory/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -260,7 +261,7 @@ func (r *cachedRepository) createMainPackageRevision(ctx context.Context, update
// Create the package if it doesn't exist
_, err := r.metadataStore.Get(ctx, pkgRevMetaNN)
if errors.IsNotFound(err) {
pkgRevMeta := meta.PackageRevisionMeta{
pkgRevMeta := metav1.ObjectMeta{
Name: updatedMain.KubeObjectName(),
Namespace: updatedMain.KubeObjectNamespace(),
}
Expand Down Expand Up @@ -426,7 +427,7 @@ func (r *cachedRepository) refreshAllCachedPackages(ctx context.Context) (map[re
return nil, nil, err
}
// Create a map so we can quickly check if a specific PackageRevisionMeta exists.
existingPkgRevCRsMap := make(map[string]meta.PackageRevisionMeta)
existingPkgRevCRsMap := make(map[string]metav1.ObjectMeta)
for i := range existingPkgRevCRs {
pr := existingPkgRevCRs[i]
existingPkgRevCRsMap[pr.Name] = pr
Expand Down Expand Up @@ -497,7 +498,7 @@ func (r *cachedRepository) refreshAllCachedPackages(ctx context.Context) (map[re
// a corresponding PackageRev CR.
for pkgRevName, pkgRev := range newPackageRevisionNames {
if _, found := existingPkgRevCRsMap[pkgRevName]; !found {
pkgRevMeta := meta.PackageRevisionMeta{
pkgRevMeta := metav1.ObjectMeta{
Name: pkgRevName,
Namespace: r.repoSpec.Namespace,
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (cad *cadEngine) CreatePackageRevision(ctx context.Context, repositoryObj *
if err != nil {
return nil, err
}
pkgRevMeta := meta.PackageRevisionMeta{
pkgRevMeta := metav1.ObjectMeta{
Name: repoPkgRev.KubeObjectName(),
Namespace: repoPkgRev.KubeObjectNamespace(),
Labels: obj.Labels,
Expand Down Expand Up @@ -325,7 +326,7 @@ func (cad *cadEngine) UpdatePackageRevision(ctx context.Context, version string,
}

func (cad *cadEngine) updatePkgRevMeta(ctx context.Context, repoPkgRev repository.PackageRevision, apiPkgRev *api.PackageRevision) error {
pkgRevMeta := meta.PackageRevisionMeta{
pkgRevMeta := metav1.ObjectMeta{
Name: repoPkgRev.KubeObjectName(),
Namespace: repoPkgRev.KubeObjectNamespace(),
Labels: apiPkgRev.Labels,
Expand Down
7 changes: 3 additions & 4 deletions pkg/git/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/pkg"
kptfile "github.com/nephio-project/porch/pkg/kpt/api/kptfile/v1"
"github.com/nephio-project/porch/pkg/meta"
"github.com/nephio-project/porch/pkg/repository"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -51,7 +50,7 @@ type gitPackageRevision struct {
tree plumbing.Hash // Cached tree of the package itself, some descendent of commit.Tree()
commit plumbing.Hash // Current version of the package (commit sha)
tasks []v1alpha1.Task
metadata meta.PackageRevisionMeta
metadata metav1.ObjectMeta
}

var _ repository.PackageRevision = &gitPackageRevision{}
Expand Down Expand Up @@ -318,11 +317,11 @@ func (p *gitPackageRevision) UpdateLifecycle(ctx context.Context, new v1alpha1.P
return p.repo.UpdateLifecycle(ctx, p, new)
}

func (p *gitPackageRevision) GetMeta() meta.PackageRevisionMeta {
func (p *gitPackageRevision) GetMeta() metav1.ObjectMeta {
return p.metadata
}

func (p *gitPackageRevision) SetMeta(metadata meta.PackageRevisionMeta) {
func (p *gitPackageRevision) SetMeta(metadata metav1.ObjectMeta) {
p.metadata = metadata
}

Expand Down
23 changes: 12 additions & 11 deletions pkg/meta/fake/memorystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,36 @@ import (
configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
"github.com/nephio-project/porch/pkg/meta"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)

// MemoryMetadataStore is an in-memory implementation of the MetadataStore interface. It
// means metadata about packagerevisions will be stored in memory, which is useful for testing.
type MemoryMetadataStore struct {
Metas []meta.PackageRevisionMeta
Metas []metav1.ObjectMeta
}

var _ meta.MetadataStore = &MemoryMetadataStore{}

func (m *MemoryMetadataStore) Get(ctx context.Context, namespacedName types.NamespacedName) (meta.PackageRevisionMeta, error) {
func (m *MemoryMetadataStore) Get(ctx context.Context, namespacedName types.NamespacedName) (metav1.ObjectMeta, error) {
for _, meta := range m.Metas {
if meta.Name == namespacedName.Name && meta.Namespace == namespacedName.Namespace {
return meta, nil
}
}
return meta.PackageRevisionMeta{}, apierrors.NewNotFound(
return metav1.ObjectMeta{}, apierrors.NewNotFound(
schema.GroupResource{Group: "config.kpt.dev", Resource: "packagerevisions"},
namespacedName.Name,
)
}

func (m *MemoryMetadataStore) List(ctx context.Context, repo *configapi.Repository) ([]meta.PackageRevisionMeta, error) {
func (m *MemoryMetadataStore) List(ctx context.Context, repo *configapi.Repository) ([]metav1.ObjectMeta, error) {
return m.Metas, nil
}

func (m *MemoryMetadataStore) Create(ctx context.Context, pkgRevMeta meta.PackageRevisionMeta, repoName string, pkgRevUID types.UID) (meta.PackageRevisionMeta, error) {
func (m *MemoryMetadataStore) Create(ctx context.Context, pkgRevMeta metav1.ObjectMeta, repoName string, pkgRevUID types.UID) (metav1.ObjectMeta, error) {
for _, m := range m.Metas {
if m.Name == pkgRevMeta.Name && m.Namespace == pkgRevMeta.Namespace {
return m, apierrors.NewAlreadyExists(
Expand All @@ -61,15 +62,15 @@ func (m *MemoryMetadataStore) Create(ctx context.Context, pkgRevMeta meta.Packag
return pkgRevMeta, nil
}

func (m *MemoryMetadataStore) Update(ctx context.Context, pkgRevMeta meta.PackageRevisionMeta) (meta.PackageRevisionMeta, error) {
func (m *MemoryMetadataStore) Update(ctx context.Context, pkgRevMeta metav1.ObjectMeta) (metav1.ObjectMeta, error) {
i := -1
for j, m := range m.Metas {
if m.Name == pkgRevMeta.Name && m.Namespace == pkgRevMeta.Namespace {
i = j
}
}
if i < 0 {
return meta.PackageRevisionMeta{}, apierrors.NewNotFound(
return metav1.ObjectMeta{}, apierrors.NewNotFound(
schema.GroupResource{Group: "config.porch.kpt.dev", Resource: "packagerevisions"},
pkgRevMeta.Name,
)
Expand All @@ -78,10 +79,10 @@ func (m *MemoryMetadataStore) Update(ctx context.Context, pkgRevMeta meta.Packag
return pkgRevMeta, nil
}

func (m *MemoryMetadataStore) Delete(ctx context.Context, namespacedName types.NamespacedName, _ bool) (meta.PackageRevisionMeta, error) {
var metas []meta.PackageRevisionMeta
func (m *MemoryMetadataStore) Delete(ctx context.Context, namespacedName types.NamespacedName, _ bool) (metav1.ObjectMeta, error) {
var metas []metav1.ObjectMeta
found := false
var deletedMeta meta.PackageRevisionMeta
var deletedMeta metav1.ObjectMeta
for _, m := range m.Metas {
if m.Name == namespacedName.Name && m.Namespace == namespacedName.Namespace {
found = true
Expand All @@ -91,7 +92,7 @@ func (m *MemoryMetadataStore) Delete(ctx context.Context, namespacedName types.N
}
}
if !found {
return meta.PackageRevisionMeta{}, apierrors.NewNotFound(
return metav1.ObjectMeta{}, apierrors.NewNotFound(
schema.GroupResource{Group: "config.kpt.dev", Resource: "packagerevisions"},
namespacedName.Name,
)
Expand Down
48 changes: 21 additions & 27 deletions pkg/meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ var (
// examples of metadata we want to keep is labels, annotations, owner references, and
// finalizers.
type MetadataStore interface {
Get(ctx context.Context, namespacedName types.NamespacedName) (PackageRevisionMeta, error)
List(ctx context.Context, repo *configapi.Repository) ([]PackageRevisionMeta, error)
Create(ctx context.Context, pkgRevMeta PackageRevisionMeta, repoName string, pkgRevUID types.UID) (PackageRevisionMeta, error)
Update(ctx context.Context, pkgRevMeta PackageRevisionMeta) (PackageRevisionMeta, error)
Delete(ctx context.Context, namespacedName types.NamespacedName, clearFinalizer bool) (PackageRevisionMeta, error)
Get(ctx context.Context, namespacedName types.NamespacedName) (metav1.ObjectMeta, error)
List(ctx context.Context, repo *configapi.Repository) ([]metav1.ObjectMeta, error)
Create(ctx context.Context, pkgRevMeta metav1.ObjectMeta, repoName string, pkgRevUID types.UID) (metav1.ObjectMeta, error)
Update(ctx context.Context, pkgRevMeta metav1.ObjectMeta) (metav1.ObjectMeta, error)
Delete(ctx context.Context, namespacedName types.NamespacedName, clearFinalizer bool) (metav1.ObjectMeta, error)
}

// PackageRevisionMeta contains metadata about a specific PackageRevision. The
Expand Down Expand Up @@ -78,20 +78,20 @@ type crdMetadataStore struct {
coreClient client.Client
}

func (c *crdMetadataStore) Get(ctx context.Context, namespacedName types.NamespacedName) (PackageRevisionMeta, error) {
func (c *crdMetadataStore) Get(ctx context.Context, namespacedName types.NamespacedName) (metav1.ObjectMeta, error) {
ctx, span := tracer.Start(ctx, "crdMetadataStore::Get", trace.WithAttributes())
defer span.End()

var internalPkgRev internalapi.PackageRev
err := c.coreClient.Get(ctx, namespacedName, &internalPkgRev)
if err != nil {
return PackageRevisionMeta{}, err
return metav1.ObjectMeta{}, err
}

return toPackageRevisionMeta(&internalPkgRev), nil
}

func (c *crdMetadataStore) List(ctx context.Context, repo *configapi.Repository) ([]PackageRevisionMeta, error) {
func (c *crdMetadataStore) List(ctx context.Context, repo *configapi.Repository) ([]metav1.ObjectMeta, error) {
ctx, span := tracer.Start(ctx, "crdMetadataStore::List", trace.WithAttributes())
defer span.End()

Expand All @@ -100,14 +100,14 @@ func (c *crdMetadataStore) List(ctx context.Context, repo *configapi.Repository)
if err != nil {
return nil, err
}
var pkgRevMetas []PackageRevisionMeta
var pkgRevMetas []metav1.ObjectMeta
for _, ipr := range internalPkgRevList.Items {
pkgRevMetas = append(pkgRevMetas, toPackageRevisionMeta(&ipr))
}
return pkgRevMetas, nil
}

func (c *crdMetadataStore) Create(ctx context.Context, pkgRevMeta PackageRevisionMeta, repoName string, pkgRevUID types.UID) (PackageRevisionMeta, error) {
func (c *crdMetadataStore) Create(ctx context.Context, pkgRevMeta metav1.ObjectMeta, repoName string, pkgRevUID types.UID) (metav1.ObjectMeta, error) {
ctx, span := tracer.Start(ctx, "crdMetadataStore::Create", trace.WithAttributes())
defer span.End()

Expand Down Expand Up @@ -141,12 +141,12 @@ func (c *crdMetadataStore) Create(ctx context.Context, pkgRevMeta PackageRevisio
if apierrors.IsAlreadyExists(err) {
return c.Update(ctx, pkgRevMeta)
}
return PackageRevisionMeta{}, err
return metav1.ObjectMeta{}, err
}
return toPackageRevisionMeta(&internalPkgRev), nil
}

func (c *crdMetadataStore) Update(ctx context.Context, pkgRevMeta PackageRevisionMeta) (PackageRevisionMeta, error) {
func (c *crdMetadataStore) Update(ctx context.Context, pkgRevMeta metav1.ObjectMeta) (metav1.ObjectMeta, error) {
ctx, span := tracer.Start(ctx, "crdMetadataStore::Update", trace.WithAttributes())
defer span.End()

Expand All @@ -157,7 +157,7 @@ func (c *crdMetadataStore) Update(ctx context.Context, pkgRevMeta PackageRevisio
}
err := c.coreClient.Get(ctx, namespacedName, &internalPkgRev)
if err != nil {
return PackageRevisionMeta{}, err
return metav1.ObjectMeta{}, err
}

// Copy updated labels to the CR and add the repository label
Expand Down Expand Up @@ -185,12 +185,12 @@ func (c *crdMetadataStore) Update(ctx context.Context, pkgRevMeta PackageRevisio

klog.Infof("Updating packagerev %s/%s", internalPkgRev.Namespace, internalPkgRev.Name)
if err := c.coreClient.Update(ctx, &internalPkgRev); err != nil {
return PackageRevisionMeta{}, err
return metav1.ObjectMeta{}, err
}
return toPackageRevisionMeta(&internalPkgRev), nil
}

func (c *crdMetadataStore) Delete(ctx context.Context, namespacedName types.NamespacedName, clearFinalizers bool) (PackageRevisionMeta, error) {
func (c *crdMetadataStore) Delete(ctx context.Context, namespacedName types.NamespacedName, clearFinalizers bool) (metav1.ObjectMeta, error) {
ctx, span := tracer.Start(ctx, "crdMetadataStore::Delete", trace.WithAttributes())
defer span.End()

Expand All @@ -210,17 +210,17 @@ func (c *crdMetadataStore) Delete(ctx context.Context, namespacedName types.Name
return nil
})
if retriedErr != nil {
return PackageRevisionMeta{}, retriedErr
return metav1.ObjectMeta{}, retriedErr
}

klog.Infof("Deleting packagerev %s/%s", internalPkgRev.Namespace, internalPkgRev.Name)
if err := c.coreClient.Delete(ctx, &internalPkgRev); err != nil {
return PackageRevisionMeta{}, err
return metav1.ObjectMeta{}, err
}
return toPackageRevisionMeta(&internalPkgRev), nil
}

func toPackageRevisionMeta(internalPkgRev *internalapi.PackageRev) PackageRevisionMeta {
func toPackageRevisionMeta(internalPkgRev *internalapi.PackageRev) metav1.ObjectMeta {
labels := internalPkgRev.Labels
delete(labels, PkgRevisionRepoLabel)

Expand All @@ -232,6 +232,7 @@ func toPackageRevisionMeta(internalPkgRev *internalapi.PackageRev) PackageRevisi
ownerReferences = append(ownerReferences, or)
}
}
internalPkgRev.OwnerReferences = ownerReferences

var finalizers []string
for _, f := range internalPkgRev.Finalizers {
Expand All @@ -240,15 +241,8 @@ func toPackageRevisionMeta(internalPkgRev *internalapi.PackageRev) PackageRevisi
}
}

return PackageRevisionMeta{
Name: internalPkgRev.Name,
Namespace: internalPkgRev.Namespace,
Labels: labels,
Annotations: internalPkgRev.Annotations,
Finalizers: finalizers,
OwnerReferences: ownerReferences,
DeletionTimestamp: internalPkgRev.DeletionTimestamp,
}
internalPkgRev.Finalizers = finalizers
return internalPkgRev.ObjectMeta
}

func isPackageRevOwnerRef(or metav1.OwnerReference, pkgRevName string) bool {
Expand Down
7 changes: 3 additions & 4 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/pkg"
kptfile "github.com/nephio-project/porch/pkg/kpt/api/kptfile/v1"
"github.com/nephio-project/porch/pkg/meta"
"github.com/nephio-project/porch/pkg/repository"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -262,7 +261,7 @@ type ociPackageRevision struct {
created time.Time
resourceVersion string
uid types.UID
metadata meta.PackageRevisionMeta
metadata metav1.ObjectMeta

parent *ociRepository
tasks []v1alpha1.Task
Expand Down Expand Up @@ -425,10 +424,10 @@ func (p *ociPackageRevision) UpdateLifecycle(ctx context.Context, new v1alpha1.P
return nil
}

func (p *ociPackageRevision) GetMeta() meta.PackageRevisionMeta {
func (p *ociPackageRevision) GetMeta() metav1.ObjectMeta {
return p.metadata
}

func (p *ociPackageRevision) SetMeta(metadata meta.PackageRevisionMeta) {
func (p *ociPackageRevision) SetMeta(metadata metav1.ObjectMeta) {
p.metadata = metadata
}
8 changes: 4 additions & 4 deletions pkg/repository/fake/packagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

"github.com/nephio-project/porch/api/porch/v1alpha1"
kptfile "github.com/nephio-project/porch/pkg/kpt/api/kptfile/v1"
"github.com/nephio-project/porch/pkg/meta"
"github.com/nephio-project/porch/pkg/repository"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -91,9 +91,9 @@ func (f *FakePackageRevision) UpdateLifecycle(context.Context, v1alpha1.PackageR
return nil
}

func (f *FakePackageRevision) GetMeta() meta.PackageRevisionMeta {
return meta.PackageRevisionMeta{}
func (f *FakePackageRevision) GetMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{}
}

func (f *FakePackageRevision) SetMeta(meta.PackageRevisionMeta) {
func (f *FakePackageRevision) SetMeta(metav1.ObjectMeta) {
}
Loading

0 comments on commit 6c7ba83

Please sign in to comment.