kubelet_getters.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "path/filepath"
  19. cadvisorapiv1 "github.com/google/cadvisor/info/v1"
  20. "k8s.io/klog"
  21. "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/kubernetes/pkg/kubelet/cm"
  24. "k8s.io/kubernetes/pkg/kubelet/config"
  25. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  26. "k8s.io/kubernetes/pkg/util/mount"
  27. utilnode "k8s.io/kubernetes/pkg/util/node"
  28. utilpath "k8s.io/utils/path"
  29. )
  30. // getRootDir returns the full path to the directory under which kubelet can
  31. // store data. These functions are useful to pass interfaces to other modules
  32. // that may need to know where to write data without getting a whole kubelet
  33. // instance.
  34. func (kl *Kubelet) getRootDir() string {
  35. return kl.rootDirectory
  36. }
  37. // getPodsDir returns the full path to the directory under which pod
  38. // directories are created.
  39. func (kl *Kubelet) getPodsDir() string {
  40. return filepath.Join(kl.getRootDir(), config.DefaultKubeletPodsDirName)
  41. }
  42. // getPluginsDir returns the full path to the directory under which plugin
  43. // directories are created. Plugins can use these directories for data that
  44. // they need to persist. Plugins should create subdirectories under this named
  45. // after their own names.
  46. func (kl *Kubelet) getPluginsDir() string {
  47. return filepath.Join(kl.getRootDir(), config.DefaultKubeletPluginsDirName)
  48. }
  49. // getPluginsRegistrationDir returns the full path to the directory under which
  50. // plugins socket should be placed to be registered.
  51. // More information is available about plugin registration in the pluginwatcher
  52. // module
  53. func (kl *Kubelet) getPluginsRegistrationDir() string {
  54. return filepath.Join(kl.getRootDir(), config.DefaultKubeletPluginsRegistrationDirName)
  55. }
  56. // getPluginDir returns a data directory name for a given plugin name.
  57. // Plugins can use these directories to store data that they need to persist.
  58. // For per-pod plugin data, see getPodPluginDir.
  59. func (kl *Kubelet) getPluginDir(pluginName string) string {
  60. return filepath.Join(kl.getPluginsDir(), pluginName)
  61. }
  62. // getVolumeDevicePluginsDir returns the full path to the directory under which plugin
  63. // directories are created. Plugins can use these directories for data that
  64. // they need to persist. Plugins should create subdirectories under this named
  65. // after their own names.
  66. func (kl *Kubelet) getVolumeDevicePluginsDir() string {
  67. return filepath.Join(kl.getRootDir(), config.DefaultKubeletPluginsDirName)
  68. }
  69. // getVolumeDevicePluginDir returns a data directory name for a given plugin name.
  70. // Plugins can use these directories to store data that they need to persist.
  71. // For per-pod plugin data, see getVolumeDevicePluginsDir.
  72. func (kl *Kubelet) getVolumeDevicePluginDir(pluginName string) string {
  73. return filepath.Join(kl.getVolumeDevicePluginsDir(), pluginName, config.DefaultKubeletVolumeDevicesDirName)
  74. }
  75. // GetPodDir returns the full path to the per-pod data directory for the
  76. // specified pod. This directory may not exist if the pod does not exist.
  77. func (kl *Kubelet) GetPodDir(podUID types.UID) string {
  78. return kl.getPodDir(podUID)
  79. }
  80. // getPodDir returns the full path to the per-pod directory for the pod with
  81. // the given UID.
  82. func (kl *Kubelet) getPodDir(podUID types.UID) string {
  83. return filepath.Join(kl.getPodsDir(), string(podUID))
  84. }
  85. // getPodVolumesSubpathsDir returns the full path to the per-pod subpaths directory under
  86. // which subpath volumes are created for the specified pod. This directory may not
  87. // exist if the pod does not exist or subpaths are not specified.
  88. func (kl *Kubelet) getPodVolumeSubpathsDir(podUID types.UID) string {
  89. return filepath.Join(kl.getPodDir(podUID), config.DefaultKubeletVolumeSubpathsDirName)
  90. }
  91. // getPodVolumesDir returns the full path to the per-pod data directory under
  92. // which volumes are created for the specified pod. This directory may not
  93. // exist if the pod does not exist.
  94. func (kl *Kubelet) getPodVolumesDir(podUID types.UID) string {
  95. return filepath.Join(kl.getPodDir(podUID), config.DefaultKubeletVolumesDirName)
  96. }
  97. // getPodVolumeDir returns the full path to the directory which represents the
  98. // named volume under the named plugin for specified pod. This directory may not
  99. // exist if the pod does not exist.
  100. func (kl *Kubelet) getPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string {
  101. return filepath.Join(kl.getPodVolumesDir(podUID), pluginName, volumeName)
  102. }
  103. // getPodVolumeDevicesDir returns the full path to the per-pod data directory under
  104. // which volumes are created for the specified pod. This directory may not
  105. // exist if the pod does not exist.
  106. func (kl *Kubelet) getPodVolumeDevicesDir(podUID types.UID) string {
  107. return filepath.Join(kl.getPodDir(podUID), config.DefaultKubeletVolumeDevicesDirName)
  108. }
  109. // getPodVolumeDeviceDir returns the full path to the directory which represents the
  110. // named plugin for specified pod. This directory may not exist if the pod does not exist.
  111. func (kl *Kubelet) getPodVolumeDeviceDir(podUID types.UID, pluginName string) string {
  112. return filepath.Join(kl.getPodVolumeDevicesDir(podUID), pluginName)
  113. }
  114. // getPodPluginsDir returns the full path to the per-pod data directory under
  115. // which plugins may store data for the specified pod. This directory may not
  116. // exist if the pod does not exist.
  117. func (kl *Kubelet) getPodPluginsDir(podUID types.UID) string {
  118. return filepath.Join(kl.getPodDir(podUID), config.DefaultKubeletPluginsDirName)
  119. }
  120. // getPodPluginDir returns a data directory name for a given plugin name for a
  121. // given pod UID. Plugins can use these directories to store data that they
  122. // need to persist. For non-per-pod plugin data, see getPluginDir.
  123. func (kl *Kubelet) getPodPluginDir(podUID types.UID, pluginName string) string {
  124. return filepath.Join(kl.getPodPluginsDir(podUID), pluginName)
  125. }
  126. // getPodContainerDir returns the full path to the per-pod data directory under
  127. // which container data is held for the specified pod. This directory may not
  128. // exist if the pod or container does not exist.
  129. func (kl *Kubelet) getPodContainerDir(podUID types.UID, ctrName string) string {
  130. return filepath.Join(kl.getPodDir(podUID), config.DefaultKubeletContainersDirName, ctrName)
  131. }
  132. // getPodResourcesSocket returns the full path to the directory containing the pod resources socket
  133. func (kl *Kubelet) getPodResourcesDir() string {
  134. return filepath.Join(kl.getRootDir(), config.DefaultKubeletPodResourcesDirName)
  135. }
  136. // getPluginsDirSELinuxLabel returns the selinux label to be applied on plugin directories
  137. func (kl *Kubelet) getPluginsDirSELinuxLabel() string {
  138. return config.KubeletPluginsDirSELinuxLabel
  139. }
  140. // GetPods returns all pods bound to the kubelet and their spec, and the mirror
  141. // pods.
  142. func (kl *Kubelet) GetPods() []*v1.Pod {
  143. pods := kl.podManager.GetPods()
  144. // a kubelet running without apiserver requires an additional
  145. // update of the static pod status. See #57106
  146. for _, p := range pods {
  147. if status, ok := kl.statusManager.GetPodStatus(p.UID); ok {
  148. p.Status = status
  149. }
  150. }
  151. return pods
  152. }
  153. // GetRunningPods returns all pods running on kubelet from looking at the
  154. // container runtime cache. This function converts kubecontainer.Pod to
  155. // v1.Pod, so only the fields that exist in both kubecontainer.Pod and
  156. // v1.Pod are considered meaningful.
  157. func (kl *Kubelet) GetRunningPods() ([]*v1.Pod, error) {
  158. pods, err := kl.runtimeCache.GetPods()
  159. if err != nil {
  160. return nil, err
  161. }
  162. apiPods := make([]*v1.Pod, 0, len(pods))
  163. for _, pod := range pods {
  164. apiPods = append(apiPods, pod.ToAPIPod())
  165. }
  166. return apiPods, nil
  167. }
  168. // GetPodByFullName gets the pod with the given 'full' name, which
  169. // incorporates the namespace as well as whether the pod was found.
  170. func (kl *Kubelet) GetPodByFullName(podFullName string) (*v1.Pod, bool) {
  171. return kl.podManager.GetPodByFullName(podFullName)
  172. }
  173. // GetPodByName provides the first pod that matches namespace and name, as well
  174. // as whether the pod was found.
  175. func (kl *Kubelet) GetPodByName(namespace, name string) (*v1.Pod, bool) {
  176. return kl.podManager.GetPodByName(namespace, name)
  177. }
  178. // GetPodByCgroupfs provides the pod that maps to the specified cgroup, as well
  179. // as whether the pod was found.
  180. func (kl *Kubelet) GetPodByCgroupfs(cgroupfs string) (*v1.Pod, bool) {
  181. pcm := kl.containerManager.NewPodContainerManager()
  182. if result, podUID := pcm.IsPodCgroup(cgroupfs); result {
  183. return kl.podManager.GetPodByUID(podUID)
  184. }
  185. return nil, false
  186. }
  187. // GetHostname Returns the hostname as the kubelet sees it.
  188. func (kl *Kubelet) GetHostname() string {
  189. return kl.hostname
  190. }
  191. // getRuntime returns the current Runtime implementation in use by the kubelet.
  192. func (kl *Kubelet) getRuntime() kubecontainer.Runtime {
  193. return kl.containerRuntime
  194. }
  195. // GetNode returns the node info for the configured node name of this Kubelet.
  196. func (kl *Kubelet) GetNode() (*v1.Node, error) {
  197. if kl.kubeClient == nil {
  198. return kl.initialNode()
  199. }
  200. return kl.nodeInfo.GetNodeInfo(string(kl.nodeName))
  201. }
  202. // getNodeAnyWay() must return a *v1.Node which is required by RunGeneralPredicates().
  203. // The *v1.Node is obtained as follows:
  204. // Return kubelet's nodeInfo for this node, except on error or if in standalone mode,
  205. // in which case return a manufactured nodeInfo representing a node with no pods,
  206. // zero capacity, and the default labels.
  207. func (kl *Kubelet) getNodeAnyWay() (*v1.Node, error) {
  208. if kl.kubeClient != nil {
  209. if n, err := kl.nodeInfo.GetNodeInfo(string(kl.nodeName)); err == nil {
  210. return n, nil
  211. }
  212. }
  213. return kl.initialNode()
  214. }
  215. // GetNodeConfig returns the container manager node config.
  216. func (kl *Kubelet) GetNodeConfig() cm.NodeConfig {
  217. return kl.containerManager.GetNodeConfig()
  218. }
  219. // GetPodCgroupRoot returns the listeral cgroupfs value for the cgroup containing all pods
  220. func (kl *Kubelet) GetPodCgroupRoot() string {
  221. return kl.containerManager.GetPodCgroupRoot()
  222. }
  223. // GetHostIP returns host IP or nil in case of error.
  224. func (kl *Kubelet) GetHostIP() (net.IP, error) {
  225. node, err := kl.GetNode()
  226. if err != nil {
  227. return nil, fmt.Errorf("cannot get node: %v", err)
  228. }
  229. return utilnode.GetNodeHostIP(node)
  230. }
  231. // getHostIPAnyway attempts to return the host IP from kubelet's nodeInfo, or
  232. // the initialNode.
  233. func (kl *Kubelet) getHostIPAnyWay() (net.IP, error) {
  234. node, err := kl.getNodeAnyWay()
  235. if err != nil {
  236. return nil, err
  237. }
  238. return utilnode.GetNodeHostIP(node)
  239. }
  240. // GetExtraSupplementalGroupsForPod returns a list of the extra
  241. // supplemental groups for the Pod. These extra supplemental groups come
  242. // from annotations on persistent volumes that the pod depends on.
  243. func (kl *Kubelet) GetExtraSupplementalGroupsForPod(pod *v1.Pod) []int64 {
  244. return kl.volumeManager.GetExtraSupplementalGroupsForPod(pod)
  245. }
  246. // getPodVolumePathListFromDisk returns a list of the volume paths by reading the
  247. // volume directories for the given pod from the disk.
  248. func (kl *Kubelet) getPodVolumePathListFromDisk(podUID types.UID) ([]string, error) {
  249. volumes := []string{}
  250. podVolDir := kl.getPodVolumesDir(podUID)
  251. if pathExists, pathErr := mount.PathExists(podVolDir); pathErr != nil {
  252. return volumes, fmt.Errorf("Error checking if path %q exists: %v", podVolDir, pathErr)
  253. } else if !pathExists {
  254. klog.Warningf("Path %q does not exist", podVolDir)
  255. return volumes, nil
  256. }
  257. volumePluginDirs, err := ioutil.ReadDir(podVolDir)
  258. if err != nil {
  259. klog.Errorf("Could not read directory %s: %v", podVolDir, err)
  260. return volumes, err
  261. }
  262. for _, volumePluginDir := range volumePluginDirs {
  263. volumePluginName := volumePluginDir.Name()
  264. volumePluginPath := filepath.Join(podVolDir, volumePluginName)
  265. volumeDirs, err := utilpath.ReadDirNoStat(volumePluginPath)
  266. if err != nil {
  267. return volumes, fmt.Errorf("Could not read directory %s: %v", volumePluginPath, err)
  268. }
  269. for _, volumeDir := range volumeDirs {
  270. volumes = append(volumes, filepath.Join(volumePluginPath, volumeDir))
  271. }
  272. }
  273. return volumes, nil
  274. }
  275. func (kl *Kubelet) getMountedVolumePathListFromDisk(podUID types.UID) ([]string, error) {
  276. mountedVolumes := []string{}
  277. volumePaths, err := kl.getPodVolumePathListFromDisk(podUID)
  278. if err != nil {
  279. return mountedVolumes, err
  280. }
  281. for _, volumePath := range volumePaths {
  282. isNotMount, err := kl.mounter.IsLikelyNotMountPoint(volumePath)
  283. if err != nil {
  284. return mountedVolumes, err
  285. }
  286. if !isNotMount {
  287. mountedVolumes = append(mountedVolumes, volumePath)
  288. }
  289. }
  290. return mountedVolumes, nil
  291. }
  292. // podVolumesSubpathsDirExists returns true if the pod volume-subpaths directory for
  293. // a given pod exists
  294. func (kl *Kubelet) podVolumeSubpathsDirExists(podUID types.UID) (bool, error) {
  295. podVolDir := kl.getPodVolumeSubpathsDir(podUID)
  296. if pathExists, pathErr := mount.PathExists(podVolDir); pathErr != nil {
  297. return true, fmt.Errorf("Error checking if path %q exists: %v", podVolDir, pathErr)
  298. } else if !pathExists {
  299. return false, nil
  300. }
  301. return true, nil
  302. }
  303. // GetVersionInfo returns information about the version of cAdvisor in use.
  304. func (kl *Kubelet) GetVersionInfo() (*cadvisorapiv1.VersionInfo, error) {
  305. return kl.cadvisor.VersionInfo()
  306. }
  307. // GetCachedMachineInfo assumes that the machine info can't change without a reboot
  308. func (kl *Kubelet) GetCachedMachineInfo() (*cadvisorapiv1.MachineInfo, error) {
  309. return kl.machineInfo, nil
  310. }