container_manager_linux.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "strconv"
  21. "sync"
  22. "time"
  23. "github.com/blang/semver"
  24. "github.com/golang/glog"
  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/kubernetes/pkg/api"
  29. "k8s.io/kubernetes/pkg/api/resource"
  30. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  31. "k8s.io/kubernetes/pkg/kubelet/qos"
  32. "k8s.io/kubernetes/pkg/util"
  33. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  34. "k8s.io/kubernetes/pkg/util/mount"
  35. "k8s.io/kubernetes/pkg/util/oom"
  36. "k8s.io/kubernetes/pkg/util/procfs"
  37. "k8s.io/kubernetes/pkg/util/runtime"
  38. "k8s.io/kubernetes/pkg/util/sets"
  39. utilsysctl "k8s.io/kubernetes/pkg/util/sysctl"
  40. "k8s.io/kubernetes/pkg/util/wait"
  41. )
  42. const (
  43. // The percent of the machine memory capacity. The value is used to calculate
  44. // docker memory resource container's hardlimit to workaround docker memory
  45. // leakage issue. Please see kubernetes/issues/9881 for more detail.
  46. DockerMemoryLimitThresholdPercent = 70
  47. // The minimum memory limit allocated to docker container: 150Mi
  48. MinDockerMemoryLimit = 150 * 1024 * 1024
  49. dockerProcessName = "docker"
  50. dockerPidFile = "/var/run/docker.pid"
  51. containerdProcessName = "docker-containerd"
  52. containerdPidFile = "/run/docker/libcontainerd/docker-containerd.pid"
  53. )
  54. var (
  55. // The docker version in which containerd was introduced.
  56. containerdVersion = semver.MustParse("1.11.0")
  57. )
  58. // A non-user container tracked by the Kubelet.
  59. type systemContainer struct {
  60. // Absolute name of the container.
  61. name string
  62. // CPU limit in millicores.
  63. cpuMillicores int64
  64. // Function that ensures the state of the container.
  65. // m is the cgroup manager for the specified container.
  66. ensureStateFunc func(m *fs.Manager) error
  67. // Manager for the cgroups of the external container.
  68. manager *fs.Manager
  69. }
  70. func newSystemCgroups(containerName string) *systemContainer {
  71. return &systemContainer{
  72. name: containerName,
  73. manager: createManager(containerName),
  74. }
  75. }
  76. type containerManagerImpl struct {
  77. sync.RWMutex
  78. cadvisorInterface cadvisor.Interface
  79. mountUtil mount.Interface
  80. NodeConfig
  81. status Status
  82. // External containers being managed.
  83. systemContainers []*systemContainer
  84. qosContainers QOSContainersInfo
  85. periodicTasks []func()
  86. // holds all the mounted cgroup subsystems
  87. subsystems *CgroupSubsystems
  88. nodeInfo *api.Node
  89. }
  90. type features struct {
  91. cpuHardcapping bool
  92. }
  93. var _ ContainerManager = &containerManagerImpl{}
  94. // checks if the required cgroups subsystems are mounted.
  95. // As of now, only 'cpu' and 'memory' are required.
  96. // cpu quota is a soft requirement.
  97. func validateSystemRequirements(mountUtil mount.Interface) (features, error) {
  98. const (
  99. cgroupMountType = "cgroup"
  100. localErr = "system validation failed"
  101. )
  102. var (
  103. cpuMountPoint string
  104. f features
  105. )
  106. mountPoints, err := mountUtil.List()
  107. if err != nil {
  108. return f, fmt.Errorf("%s - %v", localErr, err)
  109. }
  110. expectedCgroups := sets.NewString("cpu", "cpuacct", "cpuset", "memory")
  111. for _, mountPoint := range mountPoints {
  112. if mountPoint.Type == cgroupMountType {
  113. for _, opt := range mountPoint.Opts {
  114. if expectedCgroups.Has(opt) {
  115. expectedCgroups.Delete(opt)
  116. }
  117. if opt == "cpu" {
  118. cpuMountPoint = mountPoint.Path
  119. }
  120. }
  121. }
  122. }
  123. if expectedCgroups.Len() > 0 {
  124. return f, fmt.Errorf("%s - Following Cgroup subsystem not mounted: %v", localErr, expectedCgroups.List())
  125. }
  126. // Check if cpu quota is available.
  127. // CPU cgroup is required and so it expected to be mounted at this point.
  128. periodExists, err := util.FileExists(path.Join(cpuMountPoint, "cpu.cfs_period_us"))
  129. if err != nil {
  130. glog.Errorf("failed to detect if CPU cgroup cpu.cfs_period_us is available - %v", err)
  131. }
  132. quotaExists, err := util.FileExists(path.Join(cpuMountPoint, "cpu.cfs_quota_us"))
  133. if err != nil {
  134. glog.Errorf("failed to detect if CPU cgroup cpu.cfs_quota_us is available - %v", err)
  135. }
  136. if quotaExists && periodExists {
  137. f.cpuHardcapping = true
  138. }
  139. return f, nil
  140. }
  141. // TODO(vmarmol): Add limits to the system containers.
  142. // Takes the absolute name of the specified containers.
  143. // Empty container name disables use of the specified container.
  144. func NewContainerManager(mountUtil mount.Interface, cadvisorInterface cadvisor.Interface, nodeConfig NodeConfig) (ContainerManager, error) {
  145. // Check if Cgroup-root actually exists on the node
  146. if nodeConfig.CgroupsPerQOS {
  147. if nodeConfig.CgroupRoot == "" {
  148. 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")
  149. }
  150. if _, err := os.Stat(nodeConfig.CgroupRoot); err != nil {
  151. return nil, fmt.Errorf("invalid configuration: cgroup-root doesn't exist : %v", err)
  152. }
  153. }
  154. subsystems, err := GetCgroupSubsystems()
  155. if err != nil {
  156. return nil, fmt.Errorf("failed to get mounted cgroup subsystems: %v", err)
  157. }
  158. return &containerManagerImpl{
  159. cadvisorInterface: cadvisorInterface,
  160. mountUtil: mountUtil,
  161. NodeConfig: nodeConfig,
  162. subsystems: subsystems,
  163. }, nil
  164. }
  165. // NewPodContainerManager is a factory method returns a PodContainerManager object
  166. // If qosCgroups are enabled then it returns the general pod container manager
  167. // otherwise it returns a no-op manager which essentially does nothing
  168. func (cm *containerManagerImpl) NewPodContainerManager() PodContainerManager {
  169. if cm.NodeConfig.CgroupsPerQOS {
  170. return &podContainerManagerImpl{
  171. qosContainersInfo: cm.qosContainers,
  172. nodeInfo: cm.nodeInfo,
  173. subsystems: cm.subsystems,
  174. cgroupManager: NewCgroupManager(cm.subsystems),
  175. }
  176. }
  177. return &podContainerManagerNoop{
  178. cgroupRoot: cm.NodeConfig.CgroupRoot,
  179. }
  180. }
  181. // Create a cgroup container manager.
  182. func createManager(containerName string) *fs.Manager {
  183. allowAllDevices := true
  184. return &fs.Manager{
  185. Cgroups: &configs.Cgroup{
  186. Parent: "/",
  187. Name: containerName,
  188. Resources: &configs.Resources{
  189. AllowAllDevices: &allowAllDevices,
  190. },
  191. },
  192. }
  193. }
  194. type KernelTunableBehavior string
  195. const (
  196. KernelTunableWarn KernelTunableBehavior = "warn"
  197. KernelTunableError KernelTunableBehavior = "error"
  198. KernelTunableModify KernelTunableBehavior = "modify"
  199. )
  200. // InitQOS creates the top level qos cgroup containers
  201. // We create top level QoS containers for only Burstable and Best Effort
  202. // and not Guaranteed QoS class. All guaranteed pods are nested under the
  203. // RootContainer by default. InitQOS is called only once during kubelet bootstrapping.
  204. // TODO(@dubstack) Add support for cgroup-root to work on both systemd and cgroupfs
  205. // drivers. Currently we only support systems running cgroupfs driver
  206. func InitQOS(rootContainer string, subsystems *CgroupSubsystems) (QOSContainersInfo, error) {
  207. cm := NewCgroupManager(subsystems)
  208. // Top level for Qos containers are created only for Burstable
  209. // and Best Effort classes
  210. qosClasses := [2]qos.QOSClass{qos.Burstable, qos.BestEffort}
  211. // Create containers for both qos classes
  212. for _, qosClass := range qosClasses {
  213. // get the container's absolute name
  214. absoluteContainerName := path.Join(rootContainer, string(qosClass))
  215. // containerConfig object stores the cgroup specifications
  216. containerConfig := &CgroupConfig{
  217. Name: absoluteContainerName,
  218. ResourceParameters: &ResourceConfig{},
  219. }
  220. // TODO(@dubstack) Add support on systemd cgroups driver
  221. if err := cm.Create(containerConfig); err != nil {
  222. return QOSContainersInfo{}, fmt.Errorf("failed to create top level %v QOS cgroup : %v", qosClass, err)
  223. }
  224. }
  225. // Store the top level qos container names
  226. qosContainersInfo := QOSContainersInfo{
  227. Guaranteed: rootContainer,
  228. Burstable: path.Join(rootContainer, string(qos.Burstable)),
  229. BestEffort: path.Join(rootContainer, string(qos.BestEffort)),
  230. }
  231. return qosContainersInfo, nil
  232. }
  233. // setupKernelTunables validates kernel tunable flags are set as expected
  234. // depending upon the specified option, it will either warn, error, or modify the kernel tunable flags
  235. func setupKernelTunables(option KernelTunableBehavior) error {
  236. desiredState := map[string]int{
  237. utilsysctl.VmOvercommitMemory: utilsysctl.VmOvercommitMemoryAlways,
  238. utilsysctl.VmPanicOnOOM: utilsysctl.VmPanicOnOOMInvokeOOMKiller,
  239. utilsysctl.KernelPanic: utilsysctl.KernelPanicRebootTimeout,
  240. utilsysctl.KernelPanicOnOops: utilsysctl.KernelPanicOnOopsAlways,
  241. }
  242. sysctl := utilsysctl.New()
  243. errList := []error{}
  244. for flag, expectedValue := range desiredState {
  245. val, err := sysctl.GetSysctl(flag)
  246. if err != nil {
  247. errList = append(errList, err)
  248. continue
  249. }
  250. if val == expectedValue {
  251. continue
  252. }
  253. switch option {
  254. case KernelTunableError:
  255. errList = append(errList, fmt.Errorf("Invalid kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val))
  256. case KernelTunableWarn:
  257. glog.V(2).Infof("Invalid kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val)
  258. case KernelTunableModify:
  259. glog.V(2).Infof("Updating kernel flag: %v, expected value: %v, actual value: %v", flag, expectedValue, val)
  260. err = sysctl.SetSysctl(flag, expectedValue)
  261. if err != nil {
  262. errList = append(errList, err)
  263. }
  264. }
  265. }
  266. return utilerrors.NewAggregate(errList)
  267. }
  268. func (cm *containerManagerImpl) setupNode() error {
  269. f, err := validateSystemRequirements(cm.mountUtil)
  270. if err != nil {
  271. return err
  272. }
  273. if !f.cpuHardcapping {
  274. cm.status.SoftRequirements = fmt.Errorf("CPU hardcapping unsupported")
  275. }
  276. b := KernelTunableModify
  277. if cm.GetNodeConfig().ProtectKernelDefaults {
  278. b = KernelTunableError
  279. }
  280. if err := setupKernelTunables(b); err != nil {
  281. return err
  282. }
  283. // Setup top level qos containers only if CgroupsPerQOS flag is specified as true
  284. if cm.NodeConfig.CgroupsPerQOS {
  285. qosContainersInfo, err := InitQOS(cm.NodeConfig.CgroupRoot, cm.subsystems)
  286. if err != nil {
  287. return fmt.Errorf("failed to initialise top level QOS containers: %v", err)
  288. }
  289. cm.qosContainers = qosContainersInfo
  290. }
  291. systemContainers := []*systemContainer{}
  292. if cm.ContainerRuntime == "docker" {
  293. if cm.RuntimeCgroupsName != "" {
  294. cont := newSystemCgroups(cm.RuntimeCgroupsName)
  295. info, err := cm.cadvisorInterface.MachineInfo()
  296. var capacity = api.ResourceList{}
  297. if err != nil {
  298. } else {
  299. capacity = cadvisor.CapacityFromMachineInfo(info)
  300. }
  301. memoryLimit := (int64(capacity.Memory().Value() * DockerMemoryLimitThresholdPercent / 100))
  302. if memoryLimit < MinDockerMemoryLimit {
  303. glog.Warningf("Memory limit %d for container %s is too small, reset it to %d", memoryLimit, cm.RuntimeCgroupsName, MinDockerMemoryLimit)
  304. memoryLimit = MinDockerMemoryLimit
  305. }
  306. glog.V(2).Infof("Configure resource-only container %s with memory limit: %d", cm.RuntimeCgroupsName, memoryLimit)
  307. allowAllDevices := true
  308. dockerContainer := &fs.Manager{
  309. Cgroups: &configs.Cgroup{
  310. Parent: "/",
  311. Name: cm.RuntimeCgroupsName,
  312. Resources: &configs.Resources{
  313. Memory: memoryLimit,
  314. MemorySwap: -1,
  315. AllowAllDevices: &allowAllDevices,
  316. },
  317. },
  318. }
  319. dockerVersion := getDockerVersion(cm.cadvisorInterface)
  320. cont.ensureStateFunc = func(manager *fs.Manager) error {
  321. return ensureDockerInContainer(dockerVersion, -900, dockerContainer)
  322. }
  323. systemContainers = append(systemContainers, cont)
  324. } else {
  325. cm.periodicTasks = append(cm.periodicTasks, func() {
  326. cont, err := getContainerNameForProcess(dockerProcessName, dockerPidFile)
  327. if err != nil {
  328. glog.Error(err)
  329. return
  330. }
  331. glog.V(2).Infof("Discovered runtime cgroups name: %s", cont)
  332. cm.Lock()
  333. defer cm.Unlock()
  334. cm.RuntimeCgroupsName = cont
  335. })
  336. }
  337. }
  338. if cm.SystemCgroupsName != "" {
  339. if cm.SystemCgroupsName == "/" {
  340. return fmt.Errorf("system container cannot be root (\"/\")")
  341. }
  342. cont := newSystemCgroups(cm.SystemCgroupsName)
  343. rootContainer := &fs.Manager{
  344. Cgroups: &configs.Cgroup{
  345. Parent: "/",
  346. Name: "/",
  347. },
  348. }
  349. cont.ensureStateFunc = func(manager *fs.Manager) error {
  350. return ensureSystemCgroups(rootContainer, manager)
  351. }
  352. systemContainers = append(systemContainers, cont)
  353. }
  354. if cm.KubeletCgroupsName != "" {
  355. cont := newSystemCgroups(cm.KubeletCgroupsName)
  356. allowAllDevices := true
  357. manager := fs.Manager{
  358. Cgroups: &configs.Cgroup{
  359. Parent: "/",
  360. Name: cm.KubeletCgroupsName,
  361. Resources: &configs.Resources{
  362. AllowAllDevices: &allowAllDevices,
  363. },
  364. },
  365. }
  366. cont.ensureStateFunc = func(_ *fs.Manager) error {
  367. return manager.Apply(os.Getpid())
  368. }
  369. systemContainers = append(systemContainers, cont)
  370. } else {
  371. cm.periodicTasks = append(cm.periodicTasks, func() {
  372. cont, err := getContainer(os.Getpid())
  373. if err != nil {
  374. glog.Errorf("failed to find cgroups of kubelet - %v", err)
  375. return
  376. }
  377. cm.Lock()
  378. defer cm.Unlock()
  379. cm.KubeletCgroupsName = cont
  380. })
  381. }
  382. cm.systemContainers = systemContainers
  383. return nil
  384. }
  385. func getContainerNameForProcess(name, pidFile string) (string, error) {
  386. pids, err := getPidsForProcess(name, pidFile)
  387. if err != nil {
  388. return "", fmt.Errorf("failed to detect process id for %q - %v", name, err)
  389. }
  390. if len(pids) == 0 {
  391. return "", nil
  392. }
  393. cont, err := getContainer(pids[0])
  394. if err != nil {
  395. return "", err
  396. }
  397. return cont, nil
  398. }
  399. func (cm *containerManagerImpl) GetNodeConfig() NodeConfig {
  400. cm.RLock()
  401. defer cm.RUnlock()
  402. return cm.NodeConfig
  403. }
  404. func (cm *containerManagerImpl) GetMountedSubsystems() *CgroupSubsystems {
  405. return cm.subsystems
  406. }
  407. func (cm *containerManagerImpl) GetQOSContainersInfo() QOSContainersInfo {
  408. return cm.qosContainers
  409. }
  410. func (cm *containerManagerImpl) Status() Status {
  411. cm.RLock()
  412. defer cm.RUnlock()
  413. return cm.status
  414. }
  415. func (cm *containerManagerImpl) Start(node *api.Node) error {
  416. // cache the node Info including resource capacity and
  417. // allocatable of the node
  418. cm.nodeInfo = node
  419. // Setup the node
  420. if err := cm.setupNode(); err != nil {
  421. return err
  422. }
  423. // Don't run a background thread if there are no ensureStateFuncs.
  424. numEnsureStateFuncs := 0
  425. for _, cont := range cm.systemContainers {
  426. if cont.ensureStateFunc != nil {
  427. numEnsureStateFuncs++
  428. }
  429. }
  430. if numEnsureStateFuncs >= 0 {
  431. // Run ensure state functions every minute.
  432. go wait.Until(func() {
  433. for _, cont := range cm.systemContainers {
  434. if cont.ensureStateFunc != nil {
  435. if err := cont.ensureStateFunc(cont.manager); err != nil {
  436. glog.Warningf("[ContainerManager] Failed to ensure state of %q: %v", cont.name, err)
  437. }
  438. }
  439. }
  440. }, time.Minute, wait.NeverStop)
  441. }
  442. if len(cm.periodicTasks) > 0 {
  443. go wait.Until(func() {
  444. for _, task := range cm.periodicTasks {
  445. if task != nil {
  446. task()
  447. }
  448. }
  449. }, 5*time.Minute, wait.NeverStop)
  450. }
  451. return nil
  452. }
  453. func (cm *containerManagerImpl) SystemCgroupsLimit() api.ResourceList {
  454. cpuLimit := int64(0)
  455. // Sum up resources of all external containers.
  456. for _, cont := range cm.systemContainers {
  457. cpuLimit += cont.cpuMillicores
  458. }
  459. return api.ResourceList{
  460. api.ResourceCPU: *resource.NewMilliQuantity(
  461. cpuLimit,
  462. resource.DecimalSI),
  463. }
  464. }
  465. func isProcessRunningInHost(pid int) (bool, error) {
  466. // Get init mount namespace. Mount namespace is unique for all containers.
  467. initMntNs, err := os.Readlink("/proc/1/ns/mnt")
  468. if err != nil {
  469. return false, fmt.Errorf("failed to find mount namespace of init process")
  470. }
  471. processMntNs, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/mnt", pid))
  472. if err != nil {
  473. return false, fmt.Errorf("failed to find mount namespace of process %q", pid)
  474. }
  475. return initMntNs == processMntNs, nil
  476. }
  477. func getPidFromPidFile(pidFile string) (int, error) {
  478. file, err := os.Open(pidFile)
  479. if err != nil {
  480. return 0, fmt.Errorf("error opening pid file %s: %v", pidFile, err)
  481. }
  482. defer file.Close()
  483. data, err := ioutil.ReadAll(file)
  484. if err != nil {
  485. return 0, fmt.Errorf("error reading pid file %s: %v", pidFile, err)
  486. }
  487. pid, err := strconv.Atoi(string(data))
  488. if err != nil {
  489. return 0, fmt.Errorf("error parsing %s as a number: %v", string(data), err)
  490. }
  491. return pid, nil
  492. }
  493. func getPidsForProcess(name, pidFile string) ([]int, error) {
  494. if len(pidFile) > 0 {
  495. if pid, err := getPidFromPidFile(pidFile); err == nil {
  496. return []int{pid}, nil
  497. } else {
  498. // log the error and fall back to pidof
  499. runtime.HandleError(err)
  500. }
  501. }
  502. return procfs.PidOf(name)
  503. }
  504. // Ensures that the Docker daemon is in the desired container.
  505. func ensureDockerInContainer(dockerVersion semver.Version, oomScoreAdj int, manager *fs.Manager) error {
  506. type process struct{ name, file string }
  507. dockerProcs := []process{{dockerProcessName, dockerPidFile}}
  508. if dockerVersion.GTE(containerdVersion) {
  509. dockerProcs = append(dockerProcs, process{containerdProcessName, containerdPidFile})
  510. }
  511. var errs []error
  512. for _, proc := range dockerProcs {
  513. pids, err := getPidsForProcess(proc.name, proc.file)
  514. if err != nil {
  515. errs = append(errs, fmt.Errorf("failed to get pids for %q: %v", proc.name, err))
  516. continue
  517. }
  518. // Move if the pid is not already in the desired container.
  519. for _, pid := range pids {
  520. if err := ensureProcessInContainer(pid, oomScoreAdj, manager); err != nil {
  521. errs = append(errs, fmt.Errorf("errors moving %q pid: %v", proc.name, err))
  522. }
  523. }
  524. }
  525. return utilerrors.NewAggregate(errs)
  526. }
  527. func ensureProcessInContainer(pid int, oomScoreAdj int, manager *fs.Manager) error {
  528. if runningInHost, err := isProcessRunningInHost(pid); err != nil {
  529. // Err on the side of caution. Avoid moving the docker daemon unless we are able to identify its context.
  530. return err
  531. } else if !runningInHost {
  532. // Process is running inside a container. Don't touch that.
  533. return nil
  534. }
  535. var errs []error
  536. cont, err := getContainer(pid)
  537. if err != nil {
  538. errs = append(errs, fmt.Errorf("failed to find container of PID %d: %v", pid, err))
  539. }
  540. if cont != manager.Cgroups.Name {
  541. err = manager.Apply(pid)
  542. if err != nil {
  543. errs = append(errs, fmt.Errorf("failed to move PID %d (in %q) to %q", pid, cont, manager.Cgroups.Name))
  544. }
  545. }
  546. // Also apply oom-score-adj to processes
  547. oomAdjuster := oom.NewOOMAdjuster()
  548. if err := oomAdjuster.ApplyOOMScoreAdj(pid, oomScoreAdj); err != nil {
  549. errs = append(errs, fmt.Errorf("failed to apply oom score %d to PID %d", oomScoreAdj, pid))
  550. }
  551. return utilerrors.NewAggregate(errs)
  552. }
  553. // getContainer returns the cgroup associated with the specified pid.
  554. // It enforces a unified hierarchy for memory and cpu cgroups.
  555. // On systemd environments, it uses the name=systemd cgroup for the specified pid.
  556. func getContainer(pid int) (string, error) {
  557. cgs, err := cgroups.ParseCgroupFile(fmt.Sprintf("/proc/%d/cgroup", pid))
  558. if err != nil {
  559. return "", err
  560. }
  561. cpu, found := cgs["cpu"]
  562. if !found {
  563. return "", cgroups.NewNotFoundError("cpu")
  564. }
  565. memory, found := cgs["memory"]
  566. if !found {
  567. return "", cgroups.NewNotFoundError("memory")
  568. }
  569. // since we use this container for accounting, we need to ensure its a unified hierarchy.
  570. if cpu != memory {
  571. return "", fmt.Errorf("cpu and memory cgroup hierarchy not unified. cpu: %s, memory: %s", cpu, memory)
  572. }
  573. // on systemd, every pid is in a unified cgroup hierarchy (name=systemd as seen in systemd-cgls)
  574. // cpu and memory accounting is off by default, users may choose to enable it per unit or globally.
  575. // users could enable CPU and memory accounting globally via /etc/systemd/system.conf (DefaultCPUAccounting=true DefaultMemoryAccounting=true).
  576. // users could also enable CPU and memory accounting per unit via CPUAccounting=true and MemoryAccounting=true
  577. // 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.
  578. // 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
  579. // cgroup will be the closest ancestor where accounting is performed (most likely /) on systems that launch docker containers.
  580. // as a result, on those systems, you will not get cpu or memory accounting statistics for kubelet.
  581. // in addition, you would not get memory or cpu accounting for the runtime unless accounting was enabled on its unit (or globally).
  582. if systemd, found := cgs["name=systemd"]; found {
  583. if systemd != cpu {
  584. glog.Warningf("CPUAccounting not enabled for pid: %d", pid)
  585. }
  586. if systemd != memory {
  587. glog.Warningf("MemoryAccounting not enabled for pid: %d", pid)
  588. }
  589. return systemd, nil
  590. }
  591. return cpu, nil
  592. }
  593. // Ensures the system container is created and all non-kernel threads and process 1
  594. // without a container are moved to it.
  595. //
  596. // The reason of leaving kernel threads at root cgroup is that we don't want to tie the
  597. // execution of these threads with to-be defined /system quota and create priority inversions.
  598. //
  599. func ensureSystemCgroups(rootContainer *fs.Manager, manager *fs.Manager) error {
  600. // Move non-kernel PIDs to the system container.
  601. attemptsRemaining := 10
  602. var errs []error
  603. for attemptsRemaining >= 0 {
  604. // Only keep errors on latest attempt.
  605. errs = []error{}
  606. attemptsRemaining--
  607. allPids, err := rootContainer.GetPids()
  608. if err != nil {
  609. errs = append(errs, fmt.Errorf("failed to list PIDs for root: %v", err))
  610. continue
  611. }
  612. // Remove kernel pids and other protected PIDs (pid 1, PIDs already in system & kubelet containers)
  613. pids := make([]int, 0, len(allPids))
  614. for _, pid := range allPids {
  615. if pid == 1 || isKernelPid(pid) {
  616. continue
  617. }
  618. pids = append(pids, pid)
  619. }
  620. glog.Infof("Found %d PIDs in root, %d of them are not to be moved", len(allPids), len(allPids)-len(pids))
  621. // Check if we have moved all the non-kernel PIDs.
  622. if len(pids) == 0 {
  623. break
  624. }
  625. glog.Infof("Moving non-kernel processes: %v", pids)
  626. for _, pid := range pids {
  627. err := manager.Apply(pid)
  628. if err != nil {
  629. errs = append(errs, fmt.Errorf("failed to move PID %d into the system container %q: %v", pid, manager.Cgroups.Name, err))
  630. }
  631. }
  632. }
  633. if attemptsRemaining < 0 {
  634. errs = append(errs, fmt.Errorf("ran out of attempts to create system containers %q", manager.Cgroups.Name))
  635. }
  636. return utilerrors.NewAggregate(errs)
  637. }
  638. // Determines whether the specified PID is a kernel PID.
  639. func isKernelPid(pid int) bool {
  640. // Kernel threads have no associated executable.
  641. _, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
  642. return err != nil
  643. }
  644. // Helper for getting the docker version.
  645. func getDockerVersion(cadvisor cadvisor.Interface) semver.Version {
  646. var fallback semver.Version // Fallback to zero-value by default.
  647. versions, err := cadvisor.VersionInfo()
  648. if err != nil {
  649. glog.Errorf("Error requesting cAdvisor VersionInfo: %v", err)
  650. return fallback
  651. }
  652. dockerVersion, err := semver.Parse(versions.DockerVersion)
  653. if err != nil {
  654. glog.Errorf("Error parsing docker version %q: %v", versions.DockerVersion, err)
  655. return fallback
  656. }
  657. return dockerVersion
  658. }