kubelet.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package kubelet
  15. import (
  16. "path"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/hostman/hostutils/kubelet/eviction"
  21. "yunion.io/x/onecloud/pkg/util/procutils"
  22. )
  23. const (
  24. KubeletConfigurationFileName = "config.yaml"
  25. )
  26. // KubeletConfig is a interface abstracts manipulation of kubelet run directory.
  27. type KubeletConfig interface {
  28. HasDedicatedImageFs() bool
  29. GetNodeFsDevice() string
  30. GetImageFsDevice() string
  31. GetImageFs() string
  32. GetEvictionConfig() eviction.Config
  33. }
  34. // kubeletConfig implements KubeletRunDirectory interface.
  35. type kubeletConfig struct {
  36. config jsonutils.JSONObject
  37. dockerInfo *DockerInfo
  38. evictionConfig eviction.Config
  39. nodeFsDevice string
  40. imageFsDevice string
  41. }
  42. func NewKubeletConfigByDirectory(dir string) (KubeletConfig, error) {
  43. configFile := path.Join(dir, KubeletConfigurationFileName)
  44. content, err := procutils.NewRemoteCommandAsFarAsPossible("cat", configFile).Output()
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "Read config file %s", configFile)
  47. }
  48. dockerInfo, err := GetDockerInfoByRemote()
  49. if err != nil {
  50. return nil, errors.Wrap(err, "Get docker info")
  51. }
  52. return newKubeletConfig(content, dockerInfo)
  53. }
  54. func newKubeletConfig(yamlConfig []byte, dockerInfo *DockerInfo) (KubeletConfig, error) {
  55. obj, err := jsonutils.ParseYAML(string(yamlConfig))
  56. if err != nil {
  57. return nil, errors.Wrapf(err, "Parse yaml content %s", yamlConfig)
  58. }
  59. evictionConfig, err := eviction.NewConfig(yamlConfig)
  60. if err != nil {
  61. return nil, errors.Wrap(err, "New eviction config")
  62. }
  63. imageFsDev, err := GetDirectoryMountDevice(dockerInfo.DockerRootDir)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "Find docker root directory device")
  66. }
  67. nodeFsDev, err := GetDirectoryMountDevice("/")
  68. if err != nil {
  69. return nil, errors.Wrap(err, "Find node FS directory device")
  70. }
  71. k := &kubeletConfig{
  72. config: obj,
  73. dockerInfo: dockerInfo,
  74. evictionConfig: evictionConfig,
  75. nodeFsDevice: nodeFsDev,
  76. imageFsDevice: imageFsDev,
  77. }
  78. return k, nil
  79. }
  80. func GetDirectoryMountDevice(dirPath string) (string, error) {
  81. content, err := procutils.NewRemoteCommandAsFarAsPossible("findmnt", "-n", "-o", "SOURCE", "--target", dirPath).Output()
  82. if err != nil {
  83. return "", errors.Wrapf(err, "Find directory %q mount source device", dirPath)
  84. }
  85. return strings.TrimSpace(string(content)), nil
  86. }
  87. func (k *kubeletConfig) GetNodeFsDevice() string {
  88. return k.nodeFsDevice
  89. }
  90. func (k *kubeletConfig) GetImageFsDevice() string {
  91. return k.imageFsDevice
  92. }
  93. func (k *kubeletConfig) HasDedicatedImageFs() bool {
  94. return k.imageFsDevice != k.nodeFsDevice
  95. }
  96. func (k *kubeletConfig) GetImageFs() string {
  97. return k.dockerInfo.DockerRootDir
  98. }
  99. func (k *kubeletConfig) GetEvictionConfig() eviction.Config {
  100. return k.evictionConfig
  101. }