Skip to content

Commit

Permalink
Fix jobs queues for the same envs (#1055)
Browse files Browse the repository at this point in the history
* Jobs of all types run in sequence for envs

* Fixed unit-tests

* Added unit-tests
  • Loading branch information
satr authored Feb 27, 2024
1 parent c953b38 commit 7d54ea4
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 43 deletions.
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.30.0
appVersion: 1.50.0
version: 1.30.1
appVersion: 1.50.1
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
59 changes: 42 additions & 17 deletions pkg/apis/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/equinor/radix-common/utils/slice"
"github.com/equinor/radix-operator/pkg/apis/applicationconfig"
apiconfig "github.com/equinor/radix-operator/pkg/apis/config"
"github.com/equinor/radix-operator/pkg/apis/kube"
"github.com/equinor/radix-operator/pkg/apis/metrics"
Expand Down Expand Up @@ -142,7 +144,7 @@ func (job *Job) syncStatuses(ra *v1.RadixApplication) (stopReconciliation bool,
return false, err
}

if job.isOtherJobRunningOnBranch(ra, allJobs.Items) {
if job.isOtherJobRunningOnBranchOrEnvironment(ra, allJobs.Items) {
err = job.queueJob()
if err != nil {
return false, err
Expand All @@ -159,36 +161,59 @@ func (job *Job) syncStatuses(ra *v1.RadixApplication) (stopReconciliation bool,
return
}

func (job *Job) isOtherJobRunningOnBranch(ra *v1.RadixApplication, allJobs []v1.RadixJob) bool {
if len(job.radixJob.Spec.Build.Branch) == 0 {
return false
}

func (job *Job) isOtherJobRunningOnBranchOrEnvironment(ra *v1.RadixApplication, allJobs []v1.RadixJob) bool {
isJobActive := func(rj *v1.RadixJob) bool {
return rj.Status.Condition == v1.JobWaiting || rj.Status.Condition == v1.JobRunning
}

jobTargetEnvironments := getTargetEnvironments(ra, job)
for _, rj := range allJobs {
if rj.GetName() == job.radixJob.GetName() || len(rj.Spec.Build.Branch) == 0 || !isJobActive(&rj) {
if rj.GetName() == job.radixJob.GetName() || !isJobActive(&rj) {
continue
}

if ra != nil {
for _, env := range ra.Spec.Environments {
if len(env.Build.From) > 0 &&
branch.MatchesPattern(env.Build.From, rj.Spec.Build.Branch) &&
branch.MatchesPattern(env.Build.From, job.radixJob.Spec.Build.Branch) {
return true
switch rj.Spec.PipeLineType {
case v1.BuildDeploy, v1.Build:
if len(jobTargetEnvironments) > 0 {
rjTargetBranches := applicationconfig.GetTargetEnvironments(rj.Spec.Build.Branch, ra)
for _, rjEnvName := range rjTargetBranches {
if _, ok := jobTargetEnvironments[rjEnvName]; ok {
return true
}
}
} else if job.radixJob.Spec.Build.Branch == rj.Spec.Build.Branch {
return true
}
case v1.Deploy:
if _, ok := jobTargetEnvironments[rj.Spec.Deploy.ToEnvironment]; ok {
return true
}
case v1.Promote:
if _, ok := jobTargetEnvironments[rj.Spec.Promote.ToEnvironment]; ok {
return true
}
} else if job.radixJob.Spec.Build.Branch == rj.Spec.Build.Branch {
return true
}
}

return false
}

func getTargetEnvironments(ra *v1.RadixApplication, job *Job) map[string]struct{} {
targetEnvs := make(map[string]struct{})
if job.radixJob.Spec.PipeLineType == v1.BuildDeploy || job.radixJob.Spec.PipeLineType == v1.Build {
if ra != nil {
return slice.Reduce(applicationconfig.GetTargetEnvironments(job.radixJob.Spec.Build.Branch, ra),
targetEnvs, func(acc map[string]struct{}, envName string) map[string]struct{} {
acc[envName] = struct{}{}
return acc
})
}
} else if job.radixJob.Spec.PipeLineType == v1.Deploy {
targetEnvs[job.radixJob.Spec.Deploy.ToEnvironment] = struct{}{}
} else if job.radixJob.Spec.PipeLineType == v1.Promote {
targetEnvs[job.radixJob.Spec.Promote.ToEnvironment] = struct{}{}
}
return targetEnvs
}

// sync the environments in the RadixJob with environments in the RA
func (job *Job) syncTargetEnvironments(ra *v1.RadixApplication) {
rj := job.radixJob
Expand Down
Loading

0 comments on commit 7d54ea4

Please sign in to comment.