@@ -155,54 +155,61 @@ func (a *clusterResourceOverridePlugin) Admit(attr admission.Attributes) error {
155
155
156
156
// Reuse LimitRanger logic to apply limit/req defaults from the project. Ignore validation
157
157
// errors, assume that LimitRanger will run after this plugin to validate.
158
- glog .V (5 ).Infof ("%s: initial pod limits are: %#v" , api .PluginName , pod .Spec . Containers [ 0 ]. Resources )
158
+ glog .V (5 ).Infof ("%s: initial pod limits are: %#v" , api .PluginName , pod .Spec )
159
159
if err := a .LimitRanger .Admit (attr ); err != nil {
160
160
glog .V (5 ).Infof ("%s: error from LimitRanger: %#v" , api .PluginName , err )
161
161
}
162
- glog .V (5 ).Infof ("%s: pod limits after LimitRanger are: %#v" , api .PluginName , pod .Spec .Containers [0 ].Resources )
163
- for _ , container := range pod .Spec .Containers {
164
- resources := container .Resources
165
- memLimit , memFound := resources .Limits [kapi .ResourceMemory ]
166
- if memFound && a .config .memoryRequestToLimitRatio != 0 {
167
- // memory is measured in whole bytes.
168
- // the plugin rounds down to the nearest MiB rather than bytes to improve ease of use for end-users.
169
- amount := memLimit .Value () * int64 (a .config .memoryRequestToLimitRatio * 100 ) / 100
170
- // TODO: move into resource.Quantity
171
- var mod int64
172
- switch memLimit .Format {
173
- case resource .BinarySI :
174
- mod = 1024 * 1024
175
- default :
176
- mod = 1000 * 1000
177
- }
178
- if rem := amount % mod ; rem != 0 {
179
- amount = amount - rem
180
- }
181
- q := resource .NewQuantity (int64 (amount ), memLimit .Format )
182
- if memFloor .Cmp (* q ) > 0 {
183
- q = memFloor .Copy ()
184
- }
185
- resources .Requests [kapi .ResourceMemory ] = * q
162
+ for i := range pod .Spec .InitContainers {
163
+ updateContainerResources (a .config , & pod .Spec .InitContainers [i ])
164
+ }
165
+ for i := range pod .Spec .Containers {
166
+ updateContainerResources (a .config , & pod .Spec .Containers [i ])
167
+ }
168
+ glog .V (5 ).Infof ("%s: pod limits after overrides are: %#v" , api .PluginName , pod .Spec )
169
+ return nil
170
+ }
171
+
172
+ func updateContainerResources (config * internalConfig , container * kapi.Container ) {
173
+ resources := container .Resources
174
+ memLimit , memFound := resources .Limits [kapi .ResourceMemory ]
175
+ if memFound && config .memoryRequestToLimitRatio != 0 {
176
+ // memory is measured in whole bytes.
177
+ // the plugin rounds down to the nearest MiB rather than bytes to improve ease of use for end-users.
178
+ amount := memLimit .Value () * int64 (config .memoryRequestToLimitRatio * 100 ) / 100
179
+ // TODO: move into resource.Quantity
180
+ var mod int64
181
+ switch memLimit .Format {
182
+ case resource .BinarySI :
183
+ mod = 1024 * 1024
184
+ default :
185
+ mod = 1000 * 1000
186
+ }
187
+ if rem := amount % mod ; rem != 0 {
188
+ amount = amount - rem
186
189
}
187
- if memFound && a .config .limitCPUToMemoryRatio != 0 {
188
- amount := float64 (memLimit .Value ()) * a .config .limitCPUToMemoryRatio * cpuBaseScaleFactor
189
- q := resource .NewMilliQuantity (int64 (amount ), resource .DecimalSI )
190
- if cpuFloor .Cmp (* q ) > 0 {
191
- q = cpuFloor .Copy ()
192
- }
193
- resources .Limits [kapi .ResourceCPU ] = * q
190
+ q := resource .NewQuantity (int64 (amount ), memLimit .Format )
191
+ if memFloor .Cmp (* q ) > 0 {
192
+ q = memFloor .Copy ()
194
193
}
194
+ resources .Requests [kapi .ResourceMemory ] = * q
195
+ }
196
+ if memFound && config .limitCPUToMemoryRatio != 0 {
197
+ amount := float64 (memLimit .Value ()) * config .limitCPUToMemoryRatio * cpuBaseScaleFactor
198
+ q := resource .NewMilliQuantity (int64 (amount ), resource .DecimalSI )
199
+ if cpuFloor .Cmp (* q ) > 0 {
200
+ q = cpuFloor .Copy ()
201
+ }
202
+ resources .Limits [kapi .ResourceCPU ] = * q
203
+ }
195
204
196
- cpuLimit , cpuFound := resources .Limits [kapi .ResourceCPU ]
197
- if cpuFound && a .config .cpuRequestToLimitRatio != 0 {
198
- amount := float64 (cpuLimit .MilliValue ()) * a .config .cpuRequestToLimitRatio
199
- q := resource .NewMilliQuantity (int64 (amount ), cpuLimit .Format )
200
- if cpuFloor .Cmp (* q ) > 0 {
201
- q = cpuFloor .Copy ()
202
- }
203
- resources .Requests [kapi .ResourceCPU ] = * q
205
+ cpuLimit , cpuFound := resources .Limits [kapi .ResourceCPU ]
206
+ if cpuFound && config .cpuRequestToLimitRatio != 0 {
207
+ amount := float64 (cpuLimit .MilliValue ()) * config .cpuRequestToLimitRatio
208
+ q := resource .NewMilliQuantity (int64 (amount ), cpuLimit .Format )
209
+ if cpuFloor .Cmp (* q ) > 0 {
210
+ q = cpuFloor .Copy ()
204
211
}
212
+ resources .Requests [kapi .ResourceCPU ] = * q
205
213
}
206
- glog .V (5 ).Infof ("%s: pod limits after overrides are: %#v" , api .PluginName , pod .Spec .Containers [0 ].Resources )
207
- return nil
214
+
208
215
}
0 commit comments