gcpprovider.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. package cloud
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "math"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. "k8s.io/klog"
  18. "cloud.google.com/go/bigquery"
  19. "cloud.google.com/go/compute/metadata"
  20. "github.com/kubecost/cost-model/clustercache"
  21. "golang.org/x/oauth2"
  22. "golang.org/x/oauth2/google"
  23. compute "google.golang.org/api/compute/v1"
  24. "google.golang.org/api/iterator"
  25. v1 "k8s.io/api/core/v1"
  26. )
  27. const GKE_GPU_TAG = "cloud.google.com/gke-accelerator"
  28. const BigqueryUpdateType = "bigqueryupdate"
  29. type userAgentTransport struct {
  30. userAgent string
  31. base http.RoundTripper
  32. }
  33. func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  34. req.Header.Set("User-Agent", t.userAgent)
  35. return t.base.RoundTrip(req)
  36. }
  37. // GCP implements a provider interface for GCP
  38. type GCP struct {
  39. Pricing map[string]*GCPPricing
  40. Clientset clustercache.ClusterCache
  41. APIKey string
  42. BaseCPUPrice string
  43. ProjectID string
  44. BillingDataDataset string
  45. DownloadPricingDataLock sync.RWMutex
  46. ReservedInstances []*GCPReservedInstance
  47. *CustomProvider
  48. }
  49. type gcpAllocation struct {
  50. Aggregator bigquery.NullString
  51. Environment bigquery.NullString
  52. Service string
  53. Cost float64
  54. }
  55. func gcpAllocationToOutOfClusterAllocation(gcpAlloc gcpAllocation) *OutOfClusterAllocation {
  56. var aggregator string
  57. if gcpAlloc.Aggregator.Valid {
  58. aggregator = gcpAlloc.Aggregator.StringVal
  59. }
  60. var environment string
  61. if gcpAlloc.Environment.Valid {
  62. environment = gcpAlloc.Environment.StringVal
  63. }
  64. return &OutOfClusterAllocation{
  65. Aggregator: aggregator,
  66. Environment: environment,
  67. Service: gcpAlloc.Service,
  68. Cost: gcpAlloc.Cost,
  69. }
  70. }
  71. func (gcp *GCP) GetLocalStorageQuery(offset string) (string, error) {
  72. localStorageCost := 0.04 // TODO: Set to the price for the appropriate storage class. It's not trivial to determine the local storage disk type
  73. return fmt.Sprintf(`sum(sum(container_fs_limit_bytes{device!="tmpfs", id="/"} %s) by (instance, cluster_id)) by (cluster_id) / 1024 / 1024 / 1024 * %f`, offset, localStorageCost), nil
  74. }
  75. func (gcp *GCP) GetConfig() (*CustomPricing, error) {
  76. c, err := GetDefaultPricingData("gcp.json")
  77. if err != nil {
  78. return nil, err
  79. }
  80. if c.Discount == "" {
  81. c.Discount = "30%"
  82. }
  83. if c.NegotiatedDiscount == "" {
  84. c.NegotiatedDiscount = "0%"
  85. }
  86. return c, nil
  87. }
  88. type BigQueryConfig struct {
  89. ProjectID string `json:"projectID"`
  90. BillingDataDataset string `json:"billingDataDataset"`
  91. Key map[string]string `json:"key"`
  92. }
  93. func (gcp *GCP) GetManagementPlatform() (string, error) {
  94. nodes := gcp.Clientset.GetAllNodes()
  95. if len(nodes) > 0 {
  96. n := nodes[0]
  97. version := n.Status.NodeInfo.KubeletVersion
  98. if strings.Contains(version, "gke") {
  99. return "gke", nil
  100. }
  101. }
  102. return "", nil
  103. }
  104. func (gcp *GCP) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  105. c, err := GetDefaultPricingData("gcp.json")
  106. if err != nil {
  107. return nil, err
  108. }
  109. path := os.Getenv("CONFIG_PATH")
  110. if path == "" {
  111. path = "/models/"
  112. }
  113. configPath := path + "gcp.json"
  114. return configmapUpdate(c, configPath, a)
  115. }
  116. func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  117. c, err := GetDefaultPricingData("gcp.json")
  118. if err != nil {
  119. return nil, err
  120. }
  121. path := os.Getenv("CONFIG_PATH")
  122. if path == "" {
  123. path = "/models/"
  124. }
  125. if updateType == BigqueryUpdateType {
  126. a := BigQueryConfig{}
  127. err = json.NewDecoder(r).Decode(&a)
  128. if err != nil {
  129. return nil, err
  130. }
  131. c.ProjectID = a.ProjectID
  132. c.BillingDataDataset = a.BillingDataDataset
  133. j, err := json.Marshal(a.Key)
  134. if err != nil {
  135. return nil, err
  136. }
  137. keyPath := path + "key.json"
  138. err = ioutil.WriteFile(keyPath, j, 0644)
  139. if err != nil {
  140. return nil, err
  141. }
  142. } else {
  143. a := make(map[string]interface{})
  144. err = json.NewDecoder(r).Decode(&a)
  145. if err != nil {
  146. return nil, err
  147. }
  148. for k, v := range a {
  149. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  150. vstr, ok := v.(string)
  151. if ok {
  152. err := SetCustomPricingField(c, kUpper, vstr)
  153. if err != nil {
  154. return nil, err
  155. }
  156. } else {
  157. sci := v.(map[string]interface{})
  158. sc := make(map[string]string)
  159. for k, val := range sci {
  160. sc[k] = val.(string)
  161. }
  162. c.SharedCosts = sc //todo: support reflection/multiple map fields
  163. }
  164. }
  165. }
  166. cj, err := json.Marshal(c)
  167. if err != nil {
  168. return nil, err
  169. }
  170. remoteEnabled := os.Getenv(remoteEnabled)
  171. if remoteEnabled == "true" {
  172. err = UpdateClusterMeta(os.Getenv(clusterIDKey), c.ClusterName)
  173. if err != nil {
  174. return nil, err
  175. }
  176. }
  177. configPath := path + "gcp.json"
  178. err = ioutil.WriteFile(configPath, cj, 0644)
  179. if err != nil {
  180. return nil, err
  181. }
  182. return c, nil
  183. }
  184. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  185. // "start" and "end" are dates of the format YYYY-MM-DD
  186. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  187. func (gcp *GCP) ExternalAllocations(start string, end string, aggregator string, filterType string, filterValue string) ([]*OutOfClusterAllocation, error) {
  188. c, err := GetDefaultPricingData("gcp.json")
  189. if err != nil {
  190. return nil, err
  191. }
  192. // start, end formatted like: "2019-04-20 00:00:00"
  193. queryString := fmt.Sprintf(`SELECT
  194. service,
  195. labels.key as aggregator,
  196. labels.value as environment,
  197. SUM(cost) as cost
  198. FROM (SELECT
  199. service.description as service,
  200. labels,
  201. cost
  202. FROM %s
  203. WHERE usage_start_time >= "%s" AND usage_start_time < "%s")
  204. LEFT JOIN UNNEST(labels) as labels
  205. ON labels.key = "%s"
  206. GROUP BY aggregator, environment, service;`, c.BillingDataDataset, start, end, aggregator) // For example, "billing_data.gcp_billing_export_v1_01AC9F_74CF1D_5565A2"
  207. klog.V(4).Infof("Querying \"%s\" with : %s", c.ProjectID, queryString)
  208. return gcp.QuerySQL(queryString)
  209. }
  210. // QuerySQL should query BigQuery for billing data for out of cluster costs.
  211. func (gcp *GCP) QuerySQL(query string) ([]*OutOfClusterAllocation, error) {
  212. c, err := GetDefaultPricingData("gcp.json")
  213. if err != nil {
  214. return nil, err
  215. }
  216. ctx := context.Background()
  217. client, err := bigquery.NewClient(ctx, c.ProjectID) // For example, "guestbook-227502"
  218. if err != nil {
  219. return nil, err
  220. }
  221. q := client.Query(query)
  222. it, err := q.Read(ctx)
  223. if err != nil {
  224. return nil, err
  225. }
  226. var allocations []*OutOfClusterAllocation
  227. for {
  228. var a gcpAllocation
  229. err := it.Next(&a)
  230. if err == iterator.Done {
  231. break
  232. }
  233. if err != nil {
  234. return nil, err
  235. }
  236. allocations = append(allocations, gcpAllocationToOutOfClusterAllocation(a))
  237. }
  238. return allocations, nil
  239. }
  240. // ClusterName returns the name of a GKE cluster, as provided by metadata.
  241. func (gcp *GCP) ClusterInfo() (map[string]string, error) {
  242. remote := os.Getenv(remoteEnabled)
  243. remoteEnabled := false
  244. if os.Getenv(remote) == "true" {
  245. remoteEnabled = true
  246. }
  247. metadataClient := metadata.NewClient(&http.Client{Transport: userAgentTransport{
  248. userAgent: "kubecost",
  249. base: http.DefaultTransport,
  250. }})
  251. attribute, err := metadataClient.InstanceAttributeValue("cluster-name")
  252. if err != nil {
  253. return nil, err
  254. }
  255. c, err := gcp.GetConfig()
  256. if err != nil {
  257. klog.V(1).Infof("Error opening config: %s", err.Error())
  258. }
  259. if c.ClusterName != "" {
  260. attribute = c.ClusterName
  261. }
  262. m := make(map[string]string)
  263. m["name"] = attribute
  264. m["provider"] = "GCP"
  265. m["id"] = os.Getenv(clusterIDKey)
  266. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  267. return m, nil
  268. }
  269. // AddServiceKey adds the service key as required for GetDisks
  270. func (*GCP) AddServiceKey(formValues url.Values) error {
  271. key := formValues.Get("key")
  272. k := []byte(key)
  273. return ioutil.WriteFile("/var/configs/key.json", k, 0644)
  274. }
  275. // GetDisks returns the GCP disks backing PVs. Useful because sometimes k8s will not clean up PVs correctly. Requires a json config in /var/configs with key region.
  276. func (*GCP) GetDisks() ([]byte, error) {
  277. // metadata API setup
  278. metadataClient := metadata.NewClient(&http.Client{Transport: userAgentTransport{
  279. userAgent: "kubecost",
  280. base: http.DefaultTransport,
  281. }})
  282. projID, err := metadataClient.ProjectID()
  283. if err != nil {
  284. return nil, err
  285. }
  286. client, err := google.DefaultClient(oauth2.NoContext,
  287. "https://www.googleapis.com/auth/compute.readonly")
  288. if err != nil {
  289. return nil, err
  290. }
  291. svc, err := compute.New(client)
  292. if err != nil {
  293. return nil, err
  294. }
  295. res, err := svc.Disks.AggregatedList(projID).Do()
  296. if err != nil {
  297. return nil, err
  298. }
  299. return json.Marshal(res)
  300. }
  301. // GCPPricing represents GCP pricing data for a SKU
  302. type GCPPricing struct {
  303. Name string `json:"name"`
  304. SKUID string `json:"skuId"`
  305. Description string `json:"description"`
  306. Category *GCPResourceInfo `json:"category"`
  307. ServiceRegions []string `json:"serviceRegions"`
  308. PricingInfo []*PricingInfo `json:"pricingInfo"`
  309. ServiceProviderName string `json:"serviceProviderName"`
  310. Node *Node `json:"node"`
  311. PV *PV `json:"pv"`
  312. }
  313. // PricingInfo contains metadata about a cost.
  314. type PricingInfo struct {
  315. Summary string `json:"summary"`
  316. PricingExpression *PricingExpression `json:"pricingExpression"`
  317. CurrencyConversionRate int `json:"currencyConversionRate"`
  318. EffectiveTime string `json:""`
  319. }
  320. // PricingExpression contains metadata about a cost.
  321. type PricingExpression struct {
  322. UsageUnit string `json:"usageUnit"`
  323. UsageUnitDescription string `json:"usageUnitDescription"`
  324. BaseUnit string `json:"baseUnit"`
  325. BaseUnitConversionFactor int64 `json:"-"`
  326. DisplayQuantity int `json:"displayQuantity"`
  327. TieredRates []*TieredRates `json:"tieredRates"`
  328. }
  329. // TieredRates contain data about variable pricing.
  330. type TieredRates struct {
  331. StartUsageAmount int `json:"startUsageAmount"`
  332. UnitPrice *UnitPriceInfo `json:"unitPrice"`
  333. }
  334. // UnitPriceInfo contains data about the actual price being charged.
  335. type UnitPriceInfo struct {
  336. CurrencyCode string `json:"currencyCode"`
  337. Units string `json:"units"`
  338. Nanos float64 `json:"nanos"`
  339. }
  340. // GCPResourceInfo contains metadata about the node.
  341. type GCPResourceInfo struct {
  342. ServiceDisplayName string `json:"serviceDisplayName"`
  343. ResourceFamily string `json:"resourceFamily"`
  344. ResourceGroup string `json:"resourceGroup"`
  345. UsageType string `json:"usageType"`
  346. }
  347. func (gcp *GCP) parsePage(r io.Reader, inputKeys map[string]Key, pvKeys map[string]PVKey) (map[string]*GCPPricing, string, error) {
  348. gcpPricingList := make(map[string]*GCPPricing)
  349. var nextPageToken string
  350. dec := json.NewDecoder(r)
  351. for {
  352. t, err := dec.Token()
  353. if err == io.EOF {
  354. break
  355. }
  356. if t == "skus" {
  357. _, err := dec.Token() // consumes [
  358. if err != nil {
  359. return nil, "", err
  360. }
  361. for dec.More() {
  362. product := &GCPPricing{}
  363. err := dec.Decode(&product)
  364. if err != nil {
  365. return nil, "", err
  366. }
  367. usageType := strings.ToLower(product.Category.UsageType)
  368. instanceType := strings.ToLower(product.Category.ResourceGroup)
  369. if instanceType == "ssd" && !strings.Contains(product.Description, "Regional") { // TODO: support regional
  370. lastRateIndex := len(product.PricingInfo[0].PricingExpression.TieredRates) - 1
  371. var nanos float64
  372. if len(product.PricingInfo) > 0 {
  373. nanos = product.PricingInfo[0].PricingExpression.TieredRates[lastRateIndex].UnitPrice.Nanos
  374. } else {
  375. continue
  376. }
  377. hourlyPrice := (nanos * math.Pow10(-9)) / 730
  378. for _, sr := range product.ServiceRegions {
  379. region := sr
  380. candidateKey := region + "," + "ssd"
  381. if _, ok := pvKeys[candidateKey]; ok {
  382. product.PV = &PV{
  383. Cost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  384. }
  385. gcpPricingList[candidateKey] = product
  386. continue
  387. }
  388. }
  389. continue
  390. } else if instanceType == "pdstandard" && !strings.Contains(product.Description, "Regional") { // TODO: support regional
  391. lastRateIndex := len(product.PricingInfo[0].PricingExpression.TieredRates) - 1
  392. var nanos float64
  393. if len(product.PricingInfo) > 0 {
  394. nanos = product.PricingInfo[0].PricingExpression.TieredRates[lastRateIndex].UnitPrice.Nanos
  395. } else {
  396. continue
  397. }
  398. hourlyPrice := (nanos * math.Pow10(-9)) / 730
  399. for _, sr := range product.ServiceRegions {
  400. region := sr
  401. candidateKey := region + "," + "pdstandard"
  402. if _, ok := pvKeys[candidateKey]; ok {
  403. product.PV = &PV{
  404. Cost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  405. }
  406. gcpPricingList[candidateKey] = product
  407. continue
  408. }
  409. }
  410. continue
  411. }
  412. if (instanceType == "ram" || instanceType == "cpu") && strings.Contains(strings.ToUpper(product.Description), "CUSTOM") {
  413. instanceType = "custom"
  414. }
  415. if (instanceType == "ram" || instanceType == "cpu") && strings.Contains(strings.ToUpper(product.Description), "N2") {
  416. instanceType = "n2standard"
  417. }
  418. /*
  419. var partialCPU float64
  420. if strings.ToLower(instanceType) == "f1micro" {
  421. partialCPU = 0.2
  422. } else if strings.ToLower(instanceType) == "g1small" {
  423. partialCPU = 0.5
  424. }
  425. */
  426. var gpuType string
  427. provIdRx := regexp.MustCompile("(Nvidia Tesla [^ ]+) ")
  428. for matchnum, group := range provIdRx.FindStringSubmatch(product.Description) {
  429. if matchnum == 1 {
  430. gpuType = strings.ToLower(strings.Join(strings.Split(group, " "), "-"))
  431. klog.V(4).Info("GPU type found: " + gpuType)
  432. }
  433. }
  434. for _, sr := range product.ServiceRegions {
  435. region := sr
  436. candidateKey := region + "," + instanceType + "," + usageType
  437. candidateKeyGPU := candidateKey + ",gpu"
  438. if gpuType != "" {
  439. lastRateIndex := len(product.PricingInfo[0].PricingExpression.TieredRates) - 1
  440. var nanos float64
  441. if len(product.PricingInfo) > 0 {
  442. nanos = product.PricingInfo[0].PricingExpression.TieredRates[lastRateIndex].UnitPrice.Nanos
  443. } else {
  444. continue
  445. }
  446. hourlyPrice := nanos * math.Pow10(-9)
  447. for k, key := range inputKeys {
  448. if key.GPUType() == gpuType+","+usageType {
  449. if region == strings.Split(k, ",")[0] {
  450. klog.V(3).Infof("Matched GPU to node in region \"%s\"", region)
  451. klog.V(4).Infof("PRODUCT DESCRIPTION: %s", product.Description)
  452. matchedKey := key.Features()
  453. if pl, ok := gcpPricingList[matchedKey]; ok {
  454. pl.Node.GPUName = gpuType
  455. pl.Node.GPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  456. pl.Node.GPU = "1"
  457. } else {
  458. product.Node = &Node{
  459. GPUName: gpuType,
  460. GPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  461. GPU: "1",
  462. }
  463. gcpPricingList[matchedKey] = product
  464. }
  465. klog.V(3).Infof("Added data for " + matchedKey)
  466. }
  467. }
  468. }
  469. } else {
  470. _, ok := inputKeys[candidateKey]
  471. _, ok2 := inputKeys[candidateKeyGPU]
  472. if ok || ok2 {
  473. lastRateIndex := len(product.PricingInfo[0].PricingExpression.TieredRates) - 1
  474. var nanos float64
  475. if len(product.PricingInfo) > 0 {
  476. nanos = product.PricingInfo[0].PricingExpression.TieredRates[lastRateIndex].UnitPrice.Nanos
  477. } else {
  478. continue
  479. }
  480. hourlyPrice := nanos * math.Pow10(-9)
  481. if hourlyPrice == 0 {
  482. continue
  483. } else if strings.Contains(strings.ToUpper(product.Description), "RAM") {
  484. if instanceType == "custom" {
  485. klog.V(4).Infof("RAM custom sku is: " + product.Name)
  486. }
  487. if _, ok := gcpPricingList[candidateKey]; ok {
  488. gcpPricingList[candidateKey].Node.RAMCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  489. } else {
  490. product = &GCPPricing{}
  491. product.Node = &Node{
  492. RAMCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  493. }
  494. /*
  495. if partialCPU != 0 {
  496. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  497. }
  498. */
  499. product.Node.UsageType = usageType
  500. gcpPricingList[candidateKey] = product
  501. }
  502. if _, ok := gcpPricingList[candidateKeyGPU]; ok {
  503. klog.V(1).Infof("Adding RAM %f for %s", hourlyPrice, candidateKeyGPU)
  504. gcpPricingList[candidateKeyGPU].Node.RAMCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  505. } else {
  506. klog.V(1).Infof("Adding RAM %f for %s", hourlyPrice, candidateKeyGPU)
  507. product = &GCPPricing{}
  508. product.Node = &Node{
  509. RAMCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  510. }
  511. /*
  512. if partialCPU != 0 {
  513. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  514. }
  515. */
  516. product.Node.UsageType = usageType
  517. gcpPricingList[candidateKeyGPU] = product
  518. }
  519. break
  520. } else {
  521. if _, ok := gcpPricingList[candidateKey]; ok {
  522. gcpPricingList[candidateKey].Node.VCPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  523. } else {
  524. product = &GCPPricing{}
  525. product.Node = &Node{
  526. VCPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  527. }
  528. /*
  529. if partialCPU != 0 {
  530. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  531. }
  532. */
  533. product.Node.UsageType = usageType
  534. gcpPricingList[candidateKey] = product
  535. }
  536. if _, ok := gcpPricingList[candidateKeyGPU]; ok {
  537. gcpPricingList[candidateKeyGPU].Node.VCPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  538. } else {
  539. product = &GCPPricing{}
  540. product.Node = &Node{
  541. VCPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  542. }
  543. /*
  544. if partialCPU != 0 {
  545. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  546. }
  547. */
  548. product.Node.UsageType = usageType
  549. gcpPricingList[candidateKeyGPU] = product
  550. }
  551. break
  552. }
  553. }
  554. }
  555. }
  556. }
  557. }
  558. if t == "nextPageToken" {
  559. pageToken, err := dec.Token()
  560. if err != nil {
  561. klog.V(2).Infof("Error parsing nextpage token: " + err.Error())
  562. return nil, "", err
  563. }
  564. if pageToken.(string) != "" {
  565. nextPageToken = pageToken.(string)
  566. } else {
  567. nextPageToken = "done"
  568. }
  569. }
  570. }
  571. return gcpPricingList, nextPageToken, nil
  572. }
  573. func (gcp *GCP) parsePages(inputKeys map[string]Key, pvKeys map[string]PVKey) (map[string]*GCPPricing, error) {
  574. var pages []map[string]*GCPPricing
  575. url := "https://cloudbilling.googleapis.com/v1/services/6F81-5844-456A/skus?key=" + gcp.APIKey
  576. klog.V(2).Infof("Fetch GCP Billing Data from URL: %s", url)
  577. var parsePagesHelper func(string) error
  578. parsePagesHelper = func(pageToken string) error {
  579. if pageToken == "done" {
  580. return nil
  581. } else if pageToken != "" {
  582. url = url + "&pageToken=" + pageToken
  583. }
  584. resp, err := http.Get(url)
  585. if err != nil {
  586. return err
  587. }
  588. page, token, err := gcp.parsePage(resp.Body, inputKeys, pvKeys)
  589. if err != nil {
  590. return err
  591. }
  592. pages = append(pages, page)
  593. return parsePagesHelper(token)
  594. }
  595. err := parsePagesHelper("")
  596. if err != nil {
  597. return nil, err
  598. }
  599. returnPages := make(map[string]*GCPPricing)
  600. for _, page := range pages {
  601. for k, v := range page {
  602. if val, ok := returnPages[k]; ok { //keys may need to be merged
  603. if val.Node != nil {
  604. if val.Node.VCPUCost == "" {
  605. val.Node.VCPUCost = v.Node.VCPUCost
  606. }
  607. if val.Node.RAMCost == "" {
  608. val.Node.RAMCost = v.Node.RAMCost
  609. }
  610. if val.Node.GPUCost == "" {
  611. val.Node.GPUCost = v.Node.GPUCost
  612. val.Node.GPU = v.Node.GPU
  613. val.Node.GPUName = v.Node.GPUName
  614. }
  615. }
  616. if val.PV != nil {
  617. if val.PV.Cost == "" {
  618. val.PV.Cost = v.PV.Cost
  619. }
  620. }
  621. } else {
  622. returnPages[k] = v
  623. }
  624. }
  625. }
  626. klog.V(1).Infof("ALL PAGES: %+v", returnPages)
  627. for k, v := range returnPages {
  628. klog.V(1).Infof("Returned Page: %s : %+v", k, v.Node)
  629. }
  630. return returnPages, err
  631. }
  632. // DownloadPricingData fetches data from the GCP Pricing API. Requires a key-- a kubecost key is provided for quickstart, but should be replaced by a users.
  633. func (gcp *GCP) DownloadPricingData() error {
  634. gcp.DownloadPricingDataLock.Lock()
  635. defer gcp.DownloadPricingDataLock.Unlock()
  636. c, err := GetDefaultPricingData("gcp.json")
  637. if err != nil {
  638. klog.V(2).Infof("Error downloading default pricing data: %s", err.Error())
  639. return err
  640. }
  641. gcp.BaseCPUPrice = c.CPU
  642. gcp.ProjectID = c.ProjectID
  643. gcp.BillingDataDataset = c.BillingDataDataset
  644. nodeList := gcp.Clientset.GetAllNodes()
  645. inputkeys := make(map[string]Key)
  646. for _, n := range nodeList {
  647. labels := n.GetObjectMeta().GetLabels()
  648. key := gcp.GetKey(labels)
  649. inputkeys[key.Features()] = key
  650. }
  651. pvList := gcp.Clientset.GetAllPersistentVolumes()
  652. storageClasses := gcp.Clientset.GetAllStorageClasses()
  653. storageClassMap := make(map[string]map[string]string)
  654. for _, storageClass := range storageClasses {
  655. params := storageClass.Parameters
  656. storageClassMap[storageClass.ObjectMeta.Name] = params
  657. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  658. storageClassMap["default"] = params
  659. storageClassMap[""] = params
  660. }
  661. }
  662. pvkeys := make(map[string]PVKey)
  663. for _, pv := range pvList {
  664. params, ok := storageClassMap[pv.Spec.StorageClassName]
  665. if !ok {
  666. klog.Infof("Unable to find params for storageClassName %s", pv.Name)
  667. continue
  668. }
  669. key := gcp.GetPVKey(pv, params)
  670. pvkeys[key.Features()] = key
  671. }
  672. reserved, err := gcp.getReservedInstances()
  673. if err != nil {
  674. klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
  675. } else {
  676. klog.V(1).Infof("Found %d reserved instances", len(reserved))
  677. gcp.ReservedInstances = reserved
  678. for _, r := range reserved {
  679. klog.V(1).Infof("%s", r)
  680. }
  681. }
  682. pages, err := gcp.parsePages(inputkeys, pvkeys)
  683. if err != nil {
  684. return err
  685. }
  686. gcp.Pricing = pages
  687. return nil
  688. }
  689. func (gcp *GCP) PVPricing(pvk PVKey) (*PV, error) {
  690. gcp.DownloadPricingDataLock.RLock()
  691. defer gcp.DownloadPricingDataLock.RUnlock()
  692. pricing, ok := gcp.Pricing[pvk.Features()]
  693. if !ok {
  694. klog.V(4).Infof("Persistent Volume pricing not found for %s: %s", pvk.GetStorageClass(), pvk.Features())
  695. return &PV{}, nil
  696. }
  697. return pricing.PV, nil
  698. }
  699. // Stubbed NetworkPricing for GCP. Pull directly from gcp.json for now
  700. func (c *GCP) NetworkPricing() (*Network, error) {
  701. cpricing, err := GetDefaultPricingData("gcp.json")
  702. if err != nil {
  703. return nil, err
  704. }
  705. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  706. if err != nil {
  707. return nil, err
  708. }
  709. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  710. if err != nil {
  711. return nil, err
  712. }
  713. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  714. if err != nil {
  715. return nil, err
  716. }
  717. return &Network{
  718. ZoneNetworkEgressCost: znec,
  719. RegionNetworkEgressCost: rnec,
  720. InternetNetworkEgressCost: inec,
  721. }, nil
  722. }
  723. const (
  724. GCPReservedInstanceResourceTypeRAM string = "MEMORY"
  725. GCPReservedInstanceResourceTypeCPU string = "VCPU"
  726. GCPReservedInstanceStatusActive string = "ACTIVE"
  727. GCPReservedInstancePlanOneYear string = "TWELVE_MONTH"
  728. GCPReservedInstancePlanThreeYear string = "THIRTY_SIX_MONTH"
  729. )
  730. type GCPReservedInstancePlan struct {
  731. Name string
  732. CPUCost float64
  733. RAMCost float64
  734. }
  735. type GCPReservedInstance struct {
  736. ReservedRAM int64
  737. ReservedCPU int64
  738. Plan *GCPReservedInstancePlan
  739. StartDate time.Time
  740. EndDate time.Time
  741. Region string
  742. }
  743. func (r *GCPReservedInstance) String() string {
  744. return fmt.Sprintf("[CPU: %d, RAM: %d, Region: %s, Start: %s, End: %s]", r.ReservedCPU, r.ReservedRAM, r.Region, r.StartDate.String(), r.EndDate.String())
  745. }
  746. type GCPReservedCounter struct {
  747. RemainingCPU int64
  748. RemainingRAM int64
  749. Instance *GCPReservedInstance
  750. }
  751. func newReservedCounter(instance *GCPReservedInstance) *GCPReservedCounter {
  752. return &GCPReservedCounter{
  753. RemainingCPU: instance.ReservedCPU,
  754. RemainingRAM: instance.ReservedRAM,
  755. Instance: instance,
  756. }
  757. }
  758. // Two available Reservation plans for GCP, 1-year and 3-year
  759. var gcpReservedInstancePlans map[string]*GCPReservedInstancePlan = map[string]*GCPReservedInstancePlan{
  760. GCPReservedInstancePlanOneYear: &GCPReservedInstancePlan{
  761. Name: GCPReservedInstancePlanOneYear,
  762. CPUCost: 0.019915,
  763. RAMCost: 0.002669,
  764. },
  765. GCPReservedInstancePlanThreeYear: &GCPReservedInstancePlan{
  766. Name: GCPReservedInstancePlanThreeYear,
  767. CPUCost: 0.014225,
  768. RAMCost: 0.001907,
  769. },
  770. }
  771. func (gcp *GCP) ApplyReservedInstancePricing(nodes map[string]*Node) {
  772. numReserved := len(gcp.ReservedInstances)
  773. // Early return if no reserved instance data loaded
  774. if numReserved == 0 {
  775. klog.V(1).Infof("[Reserved] No Reserved Instances")
  776. return
  777. }
  778. now := time.Now()
  779. counters := make(map[string][]*GCPReservedCounter)
  780. for _, r := range gcp.ReservedInstances {
  781. if now.Before(r.StartDate) || now.After(r.EndDate) {
  782. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  783. continue
  784. }
  785. _, ok := counters[r.Region]
  786. counter := newReservedCounter(r)
  787. if !ok {
  788. counters[r.Region] = []*GCPReservedCounter{counter}
  789. } else {
  790. counters[r.Region] = append(counters[r.Region], counter)
  791. }
  792. }
  793. gcpNodes := make(map[string]*v1.Node)
  794. currentNodes := gcp.Clientset.GetAllNodes()
  795. // Create a node name -> node map
  796. for _, gcpNode := range currentNodes {
  797. gcpNodes[gcpNode.GetName()] = gcpNode
  798. }
  799. // go through all provider nodes using k8s nodes for region
  800. for nodeName, node := range nodes {
  801. // Reset reserved allocation to prevent double allocation
  802. node.Reserved = nil
  803. kNode, ok := gcpNodes[nodeName]
  804. if !ok {
  805. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  806. continue
  807. }
  808. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  809. if !ok {
  810. klog.V(1).Infof("[Reserved] Could not find node region")
  811. continue
  812. }
  813. reservedCounters, ok := counters[nodeRegion]
  814. if !ok {
  815. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  816. continue
  817. }
  818. node.Reserved = &ReservedInstanceData{
  819. ReservedCPU: 0,
  820. ReservedRAM: 0,
  821. }
  822. for _, reservedCounter := range reservedCounters {
  823. if reservedCounter.RemainingCPU != 0 {
  824. nodeCPU, _ := strconv.ParseInt(node.VCPU, 10, 64)
  825. nodeCPU -= node.Reserved.ReservedCPU
  826. node.Reserved.CPUCost = reservedCounter.Instance.Plan.CPUCost
  827. if reservedCounter.RemainingCPU >= nodeCPU {
  828. reservedCounter.RemainingCPU -= nodeCPU
  829. node.Reserved.ReservedCPU += nodeCPU
  830. } else {
  831. node.Reserved.ReservedCPU += reservedCounter.RemainingCPU
  832. reservedCounter.RemainingCPU = 0
  833. }
  834. }
  835. if reservedCounter.RemainingRAM != 0 {
  836. nodeRAMF, _ := strconv.ParseFloat(node.RAMBytes, 64)
  837. nodeRAM := int64(nodeRAMF)
  838. nodeRAM -= node.Reserved.ReservedRAM
  839. node.Reserved.RAMCost = reservedCounter.Instance.Plan.RAMCost
  840. if reservedCounter.RemainingRAM >= nodeRAM {
  841. reservedCounter.RemainingRAM -= nodeRAM
  842. node.Reserved.ReservedRAM += nodeRAM
  843. } else {
  844. node.Reserved.ReservedRAM += reservedCounter.RemainingRAM
  845. reservedCounter.RemainingRAM = 0
  846. }
  847. }
  848. }
  849. }
  850. }
  851. func (gcp *GCP) getReservedInstances() ([]*GCPReservedInstance, error) {
  852. var results []*GCPReservedInstance
  853. ctx := context.Background()
  854. computeService, err := compute.NewService(ctx)
  855. if err != nil {
  856. return nil, err
  857. }
  858. commitments, err := computeService.RegionCommitments.AggregatedList(gcp.ProjectID).Do()
  859. if err != nil {
  860. return nil, err
  861. }
  862. for regionKey, commitList := range commitments.Items {
  863. for _, commit := range commitList.Commitments {
  864. if commit.Status != GCPReservedInstanceStatusActive {
  865. continue
  866. }
  867. var vcpu int64 = 0
  868. var ram int64 = 0
  869. for _, resource := range commit.Resources {
  870. switch resource.Type {
  871. case GCPReservedInstanceResourceTypeRAM:
  872. ram = resource.Amount * 1024 * 1024
  873. case GCPReservedInstanceResourceTypeCPU:
  874. vcpu = resource.Amount
  875. default:
  876. klog.V(4).Infof("Failed to handle resource type: %s", resource.Type)
  877. }
  878. }
  879. var region string
  880. regionStr := strings.Split(regionKey, "/")
  881. if len(regionStr) == 2 {
  882. region = regionStr[1]
  883. }
  884. timeLayout := "2006-01-02T15:04:05Z07:00"
  885. startTime, err := time.Parse(timeLayout, commit.StartTimestamp)
  886. if err != nil {
  887. klog.V(1).Infof("Failed to parse start date: %s", commit.StartTimestamp)
  888. continue
  889. }
  890. endTime, err := time.Parse(timeLayout, commit.EndTimestamp)
  891. if err != nil {
  892. klog.V(1).Infof("Failed to parse end date: %s", commit.EndTimestamp)
  893. continue
  894. }
  895. // Look for a plan based on the name. Default to One Year if it fails
  896. plan, ok := gcpReservedInstancePlans[commit.Plan]
  897. if !ok {
  898. plan = gcpReservedInstancePlans[GCPReservedInstancePlanOneYear]
  899. }
  900. results = append(results, &GCPReservedInstance{
  901. Region: region,
  902. ReservedRAM: ram,
  903. ReservedCPU: vcpu,
  904. Plan: plan,
  905. StartDate: startTime,
  906. EndDate: endTime,
  907. })
  908. }
  909. }
  910. return results, nil
  911. }
  912. type pvKey struct {
  913. Labels map[string]string
  914. StorageClass string
  915. StorageClassParameters map[string]string
  916. }
  917. func (key *pvKey) GetStorageClass() string {
  918. return key.StorageClass
  919. }
  920. func (gcp *GCP) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  921. return &pvKey{
  922. Labels: pv.Labels,
  923. StorageClass: pv.Spec.StorageClassName,
  924. StorageClassParameters: parameters,
  925. }
  926. }
  927. func (key *pvKey) Features() string {
  928. // TODO: regional cluster pricing.
  929. storageClass := key.StorageClassParameters["type"]
  930. if storageClass == "pd-ssd" {
  931. storageClass = "ssd"
  932. } else if storageClass == "pd-standard" {
  933. storageClass = "pdstandard"
  934. }
  935. return key.Labels[v1.LabelZoneRegion] + "," + storageClass
  936. }
  937. type gcpKey struct {
  938. Labels map[string]string
  939. }
  940. func (gcp *GCP) GetKey(labels map[string]string) Key {
  941. return &gcpKey{
  942. Labels: labels,
  943. }
  944. }
  945. func (gcp *gcpKey) ID() string {
  946. return ""
  947. }
  948. func (gcp *gcpKey) GPUType() string {
  949. if t, ok := gcp.Labels[GKE_GPU_TAG]; ok {
  950. var usageType string
  951. if t, ok := gcp.Labels["cloud.google.com/gke-preemptible"]; ok && t == "true" {
  952. usageType = "preemptible"
  953. } else {
  954. usageType = "ondemand"
  955. }
  956. klog.V(4).Infof("GPU of type: \"%s\" found", t)
  957. return t + "," + usageType
  958. }
  959. return ""
  960. }
  961. // GetKey maps node labels to information needed to retrieve pricing data
  962. func (gcp *gcpKey) Features() string {
  963. instanceType := strings.ToLower(strings.Join(strings.Split(gcp.Labels[v1.LabelInstanceType], "-")[:2], ""))
  964. if instanceType == "n1highmem" || instanceType == "n1highcpu" {
  965. instanceType = "n1standard" // These are priced the same. TODO: support n1ultrahighmem
  966. } else if strings.HasPrefix(instanceType, "custom") {
  967. instanceType = "custom" // The suffix of custom does not matter
  968. }
  969. region := strings.ToLower(gcp.Labels[v1.LabelZoneRegion])
  970. var usageType string
  971. if t, ok := gcp.Labels["cloud.google.com/gke-preemptible"]; ok && t == "true" {
  972. usageType = "preemptible"
  973. } else {
  974. usageType = "ondemand"
  975. }
  976. if _, ok := gcp.Labels[GKE_GPU_TAG]; ok {
  977. return region + "," + instanceType + "," + usageType + "," + "gpu"
  978. }
  979. return region + "," + instanceType + "," + usageType
  980. }
  981. // AllNodePricing returns the GCP pricing objects stored
  982. func (gcp *GCP) AllNodePricing() (interface{}, error) {
  983. gcp.DownloadPricingDataLock.RLock()
  984. defer gcp.DownloadPricingDataLock.RUnlock()
  985. return gcp.Pricing, nil
  986. }
  987. // NodePricing returns GCP pricing data for a single node
  988. func (gcp *GCP) NodePricing(key Key) (*Node, error) {
  989. gcp.DownloadPricingDataLock.RLock()
  990. defer gcp.DownloadPricingDataLock.RUnlock()
  991. if n, ok := gcp.Pricing[key.Features()]; ok {
  992. klog.V(4).Infof("Returning pricing for node %s: %+v from SKU %s", key, n.Node, n.Name)
  993. n.Node.BaseCPUPrice = gcp.BaseCPUPrice
  994. return n.Node, nil
  995. }
  996. klog.V(1).Infof("Warning: no pricing data found for %s: %s", key.Features(), key)
  997. return nil, fmt.Errorf("Warning: no pricing data found for %s", key)
  998. }