Skip to content

Commit

Permalink
calculate the recommendation based on a correct resource request (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Feb 6, 2024
1 parent 83e3194 commit e3c408a
Show file tree
Hide file tree
Showing 24 changed files with 373 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
cpu: "3"
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "3"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
cpu: "3"
memory: 3Gi
containerRecommendationFromVPA:
- containerName: app
maxRecommendation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "3"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
cpu: "3"
memory: 3Gi
containerRecommendationFromVPA:
- containerName: app
maxRecommendation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ status:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "3"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
cpu: "3"
memory: 3Gi
containerRecommendationFromVPA:
- containerName: app
maxRecommendation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 10Mi
- containerName: "istio-proxy"
resource:
cpu: "100m"
memory: "100Mi"
memory: "11Mi"
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "10"
memory: 10Gi
cpu: "6"
memory: 3Gi
- containerName: "istio-proxy"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ status:
containerResourceRequests:
- containerName: "app"
resource:
cpu: "4"
cpu: "3"
memory: 4Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ status:
- containerName: "app"
resource:
cpu: "4"
memory: 4Gi
memory: 3Gi
tortoiseConditions:
- message: "HPA target utilization is updated"
reason: HPATargetUtilizationUpdated
Expand Down
21 changes: 11 additions & 10 deletions controllers/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,17 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, err
}
currentReplicaNum := dm.Status.Replicas
acr, err := r.DeploymentService.GetResourceRequests(dm)
if err != nil {
logger.Error(err, "failed to get resource requests in deployment", "tortoise", req.NamespacedName, "deployment", klog.KObj(dm))
return ctrl.Result{}, err
}

tortoise.Status.Conditions.ContainerResourceRequests = acr

// === Finish the part depending on deployment ===
// From here, we shouldn't use `dm` anymore.
if tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeOff || tortoise.Status.Conditions.ContainerResourceRequests == nil {
// If the update mode is off, we have to update ContainerResourceRequests from the deployment directly.
// If it's not off, ContainerResourceRequests should be updated in UpdateVPAFromTortoiseRecommendation in the last reconciliation.
acr, err := r.DeploymentService.GetResourceRequests(dm)
if err != nil {
logger.Error(err, "failed to get resource requests in deployment", "tortoise", req.NamespacedName, "deployment", klog.KObj(dm))
return ctrl.Result{}, err
}
tortoise.Status.Conditions.ContainerResourceRequests = acr
}

hpa, err := r.HpaService.GetHPAOnTortoiseSpec(ctx, tortoise)
if err != nil {
Expand Down Expand Up @@ -236,7 +237,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, err
}

_, updated, err := r.VpaService.UpdateVPAFromTortoiseRecommendation(ctx, tortoise, currentReplicaNum, now)
_, tortoise, updated, err := r.VpaService.UpdateVPAFromTortoiseRecommendation(ctx, tortoise, currentReplicaNum, now)
if err != nil {
logger.Error(err, "update VPA based on the recommendation in tortoise", "tortoise", req.NamespacedName)
return ctrl.Result{}, err
Expand Down
1 change: 1 addition & 0 deletions pkg/deployment/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (c *Service) RolloutRestart(ctx context.Context, dm *v1.Deployment, tortois
return nil
}

// GetResourceRequests returns the resource requests of the containers in the deployment.
func (c *Service) GetResourceRequests(dm *v1.Deployment) ([]autoscalingv1beta3.ContainerResourceRequests, error) {
actualContainerResource := []autoscalingv1beta3.ContainerResourceRequests{}
for _, c := range dm.Spec.Template.Spec.Containers {
Expand Down
15 changes: 12 additions & 3 deletions pkg/vpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (c *Service) CreateTortoiseMonitorVPA(ctx context.Context, tortoise *autosc

// UpdateVPAFromTortoiseRecommendation updates VPA with the recommendation from Tortoise.
// In the second return value, it returns true if the Pods should be updated with new resources.
func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, tortoise *autoscalingv1beta3.Tortoise, replica int32, now time.Time) (*v1.VerticalPodAutoscaler, bool, error) {
func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, tortoise *autoscalingv1beta3.Tortoise, replica int32, now time.Time) (*v1.VerticalPodAutoscaler, *autoscalingv1beta3.Tortoise, bool, error) {
newVPA := &v1.VerticalPodAutoscaler{}
// At the end of this function, this will be true:
// - if UpdateMode is Off, this will be always false.
Expand All @@ -250,6 +250,7 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto
}
newVPA = oldVPA.DeepCopy()
newRecommendations := make([]v1.RecommendedContainerResources, 0, len(tortoise.Status.Recommendations.Vertical.ContainerResourceRecommendation))
newRequests := make([]autoscalingv1beta3.ContainerResourceRequests, 0, len(tortoise.Status.Recommendations.Vertical.ContainerResourceRecommendation))
for _, r := range tortoise.Status.Recommendations.Vertical.ContainerResourceRecommendation {
if !metricsRecorded {
// only record metrics once in every reconcile loop.
Expand All @@ -272,6 +273,10 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto
UpperBound: r.RecommendedResource,
UncappedTarget: r.RecommendedResource,
})
newRequests = append(newRequests, autoscalingv1beta3.ContainerResourceRequests{
ContainerName: r.ContainerName,
Resource: r.RecommendedResource,
})
}
metricsRecorded = true
if tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeOff {
Expand Down Expand Up @@ -316,6 +321,10 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto
}
}

// The recommendation will be applied to VPA and the deployment will be restarted with the new resources.
// Update the request recorded in the status, which will be used in the next reconcile loop.
tortoise.Status.Conditions.ContainerResourceRequests = newRequests

newVPA.Status.Conditions = []v1.VerticalPodAutoscalerCondition{
{
Type: v1.RecommendationProvided,
Expand Down Expand Up @@ -359,7 +368,7 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto
}

if err := retry.RetryOnConflict(retry.DefaultRetry, updateFn); err != nil {
return newVPA, podShouldBeUpdatedWithNewResource, fmt.Errorf("update VPA status: %w", err)
return newVPA, tortoise, podShouldBeUpdatedWithNewResource, fmt.Errorf("update VPA status: %w", err)
}

if tortoise.Spec.UpdateMode != autoscalingv1beta3.UpdateModeOff && podShouldBeUpdatedWithNewResource {
Expand All @@ -378,7 +387,7 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto
}
}

return newVPA, podShouldBeUpdatedWithNewResource, nil
return newVPA, tortoise, podShouldBeUpdatedWithNewResource, nil
}

func recommendationIncreaseAnyResource(oldVPA, newVPA *v1.VerticalPodAutoscaler) bool {
Expand Down
Loading

0 comments on commit e3c408a

Please sign in to comment.