non_zero.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import "k8s.io/api/core/v1"
  15. // For each of these resources, a pod that doesn't request the resource explicitly
  16. // will be treated as having requested the amount indicated below, for the purpose
  17. // of computing priority only. This ensures that when scheduling zero-request pods, such
  18. // pods will not all be scheduled to the machine with the smallest in-use request,
  19. // and that when scheduling regular pods, such pods will not see zero-request pods as
  20. // consuming no resources whatsoever. We chose these values to be similar to the
  21. // resources that we give to cluster addon pods (#10653). But they are pretty arbitrary.
  22. // As described in #11713, we use request instead of limit to deal with resource requirements.
  23. // DefaultMilliCPURequest defines default milli cpu request number.
  24. const DefaultMilliCPURequest int64 = 100 // 0.1 core
  25. // DefaultMemoryRequest defines default memory request size.
  26. const DefaultMemoryRequest int64 = 200 * 1024 * 1024 // 200 MB
  27. // GetNonzeroRequests returns the default resource request if none is found or
  28. // what is provided on the request.
  29. func GetNonzeroRequests(requests *v1.ResourceList) (int64, int64) {
  30. var outMilliCPU, outMemory int64
  31. // Override if un-set, but not if explicitly set to zero
  32. if _, found := (*requests)[v1.ResourceCPU]; !found {
  33. outMilliCPU = DefaultMilliCPURequest
  34. } else {
  35. outMilliCPU = requests.Cpu().MilliValue()
  36. }
  37. // Override if un-set, but not if explicitly set to zero
  38. if _, found := (*requests)[v1.ResourceMemory]; !found {
  39. outMemory = DefaultMemoryRequest
  40. } else {
  41. outMemory = requests.Memory().Value()
  42. }
  43. return outMilliCPU, outMemory
  44. }