node.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package kubemodel
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // @bingen:generate:Node
  7. // Node represents a Kubernetes node with capacity-based resource tracking.
  8. // All resource measures (CPU, RAM) represent node capacity, not requests or limits.
  9. // This aligns with the principle that cost allocation should be based on provisioned capacity.
  10. type Node struct {
  11. UID string `json:"uid"`
  12. ProviderID string `json:"providerId"`
  13. Name string `json:"name"`
  14. Labels map[string]string `json:"labels"`
  15. ResourceCapacities ResourceQuantities `json:"resourceCapacities"`
  16. ResourcesAllocatable ResourceQuantities `json:"resourcesAllocatable"`
  17. FileSystem FileSystem `json:"fileSystem"`
  18. Start time.Time `json:"start"`
  19. End time.Time `json:"end"`
  20. }
  21. // @bingen:generate:FileSystem
  22. // FileSystem records information for a nodes local storage
  23. type FileSystem struct {
  24. CapacityBytes float64 `json:"capacityBytes"` // Total capacity of the volume in bytes
  25. UsageByteAvg float64 `json:"usageByteAvg"`
  26. UsageByteMax float64 `json:"usageByteMax"`
  27. }
  28. func (n *Node) ValidateNode(window Window) error {
  29. if n.UID == "" {
  30. return fmt.Errorf("UID is missing for Node with name '%s'", n.Name)
  31. }
  32. if n.Name == "" {
  33. return fmt.Errorf("Name is missing for Node '%s'", n.UID)
  34. }
  35. if err := checkWindow(window, n.Start, n.End); err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. // RegisterNode validates and adds a node to the set
  41. func (kms *KubeModelSet) RegisterNode(node *Node) error {
  42. if err := node.ValidateNode(kms.Window); err != nil {
  43. err = fmt.Errorf("RegisterNode: invalid node: %w", err)
  44. kms.Error(err)
  45. return err
  46. }
  47. if _, ok := kms.Nodes[node.UID]; !ok {
  48. if kms.Cluster == nil {
  49. kms.Warnf("RegisterNode: Cluster is nil")
  50. }
  51. kms.Nodes[node.UID] = node
  52. kms.Metadata.ObjectCount++
  53. }
  54. return nil
  55. }