service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package kubemodel
  2. import "time"
  3. type ServiceType string
  4. const (
  5. ServiceTypeClusterIP ServiceType = "ClusterIP"
  6. ServiceTypeNodePort ServiceType = "NodePort"
  7. ServiceTypeLoadBalancer ServiceType = "LoadBalancer"
  8. ServiceTypeExternalName ServiceType = "ExternalName"
  9. )
  10. type ServicePort struct {
  11. Name string `json:"name"`
  12. Port uint16 `json:"port"`
  13. TargetPort uint16 `json:"targetPort"`
  14. NodePort uint16 `json:"nodePort"`
  15. Protocol string `json:"protocol"`
  16. }
  17. // Service represents a Kubernetes Service with network traffic tracking for cost allocation.
  18. //
  19. // Network Cost Allocation Strategy:
  20. // Services expose applications and route traffic, incurring costs for:
  21. // 1. Load Balancers (LoadBalancer type) - Cloud provider LB hourly cost + data transfer
  22. // 2. Data Transfer - Egress charges based on NetworkTransferBytes
  23. // 3. Public IPs (for LoadBalancer/NodePort with external IPs)
  24. //
  25. // Cost Attribution Flow:
  26. // - LoadBalancer Services: Direct cloud resource cost (e.g., AWS ELB, GCP LB) allocated to service
  27. // - Data Transfer: NetworkTransferBytes × cloud provider egress rate (varies by region/destination)
  28. // - NetworkReceiveBytes: Typically free (ingress), tracked for visibility
  29. // - Use Selector to map service costs to backing pods/containers proportionally
  30. //
  31. // Example: AWS Application Load Balancer
  32. // - Fixed hourly cost: $0.0225/hour
  33. // - LCU cost: $0.008/hour per LCU (based on connections, requests, bandwidth)
  34. // - Data transfer: $0.09/GB for internet egress
  35. // Total Service Cost = (LB hours × hourly rate) + (LCU hours × LCU rate) + (NetworkTransferBytes × transfer rate)
  36. type Service struct {
  37. UID string `json:"uid"`
  38. NamespaceUID string `json:"namespaceUid"`
  39. Name string `json:"name"`
  40. Type ServiceType `json:"type"`
  41. Hostname string `json:"hostname,omitempty"`
  42. Labels map[string]string `json:"labels,omitempty"`
  43. Annotations map[string]string `json:"annotations,omitempty"`
  44. Ports []ServicePort `json:"ports,omitempty"`
  45. Start time.Time `json:"start"`
  46. End time.Time `json:"end"`
  47. NetworkTransferBytes Measurement `json:"networkTransferBytes"`
  48. NetworkReceiveBytes Measurement `json:"networkReceiveBytes"`
  49. // Label selector to identify pods/containers targeted by this service
  50. // Maps label keys to values (e.g., {"app": "nginx", "tier": "frontend"})
  51. // Pods with matching labels will receive traffic from this service
  52. Selector map[string]string `json:"selector,omitempty"`
  53. // Lifecycle tracking
  54. DurationSeconds Measurement `json:"durationSeconds"` // Duration service existed within measurement window
  55. }