service.go 2.8 KB

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