controllers.go 8.6 KB

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