gcpprovider.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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) ([]*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. matchedKey := key.Features()
  440. if pl, ok := gcpPricingList[matchedKey]; ok {
  441. pl.Node.GPUName = gpuType
  442. pl.Node.GPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  443. pl.Node.GPU = "1"
  444. } else {
  445. product.Node = &Node{
  446. GPUName: gpuType,
  447. GPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  448. GPU: "1",
  449. }
  450. gcpPricingList[matchedKey] = product
  451. }
  452. klog.V(3).Infof("Added data for " + matchedKey)
  453. }
  454. }
  455. }
  456. } else {
  457. _, ok := inputKeys[candidateKey]
  458. _, ok2 := inputKeys[candidateKeyGPU]
  459. if ok || ok2 {
  460. lastRateIndex := len(product.PricingInfo[0].PricingExpression.TieredRates) - 1
  461. var nanos float64
  462. if len(product.PricingInfo) > 0 {
  463. nanos = product.PricingInfo[0].PricingExpression.TieredRates[lastRateIndex].UnitPrice.Nanos
  464. } else {
  465. continue
  466. }
  467. hourlyPrice := nanos * math.Pow10(-9)
  468. if hourlyPrice == 0 {
  469. continue
  470. } else if strings.Contains(strings.ToUpper(product.Description), "RAM") {
  471. if instanceType == "custom" {
  472. klog.V(4).Infof("RAM custom sku is: " + product.Name)
  473. }
  474. if _, ok := gcpPricingList[candidateKey]; ok {
  475. gcpPricingList[candidateKey].Node.RAMCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  476. } else {
  477. product = &GCPPricing{}
  478. product.Node = &Node{
  479. RAMCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  480. }
  481. /*
  482. if partialCPU != 0 {
  483. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  484. }
  485. */
  486. product.Node.UsageType = usageType
  487. gcpPricingList[candidateKey] = product
  488. }
  489. if _, ok := gcpPricingList[candidateKeyGPU]; ok {
  490. klog.V(1).Infof("Adding RAM %f for %s", hourlyPrice, candidateKeyGPU)
  491. gcpPricingList[candidateKeyGPU].Node.RAMCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  492. } else {
  493. klog.V(1).Infof("Adding RAM %f for %s", hourlyPrice, candidateKeyGPU)
  494. product = &GCPPricing{}
  495. product.Node = &Node{
  496. RAMCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  497. }
  498. /*
  499. if partialCPU != 0 {
  500. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  501. }
  502. */
  503. product.Node.UsageType = usageType
  504. gcpPricingList[candidateKeyGPU] = product
  505. }
  506. break
  507. } else {
  508. if _, ok := gcpPricingList[candidateKey]; ok {
  509. gcpPricingList[candidateKey].Node.VCPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  510. } else {
  511. product = &GCPPricing{}
  512. product.Node = &Node{
  513. VCPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  514. }
  515. /*
  516. if partialCPU != 0 {
  517. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  518. }
  519. */
  520. product.Node.UsageType = usageType
  521. gcpPricingList[candidateKey] = product
  522. }
  523. if _, ok := gcpPricingList[candidateKeyGPU]; ok {
  524. gcpPricingList[candidateKeyGPU].Node.VCPUCost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  525. } else {
  526. product = &GCPPricing{}
  527. product.Node = &Node{
  528. VCPUCost: strconv.FormatFloat(hourlyPrice, 'f', -1, 64),
  529. }
  530. /*
  531. if partialCPU != 0 {
  532. product.Node.VCPU = fmt.Sprintf("%f", partialCPU)
  533. }
  534. */
  535. product.Node.UsageType = usageType
  536. gcpPricingList[candidateKeyGPU] = product
  537. }
  538. break
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. if t == "nextPageToken" {
  546. pageToken, err := dec.Token()
  547. if err != nil {
  548. klog.V(2).Infof("Error parsing nextpage token: " + err.Error())
  549. return nil, "", err
  550. }
  551. if pageToken.(string) != "" {
  552. nextPageToken = pageToken.(string)
  553. } else {
  554. nextPageToken = "done"
  555. }
  556. }
  557. }
  558. return gcpPricingList, nextPageToken, nil
  559. }
  560. func (gcp *GCP) parsePages(inputKeys map[string]Key, pvKeys map[string]PVKey) (map[string]*GCPPricing, error) {
  561. var pages []map[string]*GCPPricing
  562. url := "https://cloudbilling.googleapis.com/v1/services/6F81-5844-456A/skus?key=" + gcp.APIKey
  563. klog.V(2).Infof("Fetch GCP Billing Data from URL: %s", url)
  564. var parsePagesHelper func(string) error
  565. parsePagesHelper = func(pageToken string) error {
  566. if pageToken == "done" {
  567. return nil
  568. } else if pageToken != "" {
  569. url = url + "&pageToken=" + pageToken
  570. }
  571. resp, err := http.Get(url)
  572. if err != nil {
  573. return err
  574. }
  575. page, token, err := gcp.parsePage(resp.Body, inputKeys, pvKeys)
  576. if err != nil {
  577. return err
  578. }
  579. pages = append(pages, page)
  580. return parsePagesHelper(token)
  581. }
  582. err := parsePagesHelper("")
  583. if err != nil {
  584. return nil, err
  585. }
  586. returnPages := make(map[string]*GCPPricing)
  587. for _, page := range pages {
  588. for k, v := range page {
  589. if val, ok := returnPages[k]; ok { //keys may need to be merged
  590. if val.Node != nil {
  591. if val.Node.VCPUCost == "" {
  592. val.Node.VCPUCost = v.Node.VCPUCost
  593. }
  594. if val.Node.RAMCost == "" {
  595. val.Node.RAMCost = v.Node.RAMCost
  596. }
  597. if val.Node.GPUCost == "" {
  598. val.Node.GPUCost = v.Node.GPUCost
  599. }
  600. }
  601. if val.PV != nil {
  602. if val.PV.Cost == "" {
  603. val.PV.Cost = v.PV.Cost
  604. }
  605. }
  606. } else {
  607. returnPages[k] = v
  608. }
  609. }
  610. }
  611. klog.V(1).Infof("ALL PAGES: %+v", returnPages)
  612. for k, v := range returnPages {
  613. klog.V(1).Infof("Returned Page: %s : %+v", k, v.Node)
  614. }
  615. return returnPages, err
  616. }
  617. // 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.
  618. func (gcp *GCP) DownloadPricingData() error {
  619. gcp.DownloadPricingDataLock.Lock()
  620. defer gcp.DownloadPricingDataLock.Unlock()
  621. c, err := GetDefaultPricingData("gcp.json")
  622. if err != nil {
  623. klog.V(2).Infof("Error downloading default pricing data: %s", err.Error())
  624. return err
  625. }
  626. gcp.BaseCPUPrice = c.CPU
  627. gcp.ProjectID = c.ProjectID
  628. gcp.BillingDataDataset = c.BillingDataDataset
  629. nodeList := gcp.Clientset.GetAllNodes()
  630. inputkeys := make(map[string]Key)
  631. for _, n := range nodeList {
  632. labels := n.GetObjectMeta().GetLabels()
  633. key := gcp.GetKey(labels)
  634. inputkeys[key.Features()] = key
  635. }
  636. pvList := gcp.Clientset.GetAllPersistentVolumes()
  637. storageClasses := gcp.Clientset.GetAllStorageClasses()
  638. storageClassMap := make(map[string]map[string]string)
  639. for _, storageClass := range storageClasses {
  640. params := storageClass.Parameters
  641. storageClassMap[storageClass.ObjectMeta.Name] = params
  642. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  643. storageClassMap["default"] = params
  644. storageClassMap[""] = params
  645. }
  646. }
  647. pvkeys := make(map[string]PVKey)
  648. for _, pv := range pvList {
  649. params, ok := storageClassMap[pv.Spec.StorageClassName]
  650. if !ok {
  651. klog.Infof("Unable to find params for storageClassName %s", pv.Name)
  652. continue
  653. }
  654. key := gcp.GetPVKey(pv, params)
  655. pvkeys[key.Features()] = key
  656. }
  657. reserved, err := gcp.getReservedInstances()
  658. if err != nil {
  659. klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
  660. } else {
  661. klog.V(1).Infof("Found %d reserved instances", len(reserved))
  662. gcp.ReservedInstances = reserved
  663. for _, r := range reserved {
  664. klog.V(1).Infof("%s", r)
  665. }
  666. }
  667. pages, err := gcp.parsePages(inputkeys, pvkeys)
  668. if err != nil {
  669. return err
  670. }
  671. gcp.Pricing = pages
  672. return nil
  673. }
  674. func (gcp *GCP) PVPricing(pvk PVKey) (*PV, error) {
  675. gcp.DownloadPricingDataLock.RLock()
  676. defer gcp.DownloadPricingDataLock.RUnlock()
  677. pricing, ok := gcp.Pricing[pvk.Features()]
  678. if !ok {
  679. klog.V(4).Infof("Persistent Volume pricing not found for %s: %s", pvk.GetStorageClass(), pvk.Features())
  680. return &PV{}, nil
  681. }
  682. return pricing.PV, nil
  683. }
  684. // Stubbed NetworkPricing for GCP. Pull directly from gcp.json for now
  685. func (c *GCP) NetworkPricing() (*Network, error) {
  686. cpricing, err := GetDefaultPricingData("gcp.json")
  687. if err != nil {
  688. return nil, err
  689. }
  690. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  691. if err != nil {
  692. return nil, err
  693. }
  694. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  695. if err != nil {
  696. return nil, err
  697. }
  698. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  699. if err != nil {
  700. return nil, err
  701. }
  702. return &Network{
  703. ZoneNetworkEgressCost: znec,
  704. RegionNetworkEgressCost: rnec,
  705. InternetNetworkEgressCost: inec,
  706. }, nil
  707. }
  708. const (
  709. GCPReservedInstanceResourceTypeRAM string = "MEMORY"
  710. GCPReservedInstanceResourceTypeCPU string = "VCPU"
  711. GCPReservedInstanceStatusActive string = "ACTIVE"
  712. GCPReservedInstancePlanOneYear string = "TWELVE_MONTH"
  713. GCPReservedInstancePlanThreeYear string = "THIRTY_SIX_MONTH"
  714. )
  715. type GCPReservedInstancePlan struct {
  716. Name string
  717. CPUCost float64
  718. RAMCost float64
  719. }
  720. type GCPReservedInstance struct {
  721. ReservedRAM int64
  722. ReservedCPU int64
  723. Plan *GCPReservedInstancePlan
  724. StartDate time.Time
  725. EndDate time.Time
  726. Region string
  727. }
  728. func (r *GCPReservedInstance) String() string {
  729. 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())
  730. }
  731. type GCPReservedCounter struct {
  732. RemainingCPU int64
  733. RemainingRAM int64
  734. Instance *GCPReservedInstance
  735. }
  736. func newReservedCounter(instance *GCPReservedInstance) *GCPReservedCounter {
  737. return &GCPReservedCounter{
  738. RemainingCPU: instance.ReservedCPU,
  739. RemainingRAM: instance.ReservedRAM,
  740. Instance: instance,
  741. }
  742. }
  743. // Two available Reservation plans for GCP, 1-year and 3-year
  744. var gcpReservedInstancePlans map[string]*GCPReservedInstancePlan = map[string]*GCPReservedInstancePlan{
  745. GCPReservedInstancePlanOneYear: &GCPReservedInstancePlan{
  746. Name: GCPReservedInstancePlanOneYear,
  747. CPUCost: 0.019915,
  748. RAMCost: 0.002669,
  749. },
  750. GCPReservedInstancePlanThreeYear: &GCPReservedInstancePlan{
  751. Name: GCPReservedInstancePlanThreeYear,
  752. CPUCost: 0.014225,
  753. RAMCost: 0.001907,
  754. },
  755. }
  756. func (gcp *GCP) ApplyReservedInstancePricing(nodes map[string]*Node) {
  757. numReserved := len(gcp.ReservedInstances)
  758. // Early return if no reserved instance data loaded
  759. if numReserved == 0 {
  760. klog.V(1).Infof("[Reserved] No Reserved Instances")
  761. return
  762. }
  763. now := time.Now()
  764. counters := make(map[string][]*GCPReservedCounter)
  765. for _, r := range gcp.ReservedInstances {
  766. if now.Before(r.StartDate) || now.After(r.EndDate) {
  767. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  768. continue
  769. }
  770. _, ok := counters[r.Region]
  771. counter := newReservedCounter(r)
  772. if !ok {
  773. counters[r.Region] = []*GCPReservedCounter{counter}
  774. } else {
  775. counters[r.Region] = append(counters[r.Region], counter)
  776. }
  777. }
  778. gcpNodes := make(map[string]*v1.Node)
  779. currentNodes := gcp.Clientset.GetAllNodes()
  780. // Create a node name -> node map
  781. for _, gcpNode := range currentNodes {
  782. gcpNodes[gcpNode.GetName()] = gcpNode
  783. }
  784. // go through all provider nodes using k8s nodes for region
  785. for nodeName, node := range nodes {
  786. // Reset reserved allocation to prevent double allocation
  787. node.Reserved = nil
  788. kNode, ok := gcpNodes[nodeName]
  789. if !ok {
  790. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  791. continue
  792. }
  793. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  794. if !ok {
  795. klog.V(1).Infof("[Reserved] Could not find node region")
  796. continue
  797. }
  798. reservedCounters, ok := counters[nodeRegion]
  799. if !ok {
  800. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  801. continue
  802. }
  803. node.Reserved = &ReservedInstanceData{
  804. ReservedCPU: 0,
  805. ReservedRAM: 0,
  806. }
  807. for _, reservedCounter := range reservedCounters {
  808. if reservedCounter.RemainingCPU != 0 {
  809. nodeCPU, _ := strconv.ParseInt(node.VCPU, 10, 64)
  810. nodeCPU -= node.Reserved.ReservedCPU
  811. node.Reserved.CPUCost = reservedCounter.Instance.Plan.CPUCost
  812. if reservedCounter.RemainingCPU >= nodeCPU {
  813. reservedCounter.RemainingCPU -= nodeCPU
  814. node.Reserved.ReservedCPU += nodeCPU
  815. } else {
  816. node.Reserved.ReservedCPU += reservedCounter.RemainingCPU
  817. reservedCounter.RemainingCPU = 0
  818. }
  819. }
  820. if reservedCounter.RemainingRAM != 0 {
  821. nodeRAMF, _ := strconv.ParseFloat(node.RAMBytes, 64)
  822. nodeRAM := int64(nodeRAMF)
  823. nodeRAM -= node.Reserved.ReservedRAM
  824. node.Reserved.RAMCost = reservedCounter.Instance.Plan.RAMCost
  825. if reservedCounter.RemainingRAM >= nodeRAM {
  826. reservedCounter.RemainingRAM -= nodeRAM
  827. node.Reserved.ReservedRAM += nodeRAM
  828. } else {
  829. node.Reserved.ReservedRAM += reservedCounter.RemainingRAM
  830. reservedCounter.RemainingRAM = 0
  831. }
  832. }
  833. }
  834. }
  835. }
  836. func (gcp *GCP) getReservedInstances() ([]*GCPReservedInstance, error) {
  837. var results []*GCPReservedInstance
  838. ctx := context.Background()
  839. computeService, err := compute.NewService(ctx)
  840. if err != nil {
  841. return nil, err
  842. }
  843. commitments, err := computeService.RegionCommitments.AggregatedList(gcp.ProjectID).Do()
  844. if err != nil {
  845. return nil, err
  846. }
  847. for regionKey, commitList := range commitments.Items {
  848. for _, commit := range commitList.Commitments {
  849. if commit.Status != GCPReservedInstanceStatusActive {
  850. continue
  851. }
  852. var vcpu int64 = 0
  853. var ram int64 = 0
  854. for _, resource := range commit.Resources {
  855. switch resource.Type {
  856. case GCPReservedInstanceResourceTypeRAM:
  857. ram = resource.Amount * 1024 * 1024
  858. case GCPReservedInstanceResourceTypeCPU:
  859. vcpu = resource.Amount
  860. default:
  861. klog.V(4).Infof("Failed to handle resource type: %s", resource.Type)
  862. }
  863. }
  864. var region string
  865. regionStr := strings.Split(regionKey, "/")
  866. if len(regionStr) == 2 {
  867. region = regionStr[1]
  868. }
  869. timeLayout := "2006-01-02T15:04:05Z07:00"
  870. startTime, err := time.Parse(timeLayout, commit.StartTimestamp)
  871. if err != nil {
  872. klog.V(1).Infof("Failed to parse start date: %s", commit.StartTimestamp)
  873. continue
  874. }
  875. endTime, err := time.Parse(timeLayout, commit.EndTimestamp)
  876. if err != nil {
  877. klog.V(1).Infof("Failed to parse end date: %s", commit.EndTimestamp)
  878. continue
  879. }
  880. // Look for a plan based on the name. Default to One Year if it fails
  881. plan, ok := gcpReservedInstancePlans[commit.Plan]
  882. if !ok {
  883. plan = gcpReservedInstancePlans[GCPReservedInstancePlanOneYear]
  884. }
  885. results = append(results, &GCPReservedInstance{
  886. Region: region,
  887. ReservedRAM: ram,
  888. ReservedCPU: vcpu,
  889. Plan: plan,
  890. StartDate: startTime,
  891. EndDate: endTime,
  892. })
  893. }
  894. }
  895. return results, nil
  896. }
  897. type pvKey struct {
  898. Labels map[string]string
  899. StorageClass string
  900. StorageClassParameters map[string]string
  901. }
  902. func (key *pvKey) GetStorageClass() string {
  903. return key.StorageClass
  904. }
  905. func (gcp *GCP) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  906. return &pvKey{
  907. Labels: pv.Labels,
  908. StorageClass: pv.Spec.StorageClassName,
  909. StorageClassParameters: parameters,
  910. }
  911. }
  912. func (key *pvKey) Features() string {
  913. // TODO: regional cluster pricing.
  914. storageClass := key.StorageClassParameters["type"]
  915. if storageClass == "pd-ssd" {
  916. storageClass = "ssd"
  917. } else if storageClass == "pd-standard" {
  918. storageClass = "pdstandard"
  919. }
  920. return key.Labels[v1.LabelZoneRegion] + "," + storageClass
  921. }
  922. type gcpKey struct {
  923. Labels map[string]string
  924. }
  925. func (gcp *GCP) GetKey(labels map[string]string) Key {
  926. return &gcpKey{
  927. Labels: labels,
  928. }
  929. }
  930. func (gcp *gcpKey) ID() string {
  931. return ""
  932. }
  933. func (gcp *gcpKey) GPUType() string {
  934. if t, ok := gcp.Labels[GKE_GPU_TAG]; ok {
  935. var usageType string
  936. if t, ok := gcp.Labels["cloud.google.com/gke-preemptible"]; ok && t == "true" {
  937. usageType = "preemptible"
  938. } else {
  939. usageType = "ondemand"
  940. }
  941. klog.V(4).Infof("GPU of type: \"%s\" found", t)
  942. return t + "," + usageType
  943. }
  944. return ""
  945. }
  946. // GetKey maps node labels to information needed to retrieve pricing data
  947. func (gcp *gcpKey) Features() string {
  948. instanceType := strings.ToLower(strings.Join(strings.Split(gcp.Labels[v1.LabelInstanceType], "-")[:2], ""))
  949. if instanceType == "n1highmem" || instanceType == "n1highcpu" {
  950. instanceType = "n1standard" // These are priced the same. TODO: support n1ultrahighmem
  951. } else if strings.HasPrefix(instanceType, "custom") {
  952. instanceType = "custom" // The suffix of custom does not matter
  953. }
  954. region := strings.ToLower(gcp.Labels[v1.LabelZoneRegion])
  955. var usageType string
  956. if t, ok := gcp.Labels["cloud.google.com/gke-preemptible"]; ok && t == "true" {
  957. usageType = "preemptible"
  958. } else {
  959. usageType = "ondemand"
  960. }
  961. if _, ok := gcp.Labels[GKE_GPU_TAG]; ok {
  962. return region + "," + instanceType + "," + usageType + "," + "gpu"
  963. }
  964. return region + "," + instanceType + "," + usageType
  965. }
  966. // AllNodePricing returns the GCP pricing objects stored
  967. func (gcp *GCP) AllNodePricing() (interface{}, error) {
  968. gcp.DownloadPricingDataLock.RLock()
  969. defer gcp.DownloadPricingDataLock.RUnlock()
  970. return gcp.Pricing, nil
  971. }
  972. // NodePricing returns GCP pricing data for a single node
  973. func (gcp *GCP) NodePricing(key Key) (*Node, error) {
  974. gcp.DownloadPricingDataLock.RLock()
  975. defer gcp.DownloadPricingDataLock.RUnlock()
  976. if n, ok := gcp.Pricing[key.Features()]; ok {
  977. klog.V(4).Infof("Returning pricing for node %s: %+v from SKU %s", key, n.Node, n.Name)
  978. n.Node.BaseCPUPrice = gcp.BaseCPUPrice
  979. return n.Node, nil
  980. }
  981. klog.V(1).Infof("Warning: no pricing data found for %s: %s", key.Features(), key)
  982. return nil, fmt.Errorf("Warning: no pricing data found for %s", key)
  983. }