kubelet_cadvisor.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2015 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. cadvisorapi "github.com/google/cadvisor/info/v1"
  16. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  17. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  18. "k8s.io/kubernetes/pkg/types"
  19. )
  20. // GetContainerInfo returns stats (from Cadvisor) for a container.
  21. func (kl *Kubelet) GetContainerInfo(podFullName string, podUID types.UID, containerName string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  22. podUID = kl.podManager.TranslatePodUID(podUID)
  23. pods, err := kl.runtimeCache.GetPods()
  24. if err != nil {
  25. return nil, err
  26. }
  27. pod := kubecontainer.Pods(pods).FindPod(podFullName, podUID)
  28. container := pod.FindContainerByName(containerName)
  29. if container == nil {
  30. return nil, kubecontainer.ErrContainerNotFound
  31. }
  32. ci, err := kl.cadvisor.DockerContainer(container.ID.ID, req)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &ci, nil
  37. }
  38. // HasDedicatedImageFs returns true if the imagefs has a dedicated device.
  39. func (kl *Kubelet) HasDedicatedImageFs() (bool, error) {
  40. imageFsInfo, err := kl.ImagesFsInfo()
  41. if err != nil {
  42. return false, err
  43. }
  44. rootFsInfo, err := kl.RootFsInfo()
  45. if err != nil {
  46. return false, err
  47. }
  48. return imageFsInfo.Device != rootFsInfo.Device, nil
  49. }
  50. // GetContainerInfoV2 returns stats (from Cadvisor) for containers.
  51. func (kl *Kubelet) GetContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
  52. return kl.cadvisor.ContainerInfoV2(name, options)
  53. }
  54. // ImagesFsInfo returns information about docker image fs usage from
  55. // cadvisor.
  56. func (kl *Kubelet) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
  57. return kl.cadvisor.ImagesFsInfo()
  58. }
  59. // RootFsInfo returns info about the root fs from cadvisor.
  60. func (kl *Kubelet) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
  61. return kl.cadvisor.RootFsInfo()
  62. }
  63. // Returns stats (from Cadvisor) for a non-Kubernetes container.
  64. func (kl *Kubelet) GetRawContainerInfo(containerName string, req *cadvisorapi.ContainerInfoRequest, subcontainers bool) (map[string]*cadvisorapi.ContainerInfo, error) {
  65. if subcontainers {
  66. return kl.cadvisor.SubcontainerInfo(containerName, req)
  67. } else {
  68. containerInfo, err := kl.cadvisor.ContainerInfo(containerName, req)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return map[string]*cadvisorapi.ContainerInfo{
  73. containerInfo.Name: containerInfo,
  74. }, nil
  75. }
  76. }
  77. // GetCachedMachineInfo assumes that the machine info can't change without a reboot
  78. func (kl *Kubelet) GetCachedMachineInfo() (*cadvisorapi.MachineInfo, error) {
  79. if kl.machineInfo == nil {
  80. info, err := kl.cadvisor.MachineInfo()
  81. if err != nil {
  82. return nil, err
  83. }
  84. kl.machineInfo = info
  85. }
  86. return kl.machineInfo, nil
  87. }