@@ -25,6 +25,7 @@ import (
25
25
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
26
26
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
27
27
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
28
+ "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
28
29
resourcehelpers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/resources"
29
30
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
30
31
)
@@ -54,7 +55,7 @@ func (*resourcesUpdatesPatchCalculator) PatchResourceTarget() PatchResourceTarge
54
55
func (c * resourcesUpdatesPatchCalculator ) CalculatePatches (pod * core.Pod , vpa * vpa_types.VerticalPodAutoscaler ) ([]resource_admission.PatchRecord , error ) {
55
56
result := []resource_admission.PatchRecord {}
56
57
57
- containersResources , annotationsPerContainer , err := c .recommendationProvider .GetContainersResourcesForPod (pod , vpa )
58
+ initContainersResources , containersResources , annotationsPerContainer , err := c .recommendationProvider .GetContainersResourcesForPod (pod , vpa )
58
59
if err != nil {
59
60
return []resource_admission.PatchRecord {}, fmt .Errorf ("failed to calculate resource patch for pod %s/%s: %v" , pod .Namespace , pod .Name , err )
60
61
}
@@ -64,8 +65,14 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
64
65
}
65
66
66
67
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
+
67
74
for i , containerResources := range containersResources {
68
- newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources )
75
+ newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources , model . ContainerTypeStandard )
69
76
result = append (result , newPatches ... )
70
77
updatesAnnotation = append (updatesAnnotation , newUpdatesAnnotation )
71
78
}
@@ -77,33 +84,40 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
77
84
return result , nil
78
85
}
79
86
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 ) {
81
88
var patches []resource_admission.PatchRecord
82
89
// 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 )
84
98
if limits == nil && requests == nil {
85
- patches = append (patches , GetPatchInitializingEmptyResources (i ))
99
+ patches = append (patches , GetPatchInitializingEmptyResources (i , containerType ))
86
100
}
87
101
88
- annotations , found := annotationsPerContainer [pod . Spec . Containers [ i ] .Name ]
102
+ annotations , found := annotationsPerContainer [container .Name ]
89
103
if ! found {
90
104
annotations = make ([]string , 0 )
91
105
}
92
106
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 )
95
109
96
- updatesAnnotation := fmt .Sprintf ("container %d: " , i ) + strings .Join (annotations , ", " )
110
+ updatesAnnotation := fmt .Sprintf ("%s %d: " , containerType , i ) + strings .Join (annotations , ", " )
97
111
return patches , updatesAnnotation
98
112
}
99
113
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 ) {
101
115
// Add empty object if it's missing and we're about to fill it.
102
116
if current == nil && len (resources ) > 0 {
103
- patches = append (patches , GetPatchInitializingEmptyResourcesSubfield (containerIndex , fieldName ))
117
+ patches = append (patches , GetPatchInitializingEmptyResourcesSubfield (containerIndex , fieldName , containerType ))
104
118
}
105
119
for resource , request := range resources {
106
- patches = append (patches , GetAddResourceRequirementValuePatch (containerIndex , fieldName , resource , request ))
120
+ patches = append (patches , GetAddResourceRequirementValuePatch (containerIndex , fieldName , resource , request , containerType ))
107
121
annotations = append (annotations , fmt .Sprintf ("%s %s" , resource , resourceName ))
108
122
}
109
123
return patches , annotations
0 commit comments