network.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package pricing
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  6. "github.com/opencost/opencost/core/pkg/model/shared"
  7. )
  8. type NetworkPricing struct {
  9. Properties NetworkPricingProperties `json:"properties" yaml:"properties"`
  10. Prices Prices `json:"prices" yaml:"prices"`
  11. }
  12. func (sp *NetworkPricing) String() string {
  13. return sp.Properties.String() + "|" + sp.Prices.canonical()
  14. }
  15. type NetworkPricingProperties struct {
  16. Provider shared.Provider `json:"provider,omitempty" yaml:"provider,omitempty"`
  17. TrafficDirection kubemodel.TrafficDirection `json:"trafficDirection,omitempty" yaml:"trafficDirection,omitempty"`
  18. TrafficType kubemodel.TrafficType `json:"trafficType,omitempty" yaml:"trafficType,omitempty"`
  19. IsNatGateway bool `json:"isNatGateway,omitempty" yaml:"isNatGateway,omitempty"`
  20. Start *time.Time `json:"start,omitempty" yaml:"start,omitempty"`
  21. End *time.Time `json:"end,omitempty" yaml:"end,omitempty"`
  22. }
  23. func (sp *NetworkPricingProperties) String() string {
  24. return fmt.Sprintf("%s:%s:%s:nat=%t:%s",
  25. sp.Provider,
  26. sp.TrafficDirection,
  27. sp.TrafficType,
  28. sp.IsNatGateway,
  29. sp.timeKey(),
  30. )
  31. }
  32. func (sp *NetworkPricingProperties) timeKey() string {
  33. s := "nil"
  34. e := "nil"
  35. if sp.Start != nil {
  36. s = sp.Start.UTC().Format(time.RFC3339Nano)
  37. }
  38. if sp.End != nil {
  39. e = sp.End.UTC().Format(time.RFC3339Nano)
  40. }
  41. return fmt.Sprintf("%s:%s", s, e)
  42. }