@@ -25,6 +25,7 @@ import (
2525 resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2626 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
2727 vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
28+ "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
2829 resourcehelpers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/resources"
2930 vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
3031)
@@ -54,7 +55,7 @@ func (*resourcesUpdatesPatchCalculator) PatchResourceTarget() PatchResourceTarge
5455func (c * resourcesUpdatesPatchCalculator ) CalculatePatches (pod * core.Pod , vpa * vpa_types.VerticalPodAutoscaler ) ([]resource_admission.PatchRecord , error ) {
5556 result := []resource_admission.PatchRecord {}
5657
57- containersResources , annotationsPerContainer , err := c .recommendationProvider .GetContainersResourcesForPod (pod , vpa )
58+ initContainersResources , containersResources , annotationsPerContainer , err := c .recommendationProvider .GetContainersResourcesForPod (pod , vpa )
5859 if err != nil {
5960 return []resource_admission.PatchRecord {}, fmt .Errorf ("failed to calculate resource patch for pod %s/%s: %v" , pod .Namespace , pod .Name , err )
6061 }
@@ -64,8 +65,14 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
6465 }
6566
6667 updatesAnnotation := []string {}
68+ for i , containerResources := range initContainersResources {
69+ newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources , model .ContainerTypeInitSidecar )
70+ result = append (result , newPatches ... )
71+ updatesAnnotation = append (updatesAnnotation , newUpdatesAnnotation )
72+ }
73+
6774 for i , containerResources := range containersResources {
68- newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources )
75+ newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources , model . ContainerTypeStandard )
6976 result = append (result , newPatches ... )
7077 updatesAnnotation = append (updatesAnnotation , newUpdatesAnnotation )
7178 }
@@ -77,33 +84,40 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
7784 return result , nil
7885}
7986
80- func getContainerPatch (pod * core.Pod , i int , annotationsPerContainer vpa_api_util.ContainerToAnnotationsMap , containerResources vpa_api_util.ContainerResources ) ([]resource_admission.PatchRecord , string ) {
87+ func getContainerPatch (pod * core.Pod , i int , annotationsPerContainer vpa_api_util.ContainerToAnnotationsMap , containerResources vpa_api_util.ContainerResources , containerType model. ContainerType ) ([]resource_admission.PatchRecord , string ) {
8188 var patches []resource_admission.PatchRecord
8289 // Add empty resources object if missing.
83- requests , limits := resourcehelpers .ContainerRequestsAndLimits (pod .Spec .Containers [i ].Name , pod )
90+ var container * core.Container
91+ if containerType == model .ContainerTypeStandard {
92+ container = & pod .Spec .Containers [i ]
93+ } else {
94+ container = & pod .Spec .InitContainers [i ]
95+ }
96+
97+ requests , limits := resourcehelpers .ContainerRequestsAndLimits (container .Name , pod )
8498 if limits == nil && requests == nil {
85- patches = append (patches , GetPatchInitializingEmptyResources (i ))
99+ patches = append (patches , GetPatchInitializingEmptyResources (i , containerType ))
86100 }
87101
88- annotations , found := annotationsPerContainer [pod . Spec . Containers [ i ] .Name ]
102+ annotations , found := annotationsPerContainer [container .Name ]
89103 if ! found {
90104 annotations = make ([]string , 0 )
91105 }
92106
93- patches , annotations = appendPatchesAndAnnotations (patches , annotations , requests , i , containerResources .Requests , "requests" , "request" )
94- patches , annotations = appendPatchesAndAnnotations (patches , annotations , limits , i , containerResources .Limits , "limits" , "limit" )
107+ patches , annotations = appendPatchesAndAnnotations (patches , annotations , requests , i , containerResources .Requests , "requests" , "request" , containerType )
108+ patches , annotations = appendPatchesAndAnnotations (patches , annotations , limits , i , containerResources .Limits , "limits" , "limit" , containerType )
95109
96- updatesAnnotation := fmt .Sprintf ("container %d: " , i ) + strings .Join (annotations , ", " )
110+ updatesAnnotation := fmt .Sprintf ("%s %d: " , containerType , i ) + strings .Join (annotations , ", " )
97111 return patches , updatesAnnotation
98112}
99113
100- func appendPatchesAndAnnotations (patches []resource_admission.PatchRecord , annotations []string , current core.ResourceList , containerIndex int , resources core.ResourceList , fieldName , resourceName string ) ([]resource_admission.PatchRecord , []string ) {
114+ func appendPatchesAndAnnotations (patches []resource_admission.PatchRecord , annotations []string , current core.ResourceList , containerIndex int , resources core.ResourceList , fieldName , resourceName string , containerType model. ContainerType ) ([]resource_admission.PatchRecord , []string ) {
101115 // Add empty object if it's missing and we're about to fill it.
102116 if current == nil && len (resources ) > 0 {
103- patches = append (patches , GetPatchInitializingEmptyResourcesSubfield (containerIndex , fieldName ))
117+ patches = append (patches , GetPatchInitializingEmptyResourcesSubfield (containerIndex , fieldName , containerType ))
104118 }
105119 for resource , request := range resources {
106- patches = append (patches , GetAddResourceRequirementValuePatch (containerIndex , fieldName , resource , request ))
120+ patches = append (patches , GetAddResourceRequirementValuePatch (containerIndex , fieldName , resource , request , containerType ))
107121 annotations = append (annotations , fmt .Sprintf ("%s %s" , resource , resourceName ))
108122 }
109123 return patches , annotations
0 commit comments