provider.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package cloud
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/opencost/opencost/pkg/kubecost"
  14. "github.com/opencost/opencost/pkg/util"
  15. "cloud.google.com/go/compute/metadata"
  16. "github.com/opencost/opencost/pkg/clustercache"
  17. "github.com/opencost/opencost/pkg/config"
  18. "github.com/opencost/opencost/pkg/env"
  19. "github.com/opencost/opencost/pkg/log"
  20. "github.com/opencost/opencost/pkg/util/httputil"
  21. "github.com/opencost/opencost/pkg/util/watcher"
  22. v1 "k8s.io/api/core/v1"
  23. )
  24. const authSecretPath = "/var/secrets/service-key.json"
  25. const storageConfigSecretPath = "/var/azure-storage-config/azure-storage-config.json"
  26. const defaultShareTenancyCost = "true"
  27. var createTableStatements = []string{
  28. `CREATE TABLE IF NOT EXISTS names (
  29. cluster_id VARCHAR(255) NOT NULL,
  30. cluster_name VARCHAR(255) NULL,
  31. PRIMARY KEY (cluster_id)
  32. );`,
  33. }
  34. // ReservedInstanceData keeps record of resources on a node should be
  35. // priced at reserved rates
  36. type ReservedInstanceData struct {
  37. ReservedCPU int64 `json:"reservedCPU"`
  38. ReservedRAM int64 `json:"reservedRAM"`
  39. CPUCost float64 `json:"CPUHourlyCost"`
  40. RAMCost float64 `json:"RAMHourlyCost"`
  41. }
  42. // Node is the interface by which the provider and cost model communicate Node prices.
  43. // The provider will best-effort try to fill out this struct.
  44. type Node struct {
  45. Cost string `json:"hourlyCost"`
  46. VCPU string `json:"CPU"`
  47. VCPUCost string `json:"CPUHourlyCost"`
  48. RAM string `json:"RAM"`
  49. RAMBytes string `json:"RAMBytes"`
  50. RAMCost string `json:"RAMGBHourlyCost"`
  51. Storage string `json:"storage"`
  52. StorageCost string `json:"storageHourlyCost"`
  53. UsesBaseCPUPrice bool `json:"usesDefaultPrice"`
  54. BaseCPUPrice string `json:"baseCPUPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  55. BaseRAMPrice string `json:"baseRAMPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  56. BaseGPUPrice string `json:"baseGPUPrice"`
  57. UsageType string `json:"usageType"`
  58. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  59. GPUName string `json:"gpuName"`
  60. GPUCost string `json:"gpuCost"`
  61. InstanceType string `json:"instanceType,omitempty"`
  62. Region string `json:"region,omitempty"`
  63. Reserved *ReservedInstanceData `json:"reserved,omitempty"`
  64. ProviderID string `json:"providerID,omitempty"`
  65. PricingType PricingType `json:"pricingType,omitempty"`
  66. }
  67. // IsSpot determines whether or not a Node uses spot by usage type
  68. func (n *Node) IsSpot() bool {
  69. if n != nil {
  70. return strings.Contains(n.UsageType, "spot") || strings.Contains(n.UsageType, "emptible")
  71. } else {
  72. return false
  73. }
  74. }
  75. // LoadBalancer is the interface by which the provider and cost model communicate LoadBalancer prices.
  76. // The provider will best-effort try to fill out this struct.
  77. type LoadBalancer struct {
  78. IngressIPAddresses []string `json:"IngressIPAddresses"`
  79. Cost float64 `json:"hourlyCost"`
  80. }
  81. // TODO: used for dynamic cloud provider price fetching.
  82. // determine what identifies a load balancer in the json returned from the cloud provider pricing API call
  83. // type LBKey interface {
  84. // }
  85. // Network is the interface by which the provider and cost model communicate network egress prices.
  86. // The provider will best-effort try to fill out this struct.
  87. type Network struct {
  88. ZoneNetworkEgressCost float64
  89. RegionNetworkEgressCost float64
  90. InternetNetworkEgressCost float64
  91. }
  92. type OrphanedResource struct {
  93. Kind string `json:"resourceKind"`
  94. Region string `json:"region"`
  95. Description map[string]string `json:"description"`
  96. Size *int64 `json:"diskSizeInGB,omitempty"`
  97. DiskName string `json:"diskName,omitempty"`
  98. Url string `json:"url"`
  99. Address string `json:"ipAddress,omitempty"`
  100. MonthlyCost *float64 `json:"monthlyCost"`
  101. }
  102. // PV is the interface by which the provider and cost model communicate PV prices.
  103. // The provider will best-effort try to fill out this struct.
  104. type PV struct {
  105. Cost string `json:"hourlyCost"`
  106. CostPerIO string `json:"costPerIOOperation"`
  107. Class string `json:"storageClass"`
  108. Size string `json:"size"`
  109. Region string `json:"region"`
  110. ProviderID string `json:"providerID,omitempty"`
  111. Parameters map[string]string `json:"parameters"`
  112. }
  113. // Key represents a way for nodes to match between the k8s API and a pricing API
  114. type Key interface {
  115. ID() string // ID represents an exact match
  116. Features() string // Features are a comma separated string of node metadata that could match pricing
  117. GPUType() string // GPUType returns "" if no GPU exists or GPUs, but the name of the GPU otherwise
  118. GPUCount() int // GPUCount returns 0 if no GPU exists or GPUs, but the number of attached GPUs otherwise
  119. }
  120. type PVKey interface {
  121. Features() string
  122. GetStorageClass() string
  123. ID() string
  124. }
  125. // OutOfClusterAllocation represents a cloud provider cost not associated with kubernetes
  126. type OutOfClusterAllocation struct {
  127. Aggregator string `json:"aggregator"`
  128. Environment string `json:"environment"`
  129. Service string `json:"service"`
  130. Cost float64 `json:"cost"`
  131. Cluster string `json:"cluster"`
  132. }
  133. type CustomPricing struct {
  134. Provider string `json:"provider"`
  135. Description string `json:"description"`
  136. CPU string `json:"CPU"`
  137. SpotCPU string `json:"spotCPU"`
  138. RAM string `json:"RAM"`
  139. SpotRAM string `json:"spotRAM"`
  140. GPU string `json:"GPU"`
  141. SpotGPU string `json:"spotGPU"`
  142. Storage string `json:"storage"`
  143. ZoneNetworkEgress string `json:"zoneNetworkEgress"`
  144. RegionNetworkEgress string `json:"regionNetworkEgress"`
  145. InternetNetworkEgress string `json:"internetNetworkEgress"`
  146. FirstFiveForwardingRulesCost string `json:"firstFiveForwardingRulesCost"`
  147. AdditionalForwardingRuleCost string `json:"additionalForwardingRuleCost"`
  148. LBIngressDataCost string `json:"LBIngressDataCost"`
  149. SpotLabel string `json:"spotLabel,omitempty"`
  150. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  151. GpuLabel string `json:"gpuLabel,omitempty"`
  152. GpuLabelValue string `json:"gpuLabelValue,omitempty"`
  153. ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
  154. ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
  155. AlibabaServiceKeyName string `json:"alibabaServiceKeyName,omitempty"`
  156. AlibabaServiceKeySecret string `json:"alibabaServiceKeySecret,omitempty"`
  157. SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
  158. SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
  159. SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
  160. ProjectID string `json:"projectID,omitempty"`
  161. AthenaProjectID string `json:"athenaProjectID,omitempty"`
  162. AthenaBucketName string `json:"athenaBucketName"`
  163. AthenaRegion string `json:"athenaRegion"`
  164. AthenaDatabase string `json:"athenaDatabase"`
  165. AthenaTable string `json:"athenaTable"`
  166. AthenaWorkgroup string `json:"athenaWorkgroup"`
  167. MasterPayerARN string `json:"masterPayerARN"`
  168. BillingDataDataset string `json:"billingDataDataset,omitempty"`
  169. CustomPricesEnabled string `json:"customPricesEnabled"`
  170. DefaultIdle string `json:"defaultIdle"`
  171. AzureSubscriptionID string `json:"azureSubscriptionID"`
  172. AzureClientID string `json:"azureClientID"`
  173. AzureClientSecret string `json:"azureClientSecret"`
  174. AzureTenantID string `json:"azureTenantID"`
  175. AzureBillingRegion string `json:"azureBillingRegion"`
  176. AzureOfferDurableID string `json:"azureOfferDurableID"`
  177. AzureStorageSubscriptionID string `json:"azureStorageSubscriptionID"`
  178. AzureStorageAccount string `json:"azureStorageAccount"`
  179. AzureStorageAccessKey string `json:"azureStorageAccessKey"`
  180. AzureStorageContainer string `json:"azureStorageContainer"`
  181. AzureContainerPath string `json:"azureContainerPath"`
  182. AzureCloud string `json:"azureCloud"`
  183. CurrencyCode string `json:"currencyCode"`
  184. Discount string `json:"discount"`
  185. NegotiatedDiscount string `json:"negotiatedDiscount"`
  186. SharedOverhead string `json:"sharedOverhead"`
  187. ClusterName string `json:"clusterName"`
  188. SharedNamespaces string `json:"sharedNamespaces"`
  189. SharedLabelNames string `json:"sharedLabelNames"`
  190. SharedLabelValues string `json:"sharedLabelValues"`
  191. ShareTenancyCosts string `json:"shareTenancyCosts"` // TODO clean up configuration so we can use a type other that string (this should be a bool, but the app panics if it's not a string)
  192. ReadOnly string `json:"readOnly"`
  193. EditorAccess string `json:"editorAccess"`
  194. KubecostToken string `json:"kubecostToken"`
  195. GoogleAnalyticsTag string `json:"googleAnalyticsTag"`
  196. ExcludeProviderID string `json:"excludeProviderID"`
  197. }
  198. // GetSharedOverheadCostPerMonth parses and returns a float64 representation
  199. // of the configured monthly shared overhead cost. If the string version cannot
  200. // be parsed into a float, an error is logged and 0.0 is returned.
  201. func (cp *CustomPricing) GetSharedOverheadCostPerMonth() float64 {
  202. // Empty string should be interpreted as "no cost", i.e. 0.0
  203. if cp.SharedOverhead == "" {
  204. return 0.0
  205. }
  206. // Attempt to parse, but log and return 0.0 if that fails.
  207. sharedCostPerMonth, err := strconv.ParseFloat(cp.SharedOverhead, 64)
  208. if err != nil {
  209. log.Errorf("SharedOverhead: failed to parse shared overhead \"%s\": %s", cp.SharedOverhead, err)
  210. return 0.0
  211. }
  212. return sharedCostPerMonth
  213. }
  214. type ServiceAccountStatus struct {
  215. Checks []*ServiceAccountCheck `json:"checks"`
  216. }
  217. // ServiceAccountChecks is a thread safe map for holding ServiceAccountCheck objects
  218. type ServiceAccountChecks struct {
  219. sync.RWMutex
  220. serviceAccountChecks map[string]*ServiceAccountCheck
  221. }
  222. // NewServiceAccountChecks initialize ServiceAccountChecks
  223. func NewServiceAccountChecks() *ServiceAccountChecks {
  224. return &ServiceAccountChecks{
  225. serviceAccountChecks: make(map[string]*ServiceAccountCheck),
  226. }
  227. }
  228. func (sac *ServiceAccountChecks) set(key string, check *ServiceAccountCheck) {
  229. sac.Lock()
  230. defer sac.Unlock()
  231. sac.serviceAccountChecks[key] = check
  232. }
  233. // getStatus extracts ServiceAccountCheck objects into a slice and returns them in a ServiceAccountStatus
  234. func (sac *ServiceAccountChecks) getStatus() *ServiceAccountStatus {
  235. sac.Lock()
  236. defer sac.Unlock()
  237. checks := []*ServiceAccountCheck{}
  238. for _, v := range sac.serviceAccountChecks {
  239. checks = append(checks, v)
  240. }
  241. return &ServiceAccountStatus{
  242. Checks: checks,
  243. }
  244. }
  245. type ServiceAccountCheck struct {
  246. Message string `json:"message"`
  247. Status bool `json:"status"`
  248. AdditionalInfo string `json:"additionalInfo"`
  249. }
  250. type PricingSources struct {
  251. PricingSources map[string]*PricingSource
  252. }
  253. type PricingSource struct {
  254. Name string `json:"name"`
  255. Enabled bool `json:"enabled"`
  256. Available bool `json:"available"`
  257. Error string `json:"error"`
  258. }
  259. type PricingType string
  260. const (
  261. Api PricingType = "api"
  262. Spot PricingType = "spot"
  263. Reserved PricingType = "reserved"
  264. SavingsPlan PricingType = "savingsPlan"
  265. CsvExact PricingType = "csvExact"
  266. CsvClass PricingType = "csvClass"
  267. DefaultPrices PricingType = "defaultPrices"
  268. )
  269. type PricingMatchMetadata struct {
  270. TotalNodes int `json:"TotalNodes"`
  271. PricingTypeCounts map[PricingType]int `json:"PricingType"`
  272. }
  273. // Provider represents a k8s provider.
  274. type Provider interface {
  275. ClusterInfo() (map[string]string, error)
  276. GetAddresses() ([]byte, error)
  277. GetDisks() ([]byte, error)
  278. GetOrphanedResources() ([]OrphanedResource, error)
  279. NodePricing(Key) (*Node, error)
  280. PVPricing(PVKey) (*PV, error)
  281. NetworkPricing() (*Network, error) // TODO: add key interface arg for dynamic price fetching
  282. LoadBalancerPricing() (*LoadBalancer, error) // TODO: add key interface arg for dynamic price fetching
  283. AllNodePricing() (interface{}, error)
  284. DownloadPricingData() error
  285. GetKey(map[string]string, *v1.Node) Key
  286. GetPVKey(*v1.PersistentVolume, map[string]string, string) PVKey
  287. UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
  288. UpdateConfigFromConfigMap(map[string]string) (*CustomPricing, error)
  289. GetConfig() (*CustomPricing, error)
  290. GetManagementPlatform() (string, error)
  291. GetLocalStorageQuery(time.Duration, time.Duration, bool, bool) string
  292. ApplyReservedInstancePricing(map[string]*Node)
  293. ServiceAccountStatus() *ServiceAccountStatus
  294. PricingSourceStatus() map[string]*PricingSource
  295. ClusterManagementPricing() (string, float64, error)
  296. CombinedDiscountForNode(string, bool, float64, float64) float64
  297. Regions() []string
  298. }
  299. // ClusterName returns the name defined in cluster info, defaulting to the
  300. // CLUSTER_ID environment variable
  301. func ClusterName(p Provider) string {
  302. info, err := p.ClusterInfo()
  303. if err != nil {
  304. return env.GetClusterID()
  305. }
  306. name, ok := info["name"]
  307. if !ok {
  308. return env.GetClusterID()
  309. }
  310. return name
  311. }
  312. // CustomPricesEnabled returns the boolean equivalent of the cloup provider's custom prices flag,
  313. // indicating whether or not the cluster is using custom pricing.
  314. func CustomPricesEnabled(p Provider) bool {
  315. config, err := p.GetConfig()
  316. if err != nil {
  317. return false
  318. }
  319. // TODO:CLEANUP what is going on with this?
  320. if config.NegotiatedDiscount == "" {
  321. config.NegotiatedDiscount = "0%"
  322. }
  323. return config.CustomPricesEnabled == "true"
  324. }
  325. // ConfigWatcherFor returns a new ConfigWatcher instance which watches changes to the "pricing-configs"
  326. // configmap
  327. func ConfigWatcherFor(p Provider) *watcher.ConfigMapWatcher {
  328. return &watcher.ConfigMapWatcher{
  329. ConfigMapName: env.GetPricingConfigmapName(),
  330. WatchFunc: func(name string, data map[string]string) error {
  331. _, err := p.UpdateConfigFromConfigMap(data)
  332. return err
  333. },
  334. }
  335. }
  336. // AllocateIdleByDefault returns true if the application settings specify to allocate idle by default
  337. func AllocateIdleByDefault(p Provider) bool {
  338. config, err := p.GetConfig()
  339. if err != nil {
  340. return false
  341. }
  342. return config.DefaultIdle == "true"
  343. }
  344. // SharedNamespace returns a list of names of shared namespaces, as defined in the application settings
  345. func SharedNamespaces(p Provider) []string {
  346. namespaces := []string{}
  347. config, err := p.GetConfig()
  348. if err != nil {
  349. return namespaces
  350. }
  351. if config.SharedNamespaces == "" {
  352. return namespaces
  353. }
  354. // trim spaces so that "kube-system, kubecost" is equivalent to "kube-system,kubecost"
  355. for _, ns := range strings.Split(config.SharedNamespaces, ",") {
  356. namespaces = append(namespaces, strings.Trim(ns, " "))
  357. }
  358. return namespaces
  359. }
  360. // SharedLabel returns the configured set of shared labels as a parallel tuple of keys to values; e.g.
  361. // for app:kubecost,type:staging this returns (["app", "type"], ["kubecost", "staging"]) in order to
  362. // match the signature of the NewSharedResourceInfo
  363. func SharedLabels(p Provider) ([]string, []string) {
  364. names := []string{}
  365. values := []string{}
  366. config, err := p.GetConfig()
  367. if err != nil {
  368. return names, values
  369. }
  370. if config.SharedLabelNames == "" || config.SharedLabelValues == "" {
  371. return names, values
  372. }
  373. ks := strings.Split(config.SharedLabelNames, ",")
  374. vs := strings.Split(config.SharedLabelValues, ",")
  375. if len(ks) != len(vs) {
  376. log.Warnf("Shared labels have mis-matched lengths: %d names, %d values", len(ks), len(vs))
  377. return names, values
  378. }
  379. for i := range ks {
  380. names = append(names, strings.Trim(ks[i], " "))
  381. values = append(values, strings.Trim(vs[i], " "))
  382. }
  383. return names, values
  384. }
  385. // ShareTenancyCosts returns true if the application settings specify to share
  386. // tenancy costs by default.
  387. func ShareTenancyCosts(p Provider) bool {
  388. config, err := p.GetConfig()
  389. if err != nil {
  390. return false
  391. }
  392. return config.ShareTenancyCosts == "true"
  393. }
  394. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  395. func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.ConfigFileManager) (Provider, error) {
  396. nodes := cache.GetAllNodes()
  397. if len(nodes) == 0 {
  398. log.Infof("Could not locate any nodes for cluster.") // valid in ETL readonly mode
  399. return &CustomProvider{
  400. Clientset: cache,
  401. Config: NewProviderConfig(config, "default.json"),
  402. }, nil
  403. }
  404. cp := getClusterProperties(nodes[0])
  405. switch cp.provider {
  406. case kubecost.CSVProvider:
  407. log.Infof("Using CSV Provider with CSV at %s", env.GetCSVPath())
  408. return &CSVProvider{
  409. CSVLocation: env.GetCSVPath(),
  410. CustomProvider: &CustomProvider{
  411. Clientset: cache,
  412. Config: NewProviderConfig(config, cp.configFileName),
  413. },
  414. }, nil
  415. case kubecost.GCPProvider:
  416. log.Info("metadata reports we are in GCE")
  417. if apiKey == "" {
  418. return nil, errors.New("Supply a GCP Key to start getting data")
  419. }
  420. return &GCP{
  421. Clientset: cache,
  422. APIKey: apiKey,
  423. Config: NewProviderConfig(config, cp.configFileName),
  424. clusterRegion: cp.region,
  425. clusterProjectId: cp.projectID,
  426. metadataClient: metadata.NewClient(&http.Client{
  427. Transport: httputil.NewUserAgentTransport("kubecost", http.DefaultTransport),
  428. }),
  429. }, nil
  430. case kubecost.AWSProvider:
  431. log.Info("Found ProviderID starting with \"aws\", using AWS Provider")
  432. return &AWS{
  433. Clientset: cache,
  434. Config: NewProviderConfig(config, cp.configFileName),
  435. clusterRegion: cp.region,
  436. clusterAccountId: cp.accountID,
  437. serviceAccountChecks: NewServiceAccountChecks(),
  438. }, nil
  439. case kubecost.AzureProvider:
  440. log.Info("Found ProviderID starting with \"azure\", using Azure Provider")
  441. return &Azure{
  442. Clientset: cache,
  443. Config: NewProviderConfig(config, cp.configFileName),
  444. clusterRegion: cp.region,
  445. clusterAccountId: cp.accountID,
  446. serviceAccountChecks: NewServiceAccountChecks(),
  447. }, nil
  448. case kubecost.AlibabaProvider:
  449. log.Info("Found ProviderID starting with \"alibaba\", using Alibaba Cloud Provider")
  450. return &Alibaba{
  451. Clientset: cache,
  452. Config: NewProviderConfig(config, cp.configFileName),
  453. clusterRegion: cp.region,
  454. clusterAccountId: cp.accountID,
  455. serviceAccountChecks: NewServiceAccountChecks(),
  456. }, nil
  457. case kubecost.ScalewayProvider:
  458. log.Info("Found ProviderID starting with \"scaleway\", using Scaleway Provider")
  459. return &Scaleway{
  460. Clientset: cache,
  461. Config: NewProviderConfig(config, cp.configFileName),
  462. }, nil
  463. default:
  464. log.Info("Unsupported provider, falling back to default")
  465. return &CustomProvider{
  466. Clientset: cache,
  467. Config: NewProviderConfig(config, cp.configFileName),
  468. }, nil
  469. }
  470. }
  471. type clusterProperties struct {
  472. provider string
  473. configFileName string
  474. region string
  475. accountID string
  476. projectID string
  477. }
  478. func getClusterProperties(node *v1.Node) clusterProperties {
  479. providerID := strings.ToLower(node.Spec.ProviderID)
  480. region, _ := util.GetRegion(node.Labels)
  481. cp := clusterProperties{
  482. provider: "DEFAULT",
  483. configFileName: "default.json",
  484. region: region,
  485. accountID: "",
  486. projectID: "",
  487. }
  488. if metadata.OnGCE() {
  489. cp.provider = kubecost.GCPProvider
  490. cp.configFileName = "gcp.json"
  491. cp.projectID = parseGCPProjectID(providerID)
  492. } else if strings.HasPrefix(providerID, "aws") {
  493. cp.provider = kubecost.AWSProvider
  494. cp.configFileName = "aws.json"
  495. } else if strings.HasPrefix(providerID, "azure") {
  496. cp.provider = kubecost.AzureProvider
  497. cp.configFileName = "azure.json"
  498. cp.accountID = parseAzureSubscriptionID(providerID)
  499. } else if strings.HasPrefix(providerID, "scaleway") { // the scaleway provider ID looks like scaleway://instance/<instance_id>
  500. cp.provider = kubecost.ScalewayProvider
  501. cp.configFileName = "scaleway.json"
  502. } else if strings.Contains(node.Status.NodeInfo.KubeletVersion, "aliyun") { // provider ID is not prefix with any distinct keyword like other providers
  503. cp.provider = kubecost.AlibabaProvider
  504. cp.configFileName = "alibaba.json"
  505. }
  506. if env.IsUseCSVProvider() {
  507. cp.provider = kubecost.CSVProvider
  508. }
  509. return cp
  510. }
  511. func UpdateClusterMeta(cluster_id, cluster_name string) error {
  512. pw := env.GetRemotePW()
  513. address := env.GetSQLAddress()
  514. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  515. db, err := sql.Open("postgres", connStr)
  516. if err != nil {
  517. return err
  518. }
  519. defer db.Close()
  520. updateStmt := `UPDATE names SET cluster_name = $1 WHERE cluster_id = $2;`
  521. _, err = db.Exec(updateStmt, cluster_name, cluster_id)
  522. if err != nil {
  523. return err
  524. }
  525. return nil
  526. }
  527. func CreateClusterMeta(cluster_id, cluster_name string) error {
  528. pw := env.GetRemotePW()
  529. address := env.GetSQLAddress()
  530. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  531. db, err := sql.Open("postgres", connStr)
  532. if err != nil {
  533. return err
  534. }
  535. defer db.Close()
  536. for _, stmt := range createTableStatements {
  537. _, err := db.Exec(stmt)
  538. if err != nil {
  539. return err
  540. }
  541. }
  542. insertStmt := `INSERT INTO names (cluster_id, cluster_name) VALUES ($1, $2);`
  543. _, err = db.Exec(insertStmt, cluster_id, cluster_name)
  544. if err != nil {
  545. return err
  546. }
  547. return nil
  548. }
  549. func GetClusterMeta(cluster_id string) (string, string, error) {
  550. pw := env.GetRemotePW()
  551. address := env.GetSQLAddress()
  552. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  553. db, err := sql.Open("postgres", connStr)
  554. defer db.Close()
  555. query := `SELECT cluster_id, cluster_name
  556. FROM names
  557. WHERE cluster_id = ?`
  558. rows, err := db.Query(query, cluster_id)
  559. if err != nil {
  560. return "", "", err
  561. }
  562. defer rows.Close()
  563. var (
  564. sql_cluster_id string
  565. cluster_name string
  566. )
  567. for rows.Next() {
  568. if err := rows.Scan(&sql_cluster_id, &cluster_name); err != nil {
  569. return "", "", err
  570. }
  571. }
  572. return sql_cluster_id, cluster_name, nil
  573. }
  574. func GetOrCreateClusterMeta(cluster_id, cluster_name string) (string, string, error) {
  575. id, name, err := GetClusterMeta(cluster_id)
  576. if err != nil {
  577. err := CreateClusterMeta(cluster_id, cluster_name)
  578. if err != nil {
  579. return "", "", err
  580. }
  581. }
  582. if id == "" {
  583. err := CreateClusterMeta(cluster_id, cluster_name)
  584. if err != nil {
  585. return "", "", err
  586. }
  587. }
  588. return id, name, nil
  589. }
  590. var (
  591. // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
  592. providerAWSRegex = regexp.MustCompile("aws://[^/]*/[^/]*/([^/]+)")
  593. // gce://guestbook-227502/us-central1-a/gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
  594. // => gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
  595. providerGCERegex = regexp.MustCompile("gce://[^/]*/[^/]*/([^/]+)")
  596. // Capture "vol-0fc54c5e83b8d2b76" from "aws://us-east-2a/vol-0fc54c5e83b8d2b76"
  597. persistentVolumeAWSRegex = regexp.MustCompile("aws:/[^/]*/[^/]*/([^/]+)")
  598. // Capture "ad9d88195b52a47c89b5055120f28c58" from "ad9d88195b52a47c89b5055120f28c58-1037804914.us-east-2.elb.amazonaws.com"
  599. loadBalancerAWSRegex = regexp.MustCompile("^([^-]+)-.+amazonaws\\.com$")
  600. )
  601. // ParseID attempts to parse a ProviderId from a string based on formats from the various providers and
  602. // returns the string as is if it cannot find a match
  603. func ParseID(id string) string {
  604. match := providerAWSRegex.FindStringSubmatch(id)
  605. if len(match) >= 2 {
  606. return match[1]
  607. }
  608. match = providerGCERegex.FindStringSubmatch(id)
  609. if len(match) >= 2 {
  610. return match[1]
  611. }
  612. // Return id for Azure Provider, CSV Provider and Custom Provider
  613. return id
  614. }
  615. // ParsePVID attempts to parse a PV ProviderId from a string based on formats from the various providers and
  616. // returns the string as is if it cannot find a match
  617. func ParsePVID(id string) string {
  618. match := persistentVolumeAWSRegex.FindStringSubmatch(id)
  619. if len(match) >= 2 {
  620. return match[1]
  621. }
  622. // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
  623. return id
  624. }
  625. // ParseLBID attempts to parse a LB ProviderId from a string based on formats from the various providers and
  626. // returns the string as is if it cannot find a match
  627. func ParseLBID(id string) string {
  628. match := loadBalancerAWSRegex.FindStringSubmatch(id)
  629. if len(match) >= 2 {
  630. return match[1]
  631. }
  632. // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
  633. return id
  634. }