util.go 2.6 KB

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