cloudcost.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. // CloudCostSet follows the established set pattern of windowed data types. It has addition metadata types that can be
  134. // used to preserve data consistency and be used for validation.
  135. // - Integration is the ID for the integration that a CloudCostSet was sourced from, this value is cleared if when a
  136. // set is joined with another with a different key
  137. // - AggregationProperties is set by the Aggregate function and ensures that any additional inserts are keyed correctly
  138. type CloudCostSet struct {
  139. CloudCosts map[string]*CloudCost `json:"cloudCosts"`
  140. Window Window `json:"window"`
  141. Integration string `json:"-"`
  142. AggregationProperties []string `json:"aggregationProperties"`
  143. }
  144. // NewCloudCostSet instantiates a new CloudCostSet and, optionally, inserts
  145. // the given list of CloudCosts
  146. func NewCloudCostSet(start, end time.Time, cloudCosts ...*CloudCost) *CloudCostSet {
  147. ccs := &CloudCostSet{
  148. CloudCosts: map[string]*CloudCost{},
  149. Window: NewWindow(&start, &end),
  150. }
  151. for _, cc := range cloudCosts {
  152. ccs.Insert(cc)
  153. }
  154. return ccs
  155. }
  156. func (ccs *CloudCostSet) Aggregate(props []string) (*CloudCostSet, error) {
  157. if ccs == nil {
  158. return nil, errors.New("cannot aggregate a nil CloudCostSet")
  159. }
  160. if ccs.Window.IsOpen() {
  161. return nil, fmt.Errorf("cannot aggregate a CloudCostSet with an open window: %s", ccs.Window)
  162. }
  163. // Create a new result set, with the given aggregation property
  164. result := ccs.cloneSet()
  165. result.AggregationProperties = props
  166. // Insert clones of each item in the set, keyed by the given property.
  167. // The underlying insert logic will add binned items together.
  168. for name, cc := range ccs.CloudCosts {
  169. ccClone := cc.Clone()
  170. err := result.Insert(ccClone)
  171. if err != nil {
  172. return nil, fmt.Errorf("error aggregating %s by %v: %s", name, props, err)
  173. }
  174. }
  175. return result, nil
  176. }
  177. func (ccs *CloudCostSet) Accumulate(that *CloudCostSet) (*CloudCostSet, error) {
  178. if ccs.IsEmpty() {
  179. return that.Clone(), nil
  180. }
  181. acc := ccs.Clone()
  182. err := acc.accumulateInto(that)
  183. if err == nil {
  184. return nil, err
  185. }
  186. return acc, nil
  187. }
  188. // accumulateInto accumulates a the arg CloudCostSet Into the receiver
  189. func (ccs *CloudCostSet) accumulateInto(that *CloudCostSet) error {
  190. if ccs == nil {
  191. return fmt.Errorf("CloudCost: cannot accumulate into nil set")
  192. }
  193. if that.IsEmpty() {
  194. return nil
  195. }
  196. if ccs.Integration != that.Integration {
  197. ccs.Integration = ""
  198. }
  199. ccs.Window.Expand(that.Window)
  200. for _, cc := range that.CloudCosts {
  201. err := ccs.Insert(cc)
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. return nil
  207. }
  208. func (ccs *CloudCostSet) Equal(that *CloudCostSet) bool {
  209. if ccs.Integration != that.Integration {
  210. return false
  211. }
  212. if !ccs.Window.Equal(that.Window) {
  213. return false
  214. }
  215. // Check Aggregation Properties, slice order is grounds for inequality
  216. if len(ccs.AggregationProperties) != len(that.AggregationProperties) {
  217. return false
  218. }
  219. for i, prop := range ccs.AggregationProperties {
  220. if that.AggregationProperties[i] != prop {
  221. return false
  222. }
  223. }
  224. if len(ccs.CloudCosts) != len(that.CloudCosts) {
  225. return false
  226. }
  227. for k, cc := range ccs.CloudCosts {
  228. if tcc, ok := that.CloudCosts[k]; !ok || !cc.Equal(tcc) {
  229. return false
  230. }
  231. }
  232. return true
  233. }
  234. func (ccs *CloudCostSet) Filter(filters filter.Filter[*CloudCost]) *CloudCostSet {
  235. if ccs == nil {
  236. return nil
  237. }
  238. if filters == nil {
  239. return ccs.Clone()
  240. }
  241. result := ccs.cloneSet()
  242. for _, cc := range ccs.CloudCosts {
  243. if filters.Matches(cc) {
  244. result.Insert(cc.Clone())
  245. }
  246. }
  247. return result
  248. }
  249. func (ccs *CloudCostSet) Filter21(filters filter21.Filter) (*CloudCostSet, error) {
  250. if ccs == nil {
  251. return nil, nil
  252. }
  253. if filters == nil {
  254. return ccs.Clone(), nil
  255. }
  256. compiler := NewCloudCostMatchCompiler()
  257. var err error
  258. matcher, err := compiler.Compile(filters)
  259. if err != nil {
  260. return ccs.Clone(), fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(filters), err)
  261. }
  262. if matcher == nil {
  263. return ccs.Clone(), fmt.Errorf("unexpected nil filter")
  264. }
  265. result := ccs.cloneSet()
  266. for _, cc := range ccs.CloudCosts {
  267. if matcher.Matches(cc) {
  268. result.Insert(cc.Clone())
  269. }
  270. }
  271. return result, nil
  272. }
  273. // Insert adds a CloudCost to a CloudCostSet using its AggregationProperties and LabelConfig
  274. // to determine the key where it will be inserted
  275. func (ccs *CloudCostSet) Insert(cc *CloudCost) error {
  276. if ccs == nil {
  277. return fmt.Errorf("cannot insert into nil CloudCostSet")
  278. }
  279. if cc == nil {
  280. return fmt.Errorf("cannot insert nil CloudCost into CloudCostSet")
  281. }
  282. if ccs.CloudCosts == nil {
  283. ccs.CloudCosts = map[string]*CloudCost{}
  284. }
  285. ccKey := cc.Properties.GenerateKey(ccs.AggregationProperties)
  286. // Add the given CloudCost to the existing entry, if there is one;
  287. // otherwise just set directly into allocations
  288. if _, ok := ccs.CloudCosts[ccKey]; !ok {
  289. ccs.CloudCosts[ccKey] = cc.Clone()
  290. } else {
  291. ccs.CloudCosts[ccKey].add(cc)
  292. }
  293. return nil
  294. }
  295. func (ccs *CloudCostSet) Clone() *CloudCostSet {
  296. cloudCosts := make(map[string]*CloudCost, len(ccs.CloudCosts))
  297. for k, v := range ccs.CloudCosts {
  298. cloudCosts[k] = v.Clone()
  299. }
  300. cloneCCS := ccs.cloneSet()
  301. cloneCCS.CloudCosts = cloudCosts
  302. return cloneCCS
  303. }
  304. // cloneSet creates a copy of the receiver without any of its CloudCosts
  305. func (ccs *CloudCostSet) cloneSet() *CloudCostSet {
  306. aggProps := make([]string, len(ccs.AggregationProperties))
  307. for i, v := range ccs.AggregationProperties {
  308. aggProps[i] = v
  309. }
  310. return &CloudCostSet{
  311. CloudCosts: make(map[string]*CloudCost),
  312. Integration: ccs.Integration,
  313. AggregationProperties: aggProps,
  314. Window: ccs.Window.Clone(),
  315. }
  316. }
  317. func (ccs *CloudCostSet) IsEmpty() bool {
  318. if ccs == nil {
  319. return true
  320. }
  321. if len(ccs.CloudCosts) == 0 {
  322. return true
  323. }
  324. return false
  325. }
  326. func (ccs *CloudCostSet) Length() int {
  327. if ccs == nil {
  328. return 0
  329. }
  330. return len(ccs.CloudCosts)
  331. }
  332. func (ccs *CloudCostSet) GetWindow() Window {
  333. return ccs.Window
  334. }
  335. func (ccs *CloudCostSet) Merge(that *CloudCostSet) (*CloudCostSet, error) {
  336. if ccs == nil {
  337. return nil, fmt.Errorf("cannot merge nil CloudCostSets")
  338. }
  339. if that.IsEmpty() {
  340. return ccs.Clone(), nil
  341. }
  342. if !ccs.Window.Equal(that.Window) {
  343. return nil, fmt.Errorf("cannot merge CloudCostSets with different windows")
  344. }
  345. result := ccs.cloneSet()
  346. // clear integration if it is not equal
  347. if ccs.Integration != that.Integration {
  348. result.Integration = ""
  349. }
  350. for _, cc := range ccs.CloudCosts {
  351. result.Insert(cc)
  352. }
  353. for _, cc := range that.CloudCosts {
  354. result.Insert(cc)
  355. }
  356. return result, nil
  357. }
  358. type CloudCostSetRange struct {
  359. CloudCostSets []*CloudCostSet `json:"sets"`
  360. Window Window `json:"window"`
  361. }
  362. // NewCloudCostSetRange create a CloudCostSetRange containing CloudCostSets with windows of equal duration
  363. // the duration between start and end must be divisible by the window duration argument
  364. func NewCloudCostSetRange(start time.Time, end time.Time, window time.Duration, integration string) (*CloudCostSetRange, error) {
  365. windows, err := GetWindows(start, end, window)
  366. if err != nil {
  367. return nil, err
  368. }
  369. // Build slice of CloudCostSet to cover the range
  370. cloudCostItemSets := make([]*CloudCostSet, len(windows))
  371. for i, w := range windows {
  372. ccs := NewCloudCostSet(*w.Start(), *w.End())
  373. ccs.Integration = integration
  374. cloudCostItemSets[i] = ccs
  375. }
  376. return &CloudCostSetRange{
  377. Window: NewWindow(&start, &end),
  378. CloudCostSets: cloudCostItemSets,
  379. }, nil
  380. }
  381. func (ccsr *CloudCostSetRange) Clone() *CloudCostSetRange {
  382. ccsSlice := make([]*CloudCostSet, len(ccsr.CloudCostSets))
  383. for i, ccs := range ccsr.CloudCostSets {
  384. ccsSlice[i] = ccs.Clone()
  385. }
  386. return &CloudCostSetRange{
  387. Window: ccsr.Window.Clone(),
  388. CloudCostSets: ccsSlice,
  389. }
  390. }
  391. func (ccsr *CloudCostSetRange) IsEmpty() bool {
  392. for _, ccs := range ccsr.CloudCostSets {
  393. if !ccs.IsEmpty() {
  394. return false
  395. }
  396. }
  397. return true
  398. }
  399. // Accumulate sums each CloudCostSet in the given range, returning a single cumulative
  400. // CloudCostSet for the entire range.
  401. func (ccsr *CloudCostSetRange) Accumulate() (*CloudCostSet, error) {
  402. var cloudCostSet *CloudCostSet
  403. var err error
  404. for _, ccs := range ccsr.CloudCostSets {
  405. if cloudCostSet == nil {
  406. cloudCostSet = ccs.Clone()
  407. continue
  408. }
  409. err = cloudCostSet.accumulateInto(ccs)
  410. if err != nil {
  411. return nil, err
  412. }
  413. }
  414. return cloudCostSet, nil
  415. }
  416. // LoadCloudCost loads CloudCosts into existing CloudCostSets of the CloudCostSetRange.
  417. // This function service to aggregate and distribute costs over predefined windows
  418. // are accumulated here so that the resulting CloudCost with the 1d window has the correct price for the entire day.
  419. // If all or a portion of the window of the CloudCost is outside of the windows of the existing CloudCostSets,
  420. // that portion of the CloudCost's cost will not be inserted
  421. func (ccsr *CloudCostSetRange) LoadCloudCost(cloudCost *CloudCost) {
  422. window := cloudCost.Window
  423. if window.IsOpen() {
  424. log.Errorf("CloudCostSetRange: LoadCloudCost: invalid window %s", window.String())
  425. return
  426. }
  427. totalPct := 0.0
  428. // Distribute cost of the current item across one or more CloudCosts in
  429. // across each relevant CloudCostSet. Stop when the end of the current
  430. // block reaches the item's end time or the end of the range.
  431. for _, ccs := range ccsr.CloudCostSets {
  432. setWindow := ccs.Window
  433. // get percent of item window contained in set window
  434. pct := setWindow.GetPercentInWindow(window)
  435. if pct == 0 {
  436. continue
  437. }
  438. cc := cloudCost
  439. // If the current set Window only contains a portion of the CloudCost Window, insert costs relative to that portion
  440. if pct < 1.0 {
  441. cc = &CloudCost{
  442. Properties: cloudCost.Properties,
  443. Window: window.Contract(setWindow),
  444. ListCost: cloudCost.ListCost.percent(pct),
  445. NetCost: cloudCost.NetCost.percent(pct),
  446. AmortizedNetCost: cloudCost.AmortizedNetCost.percent(pct),
  447. InvoicedCost: cloudCost.InvoicedCost.percent(pct),
  448. AmortizedCost: cloudCost.AmortizedCost.percent(pct),
  449. }
  450. }
  451. err := ccs.Insert(cc)
  452. if err != nil {
  453. log.Errorf("CloudCostSetRange: LoadCloudCost: failed to load CloudCost with window %s: %s", setWindow.String(), err.Error())
  454. }
  455. // If all cost has been inserted, then there is no need to check later days in the range
  456. totalPct += pct
  457. if totalPct >= 1.0 {
  458. return
  459. }
  460. }
  461. }
  462. const (
  463. ListCostMetric string = "ListCost"
  464. NetCostMetric string = "NetCost"
  465. AmortizedNetCostMetric string = "AmortizedNetCost"
  466. InvoicedCostMetric string = "InvoicedCost"
  467. AmortizedCostMetric string = "AmortizedCost"
  468. )
  469. type CostMetric struct {
  470. Cost float64 `json:"cost"`
  471. KubernetesPercent float64 `json:"kubernetesPercent"`
  472. }
  473. func (cm CostMetric) Equal(that CostMetric) bool {
  474. return cm.Cost == that.Cost && cm.KubernetesPercent == that.KubernetesPercent
  475. }
  476. func (cm CostMetric) Clone() CostMetric {
  477. return CostMetric{
  478. Cost: cm.Cost,
  479. KubernetesPercent: cm.KubernetesPercent,
  480. }
  481. }
  482. func (cm CostMetric) add(that CostMetric) CostMetric {
  483. // Compute KubernetesPercent for sum
  484. k8sPct := 0.0
  485. sumCost := cm.Cost + that.Cost
  486. if sumCost > 0.0 {
  487. thisK8sCost := cm.Cost * cm.KubernetesPercent
  488. thatK8sCost := that.Cost * that.KubernetesPercent
  489. k8sPct = (thisK8sCost + thatK8sCost) / sumCost
  490. }
  491. return CostMetric{
  492. Cost: sumCost,
  493. KubernetesPercent: k8sPct,
  494. }
  495. }
  496. // percent returns the product of the given percent and the cost, KubernetesPercent remains the same
  497. func (cm CostMetric) percent(pct float64) CostMetric {
  498. return CostMetric{
  499. Cost: cm.Cost * pct,
  500. KubernetesPercent: cm.KubernetesPercent,
  501. }
  502. }