gcpprovider.go 32 KB

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