module.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package basic
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/opencost/opencost/core/pkg/log"
  7. "github.com/opencost/opencost/core/pkg/pricing"
  8. "github.com/opencost/opencost/core/pkg/reader"
  9. "github.com/opencost/opencost/core/pkg/unit"
  10. )
  11. type PricingModule struct {
  12. currency unit.Currency
  13. store pricing.PricingStore
  14. }
  15. func NewBasicPricingModule(store pricing.PricingStore) (*PricingModule, error) {
  16. pricingSet, err := store.GetPricingSet(context.Background())
  17. if err != nil {
  18. return nil, fmt.Errorf("checking pricing store: %w", err)
  19. }
  20. if pricingSet.IsEmpty() {
  21. // Populate store with a default pricing set.
  22. err := store.SetPricingSet(context.Background(), GetDefaultPricingSet())
  23. if err != nil {
  24. return nil, fmt.Errorf("setting default pricing: %w", err)
  25. }
  26. pricingSet, err = store.GetPricingSet(context.Background())
  27. if err != nil {
  28. return nil, fmt.Errorf("checking default pricing: %w", err)
  29. }
  30. if pricingSet.IsEmpty() {
  31. return nil, errors.New("unable to initialize store")
  32. }
  33. }
  34. currencies := pricingSet.Currencies()
  35. if len(currencies) > 0 {
  36. log.Warnf("detected multiple currencies in basic pricing module (%v): defaulting to %s", currencies, currencies[0])
  37. }
  38. pm := &PricingModule{
  39. currency: currencies[0],
  40. store: store,
  41. }
  42. return pm, nil
  43. }
  44. func (pm *PricingModule) GetCurrency() unit.Currency {
  45. return pm.currency
  46. }
  47. func (pm *PricingModule) SetCurrency(ctx context.Context, currency unit.Currency) error {
  48. prevCurrency := pm.currency
  49. if currency == prevCurrency {
  50. return nil
  51. }
  52. // 1. Convert existing node pricing to new currency
  53. np, err := pm.getNodePricing(ctx)
  54. if err != nil {
  55. return fmt.Errorf("getting node pricing: %w", err)
  56. }
  57. // Set up new Prices for the new currency
  58. newPrices := []pricing.Price{}
  59. // Convert all existing prices to the new currency
  60. oldPrices, ok := np.Prices[prevCurrency]
  61. if !ok {
  62. log.Warnf("setting currency to '%s': no node prices found for existing currency '%s'", currency, pm.currency)
  63. // There are no prices for the current currency.
  64. // Set default prices using the new currency.
  65. newPrices = GetDefaultNodePricing().Prices[unit.USD]
  66. }
  67. for _, price := range oldPrices {
  68. newPrices = append(newPrices, pricing.Price{
  69. Currency: currency,
  70. Unit: price.Unit,
  71. Price: price.Price,
  72. })
  73. }
  74. // Set new prices under new currency
  75. np.Prices = make(pricing.Prices, 1)
  76. np.Prices[currency] = newPrices
  77. // Set node pricing on the module
  78. err = pm.setNodePricing(ctx, np)
  79. if err != nil {
  80. return fmt.Errorf("setting node pricing: %w", err)
  81. }
  82. // 2. Convert existing volume pricing to new currency
  83. vp, err := pm.getVolumePricing(ctx)
  84. if err != nil {
  85. return fmt.Errorf("getting node pricing: %w", err)
  86. }
  87. // Set up new Prices for the new currency
  88. newPrices = []pricing.Price{}
  89. // Convert all existing prices to the new currency
  90. oldPrices, ok = vp.Prices[prevCurrency]
  91. if !ok {
  92. log.Warnf("setting currency to '%s': no node prices found for existing currency '%s'", currency, pm.currency)
  93. // There are no prices for the current currency.
  94. // Set default prices using the new currency.
  95. newPrices = GetDefaultVolumePricing().Prices[unit.USD]
  96. }
  97. for _, price := range oldPrices {
  98. newPrices = append(newPrices, pricing.Price{
  99. Currency: currency,
  100. Unit: price.Unit,
  101. Price: price.Price,
  102. })
  103. }
  104. // Set new prices under new currency
  105. vp.Prices = make(pricing.Prices, 1)
  106. vp.Prices[currency] = newPrices
  107. // Set node pricing on the module
  108. err = pm.setVolumePricing(ctx, vp)
  109. if err != nil {
  110. return fmt.Errorf("setting node pricing: %w", err)
  111. }
  112. return nil
  113. }
  114. func (pm *PricingModule) SetNodePricePerCPUCoreHour(ctx context.Context, price float64) error {
  115. return pm.setNodePrice(ctx, unit.VCPUHour, price)
  116. }
  117. func (pm *PricingModule) SetNodePricePerRAMGiBHour(ctx context.Context, price float64) error {
  118. return pm.setNodePrice(ctx, unit.RAMGiBHour, price)
  119. }
  120. func (pm *PricingModule) SetNodePricePerGPUHour(ctx context.Context, price float64) error {
  121. return pm.setNodePrice(ctx, unit.GPUHour, price)
  122. }
  123. func (pm *PricingModule) SetNodePricePerLocalDiskGiBHour(ctx context.Context, price float64) error {
  124. return pm.setNodePrice(ctx, unit.StorageGiBHour, price)
  125. }
  126. func (pm *PricingModule) SetVolumePricePerStorageGiBHour(ctx context.Context, price float64) error {
  127. return pm.setVolumePrice(ctx, unit.StorageGiBHour, price)
  128. }
  129. func (pm *PricingModule) NewNodePricingReader(ctx context.Context) (reader.Reader[*pricing.NodePricing], error) {
  130. np, err := pm.getNodePricing(ctx)
  131. if err != nil {
  132. return nil, fmt.Errorf("getting node pricing: %w", err)
  133. }
  134. return reader.NewSliceReader([]*pricing.NodePricing{np}), nil
  135. }
  136. func (pm *PricingModule) NewVolumePricingReader(ctx context.Context) (reader.Reader[*pricing.VolumePricing], error) {
  137. vp, err := pm.getVolumePricing(ctx)
  138. if err != nil {
  139. return nil, fmt.Errorf("getting volume pricing: %w", err)
  140. }
  141. return reader.NewSliceReader([]*pricing.VolumePricing{vp}), nil
  142. }
  143. func (pm *PricingModule) setNodePrice(ctx context.Context, unit unit.Unit, price float64) error {
  144. np, err := pm.getNodePricing(ctx)
  145. if err != nil {
  146. return fmt.Errorf("getting node pricing: %w", err)
  147. }
  148. prices, ok := np.Prices[pm.currency]
  149. if !ok {
  150. log.Warnf("setting price per %s to '%f': no node prices found for existing currency '%s'", unit, price, pm.currency)
  151. // There are no prices for the current currency.
  152. // Set default prices using the new currency.
  153. np = GetDefaultNodePricing()
  154. }
  155. // Set the price with unit GiBHour to the given price
  156. for i, p := range prices {
  157. if p.Unit == unit {
  158. prices[i] = pricing.Price{
  159. Currency: p.Currency,
  160. Unit: p.Unit,
  161. Price: price,
  162. }
  163. }
  164. }
  165. // Set the new node pricing
  166. err = pm.setNodePricing(ctx, np)
  167. if err != nil {
  168. return fmt.Errorf("setting node pricing: %w", err)
  169. }
  170. return nil
  171. }
  172. func (pm *PricingModule) setVolumePrice(ctx context.Context, unit unit.Unit, price float64) error {
  173. vp, err := pm.getVolumePricing(ctx)
  174. if err != nil {
  175. return fmt.Errorf("getting volume pricing: %w", err)
  176. }
  177. prices, ok := vp.Prices[pm.currency]
  178. if !ok {
  179. log.Warnf("setting price per %s to '%f': no volume prices found for existing currency '%s'", unit, price, pm.currency)
  180. // There are no prices for the current currency.
  181. // Set default prices using the new currency.
  182. vp = GetDefaultVolumePricing()
  183. }
  184. // Set the price with unit GiBHour to the given price
  185. for i, p := range prices {
  186. if p.Unit == unit {
  187. prices[i] = pricing.Price{
  188. Currency: p.Currency,
  189. Unit: p.Unit,
  190. Price: price,
  191. }
  192. }
  193. }
  194. // Set the new volume pricing
  195. err = pm.setVolumePricing(ctx, vp)
  196. if err != nil {
  197. return fmt.Errorf("setting node pricing: %w", err)
  198. }
  199. return nil
  200. }
  201. func (pm *PricingModule) getNodePricing(ctx context.Context) (*pricing.NodePricing, error) {
  202. ps, err := pm.store.GetPricingSet(ctx)
  203. if err != nil {
  204. return nil, fmt.Errorf("getting pricing: %w", err)
  205. }
  206. if len(ps.Nodes) == 0 {
  207. return nil, errors.New("not found")
  208. }
  209. // Only one default NodePricing is allowed in basic pricing.
  210. // If multiple exist, return only the first one.
  211. return ps.Nodes[0], nil
  212. }
  213. func (pm *PricingModule) setNodePricing(ctx context.Context, np *pricing.NodePricing) error {
  214. if np == nil {
  215. return errors.New("nil node pricing")
  216. }
  217. // Make sure precisely one currency is set
  218. currs := np.GetCurrencies()
  219. if len(currs) == 0 {
  220. return errors.New("pricing is empty")
  221. }
  222. if len(currs) > 1 {
  223. return fmt.Errorf("setting multiple currencies: %v", currs)
  224. }
  225. // Update PricingModule to use given currency
  226. pm.currency = currs[0]
  227. // Get the pricing set
  228. ps, err := pm.store.GetPricingSet(ctx)
  229. if err != nil {
  230. return fmt.Errorf("getting pricing: %w", err)
  231. }
  232. // Only one default NodePricing is allowed in basic pricing.
  233. ps.Nodes = []*pricing.NodePricing{np}
  234. // Set the new pricing set
  235. err = pm.store.SetPricingSet(ctx, ps)
  236. if err != nil {
  237. return fmt.Errorf("setting pricing: %w", err)
  238. }
  239. return nil
  240. }
  241. func (pm *PricingModule) getVolumePricing(ctx context.Context) (*pricing.VolumePricing, error) {
  242. ps, err := pm.store.GetPricingSet(ctx)
  243. if err != nil {
  244. return nil, fmt.Errorf("getting pricing: %w", err)
  245. }
  246. if len(ps.Volumes) == 0 {
  247. return nil, errors.New("not found")
  248. }
  249. // Only one default VolumePricing is allowed in basic pricing.
  250. // If multiple exist, return only the first one.
  251. return ps.Volumes[0], nil
  252. }
  253. func (pm *PricingModule) setVolumePricing(ctx context.Context, vp *pricing.VolumePricing) error {
  254. if vp == nil {
  255. return errors.New("nil volume pricing")
  256. }
  257. // Make sure precisely one currency is set
  258. currs := vp.GetCurrencies()
  259. if len(currs) == 0 {
  260. return errors.New("pricing is empty")
  261. }
  262. if len(currs) > 1 {
  263. return fmt.Errorf("setting multiple currencies: %v", currs)
  264. }
  265. // Update PricingModule to use given currency
  266. pm.currency = currs[0]
  267. // Get the pricing set
  268. ps, err := pm.store.GetPricingSet(ctx)
  269. if err != nil {
  270. return fmt.Errorf("getting pricing: %w", err)
  271. }
  272. // Only one default VolumePricing is allowed in basic pricing.
  273. ps.Volumes = []*pricing.VolumePricing{vp}
  274. // Set the new pricing set
  275. err = pm.store.SetPricingSet(ctx, ps)
  276. if err != nil {
  277. return fmt.Errorf("setting pricing: %w", err)
  278. }
  279. return nil
  280. }