Skip to content

Commit

Permalink
chore(backport release-0.8): fix(controller): fix panic when last pro…
Browse files Browse the repository at this point in the history
…motion has no freight collection (#2366)

Co-authored-by: Andy Lo-A-Foe <[email protected]>
  • Loading branch information
akuitybot and loafoe authored Jul 29, 2024
1 parent fc64b2f commit 872fb07
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func (r *reconciler) promote(
Charts: targetFreight.Charts,
Origin: targetFreight.Origin,
}
targetFreightCol := r.buildTargetFreightCollection(targetFreightRef, stage)
targetFreightCol := r.buildTargetFreightCollection(ctx, targetFreightRef, stage)

newStatus, nextFreight, err :=
r.promoMechanisms.Promote(ctx, stage, &promo, targetFreightCol.References())
Expand Down Expand Up @@ -544,20 +544,26 @@ func (r *reconciler) promote(
// FreightReferences from the previous Promotion (excepting those that are no
// longer requested), plus a FreightReference for the provided targetFreight.
func (r *reconciler) buildTargetFreightCollection(
ctx context.Context,
targetFreight kargoapi.FreightReference,
stage *kargoapi.Stage,
) *kargoapi.FreightCollection {
logger := logging.LoggerFromContext(ctx)
freightCol := &kargoapi.FreightCollection{}

// We don't simply copy the current FreightCollection because we want to
// account for the possibility that some freight contained therein are no
// longer requested by the Stage.
lastPromo := stage.Status.LastPromotion
if lastPromo != nil {
for _, req := range stage.Spec.RequestedFreight {
if freight, ok := lastPromo.Status.FreightCollection.Freight[req.Origin.String()]; ok {
freightCol.UpdateOrPush(freight)
if len(stage.Spec.RequestedFreight) > 1 {
lastPromo := stage.Status.LastPromotion
if lastPromo.Status != nil && lastPromo.Status.FreightCollection != nil {
for _, req := range stage.Spec.RequestedFreight {
if freight, ok := lastPromo.Status.FreightCollection.Freight[req.Origin.String()]; ok {
freightCol.UpdateOrPush(freight)
}
}
} else {
logger.Debug("last promotion has no collection to inherit Freight from")
}
}
freightCol.UpdateOrPush(targetFreight)
Expand Down

0 comments on commit 872fb07

Please sign in to comment.