2
0

namespace.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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) computeNamespaces(kms *kubemodel.KubeModelSet, start, end time.Time) error {
  9. grp := source.NewQueryGroup()
  10. metrics := km.ds.Metrics()
  11. nsInfoResultFuture := source.WithGroup(grp, metrics.QueryNamespaceInfo(start, end))
  12. nsUptimeResultFuture := source.WithGroup(grp, metrics.QueryNamespaceUptime(start, end))
  13. nsLabelsResultFuture := source.WithGroup(grp, metrics.QueryNamespaceLabels(start, end))
  14. nsAnnosResultFuture := source.WithGroup(grp, metrics.QueryNamespaceAnnotations(start, end))
  15. nsMap := make(map[string]*kubemodel.Namespace)
  16. // Initialize namespaces from info
  17. nsInfoResult, _ := nsInfoResultFuture.Await()
  18. for _, res := range nsInfoResult {
  19. nsMap[res.UID] = &kubemodel.Namespace{
  20. UID: res.UID,
  21. Name: res.Namespace,
  22. }
  23. }
  24. nsUptimeResult, _ := nsUptimeResultFuture.Await()
  25. for _, res := range nsUptimeResult {
  26. ns, ok := nsMap[res.UID]
  27. if !ok {
  28. log.Warnf("namespace with UID '%s' has not been initialized to add uptime", res.UID)
  29. continue
  30. }
  31. s, e := res.GetStartEnd(start, end, km.ds.Resolution())
  32. ns.Start = s
  33. ns.End = e
  34. }
  35. nsLabelsResult, _ := nsLabelsResultFuture.Await()
  36. for _, res := range nsLabelsResult {
  37. ns, ok := nsMap[res.UID]
  38. if !ok {
  39. log.Warnf("namespace with UID '%s' has not been initialized to add labels", res.UID)
  40. continue
  41. }
  42. ns.Labels = res.Labels
  43. }
  44. nsAnnosResult, _ := nsAnnosResultFuture.Await()
  45. for _, res := range nsAnnosResult {
  46. ns, ok := nsMap[res.UID]
  47. if !ok {
  48. log.Warnf("namespace with UID '%s' has not been initialized to add annotations", res.UID)
  49. continue
  50. }
  51. ns.Annotations = res.Annotations
  52. }
  53. for _, namespace := range nsMap {
  54. err := kms.RegisterNamespace(namespace)
  55. if err != nil {
  56. log.Warnf("Failed to register namespace: %s", err.Error())
  57. }
  58. }
  59. return nil
  60. }