Skip to content

Commit

Permalink
Make the .metadata.uid field of PackageRevisions really unique across…
Browse files Browse the repository at this point in the history
… the whole cluster
  • Loading branch information
kispaljr committed May 31, 2024
1 parent 463f777 commit 9b8e8d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
22 changes: 15 additions & 7 deletions pkg/git/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package git

import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
Expand All @@ -24,6 +25,7 @@ import (
"time"

"github.com/go-git/go-git/v5/plumbing"
"github.com/google/uuid"
"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"
Expand All @@ -33,6 +35,10 @@ import (
"k8s.io/klog/v2"
)

const (
uuidSpace = "aac71d91-5c67-456f-8fd2-902ef6da820e"
)

type gitPackageRevision struct {
repo *gitRepository // repo is repo containing the package
path string // the path to the package from the repo root
Expand Down Expand Up @@ -103,13 +109,15 @@ func (p *gitPackageRevision) Key() repository.PackageRevisionKey {
}

func (p *gitPackageRevision) uid() types.UID {
var s string
if p.revision == string(p.repo.branch) {
s = p.revision
} else {
s = string(p.workspaceName)
}
return types.UID(fmt.Sprintf("uid:%s:%s", p.path, s))
space := uuid.MustParse(uuidSpace)
buff := bytes.Buffer{}
buff.WriteString("packagerevisions.")
buff.WriteString(v1alpha1.SchemeGroupVersion.Identifier())
buff.WriteString("/")
buff.WriteString(p.KubeObjectNamespace())
buff.WriteString("/")
buff.WriteString(p.KubeObjectName()) // KubeObjectName() is unique in a given namespace
return types.UID(uuid.NewSHA1(space, buff.Bytes()).String())
}

func (p *gitPackageRevision) GetPackageRevision(ctx context.Context) (*v1alpha1.PackageRevision, error) {
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,10 +1141,11 @@ func (t *PorchSuite) TestDeleteAndRecreate(ctx context.Context) {
var pkg porchapi.PackageRevision
t.mustExist(ctx, client.ObjectKey{Namespace: t.namespace, Name: created.Name}, &pkg)

// Propose the package revision to be finalized
t.Log("Propose the package revision to be finalized")
pkg.Spec.Lifecycle = porchapi.PackageRevisionLifecycleProposed
t.UpdateF(ctx, &pkg)

t.Log("Approve the package revision to be finalized")
pkg.Spec.Lifecycle = porchapi.PackageRevisionLifecyclePublished
t.UpdateApprovalF(ctx, &pkg, metav1.UpdateOptions{})

Expand Down

0 comments on commit 9b8e8d1

Please sign in to comment.