cadvisor_stats_provider.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Copyright 2017 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 stats
  14. import (
  15. "fmt"
  16. "path"
  17. "sort"
  18. "strings"
  19. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  20. "k8s.io/klog"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  24. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  25. "k8s.io/kubernetes/pkg/kubelet/cm"
  26. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  27. "k8s.io/kubernetes/pkg/kubelet/leaky"
  28. "k8s.io/kubernetes/pkg/kubelet/server/stats"
  29. "k8s.io/kubernetes/pkg/kubelet/status"
  30. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  31. )
  32. // cadvisorStatsProvider implements the containerStatsProvider interface by
  33. // getting the container stats from cAdvisor. This is needed by
  34. // integrations which do not provide stats from CRI. See
  35. // `pkg/kubelet/cadvisor/util.go#UsingLegacyCadvisorStats` for the logic for
  36. // determining which integrations do not provide stats from CRI.
  37. type cadvisorStatsProvider struct {
  38. // cadvisor is used to get the stats of the cgroup for the containers that
  39. // are managed by pods.
  40. cadvisor cadvisor.Interface
  41. // resourceAnalyzer is used to get the volume stats of the pods.
  42. resourceAnalyzer stats.ResourceAnalyzer
  43. // imageService is used to get the stats of the image filesystem.
  44. imageService kubecontainer.ImageService
  45. // statusProvider is used to get pod metadata
  46. statusProvider status.PodStatusProvider
  47. }
  48. // newCadvisorStatsProvider returns a containerStatsProvider that provides
  49. // container stats from cAdvisor.
  50. func newCadvisorStatsProvider(
  51. cadvisor cadvisor.Interface,
  52. resourceAnalyzer stats.ResourceAnalyzer,
  53. imageService kubecontainer.ImageService,
  54. statusProvider status.PodStatusProvider,
  55. ) containerStatsProvider {
  56. return &cadvisorStatsProvider{
  57. cadvisor: cadvisor,
  58. resourceAnalyzer: resourceAnalyzer,
  59. imageService: imageService,
  60. statusProvider: statusProvider,
  61. }
  62. }
  63. // ListPodStats returns the stats of all the pod-managed containers.
  64. func (p *cadvisorStatsProvider) ListPodStats() ([]statsapi.PodStats, error) {
  65. // Gets node root filesystem information and image filesystem stats, which
  66. // will be used to populate the available and capacity bytes/inodes in
  67. // container stats.
  68. rootFsInfo, err := p.cadvisor.RootFsInfo()
  69. if err != nil {
  70. return nil, fmt.Errorf("failed to get rootFs info: %v", err)
  71. }
  72. imageFsInfo, err := p.cadvisor.ImagesFsInfo()
  73. if err != nil {
  74. return nil, fmt.Errorf("failed to get imageFs info: %v", err)
  75. }
  76. infos, err := getCadvisorContainerInfo(p.cadvisor)
  77. if err != nil {
  78. return nil, fmt.Errorf("failed to get container info from cadvisor: %v", err)
  79. }
  80. // removeTerminatedContainerInfo will also remove pod level cgroups, so save the infos into allInfos first
  81. allInfos := infos
  82. infos = removeTerminatedContainerInfo(infos)
  83. // Map each container to a pod and update the PodStats with container data.
  84. podToStats := map[statsapi.PodReference]*statsapi.PodStats{}
  85. for key, cinfo := range infos {
  86. // On systemd using devicemapper each mount into the container has an
  87. // associated cgroup. We ignore them to ensure we do not get duplicate
  88. // entries in our summary. For details on .mount units:
  89. // http://man7.org/linux/man-pages/man5/systemd.mount.5.html
  90. if strings.HasSuffix(key, ".mount") {
  91. continue
  92. }
  93. // Build the Pod key if this container is managed by a Pod
  94. if !isPodManagedContainer(&cinfo) {
  95. continue
  96. }
  97. ref := buildPodRef(cinfo.Spec.Labels)
  98. // Lookup the PodStats for the pod using the PodRef. If none exists,
  99. // initialize a new entry.
  100. podStats, found := podToStats[ref]
  101. if !found {
  102. podStats = &statsapi.PodStats{PodRef: ref}
  103. podToStats[ref] = podStats
  104. }
  105. // Update the PodStats entry with the stats from the container by
  106. // adding it to podStats.Containers.
  107. containerName := kubetypes.GetContainerName(cinfo.Spec.Labels)
  108. if containerName == leaky.PodInfraContainerName {
  109. // Special case for infrastructure container which is hidden from
  110. // the user and has network stats.
  111. podStats.Network = cadvisorInfoToNetworkStats("pod:"+ref.Namespace+"_"+ref.Name, &cinfo)
  112. } else {
  113. podStats.Containers = append(podStats.Containers, *cadvisorInfoToContainerStats(containerName, &cinfo, &rootFsInfo, &imageFsInfo))
  114. }
  115. }
  116. // Add each PodStats to the result.
  117. result := make([]statsapi.PodStats, 0, len(podToStats))
  118. for _, podStats := range podToStats {
  119. // Lookup the volume stats for each pod.
  120. podUID := types.UID(podStats.PodRef.UID)
  121. var ephemeralStats []statsapi.VolumeStats
  122. if vstats, found := p.resourceAnalyzer.GetPodVolumeStats(podUID); found {
  123. ephemeralStats = make([]statsapi.VolumeStats, len(vstats.EphemeralVolumes))
  124. copy(ephemeralStats, vstats.EphemeralVolumes)
  125. podStats.VolumeStats = append(vstats.EphemeralVolumes, vstats.PersistentVolumes...)
  126. }
  127. podStats.EphemeralStorage = calcEphemeralStorage(podStats.Containers, ephemeralStats, &rootFsInfo, nil, false)
  128. // Lookup the pod-level cgroup's CPU and memory stats
  129. podInfo := getCadvisorPodInfoFromPodUID(podUID, allInfos)
  130. if podInfo != nil {
  131. cpu, memory := cadvisorInfoToCPUandMemoryStats(podInfo)
  132. podStats.CPU = cpu
  133. podStats.Memory = memory
  134. }
  135. status, found := p.statusProvider.GetPodStatus(podUID)
  136. if found && status.StartTime != nil && !status.StartTime.IsZero() {
  137. podStats.StartTime = *status.StartTime
  138. // only append stats if we were able to get the start time of the pod
  139. result = append(result, *podStats)
  140. }
  141. }
  142. return result, nil
  143. }
  144. // ListPodStatsAndUpdateCPUNanoCoreUsage updates the cpu nano core usage for
  145. // the containers and returns the stats for all the pod-managed containers.
  146. // For cadvisor, cpu nano core usages are pre-computed and cached, so this
  147. // function simply calls ListPodStats.
  148. func (p *cadvisorStatsProvider) ListPodStatsAndUpdateCPUNanoCoreUsage() ([]statsapi.PodStats, error) {
  149. return p.ListPodStats()
  150. }
  151. // ListPodCPUAndMemoryStats returns the cpu and memory stats of all the pod-managed containers.
  152. func (p *cadvisorStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, error) {
  153. infos, err := getCadvisorContainerInfo(p.cadvisor)
  154. if err != nil {
  155. return nil, fmt.Errorf("failed to get container info from cadvisor: %v", err)
  156. }
  157. // removeTerminatedContainerInfo will also remove pod level cgroups, so save the infos into allInfos first
  158. allInfos := infos
  159. infos = removeTerminatedContainerInfo(infos)
  160. // Map each container to a pod and update the PodStats with container data.
  161. podToStats := map[statsapi.PodReference]*statsapi.PodStats{}
  162. for key, cinfo := range infos {
  163. // On systemd using devicemapper each mount into the container has an
  164. // associated cgroup. We ignore them to ensure we do not get duplicate
  165. // entries in our summary. For details on .mount units:
  166. // http://man7.org/linux/man-pages/man5/systemd.mount.5.html
  167. if strings.HasSuffix(key, ".mount") {
  168. continue
  169. }
  170. // Build the Pod key if this container is managed by a Pod
  171. if !isPodManagedContainer(&cinfo) {
  172. continue
  173. }
  174. ref := buildPodRef(cinfo.Spec.Labels)
  175. // Lookup the PodStats for the pod using the PodRef. If none exists,
  176. // initialize a new entry.
  177. podStats, found := podToStats[ref]
  178. if !found {
  179. podStats = &statsapi.PodStats{PodRef: ref}
  180. podToStats[ref] = podStats
  181. }
  182. // Update the PodStats entry with the stats from the container by
  183. // adding it to podStats.Containers.
  184. containerName := kubetypes.GetContainerName(cinfo.Spec.Labels)
  185. if containerName == leaky.PodInfraContainerName {
  186. // Special case for infrastructure container which is hidden from
  187. // the user and has network stats.
  188. podStats.StartTime = metav1.NewTime(cinfo.Spec.CreationTime)
  189. } else {
  190. podStats.Containers = append(podStats.Containers, *cadvisorInfoToContainerCPUAndMemoryStats(containerName, &cinfo))
  191. }
  192. }
  193. // Add each PodStats to the result.
  194. result := make([]statsapi.PodStats, 0, len(podToStats))
  195. for _, podStats := range podToStats {
  196. podUID := types.UID(podStats.PodRef.UID)
  197. // Lookup the pod-level cgroup's CPU and memory stats
  198. podInfo := getCadvisorPodInfoFromPodUID(podUID, allInfos)
  199. if podInfo != nil {
  200. cpu, memory := cadvisorInfoToCPUandMemoryStats(podInfo)
  201. podStats.CPU = cpu
  202. podStats.Memory = memory
  203. }
  204. result = append(result, *podStats)
  205. }
  206. return result, nil
  207. }
  208. // ImageFsStats returns the stats of the filesystem for storing images.
  209. func (p *cadvisorStatsProvider) ImageFsStats() (*statsapi.FsStats, error) {
  210. imageFsInfo, err := p.cadvisor.ImagesFsInfo()
  211. if err != nil {
  212. return nil, fmt.Errorf("failed to get imageFs info: %v", err)
  213. }
  214. imageStats, err := p.imageService.ImageStats()
  215. if err != nil || imageStats == nil {
  216. return nil, fmt.Errorf("failed to get image stats: %v", err)
  217. }
  218. var imageFsInodesUsed *uint64
  219. if imageFsInfo.Inodes != nil && imageFsInfo.InodesFree != nil {
  220. imageFsIU := *imageFsInfo.Inodes - *imageFsInfo.InodesFree
  221. imageFsInodesUsed = &imageFsIU
  222. }
  223. return &statsapi.FsStats{
  224. Time: metav1.NewTime(imageFsInfo.Timestamp),
  225. AvailableBytes: &imageFsInfo.Available,
  226. CapacityBytes: &imageFsInfo.Capacity,
  227. UsedBytes: &imageStats.TotalStorageBytes,
  228. InodesFree: imageFsInfo.InodesFree,
  229. Inodes: imageFsInfo.Inodes,
  230. InodesUsed: imageFsInodesUsed,
  231. }, nil
  232. }
  233. // ImageFsDevice returns name of the device where the image filesystem locates,
  234. // e.g. /dev/sda1.
  235. func (p *cadvisorStatsProvider) ImageFsDevice() (string, error) {
  236. imageFsInfo, err := p.cadvisor.ImagesFsInfo()
  237. if err != nil {
  238. return "", err
  239. }
  240. return imageFsInfo.Device, nil
  241. }
  242. // buildPodRef returns a PodReference that identifies the Pod managing cinfo
  243. func buildPodRef(containerLabels map[string]string) statsapi.PodReference {
  244. podName := kubetypes.GetPodName(containerLabels)
  245. podNamespace := kubetypes.GetPodNamespace(containerLabels)
  246. podUID := kubetypes.GetPodUID(containerLabels)
  247. return statsapi.PodReference{Name: podName, Namespace: podNamespace, UID: podUID}
  248. }
  249. // isPodManagedContainer returns true if the cinfo container is managed by a Pod
  250. func isPodManagedContainer(cinfo *cadvisorapiv2.ContainerInfo) bool {
  251. podName := kubetypes.GetPodName(cinfo.Spec.Labels)
  252. podNamespace := kubetypes.GetPodNamespace(cinfo.Spec.Labels)
  253. managed := podName != "" && podNamespace != ""
  254. if !managed && podName != podNamespace {
  255. klog.Warningf(
  256. "Expect container to have either both podName (%s) and podNamespace (%s) labels, or neither.",
  257. podName, podNamespace)
  258. }
  259. return managed
  260. }
  261. // getCadvisorPodInfoFromPodUID returns a pod cgroup information by matching the podUID with its CgroupName identifier base name
  262. func getCadvisorPodInfoFromPodUID(podUID types.UID, infos map[string]cadvisorapiv2.ContainerInfo) *cadvisorapiv2.ContainerInfo {
  263. for key, info := range infos {
  264. if cm.IsSystemdStyleName(key) {
  265. // Convert to internal cgroup name and take the last component only.
  266. internalCgroupName := cm.ParseSystemdToCgroupName(key)
  267. key = internalCgroupName[len(internalCgroupName)-1]
  268. } else {
  269. // Take last component only.
  270. key = path.Base(key)
  271. }
  272. if cm.GetPodCgroupNameSuffix(podUID) == key {
  273. return &info
  274. }
  275. }
  276. return nil
  277. }
  278. // removeTerminatedContainerInfo returns the specified containerInfo but with
  279. // the stats of the terminated containers removed.
  280. //
  281. // A ContainerInfo is considered to be of a terminated container if it has an
  282. // older CreationTime and zero CPU instantaneous and memory RSS usage.
  283. func removeTerminatedContainerInfo(containerInfo map[string]cadvisorapiv2.ContainerInfo) map[string]cadvisorapiv2.ContainerInfo {
  284. cinfoMap := make(map[containerID][]containerInfoWithCgroup)
  285. for key, cinfo := range containerInfo {
  286. if !isPodManagedContainer(&cinfo) {
  287. continue
  288. }
  289. cinfoID := containerID{
  290. podRef: buildPodRef(cinfo.Spec.Labels),
  291. containerName: kubetypes.GetContainerName(cinfo.Spec.Labels),
  292. }
  293. cinfoMap[cinfoID] = append(cinfoMap[cinfoID], containerInfoWithCgroup{
  294. cinfo: cinfo,
  295. cgroup: key,
  296. })
  297. }
  298. result := make(map[string]cadvisorapiv2.ContainerInfo)
  299. for _, refs := range cinfoMap {
  300. if len(refs) == 1 {
  301. result[refs[0].cgroup] = refs[0].cinfo
  302. continue
  303. }
  304. sort.Sort(ByCreationTime(refs))
  305. for i := len(refs) - 1; i >= 0; i-- {
  306. if hasMemoryAndCPUInstUsage(&refs[i].cinfo) {
  307. result[refs[i].cgroup] = refs[i].cinfo
  308. break
  309. }
  310. }
  311. }
  312. return result
  313. }
  314. // ByCreationTime implements sort.Interface for []containerInfoWithCgroup based
  315. // on the cinfo.Spec.CreationTime field.
  316. type ByCreationTime []containerInfoWithCgroup
  317. func (a ByCreationTime) Len() int { return len(a) }
  318. func (a ByCreationTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  319. func (a ByCreationTime) Less(i, j int) bool {
  320. if a[i].cinfo.Spec.CreationTime.Equal(a[j].cinfo.Spec.CreationTime) {
  321. // There shouldn't be two containers with the same name and/or the same
  322. // creation time. However, to make the logic here robust, we break the
  323. // tie by moving the one without CPU instantaneous or memory RSS usage
  324. // to the beginning.
  325. return hasMemoryAndCPUInstUsage(&a[j].cinfo)
  326. }
  327. return a[i].cinfo.Spec.CreationTime.Before(a[j].cinfo.Spec.CreationTime)
  328. }
  329. // containerID is the identity of a container in a pod.
  330. type containerID struct {
  331. podRef statsapi.PodReference
  332. containerName string
  333. }
  334. // containerInfoWithCgroup contains the ContainerInfo and its cgroup name.
  335. type containerInfoWithCgroup struct {
  336. cinfo cadvisorapiv2.ContainerInfo
  337. cgroup string
  338. }
  339. // hasMemoryAndCPUInstUsage returns true if the specified container info has
  340. // both non-zero CPU instantaneous usage and non-zero memory RSS usage, and
  341. // false otherwise.
  342. func hasMemoryAndCPUInstUsage(info *cadvisorapiv2.ContainerInfo) bool {
  343. if !info.Spec.HasCpu || !info.Spec.HasMemory {
  344. return false
  345. }
  346. cstat, found := latestContainerStats(info)
  347. if !found {
  348. return false
  349. }
  350. if cstat.CpuInst == nil {
  351. return false
  352. }
  353. return cstat.CpuInst.Usage.Total != 0 && cstat.Memory.RSS != 0
  354. }
  355. func getCadvisorContainerInfo(ca cadvisor.Interface) (map[string]cadvisorapiv2.ContainerInfo, error) {
  356. infos, err := ca.ContainerInfoV2("/", cadvisorapiv2.RequestOptions{
  357. IdType: cadvisorapiv2.TypeName,
  358. Count: 2, // 2 samples are needed to compute "instantaneous" CPU
  359. Recursive: true,
  360. })
  361. if err != nil {
  362. if _, ok := infos["/"]; ok {
  363. // If the failure is partial, log it and return a best-effort
  364. // response.
  365. klog.Errorf("Partial failure issuing cadvisor.ContainerInfoV2: %v", err)
  366. } else {
  367. return nil, fmt.Errorf("failed to get root cgroup stats: %v", err)
  368. }
  369. }
  370. return infos, nil
  371. }