Skip to content

Commit

Permalink
use .spec.replicas instead to take a desired replica number (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Mar 5, 2024
1 parent c875ac1 commit 09e8828
Show file tree
Hide file tree
Showing 24 changed files with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ spec:
requests:
cpu: "10"
memory: 10Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ spec:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
16 changes: 11 additions & 5 deletions controllers/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
logger.Error(err, "failed to get deployment", "tortoise", req.NamespacedName)
return ctrl.Result{}, err
}
currentReplicaNum := dm.Status.Replicas
if dm.Spec.Replicas == nil {
logger.Error(nil, "the deployment doesn't have the number of replicas and tortoise cannot calculate the recommendation", "tortoise", req.NamespacedName, "deployment", klog.KObj(dm))
return ctrl.Result{}, nil

}

currentDesiredReplicaNum := *dm.Spec.Replicas // Use the desired replica number.

if tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeOff /* When Off, ContainerResourceRequests should be reset */ ||
tortoise.Status.Conditions.ContainerResourceRequests == nil /* The first reconciliation */ {
Expand All @@ -191,7 +197,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
if tortoise.Status.TortoisePhase == autoscalingv1beta3.TortoisePhaseInitializing {
logger.Info("initializing tortoise", "tortoise", req.NamespacedName)
// need to initialize HPA and VPA.
if err := r.initializeVPAAndHPA(ctx, tortoise, currentReplicaNum, now); err != nil {
if err := r.initializeVPAAndHPA(ctx, tortoise, currentDesiredReplicaNum, now); err != nil {
return ctrl.Result{}, fmt.Errorf("initialize VPA and HPA: %w", err)
}

Expand All @@ -204,7 +210,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, fmt.Errorf("add finalizer: %w", err)
}

tortoise, err = r.HpaService.UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, currentReplicaNum, now)
tortoise, err = r.HpaService.UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, currentDesiredReplicaNum, now)
if err != nil {
logger.Error(err, "update HPA spec from Tortoise autoscaling policy", "tortoise", req.NamespacedName)
return ctrl.Result{}, err
Expand Down Expand Up @@ -243,7 +249,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_

tortoise = r.TortoiseService.UpdateContainerRecommendationFromVPA(tortoise, monitorvpa, now)

tortoise, err = r.RecommenderService.UpdateRecommendations(ctx, tortoise, hpa, currentReplicaNum, now)
tortoise, err = r.RecommenderService.UpdateRecommendations(ctx, tortoise, hpa, currentDesiredReplicaNum, now)
if err != nil {
logger.Error(err, "update recommendation in tortoise", "tortoise", req.NamespacedName)
return ctrl.Result{}, err
Expand All @@ -266,7 +272,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, err
}

tortoise, err = r.TortoiseService.UpdateResourceRequest(ctx, tortoise, currentReplicaNum, now)
tortoise, err = r.TortoiseService.UpdateResourceRequest(ctx, tortoise, currentDesiredReplicaNum, now)
if err != nil {
logger.Error(err, "update VPA based on the recommendation in tortoise", "tortoise", req.NamespacedName)
return ctrl.Result{}, err
Expand Down

0 comments on commit 09e8828

Please sign in to comment.