controllers.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. km "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. km.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. KubeModelPipelineResolutions: defaultPipelineExportResolutions(),
  50. }
  51. }
  52. // PipelineExportControllers is a facade that contains the export controllers for allocations, assets, and network insights.
  53. type PipelineExportControllers struct {
  54. AllocationExportController *export.ComputeExportControllerGroup[opencost.AllocationSet]
  55. AssetExportController *export.ComputeExportControllerGroup[opencost.AssetSet]
  56. NetworkInsightExportController *export.ComputeExportControllerGroup[opencost.NetworkInsightSet]
  57. KubeModelExportController *export.ComputeExportControllerGroup[kubemodel.KubeModelSet]
  58. }
  59. // NewPipelineExportControllers creates a new PipelineExportControllers instance with the given cluster ID, storage implementation, cost model, and configuration.
  60. // Setting the config to nil will use the default hourly and daily export resolutions for each pipeline.
  61. func NewPipelineExportControllers(clusterId string, store storage.Storage, cm ComputePipelineSource, config *PipelinesExportConfig) *PipelineExportControllers {
  62. if config == nil {
  63. config = DefaultPipelinesExportConfig()
  64. }
  65. mins := int(cm.GetDataSource().Resolution().Minutes())
  66. if mins <= 0 {
  67. mins = 1
  68. }
  69. // minimum source/query resolution
  70. sourceResolution := time.Duration(mins) * time.Minute
  71. // allocation sources and exporters
  72. allocSource := allocation.NewAllocationComputeSource(cm)
  73. allocExportControllers := []*export.ComputeExportController[opencost.AllocationSet]{}
  74. for _, res := range config.AllocationPiplineResolutions {
  75. if res < sourceResolution {
  76. 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()))
  77. continue
  78. }
  79. allocController, err := NewComputePipelineExportController(clusterId, store, allocSource, res)
  80. if err != nil {
  81. log.Errorf("Failed to create allocation export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  82. continue
  83. }
  84. allocExportControllers = append(allocExportControllers, allocController)
  85. }
  86. // asset sources and exporters
  87. assetSource := asset.NewAssetsComputeSource(cm)
  88. assetExportControllers := []*export.ComputeExportController[opencost.AssetSet]{}
  89. for _, res := range config.AssetPipelineResolutons {
  90. if res < sourceResolution {
  91. 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()))
  92. continue
  93. }
  94. assetController, err := NewComputePipelineExportController(clusterId, store, assetSource, res)
  95. if err != nil {
  96. log.Errorf("Failed to create asset export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  97. continue
  98. }
  99. assetExportControllers = append(assetExportControllers, assetController)
  100. }
  101. // network insights sources and exporters
  102. networkInsightSource := networkinsight.NewNetworkInsightsComputeSource(cm)
  103. networkInsightExportControllers := []*export.ComputeExportController[opencost.NetworkInsightSet]{}
  104. for _, res := range config.NetworkInsightPipelineResolutions {
  105. if res < sourceResolution {
  106. 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()))
  107. continue
  108. }
  109. networkInsightController, err := NewComputePipelineExportController(clusterId, store, networkInsightSource, res)
  110. if err != nil {
  111. log.Errorf("Failed to create network insight export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  112. continue
  113. }
  114. networkInsightExportControllers = append(networkInsightExportControllers, networkInsightController)
  115. }
  116. // KubeModel sources and exporters
  117. kubeModelSource := km.NewKubeModelComputeSource(cm)
  118. kubeModelExportControllers := []*export.ComputeExportController[kubemodel.KubeModelSet]{}
  119. for _, res := range config.KubeModelPipelineResolutions {
  120. if res < sourceResolution {
  121. 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()))
  122. continue
  123. }
  124. kubeModelController, err := NewComputePipelineExportController(clusterId, store, kubeModelSource, res)
  125. if err != nil {
  126. log.Errorf("Failed to create KubeModel export controller for resolution: %s - %v", timeutil.DurationString(res), err)
  127. continue
  128. }
  129. kubeModelExportControllers = append(kubeModelExportControllers, kubeModelController)
  130. }
  131. return &PipelineExportControllers{
  132. AllocationExportController: export.NewComputeExportControllerGroup(allocExportControllers...),
  133. AssetExportController: export.NewComputeExportControllerGroup(assetExportControllers...),
  134. NetworkInsightExportController: export.NewComputeExportControllerGroup(networkInsightExportControllers...),
  135. KubeModelExportController: export.NewComputeExportControllerGroup(kubeModelExportControllers...),
  136. }
  137. }
  138. func (pec *PipelineExportControllers) Start(interval time.Duration) {
  139. pec.AllocationExportController.Start(interval)
  140. pec.AssetExportController.Start(interval)
  141. pec.NetworkInsightExportController.Start(interval)
  142. }
  143. func (pec *PipelineExportControllers) Stop() {
  144. pec.AllocationExportController.Stop()
  145. pec.AssetExportController.Stop()
  146. pec.NetworkInsightExportController.Stop()
  147. }