provider.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package provider
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/opencost/opencost/core/pkg/util/retry"
  13. "github.com/opencost/opencost/pkg/cloud/alibaba"
  14. "github.com/opencost/opencost/pkg/cloud/aws"
  15. "github.com/opencost/opencost/pkg/cloud/azure"
  16. "github.com/opencost/opencost/pkg/cloud/digitalocean"
  17. "github.com/opencost/opencost/pkg/cloud/gcp"
  18. "github.com/opencost/opencost/pkg/cloud/models"
  19. "github.com/opencost/opencost/pkg/cloud/oracle"
  20. "github.com/opencost/opencost/pkg/cloud/otc"
  21. "github.com/opencost/opencost/pkg/cloud/scaleway"
  22. "github.com/opencost/opencost/core/pkg/opencost"
  23. "github.com/opencost/opencost/core/pkg/util"
  24. "cloud.google.com/go/compute/metadata"
  25. "github.com/opencost/opencost/core/pkg/clustercache"
  26. "github.com/opencost/opencost/core/pkg/log"
  27. "github.com/opencost/opencost/core/pkg/util/httputil"
  28. "github.com/opencost/opencost/pkg/config"
  29. "github.com/opencost/opencost/pkg/env"
  30. "github.com/opencost/opencost/pkg/util/watcher"
  31. )
  32. // CustomPricesEnabled returns the boolean equivalent of the cloup provider's custom prices flag,
  33. // indicating whether or not the cluster is using custom pricing.
  34. func CustomPricesEnabled(p models.Provider) bool {
  35. config, err := p.GetConfig()
  36. if err != nil {
  37. return false
  38. }
  39. // TODO:CLEANUP what is going on with this?
  40. if config.NegotiatedDiscount == "" {
  41. config.NegotiatedDiscount = "0%"
  42. }
  43. return config.CustomPricesEnabled == "true"
  44. }
  45. // ConfigWatcherFor returns a new ConfigWatcher instance which watches changes to the "pricing-configs"
  46. // configmap
  47. func ConfigWatcherFor(p models.Provider) *watcher.ConfigMapWatcher {
  48. return &watcher.ConfigMapWatcher{
  49. ConfigMapName: env.GetPricingConfigmapName(),
  50. WatchFunc: func(name string, data map[string]string) error {
  51. _, err := p.UpdateConfigFromConfigMap(data)
  52. return err
  53. },
  54. }
  55. }
  56. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  57. func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.ConfigFileManager) (models.Provider, error) {
  58. getAllNodesFunc := func() ([]*clustercache.Node, error) {
  59. nodes := cache.GetAllNodes()
  60. if len(nodes) == 0 {
  61. return nil, fmt.Errorf("no nodes found in cluster cache")
  62. }
  63. return nodes, nil
  64. }
  65. var nodes []*clustercache.Node
  66. if env.HasKubernetesResourceAccess() {
  67. // the error can be ignored because getAllNodesFunc only errors if nodes is empty, a case which we explicitly
  68. // handle by checking the length of nodes below
  69. nodes, _ = retry.Retry(context.Background(), getAllNodesFunc, 10, time.Second)
  70. } else {
  71. nodes, _ = getAllNodesFunc()
  72. }
  73. if len(nodes) == 0 {
  74. log.Infof("Could not locate any nodes for cluster.")
  75. return &CustomProvider{
  76. Clientset: cache,
  77. Config: NewProviderConfig(config, "default.json"),
  78. }, nil
  79. }
  80. cp := getClusterProperties(nodes[0])
  81. // If provider is DEFAULT, check for explicitly set cloud provider from environment variable
  82. envProvider := env.GetCloudProvider()
  83. if cp.provider == "DEFAULT" && envProvider != "" {
  84. log.Infof("Using cloud provider from environment variable: %s", envProvider)
  85. cp.provider = envProvider
  86. switch envProvider {
  87. case opencost.AWSProvider:
  88. cp.configFileName = "aws.json"
  89. case opencost.AzureProvider:
  90. cp.configFileName = "azure.json"
  91. case opencost.GCPProvider:
  92. cp.configFileName = "gcp.json"
  93. case opencost.AlibabaProvider:
  94. cp.configFileName = "alibaba.json"
  95. case opencost.OracleProvider:
  96. cp.configFileName = "oracle.json"
  97. case opencost.ScalewayProvider:
  98. cp.configFileName = "scaleway.json"
  99. case opencost.OTCProvider:
  100. cp.configFileName = "otc.json"
  101. case opencost.CSVProvider:
  102. cp.configFileName = "default.json"
  103. }
  104. }
  105. providerConfig := NewProviderConfig(config, cp.configFileName)
  106. // If ClusterAccount is set apply it to the cluster properties
  107. if providerConfig.customPricing != nil && providerConfig.customPricing.ClusterAccountID != "" {
  108. cp.accountID = providerConfig.customPricing.ClusterAccountID
  109. }
  110. providerConfig.Update(func(cp *models.CustomPricing) error {
  111. if cp.ServiceKeyName == "AKIXXX" {
  112. cp.ServiceKeyName = ""
  113. }
  114. return nil
  115. })
  116. switch cp.provider {
  117. case opencost.CSVProvider:
  118. log.Infof("Using CSV Provider with CSV at %s", env.GetCSVPath())
  119. return &CSVProvider{
  120. CSVLocation: env.GetCSVPath(),
  121. CustomProvider: &CustomProvider{
  122. Clientset: cache,
  123. ClusterRegion: cp.region,
  124. ClusterAccountID: cp.accountID,
  125. Config: NewProviderConfig(config, cp.configFileName),
  126. },
  127. }, nil
  128. case opencost.GCPProvider:
  129. log.Info("Found ProviderID starting with \"gce\", using GCP Provider")
  130. if apiKey == "" {
  131. return nil, errors.New("Supply a GCP Key to start getting data")
  132. }
  133. return &gcp.GCP{
  134. Clientset: cache,
  135. APIKey: apiKey,
  136. Config: NewProviderConfig(config, cp.configFileName),
  137. ClusterRegion: cp.region,
  138. ClusterAccountID: cp.accountID,
  139. ClusterProjectID: cp.projectID,
  140. MetadataClient: metadata.NewClient(
  141. &http.Client{
  142. Transport: httputil.NewUserAgentTransport("kubecost", &http.Transport{
  143. Dial: (&net.Dialer{
  144. Timeout: 2 * time.Second,
  145. KeepAlive: 30 * time.Second,
  146. }).Dial,
  147. }),
  148. Timeout: 5 * time.Second,
  149. }),
  150. }, nil
  151. case opencost.AWSProvider:
  152. log.Info("Found ProviderID starting with \"aws\", using AWS Provider")
  153. return &aws.AWS{
  154. Clientset: cache,
  155. Config: NewProviderConfig(config, cp.configFileName),
  156. ClusterRegion: cp.region,
  157. ClusterAccountID: cp.accountID,
  158. ServiceAccountChecks: models.NewServiceAccountChecks(),
  159. }, nil
  160. case opencost.AzureProvider:
  161. log.Info("Found ProviderID starting with \"azure\", using Azure Provider")
  162. return &azure.Azure{
  163. Clientset: cache,
  164. Config: NewProviderConfig(config, cp.configFileName),
  165. ClusterRegion: cp.region,
  166. ClusterAccountID: cp.accountID,
  167. ServiceAccountChecks: models.NewServiceAccountChecks(),
  168. }, nil
  169. case opencost.AlibabaProvider:
  170. log.Info("Found ProviderID starting with \"alibaba\", using Alibaba Cloud Provider")
  171. return &alibaba.Alibaba{
  172. Clientset: cache,
  173. Config: NewProviderConfig(config, cp.configFileName),
  174. ClusterRegion: cp.region,
  175. ClusterAccountId: cp.accountID,
  176. ServiceAccountChecks: models.NewServiceAccountChecks(),
  177. }, nil
  178. case opencost.ScalewayProvider:
  179. log.Info("Found ProviderID starting with \"scaleway\", using Scaleway Provider")
  180. return &scaleway.Scaleway{
  181. Clientset: cache,
  182. ClusterRegion: cp.region,
  183. ClusterAccountID: cp.accountID,
  184. Config: NewProviderConfig(config, cp.configFileName),
  185. }, nil
  186. case opencost.OracleProvider:
  187. log.Info("Found ProviderID starting with \"oracle\", using Oracle Provider")
  188. return &oracle.Oracle{
  189. Clientset: cache,
  190. Config: NewProviderConfig(config, cp.configFileName),
  191. ClusterRegion: cp.region,
  192. ClusterAccountID: cp.accountID,
  193. ServiceAccountChecks: models.NewServiceAccountChecks(),
  194. }, nil
  195. case opencost.OTCProvider:
  196. log.Info("Found node label \"cce.cloud.com/cce-nodepool\", using OTC Provider")
  197. return &otc.OTC{
  198. Clientset: cache,
  199. Config: NewProviderConfig(config, cp.configFileName),
  200. ClusterRegion: cp.region,
  201. }, nil
  202. case opencost.DigitalOceanProvider:
  203. log.Info("Detected DigitalOcean, using DOKS")
  204. return &digitalocean.DOKS{
  205. Config: NewProviderConfig(config, cp.configFileName),
  206. Cache: digitalocean.NewPricingCache(),
  207. Products: make(map[string][]digitalocean.DOProduct),
  208. Clientset: cache,
  209. ClusterManagementCost: 0.0,
  210. }, nil
  211. default:
  212. log.Info("Unsupported provider, falling back to default")
  213. return &CustomProvider{
  214. Clientset: cache,
  215. ClusterRegion: cp.region,
  216. ClusterAccountID: cp.accountID,
  217. Config: NewProviderConfig(config, cp.configFileName),
  218. }, nil
  219. }
  220. }
  221. type clusterProperties struct {
  222. provider string
  223. configFileName string
  224. region string
  225. accountID string
  226. projectID string
  227. }
  228. func getClusterProperties(node *clustercache.Node) clusterProperties {
  229. providerID := strings.ToLower(node.SpecProviderID)
  230. region, _ := util.GetRegion(node.Labels)
  231. cp := clusterProperties{
  232. provider: "DEFAULT",
  233. configFileName: "default.json",
  234. region: region,
  235. accountID: "",
  236. projectID: "",
  237. }
  238. // Check for custom provider settings
  239. if env.IsUseCustomProvider() {
  240. // Use CSV provider if set
  241. if env.IsUseCSVProvider() {
  242. log.Debug("using custom CSV provider")
  243. cp.provider = opencost.CSVProvider
  244. }
  245. return cp
  246. }
  247. // The second conditional is mainly if you're running opencost outside of GCE, say in a local environment.
  248. if metadata.OnGCE() || strings.HasPrefix(providerID, "gce") {
  249. log.Debug("using GCP provider")
  250. cp.provider = opencost.GCPProvider
  251. cp.configFileName = "gcp.json"
  252. cp.projectID = gcp.ParseGCPProjectID(providerID)
  253. } else if strings.HasPrefix(providerID, "aws") {
  254. log.Debug("using AWS provider")
  255. cp.provider = opencost.AWSProvider
  256. cp.configFileName = "aws.json"
  257. } else if strings.Contains(node.Status.NodeInfo.KubeletVersion, "eks") { // Additional check for EKS, via kubelet check
  258. log.Debug("using AWS provider from EKS")
  259. cp.provider = opencost.AWSProvider
  260. cp.configFileName = "aws.json"
  261. } else if strings.HasPrefix(providerID, "azure") {
  262. log.Debug("using Azure provider")
  263. cp.provider = opencost.AzureProvider
  264. cp.configFileName = "azure.json"
  265. cp.accountID = azure.ParseAzureSubscriptionID(providerID)
  266. } else if strings.HasPrefix(providerID, "scaleway") { // the scaleway provider ID looks like scaleway://instance/<instance_id>
  267. log.Debug("using Scaleway provider")
  268. cp.provider = opencost.ScalewayProvider
  269. cp.configFileName = "scaleway.json"
  270. } else if strings.Contains(node.Status.NodeInfo.KubeletVersion, "aliyun") { // provider ID is not prefix with any distinct keyword like other providers
  271. log.Debug("using Alibaba provider")
  272. cp.provider = opencost.AlibabaProvider
  273. cp.configFileName = "alibaba.json"
  274. } else if strings.HasPrefix(providerID, "ocid") {
  275. log.Debug("using Oracle provider")
  276. cp.provider = opencost.OracleProvider
  277. cp.configFileName = "oracle.json"
  278. } else if _, ok := node.Labels["cce.cloud.com/cce-nodepool"]; ok { // The node label "cce.cloud.com/cce-nodepool" exists
  279. log.Debug("using OTC provider")
  280. cp.provider = opencost.OTCProvider
  281. cp.configFileName = "otc.json"
  282. } else if strings.HasPrefix(providerID, "digitalocean") {
  283. log.Debug("using DigitalOcean provider")
  284. cp.provider = opencost.DigitalOceanProvider
  285. cp.configFileName = "digitalocean.json"
  286. }
  287. // Override provider to CSV if CSVProvider is used and custom provider is not set
  288. if env.IsUseCSVProvider() {
  289. log.Debug("using CSV provider")
  290. cp.provider = opencost.CSVProvider
  291. }
  292. return cp
  293. }
  294. var (
  295. // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
  296. providerAWSRegex = regexp.MustCompile("aws://[^/]*/[^/]*/([^/]+)")
  297. // gce://guestbook-227502/us-central1-a/gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
  298. // => gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
  299. providerGCERegex = regexp.MustCompile("gce://[^/]*/[^/]*/([^/]+)")
  300. // Capture "vol-0fc54c5e83b8d2b76" from "aws://us-east-2a/vol-0fc54c5e83b8d2b76"
  301. persistentVolumeAWSRegex = regexp.MustCompile("aws:/[^/]*/[^/]*/([^/]+)")
  302. // Capture "ad9d88195b52a47c89b5055120f28c58" from "ad9d88195b52a47c89b5055120f28c58-1037804914.us-east-2.elb.amazonaws.com"
  303. loadBalancerAWSRegex = regexp.MustCompile("^([^-]+)-.+amazonaws\\.com$")
  304. )
  305. // ParseID attempts to parse a ProviderId from a string based on formats from the various providers and
  306. // returns the string as is if it cannot find a match
  307. func ParseID(id string) string {
  308. match := providerAWSRegex.FindStringSubmatch(id)
  309. if len(match) >= 2 {
  310. return match[1]
  311. }
  312. match = providerGCERegex.FindStringSubmatch(id)
  313. if len(match) >= 2 {
  314. return match[1]
  315. }
  316. // Return id for Azure Provider, CSV Provider and Custom Provider
  317. return id
  318. }
  319. // ParsePVID attempts to parse a PV ProviderId from a string based on formats from the various providers and
  320. // returns the string as is if it cannot find a match
  321. func ParsePVID(id string) string {
  322. match := persistentVolumeAWSRegex.FindStringSubmatch(id)
  323. if len(match) >= 2 {
  324. return match[1]
  325. }
  326. // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
  327. return id
  328. }
  329. // ParseLBID attempts to parse a LB ProviderId from a string based on formats from the various providers and
  330. // returns the string as is if it cannot find a match
  331. func ParseLBID(id string) string {
  332. match := loadBalancerAWSRegex.FindStringSubmatch(id)
  333. if len(match) >= 2 {
  334. return match[1]
  335. }
  336. // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
  337. return id
  338. }
  339. // ParseLocalDiskID attempts to parse a ProviderID from the ProviderID of the node that the local disk is running on
  340. func ParseLocalDiskID(id string) string {
  341. // Parse like node
  342. id = ParseID(id)
  343. if strings.HasPrefix(id, "azure://") {
  344. // handle vmss ProviderID of type azure:///subscriptions/ae337b64-e7ba-3387-b043-187289efe4e3/resourceGroups/mc_test_eastus2/providers/Microsoft.Compute/virtualMachineScaleSets/aks-userpool-12345678-vmss/virtualMachines/11
  345. if strings.Contains(id, "virtualMachineScaleSets") {
  346. split := strings.Split(id, "/virtualMachineScaleSets/")
  347. // combine vmss name and number into a single string ending in a 6 character base 32 number
  348. vmSplit := strings.Split(split[1], "/")
  349. if len(vmSplit) != 3 {
  350. return id
  351. }
  352. vmNum, err := strconv.ParseInt(vmSplit[2], 10, 64)
  353. if err != nil {
  354. return id
  355. }
  356. id = fmt.Sprintf("%s/disks/%s%06s", split[0], vmSplit[0], strconv.FormatInt(vmNum, 32))
  357. }
  358. id = strings.Replace(id, "/virtualMachines/", "/disks/", -1)
  359. id = strings.ToLower(id)
  360. return fmt.Sprintf("%s_osdisk", id)
  361. }
  362. return id
  363. }