cronjob.go 2.1 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) computeCronJobs(kms *kubemodel.KubeModelSet, start, end time.Time) error {
  9. grp := source.NewQueryGroup()
  10. metrics := km.ds.Metrics()
  11. cronJobInfoResultFuture := source.WithGroup(grp, metrics.QueryCronJobInfo(start, end))
  12. cronJobUptimeResultFuture := source.WithGroup(grp, metrics.QueryCronJobUptime(start, end))
  13. cronJobLabelsResultFuture := source.WithGroup(grp, metrics.QueryCronJobLabels(start, end))
  14. cronJobAnnotationsResultFuture := source.WithGroup(grp, metrics.QueryCronJobAnnotations(start, end))
  15. cronJobMap := make(map[string]*kubemodel.CronJob)
  16. cronJobInfoResult, _ := cronJobInfoResultFuture.Await()
  17. for _, res := range cronJobInfoResult {
  18. cronJobMap[res.UID] = &kubemodel.CronJob{
  19. UID: res.UID,
  20. Name: res.CronJob,
  21. NamespaceUID: res.NamespaceUID,
  22. }
  23. }
  24. cronJobUptimeResult, _ := cronJobUptimeResultFuture.Await()
  25. for _, res := range cronJobUptimeResult {
  26. cronJob, ok := cronJobMap[res.UID]
  27. if !ok {
  28. log.Warnf("cronjob 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. cronJob.Start = s
  33. cronJob.End = e
  34. }
  35. cronJobLabelsResult, _ := cronJobLabelsResultFuture.Await()
  36. for _, res := range cronJobLabelsResult {
  37. cronJob, ok := cronJobMap[res.UID]
  38. if !ok {
  39. log.Warnf("cronjob with UID '%s' has not been initialized to add labels", res.UID)
  40. continue
  41. }
  42. cronJob.Labels = res.Labels
  43. }
  44. cronJobAnnotationsResult, _ := cronJobAnnotationsResultFuture.Await()
  45. for _, res := range cronJobAnnotationsResult {
  46. cronJob, ok := cronJobMap[res.UID]
  47. if !ok {
  48. log.Warnf("cronjob with UID '%s' has not been initialized to add annotations", res.UID)
  49. continue
  50. }
  51. cronJob.Annotations = res.Annotations
  52. }
  53. for _, cronJob := range cronJobMap {
  54. err := kms.RegisterCronJob(cronJob)
  55. if err != nil {
  56. log.Warnf("Failed to register cronjob: %s", err.Error())
  57. }
  58. }
  59. return nil
  60. }