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"
2021-12-17 09:06:55 +09:00
"github.com/go-logr/logr"
2021-06-23 20:25:03 +09:00
kerrors "k8s.io/apimachinery/pkg/api/errors"
2020-07-19 18:42:06 +09:00
"k8s.io/apimachinery/pkg/types"
"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"
2021-06-22 17:55:06 +09:00
"github.com/actions-runner-controller/actions-runner-controller/api/v1alpha1"
"github.com/actions-runner-controller/actions-runner-controller/controllers/metrics"
2022-07-12 09:45:00 +09:00
arcgithub "github.com/actions-runner-controller/actions-runner-controller/github"
2020-07-19 18:42:06 +09:00
)
const (
DefaultScaleDownDelay = 10 * time . Minute
)
// HorizontalRunnerAutoscalerReconciler reconciles a HorizontalRunnerAutoscaler object
type HorizontalRunnerAutoscalerReconciler struct {
client . Client
2022-07-12 09:45:00 +09:00
GitHubClient * MultiGitHubClient
2022-04-20 03:09:09 +01:00
Log logr . Logger
Recorder record . EventRecorder
Scheme * runtime . Scheme
DefaultScaleDownDelay time . Duration
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
2021-06-22 17:10:09 +09:00
func ( r * HorizontalRunnerAutoscalerReconciler ) Reconcile ( ctx context . Context , req ctrl . Request ) ( ctrl . Result , error ) {
2020-07-19 18:42:06 +09:00
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 ( ) {
2022-07-12 09:45:00 +09:00
r . GitHubClient . DeinitForHRA ( & hra )
2020-07-19 18:42:06 +09:00
return ctrl . Result { } , nil
}
2021-03-19 16:14:02 +09:00
metrics . SetHorizontalRunnerAutoscalerSpec ( hra . ObjectMeta , hra . Spec )
2021-06-23 20:25:03 +09:00
kind := hra . Spec . ScaleTargetRef . Kind
switch kind {
case "" , "RunnerDeployment" :
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
}
st := r . scaleTargetFromRD ( ctx , rd )
return r . reconcile ( ctx , req , log , hra , st , func ( newDesiredReplicas int ) error {
currentDesiredReplicas := getIntOrDefault ( rd . Spec . Replicas , defaultReplicas )
2022-02-20 06:59:56 +00:00
ephemeral := rd . Spec . Template . Spec . Ephemeral == nil || * rd . Spec . Template . Spec . Ephemeral
var effectiveTime * time . Time
for _ , r := range hra . Spec . CapacityReservations {
t := r . EffectiveTime
if effectiveTime == nil || effectiveTime . Before ( t . Time ) {
effectiveTime = & t . Time
}
}
2021-06-23 20:25:03 +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
2022-02-20 06:59:56 +00:00
if ephemeral && effectiveTime != nil {
copy . Spec . EffectiveTime = & metav1 . Time { Time : * effectiveTime }
}
if err := r . Client . Patch ( ctx , copy , client . MergeFrom ( & rd ) ) ; err != nil {
return fmt . Errorf ( "patching runnerdeployment to have %d replicas: %w" , newDesiredReplicas , err )
}
} else if ephemeral && effectiveTime != nil {
copy := rd . DeepCopy ( )
copy . Spec . EffectiveTime = & metav1 . Time { Time : * effectiveTime }
2021-06-23 20:25:03 +09:00
if err := r . Client . Patch ( ctx , copy , client . MergeFrom ( & rd ) ) ; err != nil {
return fmt . Errorf ( "patching runnerdeployment to have %d replicas: %w" , newDesiredReplicas , err )
}
}
return nil
} )
case "RunnerSet" :
var rs v1alpha1 . RunnerSet
if err := r . Get ( ctx , types . NamespacedName {
Namespace : req . Namespace ,
Name : hra . Spec . ScaleTargetRef . Name ,
} , & rs ) ; err != nil {
return ctrl . Result { } , client . IgnoreNotFound ( err )
}
if ! rs . ObjectMeta . DeletionTimestamp . IsZero ( ) {
return ctrl . Result { } , nil
}
var replicas * int
if rs . Spec . Replicas != nil {
v := int ( * rs . Spec . Replicas )
replicas = & v
}
st := scaleTarget {
st : rs . Name ,
kind : "runnerset" ,
enterprise : rs . Spec . Enterprise ,
org : rs . Spec . Organization ,
repo : rs . Spec . Repository ,
replicas : replicas ,
2022-04-24 14:41:34 +09:00
labels : rs . Spec . RunnerConfig . Labels ,
2021-06-23 20:25:03 +09:00
getRunnerMap : func ( ) ( map [ string ] struct { } , error ) {
// return the list of runners in namespace. Horizontal Runner Autoscaler should only be responsible for scaling resources in its own ns.
var runnerPodList corev1 . PodList
var opts [ ] client . ListOption
opts = append ( opts , client . InNamespace ( rs . Namespace ) )
selector , err := metav1 . LabelSelectorAsSelector ( rs . Spec . Selector )
if err != nil {
return nil , err
}
opts = append ( opts , client . MatchingLabelsSelector { Selector : selector } )
r . Log . V ( 2 ) . Info ( "Finding runnerset's runner pods with selector" , "ns" , rs . Namespace )
if err := r . List (
ctx ,
& runnerPodList ,
opts ... ,
) ; err != nil {
if ! kerrors . IsNotFound ( err ) {
return nil , err
}
}
runnerMap := make ( map [ string ] struct { } )
for _ , items := range runnerPodList . Items {
runnerMap [ items . Name ] = struct { } { }
}
return runnerMap , nil
} ,
}
return r . reconcile ( ctx , req , log , hra , st , func ( newDesiredReplicas int ) error {
var replicas * int
if rs . Spec . Replicas != nil {
v := int ( * rs . Spec . Replicas )
replicas = & v
}
currentDesiredReplicas := getIntOrDefault ( replicas , defaultReplicas )
2022-02-27 12:01:01 +00:00
ephemeral := rs . Spec . Ephemeral == nil || * rs . Spec . Ephemeral
var effectiveTime * time . Time
for _ , r := range hra . Spec . CapacityReservations {
t := r . EffectiveTime
if effectiveTime == nil || effectiveTime . Before ( t . Time ) {
effectiveTime = & t . Time
}
}
2021-06-23 20:25:03 +09:00
if currentDesiredReplicas != newDesiredReplicas {
copy := rs . DeepCopy ( )
v := int32 ( newDesiredReplicas )
copy . Spec . Replicas = & v
2022-02-27 12:01:01 +00:00
if ephemeral && effectiveTime != nil {
copy . Spec . EffectiveTime = & metav1 . Time { Time : * effectiveTime }
}
if err := r . Client . Patch ( ctx , copy , client . MergeFrom ( & rs ) ) ; err != nil {
return fmt . Errorf ( "patching runnerset to have %d replicas: %w" , newDesiredReplicas , err )
}
} else if ephemeral && effectiveTime != nil {
copy := rs . DeepCopy ( )
copy . Spec . EffectiveTime = & metav1 . Time { Time : * effectiveTime }
2021-06-23 20:25:03 +09:00
if err := r . Client . Patch ( ctx , copy , client . MergeFrom ( & rs ) ) ; err != nil {
return fmt . Errorf ( "patching runnerset to have %d replicas: %w" , newDesiredReplicas , err )
}
}
2022-02-27 12:01:01 +00:00
2021-06-23 20:25:03 +09:00
return nil
} )
2020-07-19 18:42:06 +09:00
}
2021-06-23 20:25:03 +09:00
log . Info ( fmt . Sprintf ( "Unsupported scale target %s %s: kind %s is not supported. valid kinds are %s and %s" , kind , hra . Spec . ScaleTargetRef . Name , kind , "RunnerDeployment" , "RunnerSet" ) )
return ctrl . Result { } , nil
}
func ( r * HorizontalRunnerAutoscalerReconciler ) scaleTargetFromRD ( ctx context . Context , rd v1alpha1 . RunnerDeployment ) scaleTarget {
st := scaleTarget {
st : rd . Name ,
kind : "runnerdeployment" ,
enterprise : rd . Spec . Template . Spec . Enterprise ,
org : rd . Spec . Template . Spec . Organization ,
repo : rd . Spec . Template . Spec . Repository ,
replicas : rd . Spec . Replicas ,
2022-04-24 14:41:34 +09:00
labels : rd . Spec . Template . Spec . RunnerConfig . Labels ,
2021-06-23 20:25:03 +09:00
getRunnerMap : func ( ) ( map [ string ] struct { } , error ) {
// return the list of runners in namespace. Horizontal Runner Autoscaler should only be responsible for scaling resources in its own ns.
var runnerList v1alpha1 . RunnerList
var opts [ ] client . ListOption
opts = append ( opts , client . InNamespace ( rd . Namespace ) )
selector , err := metav1 . LabelSelectorAsSelector ( getSelector ( & rd ) )
if err != nil {
return nil , err
}
opts = append ( opts , client . MatchingLabelsSelector { Selector : selector } )
r . Log . V ( 2 ) . Info ( "Finding runners with selector" , "ns" , rd . Namespace )
if err := r . List (
ctx ,
& runnerList ,
opts ... ,
) ; err != nil {
if ! kerrors . IsNotFound ( err ) {
return nil , err
}
}
runnerMap := make ( map [ string ] struct { } )
for _ , items := range runnerList . Items {
runnerMap [ items . Name ] = struct { } { }
}
return runnerMap , nil
} ,
2020-07-19 18:42:06 +09:00
}
2021-06-23 20:25:03 +09:00
return st
}
type scaleTarget struct {
st , kind string
enterprise , repo , org string
replicas * int
2022-04-24 14:41:34 +09:00
labels [ ] string
2021-06-23 20:25:03 +09:00
getRunnerMap func ( ) ( map [ string ] struct { } , error )
}
func ( r * HorizontalRunnerAutoscalerReconciler ) reconcile ( ctx context . Context , req ctrl . Request , log logr . Logger , hra v1alpha1 . HorizontalRunnerAutoscaler , st scaleTarget , updatedDesiredReplicas func ( int ) error ) ( ctrl . Result , 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
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
}
2022-07-12 09:45:00 +09:00
ghc , err := r . GitHubClient . InitForHRA ( context . Background ( ) , & hra )
if err != nil {
return ctrl . Result { } , err
}
newDesiredReplicas , err := r . computeReplicasWithCache ( ghc , log , now , st , 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
}
2021-06-23 20:25:03 +09:00
if err := updatedDesiredReplicas ( newDesiredReplicas ) ; err != nil {
return ctrl . Result { } , 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
}
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
}
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
}
2022-07-12 09:45:00 +09:00
func ( r * HorizontalRunnerAutoscalerReconciler ) computeReplicasWithCache ( ghc * arcgithub . Client , log logr . Logger , now time . Time , st scaleTarget , hra v1alpha1 . HorizontalRunnerAutoscaler , minReplicas 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
2022-07-12 09:45:00 +09:00
v , err := r . suggestDesiredReplicas ( ghc , st , hra )
2022-03-08 19:05:43 +09:00
if err != nil {
return 0 , err
}
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
2022-03-08 19:05:43 +09:00
if v == nil {
suggestedReplicas = 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
} else {
2022-03-08 19:05:43 +09:00
suggestedReplicas = * v
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 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 {
2022-04-20 03:09:09 +01:00
scaleDownDelay = r . DefaultScaleDownDelay
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
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
}
2022-03-08 10:29:53 +09:00
if maxReplicas := hra . Spec . MaxReplicas ; maxReplicas != nil {
kvs = append ( kvs , "max" , * maxReplicas )
}
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 scaleDownDelayUntil != nil {
kvs = append ( kvs , "last_scale_up_time" , * hra . Status . LastSuccessfulScaleOutTime )
kvs = append ( kvs , "scale_down_delay_until" , scaleDownDelayUntil )
}
log . V ( 1 ) . Info ( fmt . Sprintf ( "Calculated desired replicas of %d" , newDesiredReplicas ) ,
kvs ... ,
)
2022-03-08 19:05:43 +09:00
return newDesiredReplicas , nil
2020-07-19 18:42:06 +09:00
}