controllers.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package exporter
  2. import (
  3. "time"
  4. export "github.com/opencost/opencost/core/pkg/exporter"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/opencost/opencost/core/pkg/opencost/exporter/allocation"
  8. "github.com/opencost/opencost/core/pkg/opencost/exporter/asset"
  9. "github.com/opencost/opencost/core/pkg/opencost/exporter/networkinsight"
  10. "github.com/opencost/opencost/core/pkg/source"
  11. "github.com/opencost/opencost/core/pkg/storage"
  12. "github.com/opencost/opencost/core/pkg/util/timeutil"
  13. )
  14. // ComputePipelineSource is an interface that defines methods for computing all pipeline data.
  15. // For all intents and purposes, this represents costmodel.CostModel. To interface allows tests to
  16. // mock the costmodel.CostModel and return a different source for the pipeline.
  17. type ComputePipelineSource interface {
  18. allocation.AllocationSource
  19. asset.AssetSource
  20. networkinsight.NetworkInsightSource
  21. GetDataSource() source.OpenCostDataSource
  22. }
  23. // PipelinesExportConfig is a configuration struct that contains the export resolutions for
  24. // allocation, assets, and network insights pipelines.
  25. type PipelinesExportConfig struct {
  26. AllocationPiplineResolutions []time.Duration
  27. AssetPipelineResolutons []time.Duration
  28. NetworkInsightPipelineResolutions []time.Duration
  29. }
  30. // defaultPipelineExportResolutions returns the default export configuration for the pipeline
  31. // which is set to export hourly and daily.
  32. func defaultPipelineExportResolutions() []time.Duration {
  33. return []time.Duration{
  34. time.Hour,
  35. 24 * time.Hour,
  36. }
  37. }
  38. // DefaultPipelinesExportConfig returns the default export configuration for all pipelines
  39. // which is set to export hourly and daily for allocations, assets, and network insights.
  40. func DefaultPipelinesExportConfig() *PipelinesExportConfig {
  41. return &PipelinesExportConfig{
  42. AllocationPiplineResolutions: defaultPipelineExportResolutions(),
  43. AssetPipelineResolutons: defaultPipelineExportResolutions(),
  44. NetworkInsightPipelineResolutions: defaultPipelineExportResolutions(),
  45. }
  46. }
  47. // PipelineExportControllers is a facade that contains the export controllers for allocations, assets, and network insights.
  48. type PipelineExportControllers struct {
  49. AllocationExportController *export.ComputeExportControllerGroup[opencost.AllocationSet]
  50. AssetExportController *export.ComputeExportControllerGroup[opencost.AssetSet]
  51. NetworkInsightExportController *export.ComputeExportControllerGroup[opencost.NetworkInsightSet]
  52. }
  53. // NewPipelineExportControllers creates a new PipelineExportControllers instance with the given cluster ID, storage implementation, cost model, and configuration.
  54. // Setting the config to nil will use the default hourly and daily export resolutions for each pipeline.
  55. func NewPipelineExportControllers(clusterId string, store storage.Storage, cm ComputePipelineSource, config *PipelinesExportConfig) *PipelineExportControllers {
  56. if config == nil {
  57. config = DefaultPipelinesExportConfig()
  58. }
  59. mins := int(cm.GetDataSource().Resolution().Minutes())
  60. if mins <= 0 {
  61. mins = 1
  62. }
  63. // minimum source/query resolution
  64. sourceResolution := time.Duration(mins) * time.Minute
  65. // allocation sources and exporters
  66. allocSource := allocation.NewAllocationComputeSource(cm)
  67. allocExportControllers := []*export.ComputeExportController[opencost.AllocationSet]{}
  68. for _, res := range config.AllocationPiplineResolutions {
  69. if res < sourceResolution {
  70. log.Warnf("Configured allocation pipeline resolution %dm is less than source resolution %dm. Not configuring the exporter for this resolution.", int64(res.Minutes()), int64(sourceResolution.Minutes()))
  71. continue
  72. }
  73. allocController, err := NewComputePipelineExportController(clusterId, store, allocSource, res, sourceResolution)
  74. if err != nil {
  75. log.Errorf("Failed to create allocation export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  76. continue
  77. }
  78. allocExportControllers = append(allocExportControllers, allocController)
  79. }
  80. // asset sources and exporters
  81. assetSource := asset.NewAssetsComputeSource(cm)
  82. assetExportControllers := []*export.ComputeExportController[opencost.AssetSet]{}
  83. for _, res := range config.AssetPipelineResolutons {
  84. if res < sourceResolution {
  85. log.Warnf("Configured asset pipeline resolution %dm is less than source resolution %dm. Not configuring the exporter for this resolution.", int64(res.Minutes()), int64(sourceResolution.Minutes()))
  86. continue
  87. }
  88. assetController, err := NewComputePipelineExportController(clusterId, store, assetSource, res, sourceResolution)
  89. if err != nil {
  90. log.Errorf("Failed to create asset export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  91. continue
  92. }
  93. assetExportControllers = append(assetExportControllers, assetController)
  94. }
  95. // network insights sources and exporters
  96. networkInsightSource := networkinsight.NewNetworkInsightsComputeSource(cm)
  97. networkInsightExportControllers := []*export.ComputeExportController[opencost.NetworkInsightSet]{}
  98. for _, res := range config.NetworkInsightPipelineResolutions {
  99. if res < sourceResolution {
  100. log.Warnf("Configured network insight pipeline resolution %dm is less than source resolution %dm. Not configuring the exporter for this resolution.", int64(res.Minutes()), int64(sourceResolution.Minutes()))
  101. continue
  102. }
  103. networkInsightController, err := NewComputePipelineExportController(clusterId, store, networkInsightSource, res, sourceResolution)
  104. if err != nil {
  105. log.Errorf("Failed to create network insight export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  106. continue
  107. }
  108. networkInsightExportControllers = append(networkInsightExportControllers, networkInsightController)
  109. }
  110. return &PipelineExportControllers{
  111. AllocationExportController: export.NewComputeExportControllerGroup(allocExportControllers...),
  112. AssetExportController: export.NewComputeExportControllerGroup(assetExportControllers...),
  113. NetworkInsightExportController: export.NewComputeExportControllerGroup(networkInsightExportControllers...),
  114. }
  115. }
  116. func (pec *PipelineExportControllers) Start(interval time.Duration) {
  117. pec.AllocationExportController.Start(interval)
  118. pec.AssetExportController.Start(interval)
  119. pec.NetworkInsightExportController.Start(interval)
  120. }
  121. func (pec *PipelineExportControllers) Stop() {
  122. pec.AllocationExportController.Stop()
  123. pec.AssetExportController.Stop()
  124. pec.NetworkInsightExportController.Stop()
  125. }