bigqueryquerier.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package gcp
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "cloud.google.com/go/bigquery"
  7. cloudconfig "github.com/opencost/opencost/pkg/cloud/config"
  8. "github.com/opencost/opencost/pkg/kubecost"
  9. )
  10. type BigQueryQuerier struct {
  11. BigQueryConfiguration
  12. }
  13. func (bqq *BigQueryQuerier) Equals(config cloudconfig.Config) bool {
  14. thatConfig, ok := config.(*BigQueryQuerier)
  15. if !ok {
  16. return false
  17. }
  18. return bqq.BigQueryConfiguration.Equals(&thatConfig.BigQueryConfiguration)
  19. }
  20. func (bqq *BigQueryQuerier) QueryBigQuery(ctx context.Context, queryStr string) (*bigquery.RowIterator, error) {
  21. client, err := bqq.GetBigQueryClient(ctx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. query := client.Query(queryStr)
  26. return query.Read(ctx)
  27. }
  28. func GCPSelectCategory(service, description string) string {
  29. s := strings.ToLower(service)
  30. d := strings.ToLower(description)
  31. // Network descriptions
  32. if strings.Contains(d, "download") {
  33. return kubecost.NetworkCategory
  34. }
  35. if strings.Contains(d, "network") {
  36. return kubecost.NetworkCategory
  37. }
  38. if strings.Contains(d, "ingress") {
  39. return kubecost.NetworkCategory
  40. }
  41. if strings.Contains(d, "egress") {
  42. return kubecost.NetworkCategory
  43. }
  44. if strings.Contains(d, "static ip") {
  45. return kubecost.NetworkCategory
  46. }
  47. if strings.Contains(d, "external ip") {
  48. return kubecost.NetworkCategory
  49. }
  50. if strings.Contains(d, "load balanced") {
  51. return kubecost.NetworkCategory
  52. }
  53. if strings.Contains(d, "licensing fee") {
  54. return kubecost.OtherCategory
  55. }
  56. // Storage Descriptions
  57. if strings.Contains(d, "storage") {
  58. return kubecost.StorageCategory
  59. }
  60. if strings.Contains(d, "pd capacity") {
  61. return kubecost.StorageCategory
  62. }
  63. if strings.Contains(d, "pd iops") {
  64. return kubecost.StorageCategory
  65. }
  66. if strings.Contains(d, "pd snapshot") {
  67. return kubecost.StorageCategory
  68. }
  69. // Service Defaults
  70. if strings.Contains(s, "storage") {
  71. return kubecost.StorageCategory
  72. }
  73. if strings.Contains(s, "compute") {
  74. return kubecost.ComputeCategory
  75. }
  76. if strings.Contains(s, "sql") {
  77. return kubecost.StorageCategory
  78. }
  79. if strings.Contains(s, "bigquery") {
  80. return kubecost.StorageCategory
  81. }
  82. if strings.Contains(s, "kubernetes") {
  83. return kubecost.ManagementCategory
  84. } else if strings.Contains(s, "pub/sub") {
  85. return kubecost.NetworkCategory
  86. }
  87. return kubecost.OtherCategory
  88. }
  89. var parseProviderIDRx = regexp.MustCompile("^.+\\/(.+)?") // Capture "gke-cluster-3-default-pool-xxxx-yy" from "projects/###/instances/gke-cluster-3-default-pool-xxxx-yy"
  90. func GCPParseProviderID(id string) string {
  91. match := parseProviderIDRx.FindStringSubmatch(id)
  92. if len(match) == 0 {
  93. return id
  94. }
  95. return match[len(match)-1]
  96. }