kuberuntime_container_linux.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "k8s.io/api/core/v1"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. kubefeatures "k8s.io/kubernetes/pkg/features"
  21. "k8s.io/kubernetes/pkg/kubelet/qos"
  22. )
  23. // applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig.
  24. func (m *kubeGenericRuntimeManager) applyPlatformSpecificContainerConfig(config *runtimeapi.ContainerConfig, container *v1.Container, pod *v1.Pod, uid *int64, username string) error {
  25. config.Linux = m.generateLinuxContainerConfig(container, pod, uid, username)
  26. return nil
  27. }
  28. // generateLinuxContainerConfig generates linux container config for kubelet runtime v1.
  29. func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.Container, pod *v1.Pod, uid *int64, username string) *runtimeapi.LinuxContainerConfig {
  30. lc := &runtimeapi.LinuxContainerConfig{
  31. Resources: &runtimeapi.LinuxContainerResources{},
  32. SecurityContext: m.determineEffectiveSecurityContext(pod, container, uid, username),
  33. }
  34. // set linux container resources
  35. var cpuShares int64
  36. cpuRequest := container.Resources.Requests.Cpu()
  37. cpuLimit := container.Resources.Limits.Cpu()
  38. memoryLimit := container.Resources.Limits.Memory().Value()
  39. oomScoreAdj := int64(qos.GetContainerOOMScoreAdjust(pod, container,
  40. int64(m.machineInfo.MemoryCapacity)))
  41. // If request is not specified, but limit is, we want request to default to limit.
  42. // API server does this for new containers, but we repeat this logic in Kubelet
  43. // for containers running on existing Kubernetes clusters.
  44. if cpuRequest.IsZero() && !cpuLimit.IsZero() {
  45. cpuShares = milliCPUToShares(cpuLimit.MilliValue())
  46. } else {
  47. // if cpuRequest.Amount is nil, then milliCPUToShares will return the minimal number
  48. // of CPU shares.
  49. cpuShares = milliCPUToShares(cpuRequest.MilliValue())
  50. }
  51. lc.Resources.CpuShares = cpuShares
  52. if memoryLimit != 0 {
  53. lc.Resources.MemoryLimitInBytes = memoryLimit
  54. }
  55. // Set OOM score of the container based on qos policy. Processes in lower-priority pods should
  56. // be killed first if the system runs out of memory.
  57. lc.Resources.OomScoreAdj = oomScoreAdj
  58. if m.cpuCFSQuota {
  59. // if cpuLimit.Amount is nil, then the appropriate default value is returned
  60. // to allow full usage of cpu resource.
  61. cpuPeriod := int64(quotaPeriod)
  62. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUCFSQuotaPeriod) {
  63. cpuPeriod = int64(m.cpuCFSQuotaPeriod.Duration / time.Microsecond)
  64. }
  65. cpuQuota := milliCPUToQuota(cpuLimit.MilliValue(), cpuPeriod)
  66. lc.Resources.CpuQuota = cpuQuota
  67. lc.Resources.CpuPeriod = cpuPeriod
  68. }
  69. return lc
  70. }