cgroupdriver.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2018 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 util
  14. import (
  15. "strings"
  16. "github.com/pkg/errors"
  17. utilsexec "k8s.io/utils/exec"
  18. )
  19. const (
  20. // CgroupDriverSystemd holds the systemd driver type
  21. CgroupDriverSystemd = "systemd"
  22. // CgroupDriverCgroupfs holds the cgroupfs driver type
  23. CgroupDriverCgroupfs = "cgroupfs"
  24. )
  25. // TODO: add support for detecting the cgroup driver for CRI other than
  26. // Docker. Currently only Docker driver detection is supported:
  27. // Discussion:
  28. // https://github.com/kubernetes/kubeadm/issues/844
  29. // GetCgroupDriverDocker runs 'docker info -f "{{.CgroupDriver}}"' to obtain the docker cgroup driver
  30. func GetCgroupDriverDocker(execer utilsexec.Interface) (string, error) {
  31. driver, err := callDockerInfo(execer)
  32. if err != nil {
  33. return "", err
  34. }
  35. return strings.TrimSuffix(driver, "\n"), nil
  36. }
  37. func callDockerInfo(execer utilsexec.Interface) (string, error) {
  38. out, err := execer.Command("docker", "info", "-f", "{{.CgroupDriver}}").Output()
  39. if err != nil {
  40. return "", errors.Wrap(err, "cannot execute 'docker info -f {{.CgroupDriver}}'")
  41. }
  42. return string(out), nil
  43. }