controllers.go 8.2 KB

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