Skip to content

Commit

Permalink
Updating promotion algorithm to make sure it adwhere to the rfc
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Filho committed Oct 16, 2023
1 parent 58b85d6 commit c2fad40
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions controllers/leveltriggered/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,25 @@ func (r *PipelineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, nil
}

for i := 1; i < len(pipeline.Spec.Environments); i++ {
env := pipeline.Spec.Environments[i]
if !checkAllTargetsAreReady(pipeline.Status.Environments[firstEnv.Name]) {
// not all targets are ready, so we can't proceed
return ctrl.Result{}, nil
}

ok, revision := checkAllTargetsHaveSameRevision(pipeline.Status.Environments[env.Name])
if !ok {
// not all targets have the same revision, so we can't proceed
return ctrl.Result{}, nil
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]) {
continue
}

if revision != latestRevision {
err := r.promoteLatestRevision(ctx, pipeline, env, latestRevision)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error promoting new version: %w", err)
}

if checkAnyTargetHasRevision(pipeline.Status.Environments[env.Name], latestRevision) {
return ctrl.Result{}, nil
}

err := r.promoteLatestRevision(ctx, pipeline, env, latestRevision)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error promoting new version: %w", err)
}
}

return ctrl.Result{}, nil
Expand All @@ -236,6 +238,26 @@ func (r *PipelineReconciler) promoteLatestRevision(ctx context.Context, pipeline
return err
}

func checkAnyTargetHasRevision(env *v1alpha1.EnvironmentStatus, revision string) bool {
for _, target := range env.Targets {
if target.Revision == revision {
return true
}
}

return false
}

func checkAllTargetsRunRevision(env *v1alpha1.EnvironmentStatus, revision string) bool {
for _, target := range env.Targets {
if target.Revision != revision {
return false
}
}

return true
}

// checkAllTargetsHaveSameRevision loops thought the targets of an environment and returns the revision only if all targets contains the same value,
// otherwise it returns an empty string.
func checkAllTargetsHaveSameRevision(env *v1alpha1.EnvironmentStatus) (bool, string) {
Expand All @@ -253,6 +275,26 @@ func checkAllTargetsHaveSameRevision(env *v1alpha1.EnvironmentStatus) (bool, str
return true, revision
}

func checkAllTargetsAreReady(env *v1alpha1.EnvironmentStatus) bool {
for _, target := range env.Targets {
if !target.Ready {
return false
}
}

return true
}

func checkAnyTargetIsReady(env *v1alpha1.EnvironmentStatus) bool {

Check failure on line 288 in controllers/leveltriggered/controller.go

View workflow job for this annotation

GitHub Actions / lint

func `checkAnyTargetIsReady` is unused (unused)
for _, target := range env.Targets {
if target.Ready {
return true
}
}

return false
}

// getClusterClient retrieves or creates a client for the cluster in question. A `nil` value for the argument indicates the local cluster.
func (r *PipelineReconciler) getClusterClient(cluster *clusterctrlv1alpha1.GitopsCluster) (client.Client, error) {
if cluster == nil {
Expand Down

0 comments on commit c2fad40

Please sign in to comment.