2
0

node.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package pricing
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/cloud"
  8. )
  9. type NodePricing struct {
  10. Properties NodePricingProperties `json:"properties" yaml:"properties"`
  11. Prices Prices `json:"prices" yaml:"prices"`
  12. }
  13. func (np *NodePricing) String() string {
  14. return np.Properties.String() + "|" + np.Prices.canonical()
  15. }
  16. type NodePricingProperties struct {
  17. Provider cloud.Provider `json:"provider,omitempty" yaml:"provider,omitempty"`
  18. Region string `json:"region,omitempty" yaml:"region,omitempty"`
  19. InstanceType string `json:"instanceType,omitempty" yaml:"instanceType,omitempty"`
  20. Provisioning ProvisioningType `json:"provisioning,omitempty" yaml:"provisioning,omitempty"`
  21. Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"`
  22. ProviderID string `json:"providerID,omitempty" yaml:"providerID,omitempty"`
  23. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
  24. Start *time.Time `json:"start,omitempty" yaml:"start,omitempty"`
  25. End *time.Time `json:"end,omitempty" yaml:"end,omitempty"`
  26. }
  27. func (np *NodePricingProperties) String() string {
  28. return fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%s",
  29. np.Provider,
  30. np.Region,
  31. np.InstanceType,
  32. np.Provisioning,
  33. np.Cluster,
  34. np.ProviderID,
  35. np.labelsKey(),
  36. np.timeKey(),
  37. )
  38. }
  39. func (np *NodePricingProperties) labelsKey() string {
  40. if len(np.Labels) == 0 {
  41. return ""
  42. }
  43. keys := make([]string, 0, len(np.Labels))
  44. for k := range np.Labels {
  45. keys = append(keys, k)
  46. }
  47. sort.Strings(keys)
  48. var b strings.Builder
  49. for i, k := range keys {
  50. if i > 0 {
  51. b.WriteByte(':')
  52. }
  53. b.WriteString(k)
  54. b.WriteByte('=')
  55. b.WriteString(np.Labels[k])
  56. }
  57. return b.String()
  58. }
  59. func (np *NodePricingProperties) timeKey() string {
  60. s := "nil"
  61. e := "nil"
  62. if np.Start != nil {
  63. s = np.Start.UTC().Format(time.RFC3339Nano)
  64. }
  65. if np.End != nil {
  66. e = np.End.UTC().Format(time.RFC3339Nano)
  67. }
  68. return fmt.Sprintf("%s:%s", s, e)
  69. }