Skip to content

Commit

Permalink
feat: refactor kargo events (#1853)
Browse files Browse the repository at this point in the history
Signed-off-by: Soumya Ghosh Dastidar <gdsoumya@gmail.com>
  • Loading branch information
gdsoumya authored Apr 16, 2024
1 parent 5287d0e commit d3276a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func NewPromotionCreatedEventAnnotations(
annotations[AnnotationKeyEventActor] = actor
}
if f != nil {
annotations[AnnotationKeyEventFreightCreateTime] = f.CreationTimestamp.Format(time.RFC3339)
annotations[AnnotationKeyEventFreightAlias] = f.Alias
if len(f.Commits) > 0 {
data, err := json.Marshal(f.Commits)
Expand Down
39 changes: 15 additions & 24 deletions internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type reconciler struct {
types.NamespacedName,
) (*kargoapi.Stage, error)

promoteFn func(context.Context, kargoapi.Promotion) (*kargoapi.PromotionStatus, error)
promoteFn func(context.Context, kargoapi.Promotion, *kargoapi.Freight) (*kargoapi.PromotionStatus, error)
}

// SetupReconcilerWithManager initializes a reconciler for Promotion resources
Expand Down Expand Up @@ -215,11 +215,12 @@ func (r *reconciler) Reconcile(
Name: promo.Spec.Freight,
})
if err != nil {
return ctrl.Result{}, fmt.Errorf("get freight: %w", err)
}
var freightAlias string
if freight != nil {
freightAlias = freight.Alias
return ctrl.Result{}, fmt.Errorf(
"error finding Freight %q in namespace %q: %w",
promo.Spec.Freight,
promo.Namespace,
err,
)
}

logger = logger.WithFields(log.Fields{
Expand Down Expand Up @@ -275,6 +276,7 @@ func (r *reconciler) Reconcile(
otherStatus, promoteErr := r.promoteFn(
promoCtx,
*promo,
freight,
)
if promoteErr != nil {
newStatus.Phase = kargoapi.PromotionPhaseErrored
Expand Down Expand Up @@ -340,9 +342,13 @@ func (r *reconciler) Reconcile(
kargoapi.AnnotationKeyEventFreightName: promo.Spec.Freight,
kargoapi.AnnotationKeyEventStageName: promo.Spec.Stage,
}
if freightAlias != "" {
eventAnnotations[kargoapi.AnnotationKeyEventFreightAlias] = freightAlias
if freight != nil {
if freight.Alias != "" {
eventAnnotations[kargoapi.AnnotationKeyEventFreightAlias] = freight.Alias
}
eventAnnotations[kargoapi.AnnotationKeyEventFreightCreateTime] = freight.CreationTimestamp.Format(time.RFC3339)
}

if newStatus.Phase == kargoapi.PromotionPhaseSucceeded {
eventAnnotations[kargoapi.AnnotationKeyEventVerificationPending] =
strconv.FormatBool(stage.Spec.Verification != nil)
Expand Down Expand Up @@ -378,6 +384,7 @@ func (r *reconciler) Reconcile(
func (r *reconciler) promote(
ctx context.Context,
promo kargoapi.Promotion,
targetFreight *kargoapi.Freight,
) (*kargoapi.PromotionStatus, error) {
logger := logging.LoggerFromContext(ctx)
stageName := promo.Spec.Stage
Expand All @@ -399,22 +406,6 @@ func (r *reconciler) promote(
}
logger.Debug("found associated Stage")

targetFreight, err := kargoapi.GetFreight(
ctx,
r.kargoClient,
types.NamespacedName{
Namespace: promo.Namespace,
Name: promo.Spec.Freight,
},
)
if err != nil {
return nil, fmt.Errorf(
"error finding Freight %q in namespace %q: %w",
promo.Spec.Freight,
promo.Namespace,
err,
)
}
if targetFreight == nil {
return nil, fmt.Errorf("Freight %q not found in namespace %q", promo.Spec.Freight, promo.Namespace)
}
Expand Down
16 changes: 9 additions & 7 deletions internal/controller/promotions/promotions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func newFakeReconciler(

func TestReconcile(t *testing.T) {
testCases := []struct {
name string
promos []client.Object
promoteFn func(context.Context, v1alpha1.Promotion) (*kargoapi.PromotionStatus, error)
name string
promos []client.Object
promoteFn func(context.Context, v1alpha1.Promotion,
*v1alpha1.Freight) (*kargoapi.PromotionStatus, error)
promoToReconcile *types.NamespacedName // if nil, uses the first of the promos
expectPromoteFnCalled bool
expectedPhase kargoapi.PromotionPhase
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestReconcile(t *testing.T) {
promos: []client.Object{
newPromo("fake-namespace", "fake-promo", "fake-stage", kargoapi.PromotionPhasePending, before),
},
promoteFn: func(_ context.Context, _ v1alpha1.Promotion) (*kargoapi.PromotionStatus, error) {
promoteFn: func(_ context.Context, _ v1alpha1.Promotion, _ *v1alpha1.Freight) (*kargoapi.PromotionStatus, error) {
panic("expected panic")
},
},
Expand All @@ -142,7 +143,7 @@ func TestReconcile(t *testing.T) {
promos: []client.Object{
newPromo("fake-namespace", "fake-promo", "fake-stage", kargoapi.PromotionPhasePending, before),
},
promoteFn: func(_ context.Context, _ v1alpha1.Promotion) (*kargoapi.PromotionStatus, error) {
promoteFn: func(_ context.Context, _ v1alpha1.Promotion, _ *v1alpha1.Freight) (*kargoapi.PromotionStatus, error) {
return nil, errors.New("expected error")
},
},
Expand All @@ -158,10 +159,11 @@ func TestReconcile(t *testing.T) {
Spec: &kargoapi.StageSpec{},
}, nil
}
r.promoteFn = func(ctx context.Context, p v1alpha1.Promotion) (*kargoapi.PromotionStatus, error) {
r.promoteFn = func(ctx context.Context, p v1alpha1.Promotion,
f *v1alpha1.Freight) (*kargoapi.PromotionStatus, error) {
promoteWasCalled = true
if tc.promoteFn != nil {
return tc.promoteFn(ctx, p)
return tc.promoteFn(ctx, p, f)
}
return &kargoapi.PromotionStatus{Phase: kargoapi.PromotionPhaseSucceeded}, nil
}
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/stages/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,11 +1531,12 @@ func (r *reconciler) recordFreightVerificationEvent(
ar *rollouts.AnalysisRun,
) {
annotations := map[string]string{
kargoapi.AnnotationKeyEventActor: kargoapi.FormatEventControllerActor(r.cfg.Name()),
kargoapi.AnnotationKeyEventProject: s.Namespace,
kargoapi.AnnotationKeyEventStageName: s.Name,
kargoapi.AnnotationKeyEventFreightAlias: fr.Alias,
kargoapi.AnnotationKeyEventFreightName: fr.Name,
kargoapi.AnnotationKeyEventActor: kargoapi.FormatEventControllerActor(r.cfg.Name()),
kargoapi.AnnotationKeyEventProject: s.Namespace,
kargoapi.AnnotationKeyEventStageName: s.Name,
kargoapi.AnnotationKeyEventFreightAlias: fr.Alias,
kargoapi.AnnotationKeyEventFreightName: fr.Name,
kargoapi.AnnotationKeyEventFreightCreateTime: fr.CreationTimestamp.Format(time.RFC3339),
}
if vi.StartTime != nil {
annotations[kargoapi.AnnotationKeyEventVerificationStartTime] = vi.StartTime.Format(time.RFC3339)
Expand Down

0 comments on commit d3276a0

Please sign in to comment.