Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Feb 23, 2024
1 parent c0d7f0e commit a18a2c6
Show file tree
Hide file tree
Showing 83 changed files with 1,092 additions and 2,074 deletions.
54 changes: 51 additions & 3 deletions api/core/v1/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,42 @@ func (h *PodWebhook) Default(ctx context.Context, obj runtime.Object) error {
return nil
}

for i := range pod.Spec.Containers {
container := &pod.Spec.Containers[i]
newRequestsMap := map[containerNameAndResource]resource.Quantity{}
oldRequestsMap := map[containerNameAndResource]resource.Quantity{}

// Update resource requests based on the tortoise.Status.Conditions.ContainerResourceRequests
for i, container := range pod.Spec.Containers {
for k, oldReq := range container.Resources.Requests {
oldRequestsMap[containerNameAndResource{containerName: container.Name, resourceName: k}] = oldReq

newReq, ok := getRequestFromTortoise(t, container.Name, k)
if !ok {
newRequestsMap[containerNameAndResource{containerName: container.Name, resourceName: k}] = oldReq
continue
}
pod.Spec.Containers[i].Resources.Requests[k] = newReq
}
}

// Update resource limits
for i, container := range pod.Spec.Containers {
if container.Resources.Limits == nil {
container.Resources.Limits = make(v1.ResourceList)
}

for k, oldLimit := range container.Resources.Limits {
// Keeping limit proportional to request
// The container template specifies resource request for 500 milli CPU and 1 GB of RAM. The template also specifies resource limit of 2 GB RAM.
// VPA recommendation is 1000 milli CPU and 2 GB of RAM. When VPA applies the recommendation, it will also set the memory limit to 4 GB.

key := containerNameAndResource{containerName: container.Name, resourceName: k}
oldReq := oldRequestsMap[key]
oldRatio := float64(oldLimit.MilliValue()) / float64(oldReq.MilliValue())
newReq := newRequestsMap[key]
newLim := resource.NewMilliQuantity(int64(float64(newReq.MilliValue())*oldRatio), oldReq.Format)
pod.Spec.Containers[i].Resources.Limits[k] = *newLim
}

for k := range container.Resources.Requests {
if _, ok := h.resourceLimitMultiplier[string(k)]; !ok {
continue
Expand All @@ -118,9 +149,26 @@ func (h *PodWebhook) Default(ctx context.Context, obj runtime.Object) error {
if k == v1.ResourceCPU && newLimit.Cmp(h.minimumCPULimit) < 0 {
newLimit = ptr.To(h.minimumCPULimit.DeepCopy())
}
container.Resources.Limits[k] = ptr.Deref(newLimit, container.Resources.Limits[k])

pod.Spec.Containers[i].Resources.Limits[k] = ptr.Deref(newLimit, container.Resources.Limits[k])
}
}

return nil
}

type containerNameAndResource struct {
containerName string
resourceName v1.ResourceName
}

// getRequestFromTortoise returns the resource request from the tortoise.Status.Conditions.ContainerResourceRequests.
func getRequestFromTortoise(t *v1beta3.Tortoise, containerName string, resourceName v1.ResourceName) (resource.Quantity, bool) {
for _, req := range t.Status.Conditions.ContainerResourceRequests {
if req.ContainerName == containerName {
return req.Resource[resourceName], true
}
}

return resource.Quantity{}, false
}
7 changes: 5 additions & 2 deletions api/v1beta3/tortoise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ type TargetStatusVerticalPodAutoscaler struct {
type VerticalPodAutoscalerRole string

const (
VerticalPodAutoscalerRoleUpdater = "Updater"
VerticalPodAutoscalerRoleMonitor = "Monitor"
)

Expand Down Expand Up @@ -328,7 +327,10 @@ type Conditions struct {
// ContainerRecommendationFromVPA is the condition of container recommendation from VPA, which is observed last time.
// +optional
ContainerRecommendationFromVPA []ContainerRecommendationFromVPA `json:"containerRecommendationFromVPA,omitempty" protobuf:"bytes,2,opt,name=containerRecommendationFromVPA"`
// ContainerResourceRequests has the last observed resource request of each container.
// ContainerResourceRequests has the ideal resource request for each container.
// If the mode is Off, it should be the same value as the current resource request.
// If the mode is Auto, it would basically be the same value as the recommendation.
// (Tortoise sometimes doesn't immediately apply the recommendation value to the resource request for the sake of safety.)
// +optional
ContainerResourceRequests []ContainerResourceRequests `json:"containerResourceRequests,omitempty" protobuf:"bytes,3,opt,name=containerResourceRequests"`
}
Expand All @@ -346,6 +348,7 @@ const (
// TortoiseConditionTypeFailedToReconcile means tortoise failed to reconcile due to some reasons.
TortoiseConditionTypeFailedToReconcile TortoiseConditionType = "FailedToReconcile"
TortoiseConditionTypeHPATargetUtilizationUpdated TortoiseConditionType = "HPATargetUtilizationUpdated"
TortoiseConditionTypeVerticalRecommendationUpdated TortoiseConditionType = "VerticalRecommendationUpdated"
TortoiseConditionTypeScaledUpBasedOnPreferredMaxReplicas TortoiseConditionType = "ScaledUpBasedOnPreferredMaxReplicas"
)

Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/autoscaling.mercari.com_tortoises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,12 @@ spec:
type: object
type: array
containerResourceRequests:
description: ContainerResourceRequests has the last observed resource
request of each container.
description: ContainerResourceRequests has the ideal resource
request for each container. If the mode is Off, it should be
the same value as the current resource request. If the mode
is Auto, it would basically be the same value as the recommendation.
(Tortoise sometimes doesn't immediately apply the recommendation
value to the resource request for the sake of safety.)
items:
properties:
containerName:
Expand Down
2 changes: 0 additions & 2 deletions controllers/testdata/deletion-no-delete/before/tortoise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ status:
deployment: ""
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down
20 changes: 0 additions & 20 deletions controllers/testdata/deletion-no-delete/before/vpa-Updater.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions controllers/testdata/deletion-policy-all/before/tortoise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ status:
targets:
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down
20 changes: 0 additions & 20 deletions controllers/testdata/deletion-policy-all/before/vpa-Updater.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ status:
reason: HPATargetUtilizationUpdated
status: "True"
type: HPATargetUtilizationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: null
message: The recommendation is provided
status: "True"
type: VerticalRecommendationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: "2023-01-01T00:00:00Z"
status: "False"
Expand Down Expand Up @@ -133,8 +138,6 @@ status:
kind: ""
name: ""
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ status:
targets:
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ status:
reason: HPATargetUtilizationUpdated
status: "True"
type: HPATargetUtilizationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: null
message: The recommendation is provided
status: "True"
type: VerticalRecommendationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: "2023-01-01T00:00:00Z"
status: "False"
Expand Down Expand Up @@ -131,8 +136,6 @@ status:
kind: ""
name: ""
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ status:
containerName: istio-proxy
targets:
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ status:
cpu: "3"
memory: 3Gi
tortoiseConditions:
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: null
message: The recommendation is provided
status: "True"
type: VerticalRecommendationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: "2023-01-01T00:00:00Z"
status: "False"
Expand Down Expand Up @@ -127,8 +132,6 @@ status:
kind: Deployment
name: mercari-app
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ status:
name: mercari-app
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ status:
cpu: "3"
memory: 3Gi
tortoiseConditions:
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: null
message: The recommendation is provided
status: "True"
type: VerticalRecommendationUpdated
- lastTransitionTime: "2023-01-01T00:00:00Z"
lastUpdateTime: "2023-01-01T00:00:00Z"
status: "False"
Expand Down Expand Up @@ -126,8 +131,6 @@ status:
kind: Deployment
name: mercari-app
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ status:
name: mercari-app
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Expand Down

This file was deleted.

Loading

0 comments on commit a18a2c6

Please sign in to comment.