clusterinfo.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package scrape
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/clusters"
  5. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  8. )
  9. type ClusterInfoScrapper struct {
  10. clusterUID string
  11. clusterInfoProvider clusters.ClusterInfoProvider
  12. }
  13. func newClusterInfoScrapper(clusterUID string, clusterInfoProvider clusters.ClusterInfoProvider) Scraper {
  14. return &ClusterInfoScrapper{
  15. clusterUID: clusterUID,
  16. clusterInfoProvider: clusterInfoProvider,
  17. }
  18. }
  19. func (cis *ClusterInfoScrapper) Scrape() []metric.Update {
  20. var scrapeResults []metric.Update
  21. // extract label values from cluster info provider
  22. clusterInfoMap := cis.clusterInfoProvider.GetClusterInfo()
  23. clusterName := clusterInfoMap[clusters.ClusterInfoIdKey]
  24. provider := clusterInfoMap[clusters.ClusterInfoProviderKey]
  25. accountID := clusterInfoMap[clusters.ClusterInfoAccountKey]
  26. // GCP special case
  27. if accountID == "" {
  28. accountID = clusterInfoMap[clusters.ClusterInfoProjectKey]
  29. }
  30. provisioner := clusterInfoMap[clusters.ClusterInfoProvisionerKey]
  31. region := clusterInfoMap[clusters.ClusterInfoRegionKey]
  32. clusterInfo := map[string]string{
  33. source.UIDLabel: cis.clusterUID,
  34. source.ClusterNameLabel: clusterName,
  35. source.ProviderLabel: provider,
  36. source.AccountIDLabel: accountID,
  37. source.ProvisionerNameLabel: provisioner,
  38. source.RegionLabel: region,
  39. source.KubeModelVersion: fmt.Sprintf("%d", kubemodel.DefaultCodecVersion),
  40. }
  41. scrapeResults = append(scrapeResults, metric.Update{
  42. Name: metric.ClusterInfo,
  43. Labels: clusterInfo,
  44. AdditionalInfo: clusterInfo,
  45. Value: 0,
  46. })
  47. return scrapeResults
  48. }