container.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package kubemodel
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // @bingen:generate:Container
  7. type Container struct {
  8. PodUID string `json:"podUid"`
  9. Name string `json:"name"`
  10. ResourceRequests ResourceQuantities `json:"resourceRequests"`
  11. ResourceLimits ResourceQuantities `json:"resourceLimits"`
  12. CPUCoreAllocationAvg float64 `json:"cpuCoreAllocationAvg"`
  13. CPUCoreUsageAvg float64 `json:"cpuCoreUsageAvg"`
  14. CPUCoreUsageMax float64 `json:"cpuCoreUsageMax"`
  15. RAMBytesAllocationAvg float64 `json:"ramBytesAllocationAvg"`
  16. RAMBytesUsageAvg float64 `json:"ramBytesUsageAvg"`
  17. RAMBytesUsageMax float64 `json:"ramBytesUsageMax"`
  18. DeviceUsages map[string]DeviceUsage `json:"deviceUsages"` // @bingen:field[version=3]
  19. Start time.Time `json:"start"`
  20. End time.Time `json:"end"`
  21. }
  22. // DeviceUsage holds usage metrics for a single container/device pairing. The shape is
  23. // vendor-agnostic, but the only populating source currently implemented is the DCGM exporter.
  24. // It is keyed by Device.UUID under Container.DeviceUsages.
  25. // @bingen:generate:DeviceUsage
  26. type DeviceUsage struct {
  27. UsageAvg float64 `json:"usageAvg"`
  28. UsageMax float64 `json:"usageMax"`
  29. }
  30. func (c *Container) ValidateContainer(window Window) error {
  31. if c.PodUID == "" {
  32. return fmt.Errorf("PodUID is missing for Container with name '%s'", c.Name)
  33. }
  34. if c.Name == "" {
  35. return fmt.Errorf("Name is missing for Container on pod '%s'", c.PodUID)
  36. }
  37. if err := checkWindow(window, c.Start, c.End); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func (kms *KubeModelSet) RegisterContainer(container *Container) error {
  43. if err := container.ValidateContainer(kms.Window); err != nil {
  44. err = fmt.Errorf("RegisterContainer: invalid container: %w", err)
  45. kms.Error(err)
  46. return err
  47. }
  48. key := container.GetKey()
  49. if _, ok := kms.Containers[key]; !ok {
  50. kms.Containers[key] = container
  51. kms.Metadata.ObjectCount++
  52. }
  53. return nil
  54. }
  55. func (c *Container) GetKey() string {
  56. return ContainerKey(c.PodUID, c.Name)
  57. }
  58. func ContainerKey(podUID, containerName string) string {
  59. return fmt.Sprintf("%s/%s", podUID, containerName)
  60. }