kuberuntime_container_linux.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. "k8s.io/kubernetes/pkg/kubelet/qos"
  26. )
  27. // applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig.
  28. func (m *kubeGenericRuntimeManager) applyPlatformSpecificContainerConfig(config *runtimeapi.ContainerConfig, container *v1.Container, pod *v1.Pod, uid *int64, username string, nsTarget *kubecontainer.ContainerID) error {
  29. config.Linux = m.generateLinuxContainerConfig(container, pod, uid, username, nsTarget)
  30. return nil
  31. }
  32. // generateLinuxContainerConfig generates linux container config for kubelet runtime v1.
  33. func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.Container, pod *v1.Pod, uid *int64, username string, nsTarget *kubecontainer.ContainerID) *runtimeapi.LinuxContainerConfig {
  34. lc := &runtimeapi.LinuxContainerConfig{
  35. Resources: &runtimeapi.LinuxContainerResources{},
  36. SecurityContext: m.determineEffectiveSecurityContext(pod, container, uid, username),
  37. }
  38. if nsTarget != nil && lc.SecurityContext.NamespaceOptions.Pid == runtimeapi.NamespaceMode_CONTAINER {
  39. lc.SecurityContext.NamespaceOptions.Pid = runtimeapi.NamespaceMode_TARGET
  40. lc.SecurityContext.NamespaceOptions.TargetId = nsTarget.ID
  41. }
  42. // set linux container resources
  43. var cpuShares int64
  44. cpuRequest := container.Resources.Requests.Cpu()
  45. cpuLimit := container.Resources.Limits.Cpu()
  46. memoryLimit := container.Resources.Limits.Memory().Value()
  47. oomScoreAdj := int64(qos.GetContainerOOMScoreAdjust(pod, container,
  48. int64(m.machineInfo.MemoryCapacity)))
  49. // If request is not specified, but limit is, we want request to default to limit.
  50. // API server does this for new containers, but we repeat this logic in Kubelet
  51. // for containers running on existing Kubernetes clusters.
  52. if cpuRequest.IsZero() && !cpuLimit.IsZero() {
  53. cpuShares = milliCPUToShares(cpuLimit.MilliValue())
  54. } else {
  55. // if cpuRequest.Amount is nil, then milliCPUToShares will return the minimal number
  56. // of CPU shares.
  57. cpuShares = milliCPUToShares(cpuRequest.MilliValue())
  58. }
  59. lc.Resources.CpuShares = cpuShares
  60. if memoryLimit != 0 {
  61. lc.Resources.MemoryLimitInBytes = memoryLimit
  62. }
  63. // Set OOM score of the container based on qos policy. Processes in lower-priority pods should
  64. // be killed first if the system runs out of memory.
  65. lc.Resources.OomScoreAdj = oomScoreAdj
  66. if m.cpuCFSQuota {
  67. // if cpuLimit.Amount is nil, then the appropriate default value is returned
  68. // to allow full usage of cpu resource.
  69. cpuPeriod := int64(quotaPeriod)
  70. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUCFSQuotaPeriod) {
  71. cpuPeriod = int64(m.cpuCFSQuotaPeriod.Duration / time.Microsecond)
  72. }
  73. cpuQuota := milliCPUToQuota(cpuLimit.MilliValue(), cpuPeriod)
  74. lc.Resources.CpuQuota = cpuQuota
  75. lc.Resources.CpuPeriod = cpuPeriod
  76. }
  77. lc.Resources.HugepageLimits = GetHugepageLimitsFromResources(container.Resources)
  78. return lc
  79. }
  80. // GetHugepageLimitsFromResources returns limits of each hugepages from resources.
  81. func GetHugepageLimitsFromResources(resources v1.ResourceRequirements) []*runtimeapi.HugepageLimit {
  82. var hugepageLimits []*runtimeapi.HugepageLimit
  83. // For each page size, limit to 0.
  84. for _, pageSize := range cgroupfs.HugePageSizes {
  85. hugepageLimits = append(hugepageLimits, &runtimeapi.HugepageLimit{
  86. PageSize: pageSize,
  87. Limit: uint64(0),
  88. })
  89. }
  90. requiredHugepageLimits := map[string]uint64{}
  91. for resourceObj, amountObj := range resources.Limits {
  92. if !v1helper.IsHugePageResourceName(resourceObj) {
  93. continue
  94. }
  95. pageSize, err := v1helper.HugePageSizeFromResourceName(resourceObj)
  96. if err != nil {
  97. klog.Warningf("Failed to get hugepage size from resource name: %v", err)
  98. continue
  99. }
  100. sizeString, err := v1helper.HugePageUnitSizeFromByteSize(pageSize.Value())
  101. if err != nil {
  102. klog.Warningf("pageSize is invalid: %v", err)
  103. continue
  104. }
  105. requiredHugepageLimits[sizeString] = uint64(amountObj.Value())
  106. }
  107. for _, hugepageLimit := range hugepageLimits {
  108. if limit, exists := requiredHugepageLimits[hugepageLimit.PageSize]; exists {
  109. hugepageLimit.Limit = limit
  110. }
  111. }
  112. return hugepageLimits
  113. }