controllers.go 7.6 KB

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