provider.go 15 KB

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