kubemodel.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package kubemodel
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // @bingen:generate[streamable,stringtable]:KubeModelSet
  7. type KubeModelSet struct {
  8. Metadata *Metadata `json:"meta"` // @bingen:field[version=1]
  9. Window Window `json:"window"` // @bingen:field[version=1]
  10. Cluster *Cluster `json:"cluster"` // @bingen:field[version=1]
  11. Namespaces map[string]*Namespace `json:"namespaces"` // @bingen:field[version=1]
  12. ResourceQuotas map[string]*ResourceQuota `json:"resourceQuotas"` // @bingen:field[version=1]
  13. Services map[string]*Service `json:"services"` // @bingen:field[version=2]
  14. Deployments map[string]*Deployment `json:"deployments"` // @bingen:field[version=2]
  15. StatefulSets map[string]*StatefulSet `json:"statefulSets"` // @bingen:field[version=2]
  16. DaemonSets map[string]*DaemonSet `json:"daemonSets"` // @bingen:field[version=2]
  17. Jobs map[string]*Job `json:"jobs"` // @bingen:field[version=2]
  18. CronJobs map[string]*CronJob `json:"cronJobs"` // @bingen:field[version=2]
  19. ReplicaSets map[string]*ReplicaSet `json:"replicaSets"` // @bingen:field[version=2]
  20. Nodes map[string]*Node `json:"nodes"` // @bingen:field[version=2]
  21. PersistentVolumes map[string]*PersistentVolume `json:"persistentVolumes"` // @bingen:field[version=2]
  22. PersistentVolumeClaims map[string]*PersistentVolumeClaim `json:"pvcs"` // @bingen:field[version=2]
  23. Pods map[string]*Pod `json:"pods"` // @bingen:field[version=2]
  24. Containers map[string]*Container `json:"containers"` // @bingen:field[version=2]
  25. DCGMDevices map[string]*DCGMDevice `json:"dcgmDevices"` // @bingen:field[version=2]
  26. }
  27. func NewKubeModelSet(start time.Time, end time.Time) *KubeModelSet {
  28. now := time.Now().UTC()
  29. kms := &KubeModelSet{
  30. Metadata: &Metadata{
  31. CreatedAt: now,
  32. CompletedAt: now, // Will be updated when processing completes
  33. DiagnosticLevel: DefaultDiagnosticLevel,
  34. },
  35. Window: Window{
  36. Start: start,
  37. End: end,
  38. },
  39. Containers: map[string]*Container{},
  40. Deployments: map[string]*Deployment{},
  41. StatefulSets: map[string]*StatefulSet{},
  42. DaemonSets: map[string]*DaemonSet{},
  43. Jobs: map[string]*Job{},
  44. CronJobs: map[string]*CronJob{},
  45. ReplicaSets: map[string]*ReplicaSet{},
  46. Namespaces: map[string]*Namespace{},
  47. Nodes: map[string]*Node{},
  48. DCGMDevices: map[string]*DCGMDevice{},
  49. Pods: map[string]*Pod{},
  50. PersistentVolumeClaims: map[string]*PersistentVolumeClaim{},
  51. ResourceQuotas: map[string]*ResourceQuota{},
  52. Services: map[string]*Service{},
  53. PersistentVolumes: map[string]*PersistentVolume{},
  54. }
  55. return kms
  56. }
  57. // IsEmpty returns true if the KubeModelSet is nil, has no cluster, or contains no resources
  58. func (kms *KubeModelSet) IsEmpty() bool {
  59. if kms == nil || kms.Cluster == nil {
  60. return true
  61. }
  62. // Check if all resource maps are empty
  63. return len(kms.Containers) == 0 &&
  64. len(kms.Deployments) == 0 &&
  65. len(kms.StatefulSets) == 0 &&
  66. len(kms.DaemonSets) == 0 &&
  67. len(kms.Jobs) == 0 &&
  68. len(kms.CronJobs) == 0 &&
  69. len(kms.ReplicaSets) == 0 &&
  70. len(kms.Namespaces) == 0 &&
  71. len(kms.Nodes) == 0 &&
  72. len(kms.DCGMDevices) == 0 &&
  73. len(kms.Pods) == 0 &&
  74. len(kms.PersistentVolumeClaims) == 0 &&
  75. len(kms.ResourceQuotas) == 0 &&
  76. len(kms.Services) == 0 &&
  77. len(kms.PersistentVolumes) == 0
  78. }
  79. // checkWindow validates that the given start/end times are fully contained within
  80. // the KubeModelSet window. It records and returns an error if they are not.
  81. func checkWindow(window Window, start, end time.Time) error {
  82. if window.Start.After(start) ||
  83. window.Start.After(end) ||
  84. window.End.Before(start) ||
  85. window.End.Before(end) {
  86. return fmt.Errorf(
  87. "start or end time (%s-%s) is outside of the window %s-%s",
  88. start.Format(time.RFC3339),
  89. end.Format(time.RFC3339),
  90. window.Start.Format(time.RFC3339),
  91. window.End.Format(time.RFC3339),
  92. )
  93. }
  94. return nil
  95. }