container_manager.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2015 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 cm
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/util/sets"
  17. // TODO: Migrate kubelet to either use its own internal objects or client library.
  18. v1 "k8s.io/api/core/v1"
  19. internalapi "k8s.io/cri-api/pkg/apis"
  20. podresourcesapi "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
  21. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  22. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
  23. "k8s.io/kubernetes/pkg/kubelet/config"
  24. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. evictionapi "k8s.io/kubernetes/pkg/kubelet/eviction/api"
  26. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  27. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  28. "k8s.io/kubernetes/pkg/kubelet/status"
  29. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  30. "fmt"
  31. "strconv"
  32. "strings"
  33. )
  34. type ActivePodsFunc func() []*v1.Pod
  35. // Manages the containers running on a machine.
  36. type ContainerManager interface {
  37. // Runs the container manager's housekeeping.
  38. // - Ensures that the Docker daemon is in a container.
  39. // - Creates the system container where all non-containerized processes run.
  40. Start(*v1.Node, ActivePodsFunc, config.SourcesReady, status.PodStatusProvider, internalapi.RuntimeService) error
  41. // SystemCgroupsLimit returns resources allocated to system cgroups in the machine.
  42. // These cgroups include the system and Kubernetes services.
  43. SystemCgroupsLimit() v1.ResourceList
  44. // GetNodeConfig returns a NodeConfig that is being used by the container manager.
  45. GetNodeConfig() NodeConfig
  46. // Status returns internal Status.
  47. Status() Status
  48. // NewPodContainerManager is a factory method which returns a podContainerManager object
  49. // Returns a noop implementation if qos cgroup hierarchy is not enabled
  50. NewPodContainerManager() PodContainerManager
  51. // GetMountedSubsystems returns the mounted cgroup subsystems on the node
  52. GetMountedSubsystems() *CgroupSubsystems
  53. // GetQOSContainersInfo returns the names of top level QoS containers
  54. GetQOSContainersInfo() QOSContainersInfo
  55. // GetNodeAllocatableReservation returns the amount of compute resources that have to be reserved from scheduling.
  56. GetNodeAllocatableReservation() v1.ResourceList
  57. // GetCapacity returns the amount of compute resources tracked by container manager available on the node.
  58. GetCapacity() v1.ResourceList
  59. // GetDevicePluginResourceCapacity returns the node capacity (amount of total device plugin resources),
  60. // node allocatable (amount of total healthy resources reported by device plugin),
  61. // and inactive device plugin resources previously registered on the node.
  62. GetDevicePluginResourceCapacity() (v1.ResourceList, v1.ResourceList, []string)
  63. // UpdateQOSCgroups performs housekeeping updates to ensure that the top
  64. // level QoS containers have their desired state in a thread-safe way
  65. UpdateQOSCgroups() error
  66. // GetResources returns RunContainerOptions with devices, mounts, and env fields populated for
  67. // extended resources required by container.
  68. GetResources(pod *v1.Pod, container *v1.Container) (*kubecontainer.RunContainerOptions, error)
  69. // UpdatePluginResources calls Allocate of device plugin handler for potential
  70. // requests for device plugin resources, and returns an error if fails.
  71. // Otherwise, it updates allocatableResource in nodeInfo if necessary,
  72. // to make sure it is at least equal to the pod's requested capacity for
  73. // any registered device plugin resource
  74. UpdatePluginResources(*schedulernodeinfo.NodeInfo, *lifecycle.PodAdmitAttributes) error
  75. InternalContainerLifecycle() InternalContainerLifecycle
  76. // GetPodCgroupRoot returns the cgroup which contains all pods.
  77. GetPodCgroupRoot() string
  78. // GetPluginRegistrationHandler returns a plugin registration handler
  79. // The pluginwatcher's Handlers allow to have a single module for handling
  80. // registration.
  81. GetPluginRegistrationHandler() cache.PluginHandler
  82. // GetDevices returns information about the devices assigned to pods and containers
  83. GetDevices(podUID, containerName string) []*podresourcesapi.ContainerDevices
  84. // ShouldResetExtendedResourceCapacity returns whether or not the extended resources should be zeroed,
  85. // due to node recreation.
  86. ShouldResetExtendedResourceCapacity() bool
  87. // GetTopologyPodAdmitHandler returns an instance of the TopologyManager for Pod Admission
  88. GetTopologyPodAdmitHandler() topologymanager.Manager
  89. // UpdateAllocatedDevices frees any Devices that are bound to terminated pods.
  90. UpdateAllocatedDevices()
  91. }
  92. type NodeConfig struct {
  93. RuntimeCgroupsName string
  94. SystemCgroupsName string
  95. KubeletCgroupsName string
  96. ContainerRuntime string
  97. CgroupsPerQOS bool
  98. CgroupRoot string
  99. CgroupDriver string
  100. KubeletRootDir string
  101. ProtectKernelDefaults bool
  102. NodeAllocatableConfig
  103. QOSReserved map[v1.ResourceName]int64
  104. ExperimentalCPUManagerPolicy string
  105. ExperimentalCPUManagerReconcilePeriod time.Duration
  106. ExperimentalPodPidsLimit int64
  107. EnforceCPULimits bool
  108. CPUCFSQuotaPeriod time.Duration
  109. ExperimentalTopologyManagerPolicy string
  110. }
  111. type NodeAllocatableConfig struct {
  112. KubeReservedCgroupName string
  113. SystemReservedCgroupName string
  114. ReservedSystemCPUs cpuset.CPUSet
  115. EnforceNodeAllocatable sets.String
  116. KubeReserved v1.ResourceList
  117. SystemReserved v1.ResourceList
  118. HardEvictionThresholds []evictionapi.Threshold
  119. }
  120. type Status struct {
  121. // Any soft requirements that were unsatisfied.
  122. SoftRequirements error
  123. }
  124. // parsePercentage parses the percentage string to numeric value.
  125. func parsePercentage(v string) (int64, error) {
  126. if !strings.HasSuffix(v, "%") {
  127. return 0, fmt.Errorf("percentage expected, got '%s'", v)
  128. }
  129. percentage, err := strconv.ParseInt(strings.TrimRight(v, "%"), 10, 0)
  130. if err != nil {
  131. return 0, fmt.Errorf("invalid number in percentage '%s'", v)
  132. }
  133. if percentage < 0 || percentage > 100 {
  134. return 0, fmt.Errorf("percentage must be between 0 and 100")
  135. }
  136. return percentage, nil
  137. }
  138. // ParseQOSReserved parses the --qos-reserve-requests option
  139. func ParseQOSReserved(m map[string]string) (*map[v1.ResourceName]int64, error) {
  140. reservations := make(map[v1.ResourceName]int64)
  141. for k, v := range m {
  142. switch v1.ResourceName(k) {
  143. // Only memory resources are supported.
  144. case v1.ResourceMemory:
  145. q, err := parsePercentage(v)
  146. if err != nil {
  147. return nil, err
  148. }
  149. reservations[v1.ResourceName(k)] = q
  150. default:
  151. return nil, fmt.Errorf("cannot reserve %q resource", k)
  152. }
  153. }
  154. return &reservations, nil
  155. }