Skip to content

Commit

Permalink
Merge pull request #229 from uselagoon/queue-process
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Sep 19, 2023
2 parents 8a3650f + 108c490 commit fde956b
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 43 deletions.
25 changes: 23 additions & 2 deletions controllers/v1beta1/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "lagoon-sshkey",
DefaultMode: helpers.IntPtr(420),
DefaultMode: helpers.Int32Ptr(420),
},
},
},
Expand All @@ -751,7 +751,7 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: serviceaccountTokenSecret,
DefaultMode: helpers.IntPtr(420),
DefaultMode: helpers.Int32Ptr(420),
},
},
})
Expand Down Expand Up @@ -1015,3 +1015,24 @@ func (r *LagoonBuildReconciler) cancelExtraBuilds(ctx context.Context, opLog log
}
return nil
}

func sortBuilds(defaultPriority int, pendingBuilds *lagoonv1beta1.LagoonBuildList) {
sort.Slice(pendingBuilds.Items, func(i, j int) bool {
// sort by priority, then creation timestamp
iPriority := defaultPriority
jPriority := defaultPriority
if ok := pendingBuilds.Items[i].Spec.Build.Priority; ok != nil {
iPriority = *pendingBuilds.Items[i].Spec.Build.Priority
}
if ok := pendingBuilds.Items[j].Spec.Build.Priority; ok != nil {
jPriority = *pendingBuilds.Items[j].Spec.Build.Priority
}
// better sorting based on priority then creation timestamp
switch {
case iPriority != jPriority:
return iPriority < jPriority
default:
return pendingBuilds.Items[i].ObjectMeta.CreationTimestamp.Before(&pendingBuilds.Items[j].ObjectMeta.CreationTimestamp)
}
})
}
233 changes: 233 additions & 0 deletions controllers/v1beta1/build_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package v1beta1

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
lagoonv1beta1 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta1"
"github.com/uselagoon/remote-controller/internal/helpers"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func timeFromString(s string) time.Time {
time, _ := time.Parse("2006-01-02T15:04:05.000Z", s)
return time
}

func Test_sortBuilds(t *testing.T) {
type args struct {
defaultPriority int
pendingBuilds *lagoonv1beta1.LagoonBuildList
}
tests := []struct {
name string
args args
wantBuilds *lagoonv1beta1.LagoonBuildList
}{
{
name: "test1 - 5 and 6 same time order by priority",
args: args{
defaultPriority: 5,
pendingBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(6),
},
},
},
},
},
},
wantBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(6),
},
},
},
},
},
},
{
name: "test2 - 2x5 sorted by time",
args: args{
defaultPriority: 5,
pendingBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:50:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
},
},
},
wantBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:50:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
},
},
},
{
name: "test3 - 2x5 and 1x6 sorted by priority then time",
args: args{
defaultPriority: 5,
pendingBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:50:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abc1234",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:46:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(6),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
},
},
},
wantBuilds: &lagoonv1beta1.LagoonBuildList{
Items: []lagoonv1beta1.LagoonBuild{
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-1234567",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:45:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abcdefg",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:50:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(5),
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "lagoon-build-abc1234",
CreationTimestamp: v1.NewTime(timeFromString("2023-09-18T11:46:00.000Z")),
},
Spec: lagoonv1beta1.LagoonBuildSpec{
Build: lagoonv1beta1.Build{
Priority: helpers.IntPtr(6),
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sortBuilds(tt.args.defaultPriority, tt.args.pendingBuilds)
if !cmp.Equal(tt.args.pendingBuilds, tt.wantBuilds) {
t.Errorf("sortBuilds() = %v, want %v", tt.args.pendingBuilds, tt.wantBuilds)
}
})
}
}
Loading

0 comments on commit fde956b

Please sign in to comment.