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 14, 2023
1 parent 136b757 commit d75b11c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ build: generate fmt vet ## Build manager binary.

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go --log-level debug
go run ./main.go --enable-level-triggered true --log-level debug

.PHONY: docker-build
docker-build: ## Build docker image with the manager.
Expand Down
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
2 changes: 1 addition & 1 deletion charts/pipeline-controller/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: pipeline-controller
description: Pipeline-controller Helm chart for Weave GitOps Enterprise
type: application
version: "0.22.0"
version: "0.23.0"
appVersion: "v0.0.31"
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ spec:
type: object
description: Environments holds environment statuses.
type: object
latestRevision:
description: LatestRevision is the latest revision that's been promoted
to the environments.
type: string
observedGeneration:
description: ObservedGeneration is the last observed generation.
format: int64
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/pipelines.weave.works_pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ spec:
type: object
description: Environments holds environment statuses.
type: object
latestRevision:
description: LatestRevision is the latest revision that's been promoted
to the environments.
type: string
observedGeneration:
description: ObservedGeneration is the last observed generation.
format: int64
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 d75b11c

Please sign in to comment.