gcpprovider.go 32 KB

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