deployment.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package kubemodel
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/log"
  5. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. )
  8. func (km *KubeModel) computeDeployments(kms *kubemodel.KubeModelSet, start, end time.Time) error {
  9. grp := source.NewQueryGroup()
  10. metrics := km.ds.Metrics()
  11. deploymentInfoResultFuture := source.WithGroup(grp, metrics.QueryDeploymentInfo(start, end))
  12. deploymentUptimeResultFuture := source.WithGroup(grp, metrics.QueryDeploymentUptime(start, end))
  13. deploymentLabelsResultFuture := source.WithGroup(grp, metrics.QueryDeploymentLabels(start, end))
  14. deploymentAnnotationsResultFuture := source.WithGroup(grp, metrics.QueryDeploymentAnnotations(start, end))
  15. deploymentMatchLabelsResultFuture := source.WithGroup(grp, metrics.QueryDeploymentMatchLabels(start, end))
  16. deploymentMap := make(map[string]*kubemodel.Deployment)
  17. deploymentInfoResult, _ := deploymentInfoResultFuture.Await()
  18. for _, res := range deploymentInfoResult {
  19. deploymentMap[res.UID] = &kubemodel.Deployment{
  20. UID: res.UID,
  21. Name: res.Deployment,
  22. NamespaceUID: res.NamespaceUID,
  23. }
  24. }
  25. deploymentUptimeResult, _ := deploymentUptimeResultFuture.Await()
  26. for _, res := range deploymentUptimeResult {
  27. deployment, ok := deploymentMap[res.UID]
  28. if !ok {
  29. log.Warnf("deployment with UID '%s' has not been initialized to add uptime", res.UID)
  30. continue
  31. }
  32. s, e := res.GetStartEnd(start, end, km.ds.Resolution())
  33. deployment.Start = s
  34. deployment.End = e
  35. }
  36. deploymentLabelsResult, _ := deploymentLabelsResultFuture.Await()
  37. for _, res := range deploymentLabelsResult {
  38. deployment, ok := deploymentMap[res.UID]
  39. if !ok {
  40. log.Warnf("deployment with UID '%s' has not been initialized to add labels", res.UID)
  41. continue
  42. }
  43. deployment.Labels = res.Labels
  44. }
  45. deploymentAnnotationsResult, _ := deploymentAnnotationsResultFuture.Await()
  46. for _, res := range deploymentAnnotationsResult {
  47. deployment, ok := deploymentMap[res.UID]
  48. if !ok {
  49. log.Warnf("deployment with UID '%s' has not been initialized to add annotations", res.UID)
  50. continue
  51. }
  52. deployment.Annotations = res.Annotations
  53. }
  54. deploymentMatchLabelsResult, _ := deploymentMatchLabelsResultFuture.Await()
  55. for _, res := range deploymentMatchLabelsResult {
  56. deployment, ok := deploymentMap[res.UID]
  57. if !ok {
  58. log.Warnf("deployment with UID '%s' has not been initialized to add match labels", res.UID)
  59. continue
  60. }
  61. deployment.MatchLabels = res.Labels
  62. }
  63. for _, deployment := range deploymentMap {
  64. err := kms.RegisterDeployment(deployment)
  65. if err != nil {
  66. log.Warnf("Failed to register deployment: %s", err.Error())
  67. }
  68. }
  69. return nil
  70. }