util.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 cadvisor
  14. import (
  15. goruntime "runtime"
  16. cadvisorapi "github.com/google/cadvisor/info/v1"
  17. cadvisorapi2 "github.com/google/cadvisor/info/v2"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  21. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  22. )
  23. const (
  24. // CrioSocket is the path to the CRI-O socket.
  25. // Please keep this in sync with the one in:
  26. // github.com/google/cadvisor/container/crio/client.go
  27. CrioSocket = "/var/run/crio/crio.sock"
  28. )
  29. // CapacityFromMachineInfo returns the capacity of the resources from the machine info.
  30. func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
  31. c := v1.ResourceList{
  32. v1.ResourceCPU: *resource.NewMilliQuantity(
  33. int64(info.NumCores*1000),
  34. resource.DecimalSI),
  35. v1.ResourceMemory: *resource.NewQuantity(
  36. int64(info.MemoryCapacity),
  37. resource.BinarySI),
  38. }
  39. // if huge pages are enabled, we report them as a schedulable resource on the node
  40. for _, hugepagesInfo := range info.HugePages {
  41. pageSizeBytes := int64(hugepagesInfo.PageSize * 1024)
  42. hugePagesBytes := pageSizeBytes * int64(hugepagesInfo.NumPages)
  43. pageSizeQuantity := resource.NewQuantity(pageSizeBytes, resource.BinarySI)
  44. c[v1helper.HugePageResourceName(*pageSizeQuantity)] = *resource.NewQuantity(hugePagesBytes, resource.BinarySI)
  45. }
  46. return c
  47. }
  48. // EphemeralStorageCapacityFromFsInfo returns the capacity of the ephemeral storage from the FsInfo.
  49. func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceList {
  50. c := v1.ResourceList{
  51. v1.ResourceEphemeralStorage: *resource.NewQuantity(
  52. int64(info.Capacity),
  53. resource.BinarySI),
  54. }
  55. return c
  56. }
  57. // UsingLegacyCadvisorStats returns true if container stats are provided by cadvisor instead of through the CRI.
  58. // CRI integrations should get container metrics via CRI. Docker
  59. // uses the built-in cadvisor to gather such metrics on Linux for
  60. // historical reasons.
  61. // TODO: cri-o relies on cadvisor as a temporary workaround. The code should
  62. // be removed. Related issue:
  63. // https://github.com/kubernetes/kubernetes/issues/51798
  64. func UsingLegacyCadvisorStats(runtime, runtimeEndpoint string) bool {
  65. return (runtime == kubetypes.DockerContainerRuntime && goruntime.GOOS == "linux") ||
  66. runtimeEndpoint == CrioSocket || runtimeEndpoint == "unix://"+CrioSocket
  67. }