module.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package basic
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "sync"
  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. // PricingModule must satisfy the pricing.PricingModule interface
  12. var _ pricing.PricingModule = (*PricingModule)(nil)
  13. const SourceKind = "basic"
  14. const SourceName = "basic"
  15. type PricingModule struct {
  16. mu sync.RWMutex
  17. store pricing.PricingStore
  18. }
  19. func NewBasicPricingModule(store pricing.PricingStore) (*PricingModule, error) {
  20. pricingSet, err := store.GetPricingSet(context.Background())
  21. if err != nil {
  22. return nil, fmt.Errorf("checking pricing store: %w", err)
  23. }
  24. if pricingSet.IsEmpty() {
  25. // Populate store with a default pricing set.
  26. err := store.SetPricingSet(context.Background(), GetDefaultPricingSet())
  27. if err != nil {
  28. return nil, fmt.Errorf("setting default pricing: %w", err)
  29. }
  30. pricingSet, err = store.GetPricingSet(context.Background())
  31. if err != nil {
  32. return nil, fmt.Errorf("checking default pricing: %w", err)
  33. }
  34. if pricingSet.IsEmpty() {
  35. return nil, errors.New("unable to initialize store")
  36. }
  37. }
  38. pm := &PricingModule{
  39. store: store,
  40. }
  41. return pm, nil
  42. }
  43. func (pm *PricingModule) NewClusterPricingReader(ctx context.Context) (reader.Reader[*pricing.ClusterPricing], error) {
  44. pm.mu.RLock()
  45. defer pm.mu.RUnlock()
  46. cp, err := pm.getClusterPricing(ctx)
  47. if err != nil {
  48. return nil, fmt.Errorf("getting node pricing: %w", err)
  49. }
  50. return reader.NewSliceReader([]*pricing.ClusterPricing{cp}), nil
  51. }
  52. func (pm *PricingModule) NewNetworkPricingReader(ctx context.Context) (reader.Reader[*pricing.NetworkPricing], error) {
  53. pm.mu.RLock()
  54. defer pm.mu.RUnlock()
  55. np, err := pm.getNetworkPricing(ctx)
  56. if err != nil {
  57. return nil, fmt.Errorf("getting node pricing: %w", err)
  58. }
  59. return reader.NewSliceReader([]*pricing.NetworkPricing{np}), nil
  60. }
  61. func (pm *PricingModule) NewNodePricingReader(ctx context.Context) (reader.Reader[*pricing.NodePricing], error) {
  62. pm.mu.RLock()
  63. defer pm.mu.RUnlock()
  64. np, err := pm.getNodePricing(ctx)
  65. if err != nil {
  66. return nil, fmt.Errorf("getting node pricing: %w", err)
  67. }
  68. return reader.NewSliceReader([]*pricing.NodePricing{np}), nil
  69. }
  70. func (pm *PricingModule) NewPersistentVolumePricingReader(ctx context.Context) (reader.Reader[*pricing.PersistentVolumePricing], error) {
  71. pm.mu.RLock()
  72. defer pm.mu.RUnlock()
  73. pvp, err := pm.getPersistentVolumePricing(ctx)
  74. if err != nil {
  75. return nil, fmt.Errorf("getting volume pricing: %w", err)
  76. }
  77. return reader.NewSliceReader([]*pricing.PersistentVolumePricing{pvp}), nil
  78. }
  79. func (pm *PricingModule) NewServicePricingReader(ctx context.Context) (reader.Reader[*pricing.ServicePricing], error) {
  80. pm.mu.RLock()
  81. defer pm.mu.RUnlock()
  82. sp, err := pm.getServicePricing(ctx)
  83. if err != nil {
  84. return nil, fmt.Errorf("getting service pricing: %w", err)
  85. }
  86. return reader.NewSliceReader([]*pricing.ServicePricing{sp}), nil
  87. }
  88. func (pm *PricingModule) GetPricingSet(ctx context.Context) (*pricing.PricingSet, error) {
  89. pm.mu.RLock()
  90. defer pm.mu.RUnlock()
  91. return pm.store.GetPricingSet(ctx)
  92. }
  93. func (pm *PricingModule) SourceKind() string {
  94. return SourceKind
  95. }
  96. func (pm *PricingModule) SourceName() string {
  97. return SourceName
  98. }
  99. func (pm *PricingModule) Checksum(ctx context.Context) (string, error) {
  100. pricingSet, err := pm.store.GetPricingSet(ctx)
  101. if err != nil {
  102. return "", fmt.Errorf("basic pricing module: error getting pricing set: %w", err)
  103. }
  104. checksum, err := pricingSet.Checksum()
  105. if err != nil {
  106. return "", fmt.Errorf("basic pricing module: error computing checksum: %s", err)
  107. }
  108. return checksum, nil
  109. }
  110. // Public CRUD functions
  111. func (pm *PricingModule) SetClusterPricePerHour(ctx context.Context, price float64) error {
  112. pm.mu.Lock()
  113. defer pm.mu.Unlock()
  114. return pm.setClusterPrice(ctx, pricing.ResourceCluster, unit.Hour, price)
  115. }
  116. func (pm *PricingModule) SetNetworkLocalEgressPricePerGiB(ctx context.Context, price float64) error {
  117. pm.mu.Lock()
  118. defer pm.mu.Unlock()
  119. return pm.setNetworkPrice(ctx, pricing.ResourceLocalEgress, unit.GiB, price)
  120. }
  121. func (pm *PricingModule) SetNetworkCrossZoneEgressPricePerGiB(ctx context.Context, price float64) error {
  122. pm.mu.Lock()
  123. defer pm.mu.Unlock()
  124. return pm.setNetworkPrice(ctx, pricing.ResourceCrossZoneEgress, unit.GiB, price)
  125. }
  126. func (pm *PricingModule) SetNetworkCrossRegionEgressPricePerGiB(ctx context.Context, price float64) error {
  127. pm.mu.Lock()
  128. defer pm.mu.Unlock()
  129. return pm.setNetworkPrice(ctx, pricing.ResourceCrossRegionEgress, unit.GiB, price)
  130. }
  131. func (pm *PricingModule) SetNetworkInternetEgressPricePerGiB(ctx context.Context, price float64) error {
  132. pm.mu.Lock()
  133. defer pm.mu.Unlock()
  134. return pm.setNetworkPrice(ctx, pricing.ResourceInternetEgress, unit.GiB, price)
  135. }
  136. func (pm *PricingModule) SetNetworkNATGatewayEgressPricePerGiB(ctx context.Context, price float64) error {
  137. pm.mu.Lock()
  138. defer pm.mu.Unlock()
  139. return pm.setNetworkPrice(ctx, pricing.ResourceNATGatewayEgress, unit.GiB, price)
  140. }
  141. func (pm *PricingModule) SetNetworkNATGatewayIngressPricePerGiB(ctx context.Context, price float64) error {
  142. pm.mu.Lock()
  143. defer pm.mu.Unlock()
  144. return pm.setNetworkPrice(ctx, pricing.ResourceNATGatewayIngress, unit.GiB, price)
  145. }
  146. func (pm *PricingModule) SetNodePricePerCPUCoreHour(ctx context.Context, price float64) error {
  147. pm.mu.Lock()
  148. defer pm.mu.Unlock()
  149. return pm.setNodePrice(ctx, pricing.ResourceCPU, unit.VCPUHour, price)
  150. }
  151. func (pm *PricingModule) SetNodePricePerRAMGiBHour(ctx context.Context, price float64) error {
  152. pm.mu.Lock()
  153. defer pm.mu.Unlock()
  154. return pm.setNodePrice(ctx, pricing.ResourceRAM, unit.GiBHour, price)
  155. }
  156. func (pm *PricingModule) SetNodePricePerGPUHour(ctx context.Context, price float64) error {
  157. pm.mu.Lock()
  158. defer pm.mu.Unlock()
  159. return pm.setNodePrice(ctx, pricing.ResourceGPU, unit.GPUHour, price)
  160. }
  161. func (pm *PricingModule) SetNodePricePerLocalDiskGiBHour(ctx context.Context, price float64) error {
  162. pm.mu.Lock()
  163. defer pm.mu.Unlock()
  164. return pm.setNodePrice(ctx, pricing.ResourceStorage, unit.GiBHour, price)
  165. }
  166. func (pm *PricingModule) SetPersistentVolumePricePerStorageGiBHour(ctx context.Context, price float64) error {
  167. pm.mu.Lock()
  168. defer pm.mu.Unlock()
  169. return pm.setPersistentVolumePrice(ctx, pricing.ResourceStorage, unit.GiBHour, price)
  170. }
  171. func (pm *PricingModule) SetServicePricePerHour(ctx context.Context, price float64) error {
  172. pm.mu.Lock()
  173. defer pm.mu.Unlock()
  174. return pm.setServicePrice(ctx, pricing.ResourceService, unit.Hour, price)
  175. }
  176. // Private functions to set a price by resource and unit
  177. func (pm *PricingModule) setClusterPrice(ctx context.Context, resource pricing.Resource, unit unit.Unit, price float64) error {
  178. cp, err := pm.getClusterPricing(ctx)
  179. if err != nil {
  180. return fmt.Errorf("getting cluster pricing: %w", err)
  181. }
  182. if cp.Prices == nil {
  183. cp.Prices = pricing.Prices{}
  184. }
  185. cp.Prices[resource] = pricing.Price{Unit: unit, Price: price}
  186. err = pm.setClusterPricing(ctx, cp)
  187. if err != nil {
  188. return fmt.Errorf("setting cluster pricing: %w", err)
  189. }
  190. return nil
  191. }
  192. func (pm *PricingModule) setNetworkPrice(ctx context.Context, resource pricing.Resource, unit unit.Unit, price float64) error {
  193. np, err := pm.getNetworkPricing(ctx)
  194. if err != nil {
  195. return fmt.Errorf("getting network pricing: %w", err)
  196. }
  197. if np.Prices == nil {
  198. np.Prices = pricing.Prices{}
  199. }
  200. np.Prices[resource] = pricing.Price{Unit: unit, Price: price}
  201. err = pm.setNetworkPricing(ctx, np)
  202. if err != nil {
  203. return fmt.Errorf("setting network pricing: %w", err)
  204. }
  205. return nil
  206. }
  207. func (pm *PricingModule) setNodePrice(ctx context.Context, resource pricing.Resource, unit unit.Unit, price float64) error {
  208. np, err := pm.getNodePricing(ctx)
  209. if err != nil {
  210. return fmt.Errorf("getting node pricing: %w", err)
  211. }
  212. if np.Prices == nil {
  213. np.Prices = pricing.Prices{}
  214. }
  215. np.Prices[resource] = pricing.Price{Unit: unit, Price: price}
  216. err = pm.setNodePricing(ctx, np)
  217. if err != nil {
  218. return fmt.Errorf("setting node pricing: %w", err)
  219. }
  220. return nil
  221. }
  222. func (pm *PricingModule) setPersistentVolumePrice(ctx context.Context, resource pricing.Resource, unit unit.Unit, price float64) error {
  223. vp, err := pm.getPersistentVolumePricing(ctx)
  224. if err != nil {
  225. return fmt.Errorf("getting volume pricing: %w", err)
  226. }
  227. if vp.Prices == nil {
  228. vp.Prices = pricing.Prices{}
  229. }
  230. vp.Prices[resource] = pricing.Price{Unit: unit, Price: price}
  231. err = pm.setPersistentVolumePricing(ctx, vp)
  232. if err != nil {
  233. return fmt.Errorf("setting volume pricing: %w", err)
  234. }
  235. return nil
  236. }
  237. func (pm *PricingModule) setServicePrice(ctx context.Context, resource pricing.Resource, unit unit.Unit, price float64) error {
  238. sp, err := pm.getServicePricing(ctx)
  239. if err != nil {
  240. return fmt.Errorf("getting service pricing: %w", err)
  241. }
  242. if sp.Prices == nil {
  243. sp.Prices = pricing.Prices{}
  244. }
  245. sp.Prices[resource] = pricing.Price{Unit: unit, Price: price}
  246. err = pm.setServicePricing(ctx, sp)
  247. if err != nil {
  248. return fmt.Errorf("setting service pricing: %w", err)
  249. }
  250. return nil
  251. }
  252. // Private functions to get and set pricing
  253. func (pm *PricingModule) getClusterPricing(ctx context.Context) (*pricing.ClusterPricing, error) {
  254. ps, err := pm.store.GetPricingSet(ctx)
  255. if err != nil {
  256. return nil, fmt.Errorf("getting pricing: %w", err)
  257. }
  258. if len(ps.ClusterPricing) == 0 {
  259. return nil, errors.New("not found")
  260. }
  261. // Only one default ClusterPricing is allowed in basic pricing.
  262. // If multiple exist, return only the first one.
  263. return ps.ClusterPricing[0], nil
  264. }
  265. func (pm *PricingModule) setClusterPricing(ctx context.Context, cp *pricing.ClusterPricing) error {
  266. if cp == nil {
  267. return errors.New("nil cluster pricing")
  268. }
  269. // Get the pricing set
  270. ps, err := pm.store.GetPricingSet(ctx)
  271. if err != nil {
  272. return fmt.Errorf("getting pricing: %w", err)
  273. }
  274. // Only one default ClusterPricing is allowed in basic pricing.
  275. ps.ClusterPricing = []*pricing.ClusterPricing{cp}
  276. // Set the new pricing set
  277. err = pm.store.SetPricingSet(ctx, ps)
  278. if err != nil {
  279. return fmt.Errorf("setting pricing: %w", err)
  280. }
  281. return nil
  282. }
  283. func (pm *PricingModule) getNetworkPricing(ctx context.Context) (*pricing.NetworkPricing, error) {
  284. ps, err := pm.store.GetPricingSet(ctx)
  285. if err != nil {
  286. return nil, fmt.Errorf("getting pricing: %w", err)
  287. }
  288. if len(ps.NetworkPricing) == 0 {
  289. return nil, errors.New("not found")
  290. }
  291. return ps.NetworkPricing[0], nil
  292. }
  293. func (pm *PricingModule) setNetworkPricing(ctx context.Context, np *pricing.NetworkPricing) error {
  294. if np == nil {
  295. return errors.New("nil network pricing")
  296. }
  297. // Get the pricing set
  298. ps, err := pm.store.GetPricingSet(ctx)
  299. if err != nil {
  300. return fmt.Errorf("getting pricing: %w", err)
  301. }
  302. // Only one default NetworkPricing is allowed in basic pricing.
  303. ps.NetworkPricing = []*pricing.NetworkPricing{np}
  304. // Set the new pricing set
  305. err = pm.store.SetPricingSet(ctx, ps)
  306. if err != nil {
  307. return fmt.Errorf("setting pricing: %w", err)
  308. }
  309. return nil
  310. }
  311. func (pm *PricingModule) getNodePricing(ctx context.Context) (*pricing.NodePricing, error) {
  312. ps, err := pm.store.GetPricingSet(ctx)
  313. if err != nil {
  314. return nil, fmt.Errorf("getting pricing: %w", err)
  315. }
  316. if len(ps.NodePricing) == 0 {
  317. return nil, errors.New("not found")
  318. }
  319. // Only one default NodePricing is allowed in basic pricing.
  320. // If multiple exist, return only the first one.
  321. return ps.NodePricing[0], nil
  322. }
  323. func (pm *PricingModule) setNodePricing(ctx context.Context, np *pricing.NodePricing) error {
  324. if np == nil {
  325. return errors.New("nil node pricing")
  326. }
  327. // Get the pricing set
  328. ps, err := pm.store.GetPricingSet(ctx)
  329. if err != nil {
  330. return fmt.Errorf("getting pricing: %w", err)
  331. }
  332. // Only one default NodePricing is allowed in basic pricing.
  333. ps.NodePricing = []*pricing.NodePricing{np}
  334. // Set the new pricing set
  335. err = pm.store.SetPricingSet(ctx, ps)
  336. if err != nil {
  337. return fmt.Errorf("setting pricing: %w", err)
  338. }
  339. return nil
  340. }
  341. func (pm *PricingModule) getPersistentVolumePricing(ctx context.Context) (*pricing.PersistentVolumePricing, error) {
  342. ps, err := pm.store.GetPricingSet(ctx)
  343. if err != nil {
  344. return nil, fmt.Errorf("getting pricing: %w", err)
  345. }
  346. if len(ps.PersistentVolumePricing) == 0 {
  347. return nil, errors.New("not found")
  348. }
  349. // Only one default VolumePricing is allowed in basic pricing.
  350. // If multiple exist, return only the first one.
  351. return ps.PersistentVolumePricing[0], nil
  352. }
  353. func (pm *PricingModule) setPersistentVolumePricing(ctx context.Context, vp *pricing.PersistentVolumePricing) error {
  354. if vp == nil {
  355. return errors.New("nil volume pricing")
  356. }
  357. // Get the pricing set
  358. ps, err := pm.store.GetPricingSet(ctx)
  359. if err != nil {
  360. return fmt.Errorf("getting pricing: %w", err)
  361. }
  362. // Only one default VolumePricing is allowed in basic pricing.
  363. ps.PersistentVolumePricing = []*pricing.PersistentVolumePricing{vp}
  364. // Set the new pricing set
  365. err = pm.store.SetPricingSet(ctx, ps)
  366. if err != nil {
  367. return fmt.Errorf("setting pricing: %w", err)
  368. }
  369. return nil
  370. }
  371. func (pm *PricingModule) getServicePricing(ctx context.Context) (*pricing.ServicePricing, error) {
  372. ps, err := pm.store.GetPricingSet(ctx)
  373. if err != nil {
  374. return nil, fmt.Errorf("getting pricing: %w", err)
  375. }
  376. if len(ps.ServicePricing) == 0 {
  377. return nil, errors.New("not found")
  378. }
  379. // Only one default ServicePricing is allowed in basic pricing.
  380. // If multiple exist, return only the first one.
  381. return ps.ServicePricing[0], nil
  382. }
  383. func (pm *PricingModule) setServicePricing(ctx context.Context, sp *pricing.ServicePricing) error {
  384. if sp == nil {
  385. return errors.New("nil service pricing")
  386. }
  387. // Get the pricing set
  388. ps, err := pm.store.GetPricingSet(ctx)
  389. if err != nil {
  390. return fmt.Errorf("getting pricing: %w", err)
  391. }
  392. // Only one default ServicePricing is allowed in basic pricing.
  393. ps.ServicePricing = []*pricing.ServicePricing{sp}
  394. // Set the new pricing set
  395. err = pm.store.SetPricingSet(ctx, ps)
  396. if err != nil {
  397. return fmt.Errorf("setting pricing: %w", err)
  398. }
  399. return nil
  400. }