@@ -89,6 +89,44 @@ func addResourceLimitPatch(index int, res, amount string) resource_admission.Pat
8989 }
9090}
9191
92+ func addInitResourcesPatch (idx int ) resource_admission.PatchRecord {
93+ return resource_admission.PatchRecord {
94+ Op : "add" ,
95+ Path : fmt .Sprintf ("/spec/initContainers/%d/resources" , idx ),
96+ Value : core.ResourceRequirements {},
97+ }
98+ }
99+
100+ func addInitRequestsPatch (idx int ) resource_admission.PatchRecord {
101+ return resource_admission.PatchRecord {
102+ Op : "add" ,
103+ Path : fmt .Sprintf ("/spec/initContainers/%d/resources/requests" , idx ),
104+ Value : core.ResourceList {},
105+ }
106+ }
107+
108+ func addInitResourceRequestPatch (index int , res , amount string ) resource_admission.PatchRecord {
109+ return resource_admission.PatchRecord {
110+ Op : "add" ,
111+ Path : fmt .Sprintf ("/spec/initContainers/%d/resources/requests/%s" , index , res ),
112+ Value : resource .MustParse (amount ),
113+ }
114+ }
115+
116+ func addInitAnnotationRequest (updateResources [][]string , kind string ) resource_admission.PatchRecord {
117+ requests := make ([]string , 0 )
118+ for idx , podResources := range updateResources {
119+ podRequests := make ([]string , 0 )
120+ for _ , resource := range podResources {
121+ podRequests = append (podRequests , resource + " " + kind )
122+ }
123+ requests = append (requests , fmt .Sprintf ("init-sidecar %d: %s" , idx , strings .Join (podRequests , ", " )))
124+ }
125+
126+ vpaUpdates := fmt .Sprintf ("Pod resources updated by name: %s" , strings .Join (requests , "; " ))
127+ return GetAddAnnotationPatch (ResourceUpdatesAnnotation , vpaUpdates )
128+ }
129+
92130func addAnnotationRequest (updateResources [][]string , kind string ) resource_admission.PatchRecord {
93131 requests := make ([]string , 0 )
94132 for idx , podResources := range updateResources {
@@ -138,6 +176,29 @@ func TestCalculatePatches_ResourceUpdates(t *testing.T) {
138176 addAnnotationRequest ([][]string {{cpu }}, request ),
139177 },
140178 },
179+ {
180+ name : "new init cpu recommendation" ,
181+ pod : & core.Pod {
182+ Spec : core.PodSpec {
183+ InitContainers : []core.Container {{}},
184+ },
185+ },
186+ namespace : "default" ,
187+ initResources : []vpa_api_util.ContainerResources {
188+ {
189+ Requests : core.ResourceList {
190+ cpu : resource .MustParse ("1" ),
191+ },
192+ },
193+ },
194+ recommendAnnotations : vpa_api_util.ContainerToAnnotationsMap {},
195+ expectPatches : []resource_admission.PatchRecord {
196+ addInitResourcesPatch (0 ),
197+ addInitRequestsPatch (0 ),
198+ addInitResourceRequestPatch (0 , cpu , "1" ),
199+ addInitAnnotationRequest ([][]string {{cpu }}, request ),
200+ },
201+ },
141202 {
142203 name : "replacement cpu recommendation" ,
143204 pod : & core.Pod {
@@ -294,7 +355,6 @@ func TestCalculatePatches_ResourceUpdates(t *testing.T) {
294355 }
295356 for _ , tc := range tests {
296357 t .Run (tc .name , func (t * testing.T ) {
297- // TODO @jklaw tests
298358 frp := fakeRecommendationProvider {tc .initResources , tc .recommendResources , tc .recommendAnnotations , tc .recommendError }
299359 c := NewResourceUpdatesCalculator (& frp )
300360 patches , err := c .CalculatePatches (tc .pod , test .VerticalPodAutoscaler ().WithContainer ("test" ).WithName ("name" ).Get ())
@@ -337,7 +397,6 @@ func TestGetPatches_TwoReplacementResources(t *testing.T) {
337397 },
338398 }
339399 recommendAnnotations := vpa_api_util.ContainerToAnnotationsMap {}
340- // TODO @jklaw tests
341400 frp := fakeRecommendationProvider {nil , recommendResources , recommendAnnotations , nil }
342401 c := NewResourceUpdatesCalculator (& frp )
343402 patches , err := c .CalculatePatches (pod , test .VerticalPodAutoscaler ().WithName ("name" ).WithContainer ("test" ).Get ())
0 commit comments