cloudcost.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package kubecost
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/opencost/opencost/pkg/filter"
  7. filter21 "github.com/opencost/opencost/pkg/filter21"
  8. "github.com/opencost/opencost/pkg/filter21/ast"
  9. "github.com/opencost/opencost/pkg/log"
  10. )
  11. // CloudCost represents a CUR line item, identifying a cloud resource and
  12. // its cost over some period of time.
  13. type CloudCost struct {
  14. Properties *CloudCostProperties `json:"properties"`
  15. Window Window `json:"window"`
  16. ListCost CostMetric `json:"listCost"`
  17. NetCost CostMetric `json:"netCost"`
  18. AmortizedNetCost CostMetric `json:"amortizedNetCost"`
  19. InvoicedCost CostMetric `json:"invoicedCost"`
  20. AmortizedCost CostMetric `json:"amortizedCost"`
  21. }
  22. // NewCloudCost instantiates a new CloudCost
  23. func NewCloudCost(start, end time.Time, ccProperties *CloudCostProperties, kubernetesPercent, listCost, netCost, amortizedNetCost, invoicedCost, amortizedCost float64) *CloudCost {
  24. return &CloudCost{
  25. Properties: ccProperties,
  26. Window: NewWindow(&start, &end),
  27. ListCost: CostMetric{
  28. Cost: listCost,
  29. KubernetesPercent: kubernetesPercent,
  30. },
  31. NetCost: CostMetric{
  32. Cost: netCost,
  33. KubernetesPercent: kubernetesPercent,
  34. },
  35. AmortizedNetCost: CostMetric{
  36. Cost: amortizedNetCost,
  37. KubernetesPercent: kubernetesPercent,
  38. },
  39. InvoicedCost: CostMetric{
  40. Cost: invoicedCost,
  41. KubernetesPercent: kubernetesPercent,
  42. },
  43. AmortizedCost: CostMetric{
  44. Cost: amortizedCost,
  45. KubernetesPercent: kubernetesPercent,
  46. },
  47. }
  48. }
  49. func (cc *CloudCost) Clone() *CloudCost {
  50. return &CloudCost{
  51. Properties: cc.Properties.Clone(),
  52. Window: cc.Window.Clone(),
  53. ListCost: cc.ListCost.Clone(),
  54. NetCost: cc.NetCost.Clone(),
  55. AmortizedNetCost: cc.AmortizedNetCost.Clone(),
  56. InvoicedCost: cc.InvoicedCost.Clone(),
  57. AmortizedCost: cc.AmortizedCost.Clone(),
  58. }
  59. }
  60. func (cc *CloudCost) Equal(that *CloudCost) bool {
  61. if that == nil {
  62. return false
  63. }
  64. return cc.Properties.Equal(that.Properties) &&
  65. cc.Window.Equal(that.Window) &&
  66. cc.ListCost.Equal(that.ListCost) &&
  67. cc.NetCost.Equal(that.NetCost) &&
  68. cc.AmortizedNetCost.Equal(that.AmortizedNetCost) &&
  69. cc.InvoicedCost.Equal(that.InvoicedCost) &&
  70. cc.AmortizedCost.Equal(that.AmortizedCost)
  71. }
  72. func (cc *CloudCost) add(that *CloudCost) {
  73. if cc == nil {
  74. log.Warnf("cannot add to nil CloudCost")
  75. return
  76. }
  77. // Preserve properties of cloud cost that are matching between the two CloudCost
  78. cc.Properties = cc.Properties.Intersection(that.Properties)
  79. cc.ListCost = cc.ListCost.add(that.ListCost)
  80. cc.NetCost = cc.NetCost.add(that.NetCost)
  81. cc.AmortizedNetCost = cc.AmortizedNetCost.add(that.AmortizedNetCost)
  82. cc.InvoicedCost = cc.InvoicedCost.add(that.InvoicedCost)
  83. cc.AmortizedCost = cc.AmortizedCost.add(that.AmortizedCost)
  84. cc.Window = cc.Window.Expand(that.Window)
  85. }
  86. func (cc *CloudCost) StringProperty(prop string) (string, error) {
  87. if cc == nil {
  88. return "", nil
  89. }
  90. switch prop {
  91. case CloudCostInvoiceEntityIDProp:
  92. return cc.Properties.InvoiceEntityID, nil
  93. case CloudCostAccountIDProp:
  94. return cc.Properties.AccountID, nil
  95. case CloudCostProviderProp:
  96. return cc.Properties.Provider, nil
  97. case CloudCostProviderIDProp:
  98. return cc.Properties.ProviderID, nil
  99. case CloudCostServiceProp:
  100. return cc.Properties.Service, nil
  101. case CloudCostCategoryProp:
  102. return cc.Properties.Category, nil
  103. default:
  104. return "", fmt.Errorf("invalid property name: %s", prop)
  105. }
  106. }
  107. func (cc *CloudCost) StringMapProperty(property string) (map[string]string, error) {
  108. switch property {
  109. case CloudCostLabelProp:
  110. if cc.Properties == nil {
  111. return nil, nil
  112. }
  113. return cc.Properties.Labels, nil
  114. default:
  115. return nil, fmt.Errorf("CloudCost: StringMapProperty: invalid property name: %s", property)
  116. }
  117. }
  118. func (cc *CloudCost) GetCostMetric(costMetricName string) (CostMetric, error) {
  119. switch costMetricName {
  120. case ListCostMetric:
  121. return cc.ListCost, nil
  122. case NetCostMetric:
  123. return cc.NetCost, nil
  124. case AmortizedNetCostMetric:
  125. return cc.AmortizedNetCost, nil
  126. case InvoicedCostMetric:
  127. return cc.InvoicedCost, nil
  128. case AmortizedCostMetric:
  129. return cc.AmortizedCost, nil
  130. }
  131. return CostMetric{}, fmt.Errorf("invalid Cost Metric: %s", costMetricName)
  132. }
  133. // WeightCostMetrics weights all the cost metrics with the given weightedAverage
  134. func (cc *CloudCost) WeightCostMetrics(weightedAverge float64) {
  135. cc.ListCost.Cost *= weightedAverge
  136. cc.NetCost.Cost *= weightedAverge
  137. cc.AmortizedNetCost.Cost *= weightedAverge
  138. cc.InvoicedCost.Cost *= weightedAverge
  139. cc.AmortizedCost.Cost *= weightedAverge
  140. }
  141. // CloudCostSet follows the established set pattern of windowed data types. It has addition metadata types that can be
  142. // used to preserve data consistency and be used for validation.
  143. // - Integration is the ID for the integration that a CloudCostSet was sourced from, this value is cleared if when a
  144. // set is joined with another with a different key
  145. // - AggregationProperties is set by the Aggregate function and ensures that any additional inserts are keyed correctly
  146. type CloudCostSet struct {
  147. CloudCosts map[string]*CloudCost `json:"cloudCosts"`
  148. Window Window `json:"window"`
  149. Integration string `json:"-"`
  150. AggregationProperties []string `json:"aggregationProperties"`
  151. }
  152. // NewCloudCostSet instantiates a new CloudCostSet and, optionally, inserts
  153. // the given list of CloudCosts
  154. func NewCloudCostSet(start, end time.Time, cloudCosts ...*CloudCost) *CloudCostSet {
  155. ccs := &CloudCostSet{
  156. CloudCosts: map[string]*CloudCost{},
  157. Window: NewWindow(&start, &end),
  158. }
  159. for _, cc := range cloudCosts {
  160. ccs.Insert(cc)
  161. }
  162. return ccs
  163. }
  164. func (ccs *CloudCostSet) Aggregate(props []string) (*CloudCostSet, error) {
  165. if ccs == nil {
  166. return nil, errors.New("cannot aggregate a nil CloudCostSet")
  167. }
  168. if ccs.Window.IsOpen() {
  169. return nil, fmt.Errorf("cannot aggregate a CloudCostSet with an open window: %s", ccs.Window)
  170. }
  171. // Create a new result set, with the given aggregation property
  172. result := ccs.cloneSet()
  173. result.AggregationProperties = props
  174. // Insert clones of each item in the set, keyed by the given property.
  175. // The underlying insert logic will add binned items together.
  176. for name, cc := range ccs.CloudCosts {
  177. ccClone := cc.Clone()
  178. err := result.Insert(ccClone)
  179. if err != nil {
  180. return nil, fmt.Errorf("error aggregating %s by %v: %s", name, props, err)
  181. }
  182. }
  183. return result, nil
  184. }
  185. func (ccs *CloudCostSet) Accumulate(that *CloudCostSet) (*CloudCostSet, error) {
  186. if ccs.IsEmpty() {
  187. return that.Clone(), nil
  188. }
  189. acc := ccs.Clone()
  190. err := acc.accumulateInto(that)
  191. if err == nil {
  192. return nil, err
  193. }
  194. return acc, nil
  195. }
  196. // accumulateInto accumulates a the arg CloudCostSet Into the receiver
  197. func (ccs *CloudCostSet) accumulateInto(that *CloudCostSet) error {
  198. if ccs == nil {
  199. return fmt.Errorf("CloudCost: cannot accumulate into nil set")
  200. }
  201. if that.IsEmpty() {
  202. return nil
  203. }
  204. if ccs.Integration != that.Integration {
  205. ccs.Integration = ""
  206. }
  207. ccs.Window.Expand(that.Window)
  208. for _, cc := range that.CloudCosts {
  209. err := ccs.Insert(cc)
  210. if err != nil {
  211. return err
  212. }
  213. }
  214. return nil
  215. }
  216. func (ccs *CloudCostSet) Equal(that *CloudCostSet) bool {
  217. if ccs.Integration != that.Integration {
  218. return false
  219. }
  220. if !ccs.Window.Equal(that.Window) {
  221. return false
  222. }
  223. // Check Aggregation Properties, slice order is grounds for inequality
  224. if len(ccs.AggregationProperties) != len(that.AggregationProperties) {
  225. return false
  226. }
  227. for i, prop := range ccs.AggregationProperties {
  228. if that.AggregationProperties[i] != prop {
  229. return false
  230. }
  231. }
  232. if len(ccs.CloudCosts) != len(that.CloudCosts) {
  233. return false
  234. }
  235. for k, cc := range ccs.CloudCosts {
  236. if tcc, ok := that.CloudCosts[k]; !ok || !cc.Equal(tcc) {
  237. return false
  238. }
  239. }
  240. return true
  241. }
  242. func (ccs *CloudCostSet) Filter(filters filter.Filter[*CloudCost]) *CloudCostSet {
  243. if ccs == nil {
  244. return nil
  245. }
  246. if filters == nil {
  247. return ccs.Clone()
  248. }
  249. result := ccs.cloneSet()
  250. for _, cc := range ccs.CloudCosts {
  251. if filters.Matches(cc) {
  252. result.Insert(cc.Clone())
  253. }
  254. }
  255. return result
  256. }
  257. func (ccs *CloudCostSet) Filter21(filters filter21.Filter) (*CloudCostSet, error) {
  258. if ccs == nil {
  259. return nil, nil
  260. }
  261. if filters == nil {
  262. return ccs.Clone(), nil
  263. }
  264. compiler := NewCloudCostMatchCompiler()
  265. var err error
  266. matcher, err := compiler.Compile(filters)
  267. if err != nil {
  268. return ccs.Clone(), fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(filters), err)
  269. }
  270. if matcher == nil {
  271. return ccs.Clone(), fmt.Errorf("unexpected nil filter")
  272. }
  273. result := ccs.cloneSet()
  274. for _, cc := range ccs.CloudCosts {
  275. if matcher.Matches(cc) {
  276. result.Insert(cc.Clone())
  277. }
  278. }
  279. return result, nil
  280. }
  281. // Insert adds a CloudCost to a CloudCostSet using its AggregationProperties and LabelConfig
  282. // to determine the key where it will be inserted
  283. func (ccs *CloudCostSet) Insert(cc *CloudCost) error {
  284. if ccs == nil {
  285. return fmt.Errorf("cannot insert into nil CloudCostSet")
  286. }
  287. if cc == nil {
  288. return fmt.Errorf("cannot insert nil CloudCost into CloudCostSet")
  289. }
  290. if ccs.CloudCosts == nil {
  291. ccs.CloudCosts = map[string]*CloudCost{}
  292. }
  293. ccKey := cc.Properties.GenerateKey(ccs.AggregationProperties)
  294. // Add the given CloudCost to the existing entry, if there is one;
  295. // otherwise just set directly into allocations
  296. if _, ok := ccs.CloudCosts[ccKey]; !ok {
  297. ccs.CloudCosts[ccKey] = cc.Clone()
  298. } else {
  299. ccs.CloudCosts[ccKey].add(cc)
  300. }
  301. return nil
  302. }
  303. func (ccs *CloudCostSet) Clone() *CloudCostSet {
  304. cloudCosts := make(map[string]*CloudCost, len(ccs.CloudCosts))
  305. for k, v := range ccs.CloudCosts {
  306. cloudCosts[k] = v.Clone()
  307. }
  308. cloneCCS := ccs.cloneSet()
  309. cloneCCS.CloudCosts = cloudCosts
  310. return cloneCCS
  311. }
  312. // cloneSet creates a copy of the receiver without any of its CloudCosts
  313. func (ccs *CloudCostSet) cloneSet() *CloudCostSet {
  314. aggProps := make([]string, len(ccs.AggregationProperties))
  315. for i, v := range ccs.AggregationProperties {
  316. aggProps[i] = v
  317. }
  318. return &CloudCostSet{
  319. CloudCosts: make(map[string]*CloudCost),
  320. Integration: ccs.Integration,
  321. AggregationProperties: aggProps,
  322. Window: ccs.Window.Clone(),
  323. }
  324. }
  325. func (ccs *CloudCostSet) IsEmpty() bool {
  326. if ccs == nil {
  327. return true
  328. }
  329. if len(ccs.CloudCosts) == 0 {
  330. return true
  331. }
  332. return false
  333. }
  334. func (ccs *CloudCostSet) Length() int {
  335. if ccs == nil {
  336. return 0
  337. }
  338. return len(ccs.CloudCosts)
  339. }
  340. func (ccs *CloudCostSet) GetWindow() Window {
  341. return ccs.Window
  342. }
  343. func (ccs *CloudCostSet) Merge(that *CloudCostSet) (*CloudCostSet, error) {
  344. if ccs == nil {
  345. return nil, fmt.Errorf("cannot merge nil CloudCostSets")
  346. }
  347. if that.IsEmpty() {
  348. return ccs.Clone(), nil
  349. }
  350. if !ccs.Window.Equal(that.Window) {
  351. return nil, fmt.Errorf("cannot merge CloudCostSets with different windows")
  352. }
  353. result := ccs.cloneSet()
  354. // clear integration if it is not equal
  355. if ccs.Integration != that.Integration {
  356. result.Integration = ""
  357. }
  358. for _, cc := range ccs.CloudCosts {
  359. result.Insert(cc)
  360. }
  361. for _, cc := range that.CloudCosts {
  362. result.Insert(cc)
  363. }
  364. return result, nil
  365. }
  366. type CloudCostSetRange struct {
  367. CloudCostSets []*CloudCostSet `json:"sets"`
  368. Window Window `json:"window"`
  369. }
  370. // NewCloudCostSetRange create a CloudCostSetRange containing CloudCostSets with windows of equal duration
  371. // the duration between start and end must be divisible by the window duration argument
  372. func NewCloudCostSetRange(start time.Time, end time.Time, window time.Duration, integration string) (*CloudCostSetRange, error) {
  373. windows, err := GetWindows(start, end, window)
  374. if err != nil {
  375. return nil, err
  376. }
  377. // Build slice of CloudCostSet to cover the range
  378. cloudCostItemSets := make([]*CloudCostSet, len(windows))
  379. for i, w := range windows {
  380. ccs := NewCloudCostSet(*w.Start(), *w.End())
  381. ccs.Integration = integration
  382. cloudCostItemSets[i] = ccs
  383. }
  384. return &CloudCostSetRange{
  385. Window: NewWindow(&start, &end),
  386. CloudCostSets: cloudCostItemSets,
  387. }, nil
  388. }
  389. func (ccsr *CloudCostSetRange) Clone() *CloudCostSetRange {
  390. ccsSlice := make([]*CloudCostSet, len(ccsr.CloudCostSets))
  391. for i, ccs := range ccsr.CloudCostSets {
  392. ccsSlice[i] = ccs.Clone()
  393. }
  394. return &CloudCostSetRange{
  395. Window: ccsr.Window.Clone(),
  396. CloudCostSets: ccsSlice,
  397. }
  398. }
  399. func (ccsr *CloudCostSetRange) IsEmpty() bool {
  400. for _, ccs := range ccsr.CloudCostSets {
  401. if !ccs.IsEmpty() {
  402. return false
  403. }
  404. }
  405. return true
  406. }
  407. // Accumulate sums each CloudCostSet in the given range, returning a single cumulative
  408. // CloudCostSet for the entire range.
  409. func (ccsr *CloudCostSetRange) Accumulate() (*CloudCostSet, error) {
  410. var cloudCostSet *CloudCostSet
  411. var err error
  412. for _, ccs := range ccsr.CloudCostSets {
  413. if cloudCostSet == nil {
  414. cloudCostSet = ccs.Clone()
  415. continue
  416. }
  417. err = cloudCostSet.accumulateInto(ccs)
  418. if err != nil {
  419. return nil, err
  420. }
  421. }
  422. return cloudCostSet, nil
  423. }
  424. // LoadCloudCost loads CloudCosts into existing CloudCostSets of the CloudCostSetRange.
  425. // This function service to aggregate and distribute costs over predefined windows
  426. // are accumulated here so that the resulting CloudCost with the 1d window has the correct price for the entire day.
  427. // If all or a portion of the window of the CloudCost is outside of the windows of the existing CloudCostSets,
  428. // that portion of the CloudCost's cost will not be inserted
  429. func (ccsr *CloudCostSetRange) LoadCloudCost(cloudCost *CloudCost) {
  430. window := cloudCost.Window
  431. if window.IsOpen() {
  432. log.Errorf("CloudCostSetRange: LoadCloudCost: invalid window %s", window.String())
  433. return
  434. }
  435. totalPct := 0.0
  436. // Distribute cost of the current item across one or more CloudCosts in
  437. // across each relevant CloudCostSet. Stop when the end of the current
  438. // block reaches the item's end time or the end of the range.
  439. for _, ccs := range ccsr.CloudCostSets {
  440. setWindow := ccs.Window
  441. // get percent of item window contained in set window
  442. pct := setWindow.GetPercentInWindow(window)
  443. if pct == 0 {
  444. continue
  445. }
  446. cc := cloudCost
  447. // If the current set Window only contains a portion of the CloudCost Window, insert costs relative to that portion
  448. if pct < 1.0 {
  449. cc = &CloudCost{
  450. Properties: cloudCost.Properties,
  451. Window: window.Contract(setWindow),
  452. ListCost: cloudCost.ListCost.percent(pct),
  453. NetCost: cloudCost.NetCost.percent(pct),
  454. AmortizedNetCost: cloudCost.AmortizedNetCost.percent(pct),
  455. InvoicedCost: cloudCost.InvoicedCost.percent(pct),
  456. AmortizedCost: cloudCost.AmortizedCost.percent(pct),
  457. }
  458. }
  459. err := ccs.Insert(cc)
  460. if err != nil {
  461. log.Errorf("CloudCostSetRange: LoadCloudCost: failed to load CloudCost with window %s: %s", setWindow.String(), err.Error())
  462. }
  463. // If all cost has been inserted, then there is no need to check later days in the range
  464. totalPct += pct
  465. if totalPct >= 1.0 {
  466. return
  467. }
  468. }
  469. }
  470. const (
  471. ListCostMetric string = "ListCost"
  472. NetCostMetric string = "NetCost"
  473. AmortizedNetCostMetric string = "AmortizedNetCost"
  474. InvoicedCostMetric string = "InvoicedCost"
  475. AmortizedCostMetric string = "AmortizedCost"
  476. )
  477. type CostMetric struct {
  478. Cost float64 `json:"cost"`
  479. KubernetesPercent float64 `json:"kubernetesPercent"`
  480. }
  481. func (cm CostMetric) Equal(that CostMetric) bool {
  482. return cm.Cost == that.Cost && cm.KubernetesPercent == that.KubernetesPercent
  483. }
  484. func (cm CostMetric) Clone() CostMetric {
  485. return CostMetric{
  486. Cost: cm.Cost,
  487. KubernetesPercent: cm.KubernetesPercent,
  488. }
  489. }
  490. func (cm CostMetric) add(that CostMetric) CostMetric {
  491. // Compute KubernetesPercent for sum
  492. k8sPct := 0.0
  493. sumCost := cm.Cost + that.Cost
  494. if sumCost > 0.0 {
  495. thisK8sCost := cm.Cost * cm.KubernetesPercent
  496. thatK8sCost := that.Cost * that.KubernetesPercent
  497. k8sPct = (thisK8sCost + thatK8sCost) / sumCost
  498. }
  499. return CostMetric{
  500. Cost: sumCost,
  501. KubernetesPercent: k8sPct,
  502. }
  503. }
  504. // percent returns the product of the given percent and the cost, KubernetesPercent remains the same
  505. func (cm CostMetric) percent(pct float64) CostMetric {
  506. return CostMetric{
  507. Cost: cm.Cost * pct,
  508. KubernetesPercent: cm.KubernetesPercent,
  509. }
  510. }