provider.go 24 KB

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