2020-07-19 18:42:06 +09:00
|
|
|
/*
|
|
|
|
|
Copyright 2020 The actions-runner-controller authors.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-02-26 10:17:09 +09:00
|
|
|
"fmt"
|
2021-05-22 08:29:53 +09:00
|
|
|
"reflect"
|
2020-07-19 18:42:06 +09:00
|
|
|
"time"
|
|
|
|
|
|
2021-05-02 09:46:51 +02:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
"github.com/summerwind/actions-runner-controller/github"
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
"k8s.io/client-go/tools/record"
|
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
|
|
|
|
"github.com/summerwind/actions-runner-controller/api/v1alpha1"
|
2021-03-19 16:14:02 +09:00
|
|
|
"github.com/summerwind/actions-runner-controller/controllers/metrics"
|
2020-07-19 18:42:06 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DefaultScaleDownDelay = 10 * time.Minute
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// HorizontalRunnerAutoscalerReconciler reconciles a HorizontalRunnerAutoscaler object
|
|
|
|
|
type HorizontalRunnerAutoscalerReconciler struct {
|
|
|
|
|
client.Client
|
|
|
|
|
GitHubClient *github.Client
|
|
|
|
|
Log logr.Logger
|
|
|
|
|
Recorder record.EventRecorder
|
|
|
|
|
Scheme *runtime.Scheme
|
2021-02-07 17:37:27 +09:00
|
|
|
|
|
|
|
|
CacheDuration time.Duration
|
2021-02-19 10:33:04 +09:00
|
|
|
Name string
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
const defaultReplicas = 1
|
|
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments,verbs=get;list;watch;update;patch
|
|
|
|
|
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=horizontalrunnerautoscalers,verbs=get;list;watch;create;update;patch;delete
|
2020-10-06 09:23:03 +09:00
|
|
|
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=horizontalrunnerautoscalers/finalizers,verbs=get;list;watch;create;update;patch;delete
|
2020-07-19 18:42:06 +09:00
|
|
|
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=horizontalrunnerautoscalers/status,verbs=get;update;patch
|
|
|
|
|
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
|
|
|
|
|
|
|
|
|
|
func (r *HorizontalRunnerAutoscalerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
log := r.Log.WithValues("horizontalrunnerautoscaler", req.NamespacedName)
|
|
|
|
|
|
|
|
|
|
var hra v1alpha1.HorizontalRunnerAutoscaler
|
|
|
|
|
if err := r.Get(ctx, req.NamespacedName, &hra); err != nil {
|
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !hra.ObjectMeta.DeletionTimestamp.IsZero() {
|
|
|
|
|
return ctrl.Result{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 16:14:02 +09:00
|
|
|
metrics.SetHorizontalRunnerAutoscalerSpec(hra.ObjectMeta, hra.Spec)
|
|
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
var rd v1alpha1.RunnerDeployment
|
|
|
|
|
if err := r.Get(ctx, types.NamespacedName{
|
|
|
|
|
Namespace: req.Namespace,
|
|
|
|
|
Name: hra.Spec.ScaleTargetRef.Name,
|
|
|
|
|
}, &rd); err != nil {
|
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !rd.ObjectMeta.DeletionTimestamp.IsZero() {
|
|
|
|
|
return ctrl.Result{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
now := time.Now()
|
2021-02-07 17:37:27 +09:00
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
minReplicas, active, upcoming, err := r.getMinReplicas(log, now, hra)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(err, "Could not compute min replicas")
|
|
|
|
|
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newDesiredReplicas, computedReplicas, computedReplicasFromCache, err := r.computeReplicasWithCache(log, now, rd, hra, minReplicas)
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
if err != nil {
|
|
|
|
|
r.Recorder.Event(&hra, corev1.EventTypeNormal, "RunnerAutoscalingFailure", err.Error())
|
2020-07-19 18:42:06 +09:00
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
log.Error(err, "Could not compute replicas")
|
2020-07-19 18:42:06 +09:00
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
return ctrl.Result{}, err
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentDesiredReplicas := getIntOrDefault(rd.Spec.Replicas, defaultReplicas)
|
2021-02-07 17:37:27 +09:00
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
// Please add more conditions that we can in-place update the newest runnerreplicaset without disruption
|
|
|
|
|
if currentDesiredReplicas != newDesiredReplicas {
|
|
|
|
|
copy := rd.DeepCopy()
|
|
|
|
|
copy.Spec.Replicas = &newDesiredReplicas
|
|
|
|
|
|
2021-02-26 10:17:09 +09:00
|
|
|
if err := r.Client.Patch(ctx, copy, client.MergeFrom(&rd)); err != nil {
|
|
|
|
|
return ctrl.Result{}, fmt.Errorf("patching runnerdeployment to have %d replicas: %w", newDesiredReplicas, err)
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
updated := hra.DeepCopy()
|
2021-02-07 17:37:27 +09:00
|
|
|
|
2021-02-25 10:32:09 +09:00
|
|
|
if hra.Status.DesiredReplicas == nil || *hra.Status.DesiredReplicas != newDesiredReplicas {
|
|
|
|
|
if (hra.Status.DesiredReplicas == nil && newDesiredReplicas > 1) ||
|
|
|
|
|
(hra.Status.DesiredReplicas != nil && newDesiredReplicas > *hra.Status.DesiredReplicas) {
|
2020-07-19 18:42:06 +09:00
|
|
|
|
|
|
|
|
updated.Status.LastSuccessfulScaleOutTime = &metav1.Time{Time: time.Now()}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 10:32:09 +09:00
|
|
|
updated.Status.DesiredReplicas = &newDesiredReplicas
|
2021-02-07 17:37:27 +09:00
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
if computedReplicasFromCache == nil {
|
2021-02-26 09:54:24 +09:00
|
|
|
cacheEntries := getValidCacheEntries(updated, now)
|
2021-02-07 17:37:27 +09:00
|
|
|
|
|
|
|
|
var cacheDuration time.Duration
|
|
|
|
|
|
|
|
|
|
if r.CacheDuration > 0 {
|
|
|
|
|
cacheDuration = r.CacheDuration
|
|
|
|
|
} else {
|
|
|
|
|
cacheDuration = 10 * time.Minute
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 11:20:21 +09:00
|
|
|
updated.Status.CacheEntries = append(cacheEntries, v1alpha1.CacheEntry{
|
2021-02-07 17:37:27 +09:00
|
|
|
Key: v1alpha1.CacheEntryKeyDesiredReplicas,
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
Value: computedReplicas,
|
2021-02-07 17:37:27 +09:00
|
|
|
ExpirationTime: metav1.Time{Time: time.Now().Add(cacheDuration)},
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-07-19 18:42:06 +09:00
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
var overridesSummary string
|
|
|
|
|
|
|
|
|
|
if (active != nil && upcoming == nil) || (active != nil && upcoming != nil && active.Period.EndTime.Before(upcoming.Period.StartTime)) {
|
|
|
|
|
after := defaultReplicas
|
|
|
|
|
if hra.Spec.MinReplicas != nil && *hra.Spec.MinReplicas >= 0 {
|
|
|
|
|
after = *hra.Spec.MinReplicas
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
overridesSummary = fmt.Sprintf("min=%d time=%s", after, active.Period.EndTime)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if active == nil && upcoming != nil || (active != nil && upcoming != nil && active.Period.EndTime.After(upcoming.Period.StartTime)) {
|
|
|
|
|
if upcoming.ScheduledOverride.MinReplicas != nil {
|
|
|
|
|
overridesSummary = fmt.Sprintf("min=%d time=%s", *upcoming.ScheduledOverride.MinReplicas, upcoming.Period.StartTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if overridesSummary != "" {
|
|
|
|
|
updated.Status.ScheduledOverridesSummary = &overridesSummary
|
|
|
|
|
} else {
|
|
|
|
|
updated.Status.ScheduledOverridesSummary = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(hra.Status, updated.Status) {
|
2021-03-19 16:14:02 +09:00
|
|
|
metrics.SetHorizontalRunnerAutoscalerStatus(updated.ObjectMeta, updated.Status)
|
|
|
|
|
|
2021-02-26 10:17:09 +09:00
|
|
|
if err := r.Status().Patch(ctx, updated, client.MergeFrom(&hra)); err != nil {
|
2021-05-22 08:29:53 +09:00
|
|
|
return ctrl.Result{}, fmt.Errorf("patching horizontalrunnerautoscaler status: %w", err)
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctrl.Result{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 09:54:24 +09:00
|
|
|
func getValidCacheEntries(hra *v1alpha1.HorizontalRunnerAutoscaler, now time.Time) []v1alpha1.CacheEntry {
|
|
|
|
|
var cacheEntries []v1alpha1.CacheEntry
|
|
|
|
|
|
|
|
|
|
for _, ent := range hra.Status.CacheEntries {
|
|
|
|
|
if ent.ExpirationTime.After(now) {
|
|
|
|
|
cacheEntries = append(cacheEntries, ent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cacheEntries
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
func (r *HorizontalRunnerAutoscalerReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
2021-02-16 18:51:33 +09:00
|
|
|
name := "horizontalrunnerautoscaler-controller"
|
2021-02-19 10:33:04 +09:00
|
|
|
if r.Name != "" {
|
|
|
|
|
name = r.Name
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 18:51:33 +09:00
|
|
|
r.Recorder = mgr.GetEventRecorderFor(name)
|
2020-07-19 18:42:06 +09:00
|
|
|
|
|
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
2020-07-29 21:16:48 +09:00
|
|
|
For(&v1alpha1.HorizontalRunnerAutoscaler{}).
|
2021-02-16 18:51:33 +09:00
|
|
|
Named(name).
|
2020-07-19 18:42:06 +09:00
|
|
|
Complete(r)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
type Override struct {
|
|
|
|
|
ScheduledOverride v1alpha1.ScheduledOverride
|
|
|
|
|
Period Period
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *HorizontalRunnerAutoscalerReconciler) matchScheduledOverrides(log logr.Logger, now time.Time, hra v1alpha1.HorizontalRunnerAutoscaler) (*int, *Override, *Override, error) {
|
2021-05-03 23:31:17 +09:00
|
|
|
var minReplicas *int
|
2021-05-22 08:29:53 +09:00
|
|
|
var active, upcoming *Override
|
2021-05-03 23:31:17 +09:00
|
|
|
|
|
|
|
|
for _, o := range hra.Spec.ScheduledOverrides {
|
2021-05-11 17:30:22 +09:00
|
|
|
log.V(1).Info(
|
|
|
|
|
"Checking scheduled override",
|
|
|
|
|
"now", now,
|
|
|
|
|
"startTime", o.StartTime,
|
|
|
|
|
"endTime", o.EndTime,
|
|
|
|
|
"frequency", o.RecurrenceRule.Frequency,
|
|
|
|
|
"untilTime", o.RecurrenceRule.UntilTime,
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-03 23:31:17 +09:00
|
|
|
a, u, err := MatchSchedule(
|
|
|
|
|
now, o.StartTime.Time, o.EndTime.Time,
|
|
|
|
|
RecurrenceRule{
|
|
|
|
|
Frequency: o.RecurrenceRule.Frequency,
|
|
|
|
|
UntilTime: o.RecurrenceRule.UntilTime.Time,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return minReplicas, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the first when there are two or more active scheduled overrides,
|
|
|
|
|
// as the spec defines that the earlier scheduled override is prioritized higher than later ones.
|
2021-05-11 17:30:22 +09:00
|
|
|
if a != nil && active == nil {
|
2021-05-22 08:29:53 +09:00
|
|
|
active = &Override{Period: *a, ScheduledOverride: o}
|
2021-05-03 23:31:17 +09:00
|
|
|
|
|
|
|
|
if o.MinReplicas != nil {
|
|
|
|
|
minReplicas = o.MinReplicas
|
2021-05-11 17:30:22 +09:00
|
|
|
|
|
|
|
|
log.V(1).Info(
|
|
|
|
|
"Found active scheduled override",
|
|
|
|
|
"activeStartTime", a.StartTime,
|
|
|
|
|
"activeEndTime", a.EndTime,
|
|
|
|
|
"activeMinReplicas", minReplicas,
|
|
|
|
|
)
|
2021-05-03 23:31:17 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
if u != nil && (upcoming == nil || u.StartTime.Before(upcoming.Period.StartTime)) {
|
|
|
|
|
upcoming = &Override{Period: *u, ScheduledOverride: o}
|
2021-05-11 17:30:22 +09:00
|
|
|
|
|
|
|
|
log.V(1).Info(
|
|
|
|
|
"Found upcoming scheduled override",
|
|
|
|
|
"upcomingStartTime", u.StartTime,
|
|
|
|
|
"upcomingEndTime", u.EndTime,
|
|
|
|
|
"upcomingMinReplicas", o.MinReplicas,
|
|
|
|
|
)
|
2021-05-03 23:31:17 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return minReplicas, active, upcoming, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
func (r *HorizontalRunnerAutoscalerReconciler) getMinReplicas(log logr.Logger, now time.Time, hra v1alpha1.HorizontalRunnerAutoscaler) (int, *Override, *Override, error) {
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
minReplicas := defaultReplicas
|
2021-05-02 09:46:51 +02:00
|
|
|
if hra.Spec.MinReplicas != nil && *hra.Spec.MinReplicas >= 0 {
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
minReplicas = *hra.Spec.MinReplicas
|
|
|
|
|
}
|
2020-07-19 18:42:06 +09:00
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
m, active, upcoming, err := r.matchScheduledOverrides(log, now, hra)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, nil, err
|
2021-05-03 23:31:17 +09:00
|
|
|
} else if m != nil {
|
|
|
|
|
minReplicas = *m
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 08:29:53 +09:00
|
|
|
return minReplicas, active, upcoming, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *HorizontalRunnerAutoscalerReconciler) computeReplicasWithCache(log logr.Logger, now time.Time, rd v1alpha1.RunnerDeployment, hra v1alpha1.HorizontalRunnerAutoscaler, minReplicas int) (int, int, *int, error) {
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
var suggestedReplicas int
|
|
|
|
|
|
|
|
|
|
suggestedReplicasFromCache := r.fetchSuggestedReplicasFromCache(hra)
|
|
|
|
|
|
|
|
|
|
var cached *int
|
|
|
|
|
|
|
|
|
|
if suggestedReplicasFromCache != nil {
|
|
|
|
|
cached = suggestedReplicasFromCache
|
|
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
|
suggestedReplicas = minReplicas
|
|
|
|
|
} else {
|
|
|
|
|
suggestedReplicas = *cached
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
v, err := r.suggestDesiredReplicas(rd, hra)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
|
suggestedReplicas = minReplicas
|
|
|
|
|
} else {
|
|
|
|
|
suggestedReplicas = *v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reserved int
|
|
|
|
|
|
|
|
|
|
for _, reservation := range hra.Spec.CapacityReservations {
|
|
|
|
|
if reservation.ExpirationTime.Time.After(now) {
|
|
|
|
|
reserved += reservation.Replicas
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newDesiredReplicas := suggestedReplicas + reserved
|
|
|
|
|
|
|
|
|
|
if newDesiredReplicas < minReplicas {
|
|
|
|
|
newDesiredReplicas = minReplicas
|
|
|
|
|
} else if hra.Spec.MaxReplicas != nil && newDesiredReplicas > *hra.Spec.MaxReplicas {
|
|
|
|
|
newDesiredReplicas = *hra.Spec.MaxReplicas
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
//
|
|
|
|
|
// Delay scaling-down for ScaleDownDelaySecondsAfterScaleUp or DefaultScaleDownDelay
|
|
|
|
|
//
|
|
|
|
|
|
2020-07-19 18:42:06 +09:00
|
|
|
var scaleDownDelay time.Duration
|
|
|
|
|
|
|
|
|
|
if hra.Spec.ScaleDownDelaySecondsAfterScaleUp != nil {
|
|
|
|
|
scaleDownDelay = time.Duration(*hra.Spec.ScaleDownDelaySecondsAfterScaleUp) * time.Second
|
|
|
|
|
} else {
|
|
|
|
|
scaleDownDelay = DefaultScaleDownDelay
|
|
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
var scaleDownDelayUntil *time.Time
|
2020-07-19 18:42:06 +09:00
|
|
|
|
|
|
|
|
if hra.Status.DesiredReplicas == nil ||
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
*hra.Status.DesiredReplicas < newDesiredReplicas ||
|
|
|
|
|
hra.Status.LastSuccessfulScaleOutTime == nil {
|
2020-07-19 18:42:06 +09:00
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
} else if hra.Status.LastSuccessfulScaleOutTime != nil {
|
|
|
|
|
t := hra.Status.LastSuccessfulScaleOutTime.Add(scaleDownDelay)
|
|
|
|
|
|
|
|
|
|
// ScaleDownDelay is not passed
|
|
|
|
|
if t.After(now) {
|
|
|
|
|
scaleDownDelayUntil = &t
|
|
|
|
|
newDesiredReplicas = *hra.Status.DesiredReplicas
|
|
|
|
|
}
|
2020-07-19 18:42:06 +09:00
|
|
|
} else {
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
newDesiredReplicas = *hra.Status.DesiredReplicas
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Logs various numbers for monitoring and debugging purpose
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
kvs := []interface{}{
|
|
|
|
|
"suggested", suggestedReplicas,
|
|
|
|
|
"reserved", reserved,
|
|
|
|
|
"min", minReplicas,
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|
|
|
|
|
|
Do not delay min/maxReplicas propagation from HRA to RD due to caching (#406)
As part of #282, I have introduced some caching mechanism to avoid excessive GitHub API calls due to the autoscaling calculation involving GitHub API calls is executed on each Webhook event.
Apparently, it was saving the wrong value in the cache- The value was one after applying `HRA.Spec.{Max,Min}Replicas` so manual changes to {Max,Min}Replicas doesn't affect RunnerDeployment.Spec.Replicas until the cache expires. This isn't what I had wanted.
This patch fixes that, by changing the value being cached to one before applying {Min,Max}Replicas.
Additionally, I've also updated logging so that you observe which number was fetched from cache, and what number was suggested by either TotalNumberOfQueuedAndInProgressWorkflowRuns or PercentageRunnersBusy, and what was the final number used as the desired-replicas(after applying {Min,Max}Replicas).
Follow-up for #282
2021-03-19 12:58:02 +09:00
|
|
|
if cached != nil {
|
|
|
|
|
kvs = append(kvs, "cached", *cached)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if scaleDownDelayUntil != nil {
|
|
|
|
|
kvs = append(kvs, "last_scale_up_time", *hra.Status.LastSuccessfulScaleOutTime)
|
|
|
|
|
kvs = append(kvs, "scale_down_delay_until", scaleDownDelayUntil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if maxReplicas := hra.Spec.MaxReplicas; maxReplicas != nil {
|
|
|
|
|
kvs = append(kvs, "max", *maxReplicas)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.V(1).Info(fmt.Sprintf("Calculated desired replicas of %d", newDesiredReplicas),
|
|
|
|
|
kvs...,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return newDesiredReplicas, suggestedReplicas, suggestedReplicasFromCache, nil
|
2020-07-19 18:42:06 +09:00
|
|
|
}
|