container.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. CPUCoreUsageAvg float64 `json:"cpuCoreUsageAvg"`
  13. CPUCoreUsageMax float64 `json:"cpuCoreUsageMax"`
  14. RAMBytesUsageAvg float64 `json:"ramBytesUsageAvg"`
  15. RAMBytesUsageMax float64 `json:"ramBytesUsageMax"`
  16. Start time.Time `json:"start"`
  17. End time.Time `json:"end"`
  18. }
  19. func (c *Container) GetKey() string {
  20. return fmt.Sprintf("%s/%s", c.PodUID, c.Name)
  21. }
  22. func (c *Container) ValidateContainer(window Window) error {
  23. if c.PodUID == "" {
  24. return fmt.Errorf("PodUID is missing for Container with name '%s'", c.Name)
  25. }
  26. if c.Name == "" {
  27. return fmt.Errorf("Name is missing for Container on pod '%s'", c.PodUID)
  28. }
  29. if err := checkWindow(window, c.Start, c.End); err != nil {
  30. return err
  31. }
  32. return nil
  33. }
  34. func (kms *KubeModelSet) RegisterContainer(container *Container) error {
  35. if err := container.ValidateContainer(kms.Window); err != nil {
  36. err = fmt.Errorf("RegisterContainer: invalid container: %w", err)
  37. kms.Error(err)
  38. return err
  39. }
  40. key := container.GetKey()
  41. if _, ok := kms.Containers[key]; !ok {
  42. kms.Containers[key] = container
  43. kms.Metadata.ObjectCount++
  44. }
  45. return nil
  46. }