Skip to content

Commit

Permalink
fix(controller): fix panic when last promotion failed
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lo-A-Foe <andy.loafoe@gmail.com>
  • Loading branch information
loafoe committed Jul 29, 2024
1 parent aff97d8 commit 8266961
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (r *reconciler) promote(
Charts: targetFreight.Charts,
Origin: targetFreight.Origin,
}
targetFreightCol := r.buildTargetFreightCollection(targetFreightRef, stage)
targetFreightCol := r.buildTargetFreightCollection(ctx, targetFreightRef, stage)

Check warning on line 489 in internal/controller/promotions/promotions.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotions/promotions.go#L489

Added line #L489 was not covered by tests

newStatus, nextFreight, err :=
r.promoMechanisms.Promote(ctx, stage, &promo, targetFreightCol.References())
Expand Down Expand Up @@ -533,9 +533,11 @@ 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)

Check warning on line 540 in internal/controller/promotions/promotions.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotions/promotions.go#L540

Added line #L540 was not covered by tests
freightCol := &kargoapi.FreightCollection{}

// We don't simply copy the current FreightCollection because we want to
Expand All @@ -544,6 +546,10 @@ func (r *reconciler) buildTargetFreightCollection(
lastPromo := stage.Status.LastPromotion
if lastPromo != nil {
for _, req := range stage.Spec.RequestedFreight {
if lastPromo.Status == nil || lastPromo.Status.FreightCollection == nil {
logger.Error(nil, "last promotion has no freight collection")
continue

Check warning on line 551 in internal/controller/promotions/promotions.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotions/promotions.go#L549-L551

Added lines #L549 - L551 were not covered by tests
}
if freight, ok := lastPromo.Status.FreightCollection.Freight[req.Origin.String()]; ok {
freightCol.UpdateOrPush(freight)
}
Expand Down

0 comments on commit 8266961

Please sign in to comment.