replicaset.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) computeReplicaSets(kms *kubemodel.KubeModelSet, start, end time.Time) error {
  9. grp := source.NewQueryGroup()
  10. metrics := km.ds.Metrics()
  11. replicaSetInfoResultFuture := source.WithGroup(grp, metrics.QueryReplicaSetInfo(start, end))
  12. replicaSetUptimeResultFuture := source.WithGroup(grp, metrics.QueryReplicaSetUptime(start, end))
  13. replicaSetOwnerResultFuture := source.WithGroup(grp, metrics.QueryReplicaSetOwners(start, end))
  14. replicaSetLabelsResultFuture := source.WithGroup(grp, metrics.QueryReplicaSetLabels(start, end))
  15. replicaSetAnnotationsResultFuture := source.WithGroup(grp, metrics.QueryReplicaSetAnnotations(start, end))
  16. replicaSetMap := make(map[string]*kubemodel.ReplicaSet)
  17. replicaSetInfoResult, _ := replicaSetInfoResultFuture.Await()
  18. for _, res := range replicaSetInfoResult {
  19. replicaSetMap[res.UID] = &kubemodel.ReplicaSet{
  20. UID: res.UID,
  21. Name: res.ReplicaSet,
  22. NamespaceUID: res.NamespaceUID,
  23. }
  24. }
  25. replicaSetUptimeResult, _ := replicaSetUptimeResultFuture.Await()
  26. for _, res := range replicaSetUptimeResult {
  27. replicaSet, ok := replicaSetMap[res.UID]
  28. if !ok {
  29. log.Warnf("replicaset 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. replicaSet.Start = s
  34. replicaSet.End = e
  35. }
  36. replicaSetOwnersResult, _ := replicaSetOwnerResultFuture.Await()
  37. for _, res := range replicaSetOwnersResult {
  38. replicaSet, ok := replicaSetMap[res.UID]
  39. if !ok {
  40. log.Warnf("replicaset with UID '%s' has not been initialized to add owner", res.UID)
  41. continue
  42. }
  43. replicaSet.Owners = append(replicaSet.Owners, kubemodel.Owner{
  44. UID: res.OwnerUID,
  45. Kind: kubemodel.ParseOwnerKind(res.OwnerKind),
  46. Controller: res.Controller,
  47. })
  48. }
  49. replicaSetLabelsResult, _ := replicaSetLabelsResultFuture.Await()
  50. for _, res := range replicaSetLabelsResult {
  51. replicaSet, ok := replicaSetMap[res.UID]
  52. if !ok {
  53. log.Warnf("replicaset with UID '%s' has not been initialized to add labels", res.UID)
  54. continue
  55. }
  56. replicaSet.Labels = res.Labels
  57. }
  58. replicaSetAnnotationsResult, _ := replicaSetAnnotationsResultFuture.Await()
  59. for _, res := range replicaSetAnnotationsResult {
  60. replicaSet, ok := replicaSetMap[res.UID]
  61. if !ok {
  62. log.Warnf("replicaset with UID '%s' has not been initialized to add annotations", res.UID)
  63. continue
  64. }
  65. replicaSet.Annotations = res.Annotations
  66. }
  67. for _, replicaSet := range replicaSetMap {
  68. err := kms.RegisterReplicaSet(replicaSet)
  69. if err != nil {
  70. log.Warnf("Failed to register replicaset: %s", err.Error())
  71. }
  72. }
  73. return nil
  74. }