Skip to content

Commit

Permalink
Setting lastest revision to pipeline status
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Filho committed Nov 8, 2023
1 parent de1015e commit 6b0faee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ type PipelineStatus struct {
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// LatestRevision is the latest revision that's been promoted to the environments.
// +optional
LatestRevision string `json:"latestRevision,omitempty"`

// Environments holds environment statuses.
// +optional
Environments map[string]*EnvironmentStatus `json:"environments"`
Expand Down
22 changes: 22 additions & 0 deletions controllers/leveltriggered/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func (r *PipelineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, fmt.Errorf("error removing pending condition: %w", err)
}

if err := r.setLatestRevision(ctx, pipeline, latestRevision); err != nil {
return ctrl.Result{}, fmt.Errorf("error setting latest revision: %w", err)
}

for _, env := range pipeline.Spec.Environments[1:] {
// if all targets run the latest revision and are ready, we can skip this environment
if checkAllTargetsRunRevision(pipeline.Status.Environments[env.Name], latestRevision) && checkAllTargetsAreReady(pipeline.Status.Environments[env.Name]) {
Expand All @@ -253,6 +257,24 @@ func (r *PipelineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, nil
}

func (r *PipelineReconciler) setLatestRevision(ctx context.Context, pipeline v1alpha1.Pipeline, revision string) error {
pipeline.Status.LatestRevision = revision

fmt.Println("setting latest revision to", revision)
if err := r.patchStatus(ctx, client.ObjectKeyFromObject(&pipeline), pipeline.Status); err != nil {
r.emitEventf(
&pipeline,
corev1.EventTypeWarning,
"SetStatus", "Failed to set LatestRevision status for pipeline %s/%s: %s",
pipeline.GetNamespace(), pipeline.GetName(),
err,
)
return err
}

return nil
}

func (r *PipelineReconciler) setPendingCondition(ctx context.Context, pipeline v1alpha1.Pipeline, reason, message string) error {
condition := metav1.Condition{
Type: conditions.PromotionPendingCondition,
Expand Down
22 changes: 22 additions & 0 deletions controllers/leveltriggered/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ func TestReconcile(t *testing.T) {
g.Expect(targetStatus.Revision).To(Equal(appRevision))
})

t.Run("sets the lastestRevision status field", func(t *testing.T) {
g := testingutils.NewGomegaWithT(t)
name := "pipeline-" + rand.String(5)
ns := testingutils.NewNamespace(ctx, g, k8sClient)
t.Cleanup(deleteObjectCleanup(ctx, g, ns))

const appRevision = "v1.0.1"

hr := createApp(ctx, k8sClient, g, name, ns.Name)
setAppRevisionAndReadyStatus(ctx, g, hr, appRevision)

pipeline := newPipeline(name, ns.Name, nil)
g.Expect(k8sClient.Create(ctx, pipeline)).To(Succeed())

checkCondition(ctx, g, client.ObjectKeyFromObject(pipeline), meta.ReadyCondition, metav1.ConditionTrue, v1alpha1.ReconciliationSucceededReason)

g.Eventually(func() string {
p := getPipeline(ctx, g, client.ObjectKeyFromObject(pipeline))
return p.Status.LatestRevision
}, "5s", "0.2s").Should(Equal(appRevision))
})

t.Run("promotes revision to all environments", func(t *testing.T) {
g := testingutils.NewGomegaWithT(t)
mockStrategy := setStrategyRegistry(t, pipelineReconciler)
Expand Down

0 comments on commit 6b0faee

Please sign in to comment.