gcpprovider.go 31 KB

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