Skip to content

Commit 64c3ec9

Browse files
#1102 - Fix upgrade CLI to run get to find package revision (#547)
* Fix upgrade CLI to run get to find origin * Always list on fallback --------- Co-authored-by: Liam Fallon <35595825+liamfallon@users.noreply.github.com>
1 parent 9b10233 commit 64c3ec9

2 files changed

Lines changed: 55 additions & 18 deletions

File tree

pkg/cli/commands/rpkg/upgrade/command.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ func (r *runner) findUpstreamName(pr *porchapi.PackageRevision) string {
386386
return n
387387
}
388388
if pr.Status.UpstreamLock != nil {
389+
if err := r.listPackageRevisions(); err != nil {
390+
return ""
391+
}
389392
if up := r.findUpstreamByLock(pr.Status.UpstreamLock); up != nil {
390393
return up.Name
391394
}
@@ -402,20 +405,27 @@ func (r *runner) findEditOrigin(currentPr *porchapi.PackageRevision) string {
402405
pr := currentPr
403406
for pr != nil && pr.Spec.Tasks[0].Type == porchapi.TaskTypeEdit {
404407
sourceName := pr.Spec.Tasks[0].Edit.Source.Name
405-
pr = nil
406-
for _, p := range r.prs {
407-
if p.Name == sourceName {
408-
pr = &p
409-
break
410-
}
411-
}
408+
pr = r.findPackageRevision(sourceName)
412409
}
413410
if pr != nil {
414411
return r.findUpstreamName(pr)
415412
}
416413
return ""
417414
}
418415

416+
func (r *runner) listPackageRevisions() error {
417+
packageRevisionList := porchapi.PackageRevisionList{}
418+
listOpts := []client.ListOption{}
419+
if r.cfg.Namespace != nil && *r.cfg.Namespace != "" {
420+
listOpts = append(listOpts, client.InNamespace(*r.cfg.Namespace))
421+
}
422+
if err := r.client.List(r.ctx, &packageRevisionList, listOpts...); err != nil {
423+
return err
424+
}
425+
r.prs = packageRevisionList.Items
426+
return nil
427+
}
428+
419429
func (r *runner) findUpstreamByLock(lock *porchapi.Locator) *porchapi.PackageRevision {
420430
if lock == nil || lock.Git == nil {
421431
return nil

pkg/cli/commands/rpkg/upgrade/command_test.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ func TestFindEditOrigin(t *testing.T) {
335335
const ns = "ns"
336336
downstreamv1 := porchapi.PackageRevision{
337337
ObjectMeta: metav1.ObjectMeta{
338-
Name: "downstream.v1",
338+
Name: "downstream.v1",
339+
Namespace: ns,
339340
},
340341
Spec: porchapi.PackageRevisionSpec{
341342
Tasks: []porchapi.Task{
@@ -354,7 +355,8 @@ func TestFindEditOrigin(t *testing.T) {
354355
}
355356
downstreamv2 := porchapi.PackageRevision{
356357
ObjectMeta: metav1.ObjectMeta{
357-
Name: "downstream.v2",
358+
Name: "downstream.v2",
359+
Namespace: ns,
358360
},
359361
Spec: porchapi.PackageRevisionSpec{
360362
Tasks: []porchapi.Task{
@@ -372,13 +374,18 @@ func TestFindEditOrigin(t *testing.T) {
372374
downstreamv3 := *downstreamv2.DeepCopy()
373375
downstreamv3.Name = "downstream.v3"
374376
downstreamv3.Spec.Tasks[0].Edit.Source = &porchapi.PackageRevisionRef{Name: "downstream.v2"}
375-
prs := []porchapi.PackageRevision{
376-
downstreamv1,
377-
downstreamv2,
378-
downstreamv3,
377+
378+
scheme := runtime.NewScheme()
379+
if err := porchapi.AddToScheme(scheme); err != nil {
380+
t.Fatalf("Failed to add porch API to scheme: %v", err)
379381
}
382+
c := fake.NewClientBuilder().
383+
WithScheme(scheme).
384+
WithObjects(&downstreamv1, &downstreamv2, &downstreamv3).
385+
Build()
380386

381-
r := createRunner(context.Background(), fake.NewClientBuilder().Build(), prs, ns, 0)
387+
// empty prs to force GET-based traversal in findEditOrigin
388+
r := createRunner(context.Background(), c, []porchapi.PackageRevision{}, ns, 0)
382389

383390
found := r.findUpstreamName(&downstreamv3)
384391
assert.Equal(t, "upstream.v1", found)
@@ -901,7 +908,8 @@ func TestFindUpstreamInEditTaskWithUpstreamLock(t *testing.T) {
901908

902909
editPr := porchapi.PackageRevision{
903910
ObjectMeta: metav1.ObjectMeta{
904-
Name: "broken-edit-pr",
911+
Name: "broken-edit-pr",
912+
Namespace: ns,
905913
},
906914
Spec: porchapi.PackageRevisionSpec{
907915
Tasks: []porchapi.Task{
@@ -928,7 +936,8 @@ func TestFindUpstreamInEditTaskWithUpstreamLock(t *testing.T) {
928936

929937
upstreamPr := porchapi.PackageRevision{
930938
ObjectMeta: metav1.ObjectMeta{
931-
Name: "upstream-pr-v2",
939+
Name: "upstream-pr-v2",
940+
Namespace: ns,
932941
},
933942
Spec: porchapi.PackageRevisionSpec{
934943
Revision: 2,
@@ -945,8 +954,26 @@ func TestFindUpstreamInEditTaskWithUpstreamLock(t *testing.T) {
945954
},
946955
}
947956

948-
prs := []porchapi.PackageRevision{editPr, upstreamPr}
949-
r := createRunner(context.Background(), fake.NewClientBuilder().Build(), prs, ns, 0)
957+
scheme := runtime.NewScheme()
958+
if err := porchapi.AddToScheme(scheme); err != nil {
959+
t.Fatalf("Failed to add porch API to scheme: %v", err)
960+
}
961+
interceptorFuncs := interceptor.Funcs{
962+
List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
963+
if prList, ok := list.(*porchapi.PackageRevisionList); ok {
964+
prList.Items = []porchapi.PackageRevision{editPr, upstreamPr}
965+
}
966+
return nil
967+
},
968+
}
969+
c := fake.NewClientBuilder().
970+
WithScheme(scheme).
971+
WithObjects(&editPr, &upstreamPr).
972+
WithInterceptorFuncs(interceptorFuncs).
973+
Build()
974+
975+
// empty prs to force listPackageRevisions call
976+
r := createRunner(context.Background(), c, []porchapi.PackageRevision{}, ns, 0)
950977

951978
result := r.findUpstreamName(&editPr)
952979

0 commit comments

Comments
 (0)