container_manager_linux.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // +build linux
  2. /*
  3. Copyright 2015 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 cm
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "sync"
  24. "time"
  25. "github.com/opencontainers/runc/libcontainer/cgroups"
  26. "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  27. "github.com/opencontainers/runc/libcontainer/configs"
  28. "k8s.io/klog"
  29. utilio "k8s.io/utils/io"
  30. "k8s.io/utils/mount"
  31. utilpath "k8s.io/utils/path"
  32. v1 "k8s.io/api/core/v1"
  33. "k8s.io/apimachinery/pkg/api/resource"
  34. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  35. "k8s.io/apimachinery/pkg/util/sets"
  36. utilversion "k8s.io/apimachinery/pkg/util/version"
  37. "k8s.io/apimachinery/pkg/util/wait"
  38. utilfeature "k8s.io/apiserver/pkg/util/feature"
  39. "k8s.io/client-go/tools/record"
  40. internalapi "k8s.io/cri-api/pkg/apis"
  41. kubefeatures "k8s.io/kubernetes/pkg/features"
  42. podresourcesapi "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
  43. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  44. "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager"
  45. "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/containermap"
  46. cputopology "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
  47. "k8s.io/kubernetes/pkg/kubelet/cm/devicemanager"
  48. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
  49. cmutil "k8s.io/kubernetes/pkg/kubelet/cm/util"
  50. "k8s.io/kubernetes/pkg/kubelet/config"
  51. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  52. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  53. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  54. "k8s.io/kubernetes/pkg/kubelet/qos"
  55. "k8s.io/kubernetes/pkg/kubelet/stats/pidlimit"
  56. "k8s.io/kubernetes/pkg/kubelet/status"
  57. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  58. "k8s.io/kubernetes/pkg/util/oom"
  59. "k8s.io/kubernetes/pkg/util/procfs"
  60. utilsysctl "k8s.io/kubernetes/pkg/util/sysctl"
  61. )
  62. const (
  63. dockerProcessName = "dockerd"
  64. dockerPidFile = "/var/run/docker.pid"
  65. containerdProcessName = "docker-containerd"
  66. containerdPidFile = "/run/docker/libcontainerd/docker-containerd.pid"
  67. maxPidFileLength = 1 << 10 // 1KB
  68. )
  69. var (
  70. // The docker version in which containerd was introduced.
  71. containerdAPIVersion = utilversion.MustParseGeneric("1.23")
  72. )
  73. // A non-user container tracked by the Kubelet.
  74. type systemContainer struct {
  75. // Absolute name of the container.
  76. name string
  77. // CPU limit in millicores.
  78. cpuMillicores int64
  79. // Function that ensures the state of the container.
  80. // m is the cgroup manager for the specified container.
  81. ensureStateFunc func(m *fs.Manager) error
  82. // Manager for the cgroups of the external container.
  83. manager *fs.Manager
  84. }
  85. func newSystemCgroups(containerName string) *systemContainer {
  86. return &systemContainer{
  87. name: containerName,
  88. manager: createManager(containerName),
  89. }
  90. }
  91. type containerManagerImpl struct {
  92. sync.RWMutex
  93. cadvisorInterface cadvisor.Interface
  94. mountUtil mount.Interface
  95. NodeConfig
  96. status Status
  97. // External containers being managed.
  98. systemContainers []*systemContainer
  99. // Tasks that are run periodically
  100. periodicTasks []func()
  101. // Holds all the mounted cgroup subsystems
  102. subsystems *CgroupSubsystems
  103. nodeInfo *v1.Node
  104. // Interface for cgroup management
  105. cgroupManager CgroupManager
  106. // Capacity of this node.
  107. capacity v1.ResourceList
  108. // Capacity of this node, including internal resources.
  109. internalCapacity v1.ResourceList
  110. // Absolute cgroupfs path to a cgroup that Kubelet needs to place all pods under.
  111. // This path include a top level container for enforcing Node Allocatable.
  112. cgroupRoot CgroupName
  113. // Event recorder interface.
  114. recorder record.EventRecorder
  115. // Interface for QoS cgroup management
  116. qosContainerManager QOSContainerManager
  117. // Interface for exporting and allocating devices reported by device plugins.
  118. deviceManager devicemanager.Manager
  119. // Interface for CPU affinity management.
  120. cpuManager cpumanager.Manager
  121. // Interface for Topology resource co-ordination
  122. topologyManager topologymanager.Manager
  123. }
  124. type features struct {
  125. cpuHardcapping bool
  126. }
  127. var _ ContainerManager = &containerManagerImpl{}
  128. // checks if the required cgroups subsystems are mounted.
  129. // As of now, only 'cpu' and 'memory' are required.
  130. // cpu quota is a soft requirement.
  131. func validateSystemRequirements(mountUtil mount.Interface) (features, error) {
  132. const (
  133. cgroupMountType = "cgroup"
  134. localErr = "system validation failed"
  135. )
  136. var (
  137. cpuMountPoint string
  138. f features
  139. )
  140. mountPoints, err := mountUtil.List()
  141. if err != nil {
  142. return f, fmt.Errorf("%s - %v", localErr, err)
  143. }
  144. expectedCgroups := sets.NewString("cpu", "cpuacct", "cpuset", "memory")
  145. for _, mountPoint := range mountPoints {
  146. if mountPoint.Type == cgroupMountType {
  147. for _, opt := range mountPoint.Opts {
  148. if expectedCgroups.Has(opt) {
  149. expectedCgroups.Delete(opt)
  150. }
  151. if opt == "cpu" {
  152. cpuMountPoint = mountPoint.Path
  153. }
  154. }
  155. }
  156. }
  157. if expectedCgroups.Len() > 0 {
  158. return f, fmt.Errorf("%s - Following Cgroup subsystem not mounted: %v", localErr, expectedCgroups.List())
  159. }
  160. // Check if cpu quota is available.
  161. // CPU cgroup is required and so it expected to be mounted at this point.
  162. periodExists, err := utilpath.Exists(utilpath.CheckFollowSymlink, path.Join(cpuMountPoint, "cpu.cfs_period_us"))
  163. if err != nil {
  164. klog.Errorf("failed to detect if CPU cgroup cpu.cfs_period_us is available - %v", err)
  165. }
  166. quotaExists, err := utilpath.Exists(utilpath.CheckFollowSymlink, path.Join(cpuMountPoint, "cpu.cfs_quota_us"))
  167. if err != nil {
  168. klog.Errorf("failed to detect if CPU cgroup cpu.cfs_quota_us is available - %v", err)
  169. }
  170. if quotaExists && periodExists {
  171. f.cpuHardcapping = true
  172. }
  173. return f, nil
  174. }
  175. // TODO(vmarmol): Add limits to the system containers.
  176. // Takes the absolute name of the specified containers.
  177. // Empty container name disables use of the specified container.
  178. func NewContainerManager(mountUtil mount.Interface, cadvisorInterface cadvisor.Interface, nodeConfig NodeConfig, failSwapOn bool, devicePluginEnabled bool, recorder record.EventRecorder) (ContainerManager, error) {
  179. subsystems, err := GetCgroupSubsystems()
  180. if err != nil {
  181. return nil, fmt.Errorf("failed to get mounted cgroup subsystems: %v", err)
  182. }
  183. if failSwapOn {
  184. // Check whether swap is enabled. The Kubelet does not support running with swap enabled.
  185. swapData, err := ioutil.ReadFile("/proc/swaps")
  186. if err != nil {
  187. return nil, err
  188. }
  189. swapData = bytes.TrimSpace(swapData) // extra trailing \n
  190. swapLines := strings.Split(string(swapData), "\n")
  191. // If there is more than one line (table headers) in /proc/swaps, swap is enabled and we should
  192. // error out unless --fail-swap-on is set to false.
  193. if len(swapLines) > 1 {
  194. return nil, fmt.Errorf("running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false. /proc/swaps contained: %v", swapLines)
  195. }
  196. }
  197. var internalCapacity = v1.ResourceList{}
  198. // It is safe to invoke `MachineInfo` on cAdvisor before logically initializing cAdvisor here because
  199. // machine info is computed and cached once as part of cAdvisor object creation.
  200. // But `RootFsInfo` and `ImagesFsInfo` are not available at this moment so they will be called later during manager starts
  201. machineInfo, err := cadvisorInterface.MachineInfo()
  202. if err != nil {
  203. return nil, err
  204. }
  205. // Correct NUMA information is currently missing from cadvisor's
  206. // MachineInfo struct, so we use the CPUManager's internal logic for
  207. // gathering NUMANodeInfo to pass to components that care about it.
  208. numaNodeInfo, err := cputopology.GetNUMANodeInfo()
  209. if err != nil {
  210. return nil, err
  211. }
  212. capacity := cadvisor.CapacityFromMachineInfo(machineInfo)
  213. for k, v := range capacity {
  214. internalCapacity[k] = v
  215. }
  216. pidlimits, err := pidlimit.Stats()
  217. if err == nil && pidlimits != nil && pidlimits.MaxPID != nil {
  218. internalCapacity[pidlimit.PIDs] = *resource.NewQuantity(
  219. int64(*pidlimits.MaxPID),
  220. resource.DecimalSI)
  221. }
  222. // Turn CgroupRoot from a string (in cgroupfs path format) to internal CgroupName
  223. cgroupRoot := ParseCgroupfsToCgroupName(nodeConfig.CgroupRoot)
  224. cgroupManager := NewCgroupManager(subsystems, nodeConfig.CgroupDriver)
  225. // Check if Cgroup-root actually exists on the node
  226. if nodeConfig.CgroupsPerQOS {
  227. // this does default to / when enabled, but this tests against regressions.
  228. if nodeConfig.CgroupRoot == "" {
  229. return nil, fmt.Errorf("invalid configuration: cgroups-per-qos was specified and cgroup-root was not specified. To enable the QoS cgroup hierarchy you need to specify a valid cgroup-root")
  230. }
  231. // we need to check that the cgroup root actually exists for each subsystem
  232. // of note, we always use the cgroupfs driver when performing this check since
  233. // the input is provided in that format.
  234. // this is important because we do not want any name conversion to occur.
  235. if !cgroupManager.Exists(cgroupRoot) {
  236. return nil, fmt.Errorf("invalid configuration: cgroup-root %q doesn't exist", cgroupRoot)
  237. }
  238. klog.Infof("container manager verified user specified cgroup-root exists: %v", cgroupRoot)
  239. // Include the top level cgroup for enforcing node allocatable into cgroup-root.
  240. // This way, all sub modules can avoid having to understand the concept of node allocatable.
  241. cgroupRoot = NewCgroupName(cgroupRoot, defaultNodeAllocatableCgroupName)
  242. }
  243. klog.Infof("Creating Container Manager object based on Node Config: %+v", nodeConfig)
  244. qosContainerManager, err := NewQOSContainerManager(subsystems, cgroupRoot, nodeConfig, cgroupManager)
  245. if err != nil {
  246. return nil, err
  247. }
  248. cm := &containerManagerImpl{
  249. cadvisorInterface: cadvisorInterface,
  250. mountUtil: mountUtil,
  251. NodeConfig: nodeConfig,
  252. subsystems: subsystems,
  253. cgroupManager: cgroupManager,
  254. capacity: capacity,
  255. internalCapacity: internalCapacity,
  256. cgroupRoot: cgroupRoot,
  257. recorder: recorder,
  258. qosContainerManager: qosContainerManager,
  259. }
  260. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.TopologyManager) {
  261. cm.topologyManager, err = topologymanager.NewManager(
  262. numaNodeInfo,
  263. nodeConfig.ExperimentalTopologyManagerPolicy,
  264. )
  265. if err != nil {
  266. return nil, err
  267. }
  268. klog.Infof("[topologymanager] Initializing Topology Manager with %s policy", nodeConfig.ExperimentalTopologyManagerPolicy)
  269. } else {
  270. cm.topologyManager = topologymanager.NewFakeManager()
  271. }
  272. klog.Infof("Creating device plugin manager: %t", devicePluginEnabled)
  273. if devicePluginEnabled {
  274. cm.deviceManager, err = devicemanager.NewManagerImpl(numaNodeInfo, cm.topologyManager)
  275. cm.topologyManager.AddHintProvider(cm.deviceManager)
  276. } else {
  277. cm.deviceManager, err = devicemanager.NewManagerStub()
  278. }
  279. if err != nil {
  280. return nil, err
  281. }
  282. // Initialize CPU manager
  283. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManager) {
  284. cm.cpuManager, err = cpumanager.NewManager(
  285. nodeConfig.ExperimentalCPUManagerPolicy,
  286. nodeConfig.ExperimentalCPUManagerReconcilePeriod,
  287. machineInfo,
  288. numaNodeInfo,
  289. nodeConfig.NodeAllocatableConfig.ReservedSystemCPUs,
  290. cm.GetNodeAllocatableReservation(),
  291. nodeConfig.KubeletRootDir,
  292. cm.topologyManager,
  293. )
  294. if err != nil {
  295. klog.Errorf("failed to initialize cpu manager: %v", err)
  296. return nil, err
  297. }
  298. cm.topologyManager.AddHintProvider(cm.cpuManager)
  299. }
  300. return cm, nil
  301. }
  302. // NewPodContainerManager is a factory method returns a PodContainerManager object
  303. // If qosCgroups are enabled then it returns the general pod container manager
  304. // otherwise it returns a no-op manager which essentially does nothing
  305. func (cm *containerManagerImpl) NewPodContainerManager() PodContainerManager {
  306. if cm.NodeConfig.CgroupsPerQOS {
  307. return &podContainerManagerImpl{
  308. qosContainersInfo: cm.GetQOSContainersInfo(),
  309. subsystems: cm.subsystems,
  310. cgroupManager: cm.cgroupManager,
  311. podPidsLimit: cm.ExperimentalPodPidsLimit,
  312. enforceCPULimits: cm.EnforceCPULimits,
  313. cpuCFSQuotaPeriod: uint64(cm.CPUCFSQuotaPeriod / time.Microsecond),
  314. }
  315. }
  316. return &podContainerManagerNoop{
  317. cgroupRoot: cm.cgroupRoot,
  318. }
  319. }
  320. func (cm *containerManagerImpl) InternalContainerLifecycle() InternalContainerLifecycle {
  321. return &internalContainerLifecycleImpl{cm.cpuManager, cm.topologyManager}
  322. }
  323. // Create a cgroup container manager.
  324. func createManager(containerName string) *fs.Manager {
  325. allowAllDevices := true
  326. return &fs.Manager{
  327. Cgroups: &configs.Cgroup{
  328. Parent: "/",
  329. Name: containerName,
  330. Resources: &configs.Resources{
  331. AllowAllDevices: &allowAllDevices,
  332. },
  333. },
  334. }
  335. }
  336. type KernelTunableBehavior string
  337. const (
  338. KernelTunableWarn KernelTunableBehavior = "warn"
  339. KernelTunableError KernelTunableBehavior = "error"
  340. KernelTunableModify KernelTunableBehavior = "modify"
  341. )
  342. // setupKernelTunables validates kernel tunable flags are set as expected
  343. // depending upon the specified option, it will either warn, error, or modify the kernel tunable flags
  344. func setupKernelTunables(option KernelTunableBehavior) error {
  345. desiredState := map[string]int{
  346. utilsysctl.VMOvercommitMemory: utilsysctl.VMOvercommitMemoryAlways,
  347. utilsysctl.VMPanicOnOOM: utilsysctl.VMPanicOnOOMInvokeOOMKiller,
  348. utilsysctl.KernelPanic: utilsysctl.KernelPanicRebootTimeout,
  349. utilsysctl.KernelPanicOnOops: utilsysctl.KernelPanicOnOopsAlways,
  350. utilsysctl.RootMaxKeys: utilsysctl.RootMaxKeysSetting,
  351. utilsysctl.RootMaxBytes: utilsysctl.RootMaxBytesSetting,
  352. }
  353. sysctl := utilsysctl.New()
  354. errList := []error{}
  355. for flag, expectedValue := range desiredState {
  356. val, err := sysctl.GetSysctl(flag)
  357. if err != nil {
  358. errList = append(errList, err)
  359. continue
  360. }
  361. if val == expectedValue {
  362. continue
  363. }
  364. switch option {
  365. case KernelTunableError:
  366. errList = append(errList, fmt.Errorf("invalid kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val))
  367. case KernelTunableWarn:
  368. klog.V(2).Infof("Invalid kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val)
  369. case KernelTunableModify:
  370. klog.V(2).Infof("Updating kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val)
  371. err = sysctl.SetSysctl(flag, expectedValue)
  372. if err != nil {
  373. errList = append(errList, err)
  374. }
  375. }
  376. }
  377. return utilerrors.NewAggregate(errList)
  378. }
  379. func (cm *containerManagerImpl) setupNode(activePods ActivePodsFunc) error {
  380. f, err := validateSystemRequirements(cm.mountUtil)
  381. if err != nil {
  382. return err
  383. }
  384. if !f.cpuHardcapping {
  385. cm.status.SoftRequirements = fmt.Errorf("CPU hardcapping unsupported")
  386. }
  387. b := KernelTunableModify
  388. if cm.GetNodeConfig().ProtectKernelDefaults {
  389. b = KernelTunableError
  390. }
  391. if err := setupKernelTunables(b); err != nil {
  392. return err
  393. }
  394. // Setup top level qos containers only if CgroupsPerQOS flag is specified as true
  395. if cm.NodeConfig.CgroupsPerQOS {
  396. if err := cm.createNodeAllocatableCgroups(); err != nil {
  397. return err
  398. }
  399. err = cm.qosContainerManager.Start(cm.getNodeAllocatableAbsolute, activePods)
  400. if err != nil {
  401. return fmt.Errorf("failed to initialize top level QOS containers: %v", err)
  402. }
  403. }
  404. // Enforce Node Allocatable (if required)
  405. if err := cm.enforceNodeAllocatableCgroups(); err != nil {
  406. return err
  407. }
  408. systemContainers := []*systemContainer{}
  409. if cm.ContainerRuntime == "docker" {
  410. // With the docker-CRI integration, dockershim manages the cgroups
  411. // and oom score for the docker processes.
  412. // Check the cgroup for docker periodically, so kubelet can serve stats for the docker runtime.
  413. // TODO(KEP#866): remove special processing for CRI "docker" enablement
  414. cm.periodicTasks = append(cm.periodicTasks, func() {
  415. klog.V(4).Infof("[ContainerManager]: Adding periodic tasks for docker CRI integration")
  416. cont, err := getContainerNameForProcess(dockerProcessName, dockerPidFile)
  417. if err != nil {
  418. klog.Error(err)
  419. return
  420. }
  421. klog.V(2).Infof("[ContainerManager]: Discovered runtime cgroups name: %s", cont)
  422. cm.Lock()
  423. defer cm.Unlock()
  424. cm.RuntimeCgroupsName = cont
  425. })
  426. }
  427. if cm.SystemCgroupsName != "" {
  428. if cm.SystemCgroupsName == "/" {
  429. return fmt.Errorf("system container cannot be root (\"/\")")
  430. }
  431. cont := newSystemCgroups(cm.SystemCgroupsName)
  432. cont.ensureStateFunc = func(manager *fs.Manager) error {
  433. return ensureSystemCgroups("/", manager)
  434. }
  435. systemContainers = append(systemContainers, cont)
  436. }
  437. if cm.KubeletCgroupsName != "" {
  438. cont := newSystemCgroups(cm.KubeletCgroupsName)
  439. allowAllDevices := true
  440. manager := fs.Manager{
  441. Cgroups: &configs.Cgroup{
  442. Parent: "/",
  443. Name: cm.KubeletCgroupsName,
  444. Resources: &configs.Resources{
  445. AllowAllDevices: &allowAllDevices,
  446. },
  447. },
  448. }
  449. cont.ensureStateFunc = func(_ *fs.Manager) error {
  450. return ensureProcessInContainerWithOOMScore(os.Getpid(), qos.KubeletOOMScoreAdj, &manager)
  451. }
  452. systemContainers = append(systemContainers, cont)
  453. } else {
  454. cm.periodicTasks = append(cm.periodicTasks, func() {
  455. if err := ensureProcessInContainerWithOOMScore(os.Getpid(), qos.KubeletOOMScoreAdj, nil); err != nil {
  456. klog.Error(err)
  457. return
  458. }
  459. cont, err := getContainer(os.Getpid())
  460. if err != nil {
  461. klog.Errorf("failed to find cgroups of kubelet - %v", err)
  462. return
  463. }
  464. cm.Lock()
  465. defer cm.Unlock()
  466. cm.KubeletCgroupsName = cont
  467. })
  468. }
  469. cm.systemContainers = systemContainers
  470. return nil
  471. }
  472. func getContainerNameForProcess(name, pidFile string) (string, error) {
  473. pids, err := getPidsForProcess(name, pidFile)
  474. if err != nil {
  475. return "", fmt.Errorf("failed to detect process id for %q - %v", name, err)
  476. }
  477. if len(pids) == 0 {
  478. return "", nil
  479. }
  480. cont, err := getContainer(pids[0])
  481. if err != nil {
  482. return "", err
  483. }
  484. return cont, nil
  485. }
  486. func (cm *containerManagerImpl) GetNodeConfig() NodeConfig {
  487. cm.RLock()
  488. defer cm.RUnlock()
  489. return cm.NodeConfig
  490. }
  491. // GetPodCgroupRoot returns the literal cgroupfs value for the cgroup containing all pods.
  492. func (cm *containerManagerImpl) GetPodCgroupRoot() string {
  493. return cm.cgroupManager.Name(cm.cgroupRoot)
  494. }
  495. func (cm *containerManagerImpl) GetMountedSubsystems() *CgroupSubsystems {
  496. return cm.subsystems
  497. }
  498. func (cm *containerManagerImpl) GetQOSContainersInfo() QOSContainersInfo {
  499. return cm.qosContainerManager.GetQOSContainersInfo()
  500. }
  501. func (cm *containerManagerImpl) UpdateQOSCgroups() error {
  502. return cm.qosContainerManager.UpdateCgroups()
  503. }
  504. func (cm *containerManagerImpl) Status() Status {
  505. cm.RLock()
  506. defer cm.RUnlock()
  507. return cm.status
  508. }
  509. func (cm *containerManagerImpl) Start(node *v1.Node,
  510. activePods ActivePodsFunc,
  511. sourcesReady config.SourcesReady,
  512. podStatusProvider status.PodStatusProvider,
  513. runtimeService internalapi.RuntimeService) error {
  514. // Initialize CPU manager
  515. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManager) {
  516. containerMap, err := buildContainerMapFromRuntime(runtimeService)
  517. if err != nil {
  518. return fmt.Errorf("failed to build map of initial containers from runtime: %v", err)
  519. }
  520. err = cm.cpuManager.Start(cpumanager.ActivePodsFunc(activePods), sourcesReady, podStatusProvider, runtimeService, containerMap)
  521. if err != nil {
  522. return fmt.Errorf("start cpu manager error: %v", err)
  523. }
  524. }
  525. // cache the node Info including resource capacity and
  526. // allocatable of the node
  527. cm.nodeInfo = node
  528. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.LocalStorageCapacityIsolation) {
  529. rootfs, err := cm.cadvisorInterface.RootFsInfo()
  530. if err != nil {
  531. return fmt.Errorf("failed to get rootfs info: %v", err)
  532. }
  533. for rName, rCap := range cadvisor.EphemeralStorageCapacityFromFsInfo(rootfs) {
  534. cm.capacity[rName] = rCap
  535. }
  536. }
  537. // Ensure that node allocatable configuration is valid.
  538. if err := cm.validateNodeAllocatable(); err != nil {
  539. return err
  540. }
  541. // Setup the node
  542. if err := cm.setupNode(activePods); err != nil {
  543. return err
  544. }
  545. // Don't run a background thread if there are no ensureStateFuncs.
  546. hasEnsureStateFuncs := false
  547. for _, cont := range cm.systemContainers {
  548. if cont.ensureStateFunc != nil {
  549. hasEnsureStateFuncs = true
  550. break
  551. }
  552. }
  553. if hasEnsureStateFuncs {
  554. // Run ensure state functions every minute.
  555. go wait.Until(func() {
  556. for _, cont := range cm.systemContainers {
  557. if cont.ensureStateFunc != nil {
  558. if err := cont.ensureStateFunc(cont.manager); err != nil {
  559. klog.Warningf("[ContainerManager] Failed to ensure state of %q: %v", cont.name, err)
  560. }
  561. }
  562. }
  563. }, time.Minute, wait.NeverStop)
  564. }
  565. if len(cm.periodicTasks) > 0 {
  566. go wait.Until(func() {
  567. for _, task := range cm.periodicTasks {
  568. if task != nil {
  569. task()
  570. }
  571. }
  572. }, 5*time.Minute, wait.NeverStop)
  573. }
  574. // Starts device manager.
  575. if err := cm.deviceManager.Start(devicemanager.ActivePodsFunc(activePods), sourcesReady); err != nil {
  576. return err
  577. }
  578. return nil
  579. }
  580. func (cm *containerManagerImpl) GetPluginRegistrationHandler() cache.PluginHandler {
  581. return cm.deviceManager.GetWatcherHandler()
  582. }
  583. // TODO: move the GetResources logic to PodContainerManager.
  584. func (cm *containerManagerImpl) GetResources(pod *v1.Pod, container *v1.Container) (*kubecontainer.RunContainerOptions, error) {
  585. opts := &kubecontainer.RunContainerOptions{}
  586. // Allocate should already be called during predicateAdmitHandler.Admit(),
  587. // just try to fetch device runtime information from cached state here
  588. devOpts, err := cm.deviceManager.GetDeviceRunContainerOptions(pod, container)
  589. if err != nil {
  590. return nil, err
  591. } else if devOpts == nil {
  592. return opts, nil
  593. }
  594. opts.Devices = append(opts.Devices, devOpts.Devices...)
  595. opts.Mounts = append(opts.Mounts, devOpts.Mounts...)
  596. opts.Envs = append(opts.Envs, devOpts.Envs...)
  597. opts.Annotations = append(opts.Annotations, devOpts.Annotations...)
  598. return opts, nil
  599. }
  600. func (cm *containerManagerImpl) UpdatePluginResources(node *schedulernodeinfo.NodeInfo, attrs *lifecycle.PodAdmitAttributes) error {
  601. return cm.deviceManager.Allocate(node, attrs)
  602. }
  603. func (cm *containerManagerImpl) GetTopologyPodAdmitHandler() topologymanager.Manager {
  604. return cm.topologyManager
  605. }
  606. func (cm *containerManagerImpl) SystemCgroupsLimit() v1.ResourceList {
  607. cpuLimit := int64(0)
  608. // Sum up resources of all external containers.
  609. for _, cont := range cm.systemContainers {
  610. cpuLimit += cont.cpuMillicores
  611. }
  612. return v1.ResourceList{
  613. v1.ResourceCPU: *resource.NewMilliQuantity(
  614. cpuLimit,
  615. resource.DecimalSI),
  616. }
  617. }
  618. func buildContainerMapFromRuntime(runtimeService internalapi.RuntimeService) (containermap.ContainerMap, error) {
  619. podSandboxMap := make(map[string]string)
  620. podSandboxList, _ := runtimeService.ListPodSandbox(nil)
  621. for _, p := range podSandboxList {
  622. podSandboxMap[p.Id] = p.Metadata.Uid
  623. }
  624. containerMap := containermap.NewContainerMap()
  625. containerList, _ := runtimeService.ListContainers(nil)
  626. for _, c := range containerList {
  627. if _, exists := podSandboxMap[c.PodSandboxId]; !exists {
  628. return nil, fmt.Errorf("no PodsandBox found with Id '%s'", c.PodSandboxId)
  629. }
  630. containerMap.Add(podSandboxMap[c.PodSandboxId], c.Metadata.Name, c.Id)
  631. }
  632. return containerMap, nil
  633. }
  634. func isProcessRunningInHost(pid int) (bool, error) {
  635. // Get init pid namespace.
  636. initPidNs, err := os.Readlink("/proc/1/ns/pid")
  637. if err != nil {
  638. return false, fmt.Errorf("failed to find pid namespace of init process")
  639. }
  640. klog.V(10).Infof("init pid ns is %q", initPidNs)
  641. processPidNs, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/pid", pid))
  642. if err != nil {
  643. return false, fmt.Errorf("failed to find pid namespace of process %q", pid)
  644. }
  645. klog.V(10).Infof("Pid %d pid ns is %q", pid, processPidNs)
  646. return initPidNs == processPidNs, nil
  647. }
  648. func getPidFromPidFile(pidFile string) (int, error) {
  649. file, err := os.Open(pidFile)
  650. if err != nil {
  651. return 0, fmt.Errorf("error opening pid file %s: %v", pidFile, err)
  652. }
  653. defer file.Close()
  654. data, err := utilio.ReadAtMost(file, maxPidFileLength)
  655. if err != nil {
  656. return 0, fmt.Errorf("error reading pid file %s: %v", pidFile, err)
  657. }
  658. pid, err := strconv.Atoi(string(data))
  659. if err != nil {
  660. return 0, fmt.Errorf("error parsing %s as a number: %v", string(data), err)
  661. }
  662. return pid, nil
  663. }
  664. func getPidsForProcess(name, pidFile string) ([]int, error) {
  665. if len(pidFile) == 0 {
  666. return procfs.PidOf(name)
  667. }
  668. pid, err := getPidFromPidFile(pidFile)
  669. if err == nil {
  670. return []int{pid}, nil
  671. }
  672. // Try to lookup pid by process name
  673. pids, err2 := procfs.PidOf(name)
  674. if err2 == nil {
  675. return pids, nil
  676. }
  677. // Return error from getPidFromPidFile since that should have worked
  678. // and is the real source of the problem.
  679. klog.V(4).Infof("unable to get pid from %s: %v", pidFile, err)
  680. return []int{}, err
  681. }
  682. // Ensures that the Docker daemon is in the desired container.
  683. // Temporarily export the function to be used by dockershim.
  684. // TODO(yujuhong): Move this function to dockershim once kubelet migrates to
  685. // dockershim as the default.
  686. func EnsureDockerInContainer(dockerAPIVersion *utilversion.Version, oomScoreAdj int, manager *fs.Manager) error {
  687. type process struct{ name, file string }
  688. dockerProcs := []process{{dockerProcessName, dockerPidFile}}
  689. if dockerAPIVersion.AtLeast(containerdAPIVersion) {
  690. dockerProcs = append(dockerProcs, process{containerdProcessName, containerdPidFile})
  691. }
  692. var errs []error
  693. for _, proc := range dockerProcs {
  694. pids, err := getPidsForProcess(proc.name, proc.file)
  695. if err != nil {
  696. errs = append(errs, fmt.Errorf("failed to get pids for %q: %v", proc.name, err))
  697. continue
  698. }
  699. // Move if the pid is not already in the desired container.
  700. for _, pid := range pids {
  701. if err := ensureProcessInContainerWithOOMScore(pid, oomScoreAdj, manager); err != nil {
  702. errs = append(errs, fmt.Errorf("errors moving %q pid: %v", proc.name, err))
  703. }
  704. }
  705. }
  706. return utilerrors.NewAggregate(errs)
  707. }
  708. func ensureProcessInContainerWithOOMScore(pid int, oomScoreAdj int, manager *fs.Manager) error {
  709. if runningInHost, err := isProcessRunningInHost(pid); err != nil {
  710. // Err on the side of caution. Avoid moving the docker daemon unless we are able to identify its context.
  711. return err
  712. } else if !runningInHost {
  713. // Process is running inside a container. Don't touch that.
  714. klog.V(2).Infof("pid %d is not running in the host namespaces", pid)
  715. return nil
  716. }
  717. var errs []error
  718. if manager != nil {
  719. cont, err := getContainer(pid)
  720. if err != nil {
  721. errs = append(errs, fmt.Errorf("failed to find container of PID %d: %v", pid, err))
  722. }
  723. if cont != manager.Cgroups.Name {
  724. err = manager.Apply(pid)
  725. if err != nil {
  726. errs = append(errs, fmt.Errorf("failed to move PID %d (in %q) to %q: %v", pid, cont, manager.Cgroups.Name, err))
  727. }
  728. }
  729. }
  730. // Also apply oom-score-adj to processes
  731. oomAdjuster := oom.NewOOMAdjuster()
  732. klog.V(5).Infof("attempting to apply oom_score_adj of %d to pid %d", oomScoreAdj, pid)
  733. if err := oomAdjuster.ApplyOOMScoreAdj(pid, oomScoreAdj); err != nil {
  734. klog.V(3).Infof("Failed to apply oom_score_adj %d for pid %d: %v", oomScoreAdj, pid, err)
  735. errs = append(errs, fmt.Errorf("failed to apply oom score %d to PID %d: %v", oomScoreAdj, pid, err))
  736. }
  737. return utilerrors.NewAggregate(errs)
  738. }
  739. // getContainer returns the cgroup associated with the specified pid.
  740. // It enforces a unified hierarchy for memory and cpu cgroups.
  741. // On systemd environments, it uses the name=systemd cgroup for the specified pid.
  742. func getContainer(pid int) (string, error) {
  743. cgs, err := cgroups.ParseCgroupFile(fmt.Sprintf("/proc/%d/cgroup", pid))
  744. if err != nil {
  745. return "", err
  746. }
  747. cpu, found := cgs["cpu"]
  748. if !found {
  749. return "", cgroups.NewNotFoundError("cpu")
  750. }
  751. memory, found := cgs["memory"]
  752. if !found {
  753. return "", cgroups.NewNotFoundError("memory")
  754. }
  755. // since we use this container for accounting, we need to ensure its a unified hierarchy.
  756. if cpu != memory {
  757. return "", fmt.Errorf("cpu and memory cgroup hierarchy not unified. cpu: %s, memory: %s", cpu, memory)
  758. }
  759. // on systemd, every pid is in a unified cgroup hierarchy (name=systemd as seen in systemd-cgls)
  760. // cpu and memory accounting is off by default, users may choose to enable it per unit or globally.
  761. // users could enable CPU and memory accounting globally via /etc/systemd/system.conf (DefaultCPUAccounting=true DefaultMemoryAccounting=true).
  762. // users could also enable CPU and memory accounting per unit via CPUAccounting=true and MemoryAccounting=true
  763. // we only warn if accounting is not enabled for CPU or memory so as to not break local development flows where kubelet is launched in a terminal.
  764. // for example, the cgroup for the user session will be something like /user.slice/user-X.slice/session-X.scope, but the cpu and memory
  765. // cgroup will be the closest ancestor where accounting is performed (most likely /) on systems that launch docker containers.
  766. // as a result, on those systems, you will not get cpu or memory accounting statistics for kubelet.
  767. // in addition, you would not get memory or cpu accounting for the runtime unless accounting was enabled on its unit (or globally).
  768. if systemd, found := cgs["name=systemd"]; found {
  769. if systemd != cpu {
  770. klog.Warningf("CPUAccounting not enabled for pid: %d", pid)
  771. }
  772. if systemd != memory {
  773. klog.Warningf("MemoryAccounting not enabled for pid: %d", pid)
  774. }
  775. return systemd, nil
  776. }
  777. return cpu, nil
  778. }
  779. // Ensures the system container is created and all non-kernel threads and process 1
  780. // without a container are moved to it.
  781. //
  782. // The reason of leaving kernel threads at root cgroup is that we don't want to tie the
  783. // execution of these threads with to-be defined /system quota and create priority inversions.
  784. //
  785. func ensureSystemCgroups(rootCgroupPath string, manager *fs.Manager) error {
  786. // Move non-kernel PIDs to the system container.
  787. // Only keep errors on latest attempt.
  788. var finalErr error
  789. for i := 0; i <= 10; i++ {
  790. allPids, err := cmutil.GetPids(rootCgroupPath)
  791. if err != nil {
  792. finalErr = fmt.Errorf("failed to list PIDs for root: %v", err)
  793. continue
  794. }
  795. // Remove kernel pids and other protected PIDs (pid 1, PIDs already in system & kubelet containers)
  796. pids := make([]int, 0, len(allPids))
  797. for _, pid := range allPids {
  798. if pid == 1 || isKernelPid(pid) {
  799. continue
  800. }
  801. pids = append(pids, pid)
  802. }
  803. klog.Infof("Found %d PIDs in root, %d of them are not to be moved", len(allPids), len(allPids)-len(pids))
  804. // Check if we have moved all the non-kernel PIDs.
  805. if len(pids) == 0 {
  806. return nil
  807. }
  808. klog.Infof("Moving non-kernel processes: %v", pids)
  809. for _, pid := range pids {
  810. err := manager.Apply(pid)
  811. if err != nil {
  812. finalErr = fmt.Errorf("failed to move PID %d into the system container %q: %v", pid, manager.Cgroups.Name, err)
  813. }
  814. }
  815. }
  816. return finalErr
  817. }
  818. // Determines whether the specified PID is a kernel PID.
  819. func isKernelPid(pid int) bool {
  820. // Kernel threads have no associated executable.
  821. _, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
  822. return err != nil && os.IsNotExist(err)
  823. }
  824. func (cm *containerManagerImpl) GetCapacity() v1.ResourceList {
  825. return cm.capacity
  826. }
  827. func (cm *containerManagerImpl) GetDevicePluginResourceCapacity() (v1.ResourceList, v1.ResourceList, []string) {
  828. return cm.deviceManager.GetCapacity()
  829. }
  830. func (cm *containerManagerImpl) GetDevices(podUID, containerName string) []*podresourcesapi.ContainerDevices {
  831. return cm.deviceManager.GetDevices(podUID, containerName)
  832. }
  833. func (cm *containerManagerImpl) ShouldResetExtendedResourceCapacity() bool {
  834. return cm.deviceManager.ShouldResetExtendedResourceCapacity()
  835. }
  836. func (cm *containerManagerImpl) UpdateAllocatedDevices() {
  837. cm.deviceManager.UpdateAllocatedDevices()
  838. }