2
0

controllers.go 8.0 KB

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