kuberuntime_container_linux.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // +build linux
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kuberuntime
  15. import (
  16. "time"
  17. cgroupfs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  18. v1 "k8s.io/api/core/v1"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  21. "k8s.io/klog"
  22. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  23. kubefeatures "k8s.io/kubernetes/pkg/features"
  24. "k8s.io/kubernetes/pkg/kubelet/qos"
  25. )
  26. // applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig.
  27. func (m *kubeGenericRuntimeManager) applyPlatformSpecificContainerConfig(config *runtimeapi.ContainerConfig, container *v1.Container, pod *v1.Pod, uid *int64, username string) error {
  28. config.Linux = m.generateLinuxContainerConfig(container, pod, uid, username)
  29. return nil
  30. }
  31. // generateLinuxContainerConfig generates linux container config for kubelet runtime v1.
  32. func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.Container, pod *v1.Pod, uid *int64, username string) *runtimeapi.LinuxContainerConfig {
  33. lc := &runtimeapi.LinuxContainerConfig{
  34. Resources: &runtimeapi.LinuxContainerResources{},
  35. SecurityContext: m.determineEffectiveSecurityContext(pod, container, uid, username),
  36. }
  37. // set linux container resources
  38. var cpuShares int64
  39. cpuRequest := container.Resources.Requests.Cpu()
  40. cpuLimit := container.Resources.Limits.Cpu()
  41. memoryLimit := container.Resources.Limits.Memory().Value()
  42. oomScoreAdj := int64(qos.GetContainerOOMScoreAdjust(pod, container,
  43. int64(m.machineInfo.MemoryCapacity)))
  44. // If request is not specified, but limit is, we want request to default to limit.
  45. // API server does this for new containers, but we repeat this logic in Kubelet
  46. // for containers running on existing Kubernetes clusters.
  47. if cpuRequest.IsZero() && !cpuLimit.IsZero() {
  48. cpuShares = milliCPUToShares(cpuLimit.MilliValue())
  49. } else {
  50. // if cpuRequest.Amount is nil, then milliCPUToShares will return the minimal number
  51. // of CPU shares.
  52. cpuShares = milliCPUToShares(cpuRequest.MilliValue())
  53. }
  54. lc.Resources.CpuShares = cpuShares
  55. if memoryLimit != 0 {
  56. lc.Resources.MemoryLimitInBytes = memoryLimit
  57. }
  58. // Set OOM score of the container based on qos policy. Processes in lower-priority pods should
  59. // be killed first if the system runs out of memory.
  60. lc.Resources.OomScoreAdj = oomScoreAdj
  61. if m.cpuCFSQuota {
  62. // if cpuLimit.Amount is nil, then the appropriate default value is returned
  63. // to allow full usage of cpu resource.
  64. cpuPeriod := int64(quotaPeriod)
  65. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUCFSQuotaPeriod) {
  66. cpuPeriod = int64(m.cpuCFSQuotaPeriod.Duration / time.Microsecond)
  67. }
  68. cpuQuota := milliCPUToQuota(cpuLimit.MilliValue(), cpuPeriod)
  69. lc.Resources.CpuQuota = cpuQuota
  70. lc.Resources.CpuPeriod = cpuPeriod
  71. }
  72. lc.Resources.HugepageLimits = GetHugepageLimitsFromResources(container.Resources)
  73. return lc
  74. }
  75. // GetHugepageLimitsFromResources returns limits of each hugepages from resources.
  76. func GetHugepageLimitsFromResources(resources v1.ResourceRequirements) []*runtimeapi.HugepageLimit {
  77. var hugepageLimits []*runtimeapi.HugepageLimit
  78. // For each page size, limit to 0.
  79. for _, pageSize := range cgroupfs.HugePageSizes {
  80. hugepageLimits = append(hugepageLimits, &runtimeapi.HugepageLimit{
  81. PageSize: pageSize,
  82. Limit: uint64(0),
  83. })
  84. }
  85. requiredHugepageLimits := map[string]uint64{}
  86. for resourceObj, amountObj := range resources.Limits {
  87. if !v1helper.IsHugePageResourceName(resourceObj) {
  88. continue
  89. }
  90. pageSize, err := v1helper.HugePageSizeFromResourceName(resourceObj)
  91. if err != nil {
  92. klog.Warningf("Failed to get hugepage size from resource name: %v", err)
  93. continue
  94. }
  95. sizeString, err := v1helper.HugePageUnitSizeFromByteSize(pageSize.Value())
  96. if err != nil {
  97. klog.Warningf("pageSize is invalid: %v", err)
  98. continue
  99. }
  100. requiredHugepageLimits[sizeString] = uint64(amountObj.Value())
  101. }
  102. for _, hugepageLimit := range hugepageLimits {
  103. if limit, exists := requiredHugepageLimits[hugepageLimit.PageSize]; exists {
  104. hugepageLimit.Limit = limit
  105. }
  106. }
  107. return hugepageLimits
  108. }