Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement deletion order. #154

Merged
merged 4 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/v1alpha1/githubactionrunner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,24 @@ type GithubActionRunnerSpec struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default="1m"
ReconciliationPeriod string `json:"reconciliationPeriod"`

// What order to delete idle pods in
// +kubebuilder:default="LeastRecent"
// +kubebuilder:validation:Optional
DeletionOrder SortOrder `json:"deletionOrder"`
}

const (
// LeastRecent first.
LeastRecent SortOrder = "LeastRecent"
// MostRecent first.
MostRecent SortOrder = "MostRecent"
)

// SortOrder defines order to sort by when sorting on creation timestamp.
// +kubebuilder:validation:Enum=MostRecent;LeastRecent
type SortOrder string

// IsValid validates conditions not covered by basic OpenAPI constraints
func (r GithubActionRunnerSpec) IsValid() (bool, error) {
if r.MaxRunners < r.MinRunners {
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/garo.tietoevry.com_githubactionrunners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ spec:
spec:
description: GithubActionRunnerSpec defines the desired state of GithubActionRunner
properties:
deletionOrder:
default: LeastRecent
description: What order to delete idle pods in
enum:
- MostRecent
- LeastRecent
type: string
maxRunners:
description: Maximum pool-size. Must be greater or equal to minRunners
minimum: 1
Expand Down
2 changes: 1 addition & 1 deletion controllers/githubactionrunner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r *GithubActionRunnerReconciler) handleScaling(ctx context.Context, instan
} else if shouldScaleDown(podRunnerPairs, instance) {
logger.Info("Scaling down", "runners at github", podRunnerPairs.numRunners(), "maxrunners in CR", instance.Spec.MaxRunners)

pod := podRunnerPairs.getIdlePods()[0]
pod := podRunnerPairs.getIdlePods(instance.Spec.DeletionOrder)[0]
err := r.DeleteResourceIfExists(ctx, &pod)
if err == nil {
r.GetRecorder().Event(instance, corev1.EventTypeNormal, "Scaling", fmt.Sprintf("Deleted pod %s/%s", pod.Namespace, pod.Name))
Expand Down
15 changes: 13 additions & 2 deletions controllers/podrunner_types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package controllers

import (
"github.com/evryfs/github-actions-runner-operator/api/v1alpha1"
"github.com/google/go-github/v33/github"
"github.com/redhat-cop/operator-utils/pkg/util"
"github.com/thoas/go-funk"
corev1 "k8s.io/api/core/v1"
"sort"
)

type podRunnerPair struct {
Expand Down Expand Up @@ -66,13 +68,22 @@ func (r podRunnerPairList) inSync() bool {
return r.numPods() == r.numRunners()
}

func (r podRunnerPairList) getIdlePods() []corev1.Pod {
func (r podRunnerPairList) getIdlePods(sortOrder v1alpha1.SortOrder) []corev1.Pod {
idles := funk.Filter(r.pairs, func(pair podRunnerPair) bool {
return !(pair.runner.GetBusy() || util.IsBeingDeleted(&pair.pod))
}).([]podRunnerPair)
return funk.Map(idles, func(pair podRunnerPair) corev1.Pod {
pods := funk.Map(idles, func(pair podRunnerPair) corev1.Pod {
return pair.pod
}).([]corev1.Pod)

sort.SliceStable(pods, func(i, j int) bool {
if sortOrder == v1alpha1.LeastRecent {
return pods[i].CreationTimestamp.Unix() < pods[j].CreationTimestamp.Unix()
}
return pods[i].CreationTimestamp.Unix() > pods[j].CreationTimestamp.Unix()
})

return pods
}

func (r podRunnerPairList) getPodsBeingDeleted() []podRunnerPair {
Expand Down
24 changes: 22 additions & 2 deletions controllers/podrunner_types_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package controllers

import (
"github.com/evryfs/github-actions-runner-operator/api/v1alpha1"
"github.com/google/go-github/v33/github"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"testing"
"time"
)

var podList = v1.PodList{
Expand All @@ -16,15 +18,17 @@ var podList = v1.PodList{
{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "name1",
Name: "name1",
CreationTimestamp: metav1.NewTime(time.Now()),
},
Spec: v1.PodSpec{},
Status: v1.PodStatus{},
},
{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "name2",
Name: "name2",
CreationTimestamp: metav1.NewTime(time.Now().Add(time.Minute)),
},
Spec: v1.PodSpec{},
Status: v1.PodStatus{},
Expand Down Expand Up @@ -67,3 +71,19 @@ func TestPodRunnerPairList(t *testing.T) {
assert.Equal(t, podRunnerPairList.inSync(), tc.inSync)
}
}

func TestSort(t *testing.T) {
testCases := []struct {
sortOrder v1alpha1.SortOrder
podRunnerPairList podRunnerPairList
podList []v1.Pod
}{
{v1alpha1.LeastRecent, from(&podList, runners), []v1.Pod{podList.Items[0], podList.Items[1]}},
{v1alpha1.MostRecent, from(&podList, runners), []v1.Pod{podList.Items[1], podList.Items[0]}},
}

for _, tc := range testCases {
podList := tc.podRunnerPairList.getIdlePods(tc.sortOrder)
assert.Equal(t, podList, tc.podList)
}
}