provider.go 15 KB

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