node.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. InstanceType string `json:"instanceType"`
  16. Preemptible bool `json:"preemptible"` // TODO unpopulated
  17. ResourceCapacities ResourceQuantities `json:"resourceCapacities"`
  18. ResourcesAllocatable ResourceQuantities `json:"resourcesAllocatable"`
  19. FileSystem FileSystem `json:"fileSystem"`
  20. Start time.Time `json:"start"`
  21. End time.Time `json:"end"`
  22. }
  23. // @bingen:generate:FileSystem
  24. // FileSystem records information for a nodes local storage
  25. type FileSystem struct {
  26. CapacityBytes float64 `json:"capacityBytes"` // Total capacity of the volume in bytes
  27. UsageByteAvg float64 `json:"usageByteAvg"`
  28. UsageByteMax float64 `json:"usageByteMax"`
  29. }
  30. func (n *Node) ValidateNode(window Window) error {
  31. if n.UID == "" {
  32. return fmt.Errorf("UID is missing for Node with name '%s'", n.Name)
  33. }
  34. if n.Name == "" {
  35. return fmt.Errorf("Name is missing for Node '%s'", n.UID)
  36. }
  37. if err := checkWindow(window, n.Start, n.End); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. // RegisterNode validates and adds a node to the set
  43. func (kms *KubeModelSet) RegisterNode(node *Node) error {
  44. if err := node.ValidateNode(kms.Window); err != nil {
  45. err = fmt.Errorf("RegisterNode: invalid node: %w", err)
  46. kms.Error(err)
  47. return err
  48. }
  49. if _, ok := kms.Nodes[node.UID]; !ok {
  50. if kms.Cluster == nil {
  51. kms.Warnf("RegisterNode: Cluster is nil")
  52. }
  53. kms.Nodes[node.UID] = node
  54. kms.Metadata.ObjectCount++
  55. }
  56. return nil
  57. }