@@ -50,6 +50,7 @@ type dbPackageRevision struct {
5050 updated time.Time
5151 updatedBy string
5252 lifecycle porchapi.PackageRevisionLifecycle
53+ extPRID kptfile.UpstreamLock
5354 latest bool
5455 tasks []porchapi.Task
5556 resources map [string ]string
@@ -83,7 +84,7 @@ func (pr *dbPackageRevision) savePackageRevision(ctx context.Context, saveResour
8384 _ , err := pkgRevReadFromDB (ctx , pr .Key (), false )
8485 if err == nil {
8586 updErr := pkgRevUpdateDB (ctx , pr , saveResources )
86- if updErr == nil {
87+ if updErr == nil && saveResources {
8788 sent := pr .repo .repoPRChangeNotifier .NotifyPackageRevisionChange (watch .Modified , pr )
8889 klog .Infof ("DB cache %+v: sent %d notifications for updated package revision %+v" , pr .repo .Key (), sent , pr .Key ())
8990 }
@@ -151,29 +152,11 @@ func (pr *dbPackageRevision) GetPackageRevision(ctx context.Context) (*porchapi.
151152 }
152153 }
153154
154- _ , lock , _ := pr .GetUpstreamLock (ctx )
155- lockCopy := & porchapi.UpstreamLock {}
156-
157- // TODO: Comment copied from pkg/externalrepo/git/package.go
158- // Use kpt definition of UpstreamLock in the package revision status
159- // when https://github.com/GoogleContainerTools/kpt/issues/3297 is complete.
160- // Until then, we have to translate from one type to another.
161- if lock .Git != nil {
162- lockCopy = & porchapi.UpstreamLock {
163- Type : porchapi .OriginType (lock .Type ),
164- Git : & porchapi.GitLock {
165- Repo : lock .Git .Repo ,
166- Directory : lock .Git .Directory ,
167- Commit : lock .Git .Commit ,
168- Ref : lock .Git .Ref ,
169- },
170- }
171- }
172-
155+ _ , upstreamLock , _ := pr .GetUpstreamLock (ctx )
173156 kf , _ := readPR .GetKptfile (ctx )
174157
175158 status := porchapi.PackageRevisionStatus {
176- UpstreamLock : lockCopy ,
159+ UpstreamLock : repository . KptUpstreamLock2APIUpstreamLock ( upstreamLock ) ,
177160 Deployment : pr .repo .deployment ,
178161 Conditions : repository .ToAPIConditions (kf ),
179162 }
@@ -293,11 +276,14 @@ func (pr *dbPackageRevision) ToMainPackageRevision(ctx context.Context) reposito
293276 updated : time .Now (),
294277 updatedBy : getCurrentUser (),
295278 lifecycle : pr .lifecycle ,
279+ extPRID : pr .extPRID ,
296280 latest : false ,
297281 tasks : pr .tasks ,
298282 resources : pr .resources ,
299283 }
300284
285+ mainPR .meta .CreationTimestamp = metav1.Time {Time : time .Now ()}
286+
301287 if mainPR .pkgRevKey .WorkspaceName == "" {
302288 mainPR .pkgRevKey .WorkspaceName = "main"
303289 }
@@ -336,31 +322,7 @@ func (pr *dbPackageRevision) GetKptfile(ctx context.Context) (kptfile.KptFile, e
336322}
337323
338324func (pr * dbPackageRevision ) GetLock () (kptfile.Upstream , kptfile.UpstreamLock , error ) {
339- if porchapi .LifecycleIsPublished (pr .lifecycle ) {
340- externalPr , err := pr .repo .getExternalPr (context .Background (), pr .Key ())
341- if err != nil {
342- return kptfile.Upstream {}, kptfile.UpstreamLock {},
343- pkgerrors .Wrapf (err , "dbPackageRevision:GetLock: getting lock of %+v failed, could not find package revision on external repository" , pr .Key ())
344- }
345-
346- return externalPr .GetLock ()
347- } else {
348- return kptfile.Upstream {
349- Type : kptfile .GitOrigin ,
350- Git : & kptfile.Git {
351- Repo : pr .repo .spec .Spec .Git .Repo ,
352- Directory : pr .Key ().PKey ().ToPkgPathname (),
353- Ref : "drafts/" + pr .Key ().PKey ().ToPkgPathname () + "/" + pr .Key ().WorkspaceName ,
354- },
355- }, kptfile.UpstreamLock {
356- Type : kptfile .GitOrigin ,
357- Git : & kptfile.GitLock {
358- Repo : pr .repo .spec .Spec .Git .Repo ,
359- Directory : pr .Key ().PKey ().ToPkgPathname (),
360- Ref : "drafts/" + pr .Key ().PKey ().ToPkgPathname () + "/" + pr .Key ().WorkspaceName ,
361- },
362- }, nil
363- }
325+ return repository .KptUpstreamLock2KptUpstream (pr .extPRID ), pr .extPRID , nil
364326}
365327
366328func (pr * dbPackageRevision ) ResourceVersion () string {
@@ -427,25 +389,50 @@ func (pr *dbPackageRevision) publishPR(ctx context.Context, newLifecycle porchap
427389 pr .pkgRevKey .Revision = latestRev + 1
428390 pr .lifecycle = newLifecycle
429391
430- if _ , err := engine .PushPackageRevision (ctx , pr .repo .externalRepo , pr ); err != nil {
392+ pushedPRExtID , err := engine .PushPackageRevision (ctx , pr .repo .externalRepo , pr )
393+ if err != nil {
431394 klog .Warningf ("push of package revision %+v to external repo failed, %q" , pr .Key (), err )
432395 pr .pkgRevKey .Revision = 0
433396 pr .lifecycle = porchapi .PackageRevisionLifecycleProposed
434397 return pkgerrors .Wrapf (err , "dbPackageRevision:publishPR: push of package revision %+v to external repo failed" , pr .Key ())
435398 }
436399
437- if pr .pkgRevKey .Revision == 1 {
438- if err = pkgRevWriteToDB (ctx , pr .ToMainPackageRevision (ctx ).(* dbPackageRevision )); err != nil {
439- return pkgerrors .Wrapf (err , "dbPackageRevision:UpdateLifecycle: could not write placeholder package revision for package revision %+v to DB" , pr .Key ())
400+ pr .extPRID = pushedPRExtID
401+
402+ if err = pkgRevUpdateDB (ctx , pr , false ); err != nil {
403+ return pkgerrors .Wrapf (err , "dbPackageRevision:publishPR: failed to save package revision %+v to database after push to external repo" , pr .Key ())
404+ }
405+
406+ return pr .publishPlaceholderPRForPR (ctx )
407+ }
408+
409+ func (pr * dbPackageRevision ) publishPlaceholderPRForPR (ctx context.Context ) error {
410+ _ , span := tracer .Start (ctx , "dbPackageRevision::publishPlaceholderPRForPR" , trace .WithAttributes ())
411+ defer span .End ()
412+
413+ prWithResources := pr
414+ if len (prWithResources .resources ) == 0 {
415+ if readPR , err := pkgRevReadFromDB (ctx , pr .Key (), true ); err == nil {
416+ prWithResources = readPR
417+ } else {
418+ return pkgerrors .Wrapf (err , "dbPackageRevision:publishPlaceholderPRForPR: could read resources for package revision %+v to DB" , pr .Key ())
419+ }
420+ }
421+
422+ placeholderPR := prWithResources .ToMainPackageRevision (ctx ).(* dbPackageRevision )
423+
424+ if prWithResources .pkgRevKey .Revision == 1 {
425+ if err := pkgRevWriteToDB (ctx , placeholderPR ); err != nil {
426+ return pkgerrors .Wrapf (err , "dbPackageRevision:publishPlaceholderPRForPR: could not write placeholder package revision for package revision %+v to DB" , placeholderPR .Key ())
440427 }
441- sent := pr .repo .repoPRChangeNotifier .NotifyPackageRevisionChange (watch .Added , pr )
442- klog .Infof ("DB cache %+v: sent %d notifications for added package revision %+v" , pr .repo .Key (), sent , pr .Key ())
443- } else if pr .pkgRevKey .Revision > 1 {
444- if err = pkgRevUpdateDB (ctx , pr . ToMainPackageRevision ( ctx ).( * dbPackageRevision ) , true ); err != nil {
445- return pkgerrors .Wrapf (err , "dbPackageRevision:UpdateLifecycle : could not update placeholder package revision for package revision %+v to DB" , pr .Key ())
428+ sent := placeholderPR .repo .repoPRChangeNotifier .NotifyPackageRevisionChange (watch .Added , placeholderPR )
429+ klog .Infof ("DB cache %+v: sent %d notifications for added package revision %+v" , placeholderPR .repo .Key (), sent , placeholderPR .Key ())
430+ } else if prWithResources .pkgRevKey .Revision > 1 {
431+ if err : = pkgRevUpdateDB (ctx , placeholderPR , true ); err != nil {
432+ return pkgerrors .Wrapf (err , "dbPackageRevision:publishPlaceholderPRForPR : could not update placeholder package revision for package revision %+v to DB" , placeholderPR .Key ())
446433 }
447- sent := pr .repo .repoPRChangeNotifier .NotifyPackageRevisionChange (watch .Modified , pr )
448- klog .Infof ("DB cache %+v: sent %d notifications for updated package revision %+v" , pr .repo .Key (), sent , pr .Key ())
434+ sent := placeholderPR .repo .repoPRChangeNotifier .NotifyPackageRevisionChange (watch .Modified , placeholderPR )
435+ klog .Infof ("DB cache %+v: sent %d notifications for updated package revision %+v" , placeholderPR .repo .Key (), sent , placeholderPR .Key ())
449436 }
450437
451438 return nil
0 commit comments