awsprovider.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. package cloud
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/csv"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. "k8s.io/klog"
  19. "github.com/kubecost/cost-model/pkg/clustercache"
  20. "github.com/aws/aws-sdk-go/aws"
  21. "github.com/aws/aws-sdk-go/aws/awserr"
  22. "github.com/aws/aws-sdk-go/aws/session"
  23. "github.com/aws/aws-sdk-go/service/athena"
  24. "github.com/aws/aws-sdk-go/service/ec2"
  25. "github.com/aws/aws-sdk-go/service/s3"
  26. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  27. "github.com/jszwec/csvutil"
  28. v1 "k8s.io/api/core/v1"
  29. )
  30. const awsAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  31. const awsAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
  32. const awsReservedInstancePricePerHour = 0.0287
  33. const supportedSpotFeedVersion = "1"
  34. const SpotInfoUpdateType = "spotinfo"
  35. const AthenaInfoUpdateType = "athenainfo"
  36. // AWS represents an Amazon Provider
  37. type AWS struct {
  38. Pricing map[string]*AWSProductTerms
  39. SpotPricingByInstanceID map[string]*spotInfo
  40. ValidPricingKeys map[string]bool
  41. Clientset clustercache.ClusterCache
  42. BaseCPUPrice string
  43. BaseRAMPrice string
  44. BaseGPUPrice string
  45. BaseSpotCPUPrice string
  46. BaseSpotRAMPrice string
  47. SpotLabelName string
  48. SpotLabelValue string
  49. ServiceKeyName string
  50. ServiceKeySecret string
  51. SpotDataRegion string
  52. SpotDataBucket string
  53. SpotDataPrefix string
  54. ProjectID string
  55. DownloadPricingDataLock sync.RWMutex
  56. ReservedInstances []*AWSReservedInstance
  57. Config *ProviderConfig
  58. *CustomProvider
  59. }
  60. // AWSPricing maps a k8s node to an AWS Pricing "product"
  61. type AWSPricing struct {
  62. Products map[string]*AWSProduct `json:"products"`
  63. Terms AWSPricingTerms `json:"terms"`
  64. }
  65. // AWSProduct represents a purchased SKU
  66. type AWSProduct struct {
  67. Sku string `json:"sku"`
  68. Attributes AWSProductAttributes `json:"attributes"`
  69. }
  70. // AWSProductAttributes represents metadata about the product used to map to a node.
  71. type AWSProductAttributes struct {
  72. Location string `json:"location"`
  73. InstanceType string `json:"instanceType"`
  74. Memory string `json:"memory"`
  75. Storage string `json:"storage"`
  76. VCpu string `json:"vcpu"`
  77. UsageType string `json:"usagetype"`
  78. OperatingSystem string `json:"operatingSystem"`
  79. PreInstalledSw string `json:"preInstalledSw"`
  80. InstanceFamily string `json:"instanceFamily"`
  81. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  82. }
  83. // AWSPricingTerms are how you pay for the node: OnDemand, Reserved, or (TODO) Spot
  84. type AWSPricingTerms struct {
  85. OnDemand map[string]map[string]*AWSOfferTerm `json:"OnDemand"`
  86. Reserved map[string]map[string]*AWSOfferTerm `json:"Reserved"`
  87. }
  88. // AWSOfferTerm is a sku extension used to pay for the node.
  89. type AWSOfferTerm struct {
  90. Sku string `json:"sku"`
  91. PriceDimensions map[string]*AWSRateCode `json:"priceDimensions"`
  92. }
  93. // AWSRateCode encodes data about the price of a product
  94. type AWSRateCode struct {
  95. Unit string `json:"unit"`
  96. PricePerUnit AWSCurrencyCode `json:"pricePerUnit"`
  97. }
  98. // AWSCurrencyCode is the localized currency. (TODO: support non-USD)
  99. type AWSCurrencyCode struct {
  100. USD string `json:"USD"`
  101. }
  102. // AWSProductTerms represents the full terms of the product
  103. type AWSProductTerms struct {
  104. Sku string `json:"sku"`
  105. OnDemand *AWSOfferTerm `json:"OnDemand"`
  106. Reserved *AWSOfferTerm `json:"Reserved"`
  107. Memory string `json:"memory"`
  108. Storage string `json:"storage"`
  109. VCpu string `json:"vcpu"`
  110. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  111. PV *PV `json:"pv"`
  112. }
  113. // ClusterIdEnvVar is the environment variable in which one can manually set the ClusterId
  114. const ClusterIdEnvVar = "AWS_CLUSTER_ID"
  115. // OnDemandRateCode is appended to an node sku
  116. const OnDemandRateCode = ".JRTCKXETXF"
  117. // ReservedRateCode is appended to a node sku
  118. const ReservedRateCode = ".38NPMPTW36"
  119. // HourlyRateCode is appended to a node sku
  120. const HourlyRateCode = ".6YS6EN2CT7"
  121. // volTypes are used to map between AWS UsageTypes and
  122. // EBS volume types, as they would appear in K8s storage class
  123. // name and the EC2 API.
  124. var volTypes = map[string]string{
  125. "EBS:VolumeUsage.gp2": "gp2",
  126. "EBS:VolumeUsage": "standard",
  127. "EBS:VolumeUsage.sc1": "sc1",
  128. "EBS:VolumeP-IOPS.piops": "io1",
  129. "EBS:VolumeUsage.st1": "st1",
  130. "EBS:VolumeUsage.piops": "io1",
  131. "gp2": "EBS:VolumeUsage.gp2",
  132. "standard": "EBS:VolumeUsage",
  133. "sc1": "EBS:VolumeUsage.sc1",
  134. "io1": "EBS:VolumeUsage.piops",
  135. "st1": "EBS:VolumeUsage.st1",
  136. }
  137. // locationToRegion maps AWS region names (As they come from Billing)
  138. // to actual region identifiers
  139. var locationToRegion = map[string]string{
  140. "US East (Ohio)": "us-east-2",
  141. "US East (N. Virginia)": "us-east-1",
  142. "US West (N. California)": "us-west-1",
  143. "US West (Oregon)": "us-west-2",
  144. "Asia Pacific (Hong Kong)": "ap-east-1",
  145. "Asia Pacific (Mumbai)": "ap-south-1",
  146. "Asia Pacific (Osaka-Local)": "ap-northeast-3",
  147. "Asia Pacific (Seoul)": "ap-northeast-2",
  148. "Asia Pacific (Singapore)": "ap-southeast-1",
  149. "Asia Pacific (Sydney)": "ap-southeast-2",
  150. "Asia Pacific (Tokyo)": "ap-northeast-1",
  151. "Canada (Central)": "ca-central-1",
  152. "China (Beijing)": "cn-north-1",
  153. "China (Ningxia)": "cn-northwest-1",
  154. "EU (Frankfurt)": "eu-central-1",
  155. "EU (Ireland)": "eu-west-1",
  156. "EU (London)": "eu-west-2",
  157. "EU (Paris)": "eu-west-3",
  158. "EU (Stockholm)": "eu-north-1",
  159. "South America (Sao Paulo)": "sa-east-1",
  160. "AWS GovCloud (US-East)": "us-gov-east-1",
  161. "AWS GovCloud (US)": "us-gov-west-1",
  162. }
  163. var regionToBillingRegionCode = map[string]string{
  164. "us-east-2": "USE2",
  165. "us-east-1": "",
  166. "us-west-1": "USW1",
  167. "us-west-2": "USW2",
  168. "ap-east-1": "APE1",
  169. "ap-south-1": "APS3",
  170. "ap-northeast-3": "APN3",
  171. "ap-northeast-2": "APN2",
  172. "ap-southeast-1": "APS1",
  173. "ap-southeast-2": "APS2",
  174. "ap-northeast-1": "APN1",
  175. "ca-central-1": "CAN1",
  176. "cn-north-1": "",
  177. "cn-northwest-1": "",
  178. "eu-central-1": "EUC1",
  179. "eu-west-1": "EU",
  180. "eu-west-2": "EUW2",
  181. "eu-west-3": "EUW3",
  182. "eu-north-1": "EUN1",
  183. "sa-east-1": "SAE1",
  184. "us-gov-east-1": "UGE1",
  185. "us-gov-west-1": "UGW1",
  186. }
  187. func (aws *AWS) GetLocalStorageQuery(window, offset string, rate bool) string {
  188. return ""
  189. }
  190. // KubeAttrConversion maps the k8s labels for region to an aws region
  191. func (aws *AWS) KubeAttrConversion(location, instanceType, operatingSystem string) string {
  192. operatingSystem = strings.ToLower(operatingSystem)
  193. region := locationToRegion[location]
  194. return region + "," + instanceType + "," + operatingSystem
  195. }
  196. type AwsSpotFeedInfo struct {
  197. BucketName string `json:"bucketName"`
  198. Prefix string `json:"prefix"`
  199. Region string `json:"region"`
  200. AccountID string `json:"projectID"`
  201. ServiceKeyName string `json:"serviceKeyName"`
  202. ServiceKeySecret string `json:"serviceKeySecret"`
  203. SpotLabel string `json:"spotLabel"`
  204. SpotLabelValue string `json:"spotLabelValue"`
  205. }
  206. type AwsAthenaInfo struct {
  207. AthenaBucketName string `json:"athenaBucketName"`
  208. AthenaRegion string `json:"athenaRegion"`
  209. AthenaDatabase string `json:"athenaDatabase"`
  210. AthenaTable string `json:"athenaTable"`
  211. ServiceKeyName string `json:"serviceKeyName"`
  212. ServiceKeySecret string `json:"serviceKeySecret"`
  213. AccountID string `json:"projectID"`
  214. }
  215. func (aws *AWS) GetManagementPlatform() (string, error) {
  216. nodes := aws.Clientset.GetAllNodes()
  217. if len(nodes) > 0 {
  218. n := nodes[0]
  219. version := n.Status.NodeInfo.KubeletVersion
  220. if strings.Contains(version, "eks") {
  221. return "eks", nil
  222. }
  223. if _, ok := n.Labels["kops.k8s.io/instancegroup"]; ok {
  224. return "kops", nil
  225. }
  226. }
  227. return "", nil
  228. }
  229. func (aws *AWS) GetConfig() (*CustomPricing, error) {
  230. c, err := aws.Config.GetCustomPricingData()
  231. if c.Discount == "" {
  232. c.Discount = "0%"
  233. }
  234. if c.NegotiatedDiscount == "" {
  235. c.NegotiatedDiscount = "0%"
  236. }
  237. if err != nil {
  238. return nil, err
  239. }
  240. return c, nil
  241. }
  242. func (aws *AWS) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  243. return aws.Config.UpdateFromMap(a)
  244. }
  245. func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  246. return aws.Config.Update(func(c *CustomPricing) error {
  247. if updateType == SpotInfoUpdateType {
  248. a := AwsSpotFeedInfo{}
  249. err := json.NewDecoder(r).Decode(&a)
  250. if err != nil {
  251. return err
  252. }
  253. c.ServiceKeyName = a.ServiceKeyName
  254. c.ServiceKeySecret = a.ServiceKeySecret
  255. c.SpotDataPrefix = a.Prefix
  256. c.SpotDataBucket = a.BucketName
  257. c.ProjectID = a.AccountID
  258. c.SpotDataRegion = a.Region
  259. c.SpotLabel = a.SpotLabel
  260. c.SpotLabelValue = a.SpotLabelValue
  261. } else if updateType == AthenaInfoUpdateType {
  262. a := AwsAthenaInfo{}
  263. err := json.NewDecoder(r).Decode(&a)
  264. if err != nil {
  265. return err
  266. }
  267. c.AthenaBucketName = a.AthenaBucketName
  268. c.AthenaRegion = a.AthenaRegion
  269. c.AthenaDatabase = a.AthenaDatabase
  270. c.AthenaTable = a.AthenaTable
  271. c.ServiceKeyName = a.ServiceKeyName
  272. c.ServiceKeySecret = a.ServiceKeySecret
  273. c.AthenaProjectID = a.AccountID
  274. } else {
  275. a := make(map[string]interface{})
  276. err := json.NewDecoder(r).Decode(&a)
  277. if err != nil {
  278. return err
  279. }
  280. for k, v := range a {
  281. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  282. vstr, ok := v.(string)
  283. if ok {
  284. err := SetCustomPricingField(c, kUpper, vstr)
  285. if err != nil {
  286. return err
  287. }
  288. } else {
  289. sci := v.(map[string]interface{})
  290. sc := make(map[string]string)
  291. for k, val := range sci {
  292. sc[k] = val.(string)
  293. }
  294. c.SharedCosts = sc //todo: support reflection/multiple map fields
  295. }
  296. }
  297. }
  298. remoteEnabled := os.Getenv(remoteEnabled)
  299. if remoteEnabled == "true" {
  300. err := UpdateClusterMeta(os.Getenv(clusterIDKey), c.ClusterName)
  301. if err != nil {
  302. return err
  303. }
  304. }
  305. return nil
  306. })
  307. }
  308. type awsKey struct {
  309. SpotLabelName string
  310. SpotLabelValue string
  311. Labels map[string]string
  312. ProviderID string
  313. }
  314. func (k *awsKey) GPUType() string {
  315. return ""
  316. }
  317. func (k *awsKey) ID() string {
  318. provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)") // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
  319. for matchNum, group := range provIdRx.FindStringSubmatch(k.ProviderID) {
  320. if matchNum == 2 {
  321. return group
  322. }
  323. }
  324. klog.V(3).Infof("Could not find instance ID in \"%s\"", k.ProviderID)
  325. return ""
  326. }
  327. func (k *awsKey) Features() string {
  328. instanceType := k.Labels[v1.LabelInstanceType]
  329. var operatingSystem string
  330. operatingSystem, ok := k.Labels[v1.LabelOSStable]
  331. if !ok {
  332. operatingSystem = k.Labels["beta.kubernetes.io/os"]
  333. }
  334. region := k.Labels[v1.LabelZoneRegion]
  335. key := region + "," + instanceType + "," + operatingSystem
  336. usageType := "preemptible"
  337. spotKey := key + "," + usageType
  338. if l, ok := k.Labels["lifecycle"]; ok && l == "EC2Spot" {
  339. return spotKey
  340. }
  341. if l, ok := k.Labels[k.SpotLabelName]; ok && l == k.SpotLabelValue {
  342. return spotKey
  343. }
  344. return key
  345. }
  346. func (aws *AWS) PVPricing(pvk PVKey) (*PV, error) {
  347. pricing, ok := aws.Pricing[pvk.Features()]
  348. if !ok {
  349. klog.V(4).Infof("Persistent Volume pricing not found for %s: %s", pvk.GetStorageClass(), pvk.Features())
  350. return &PV{}, nil
  351. }
  352. return pricing.PV, nil
  353. }
  354. type awsPVKey struct {
  355. Labels map[string]string
  356. StorageClassParameters map[string]string
  357. StorageClassName string
  358. Name string
  359. }
  360. func (aws *AWS) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  361. return &awsPVKey{
  362. Labels: pv.Labels,
  363. StorageClassName: pv.Spec.StorageClassName,
  364. StorageClassParameters: parameters,
  365. Name: pv.Name,
  366. }
  367. }
  368. func (key *awsPVKey) GetStorageClass() string {
  369. return key.StorageClassName
  370. }
  371. func (key *awsPVKey) Features() string {
  372. storageClass := key.StorageClassParameters["type"]
  373. if storageClass == "standard" {
  374. storageClass = "gp2"
  375. }
  376. // Storage class names are generally EBS volume types (gp2)
  377. // Keys in Pricing are based on UsageTypes (EBS:VolumeType.gp2)
  378. // Converts between the 2
  379. region := key.Labels[v1.LabelZoneRegion]
  380. //if region == "" {
  381. // region = "us-east-1"
  382. //}
  383. class, ok := volTypes[storageClass]
  384. if !ok {
  385. klog.V(4).Infof("No voltype mapping for %s's storageClass: %s", key.Name, storageClass)
  386. }
  387. return region + "," + class
  388. }
  389. // GetKey maps node labels to information needed to retrieve pricing data
  390. func (aws *AWS) GetKey(labels map[string]string) Key {
  391. return &awsKey{
  392. SpotLabelName: aws.SpotLabelName,
  393. SpotLabelValue: aws.SpotLabelValue,
  394. Labels: labels,
  395. ProviderID: labels["providerID"],
  396. }
  397. }
  398. func (aws *AWS) isPreemptible(key string) bool {
  399. s := strings.Split(key, ",")
  400. if len(s) == 4 && s[3] == "preemptible" {
  401. return true
  402. }
  403. return false
  404. }
  405. // DownloadPricingData fetches data from the AWS Pricing API
  406. func (aws *AWS) DownloadPricingData() error {
  407. aws.DownloadPricingDataLock.Lock()
  408. defer aws.DownloadPricingDataLock.Unlock()
  409. c, err := aws.Config.GetCustomPricingData()
  410. if err != nil {
  411. klog.V(1).Infof("Error downloading default pricing data: %s", err.Error())
  412. }
  413. aws.BaseCPUPrice = c.CPU
  414. aws.BaseRAMPrice = c.RAM
  415. aws.BaseGPUPrice = c.GPU
  416. aws.BaseSpotCPUPrice = c.SpotCPU
  417. aws.BaseSpotRAMPrice = c.SpotRAM
  418. aws.SpotLabelName = c.SpotLabel
  419. aws.SpotLabelValue = c.SpotLabelValue
  420. aws.SpotDataBucket = c.SpotDataBucket
  421. aws.SpotDataPrefix = c.SpotDataPrefix
  422. aws.ProjectID = c.ProjectID
  423. aws.SpotDataRegion = c.SpotDataRegion
  424. aws.ServiceKeyName = c.ServiceKeyName
  425. aws.ServiceKeySecret = c.ServiceKeySecret
  426. if len(aws.SpotDataBucket) != 0 && len(aws.ProjectID) == 0 {
  427. klog.V(1).Infof("using SpotDataBucket \"%s\" without ProjectID will not end well", aws.SpotDataBucket)
  428. }
  429. nodeList := aws.Clientset.GetAllNodes()
  430. inputkeys := make(map[string]bool)
  431. for _, n := range nodeList {
  432. labels := n.GetObjectMeta().GetLabels()
  433. key := aws.GetKey(labels)
  434. inputkeys[key.Features()] = true
  435. }
  436. pvList := aws.Clientset.GetAllPersistentVolumes()
  437. storageClasses := aws.Clientset.GetAllStorageClasses()
  438. storageClassMap := make(map[string]map[string]string)
  439. for _, storageClass := range storageClasses {
  440. params := storageClass.Parameters
  441. storageClassMap[storageClass.ObjectMeta.Name] = params
  442. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  443. storageClassMap["default"] = params
  444. storageClassMap[""] = params
  445. }
  446. }
  447. pvkeys := make(map[string]PVKey)
  448. for _, pv := range pvList {
  449. params, ok := storageClassMap[pv.Spec.StorageClassName]
  450. if !ok {
  451. klog.V(2).Infof("Unable to find params for storageClassName %s, falling back to default pricing", pv.Spec.StorageClassName)
  452. continue
  453. }
  454. key := aws.GetPVKey(pv, params)
  455. pvkeys[key.Features()] = key
  456. }
  457. reserved, err := aws.getReservedInstances()
  458. if err != nil {
  459. klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
  460. } else {
  461. klog.V(1).Infof("Found %d reserved instances", len(reserved))
  462. aws.ReservedInstances = reserved
  463. for _, r := range reserved {
  464. klog.V(1).Infof("%s", r)
  465. }
  466. }
  467. aws.Pricing = make(map[string]*AWSProductTerms)
  468. aws.ValidPricingKeys = make(map[string]bool)
  469. skusToKeys := make(map[string]string)
  470. pricingURL := "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.json"
  471. klog.V(2).Infof("starting download of \"%s\", which is quite large ...", pricingURL)
  472. resp, err := http.Get(pricingURL)
  473. if err != nil {
  474. klog.V(2).Infof("Bogus fetch of \"%s\": %v", pricingURL, err)
  475. return err
  476. }
  477. klog.V(2).Infof("Finished downloading \"%s\"", pricingURL)
  478. dec := json.NewDecoder(resp.Body)
  479. for {
  480. t, err := dec.Token()
  481. if err == io.EOF {
  482. klog.V(2).Infof("done loading \"%s\"\n", pricingURL)
  483. break
  484. }
  485. if t == "products" {
  486. _, err := dec.Token() // this should parse the opening "{""
  487. if err != nil {
  488. return err
  489. }
  490. for dec.More() {
  491. _, err := dec.Token() // the sku token
  492. if err != nil {
  493. return err
  494. }
  495. product := &AWSProduct{}
  496. err = dec.Decode(&product)
  497. if err != nil {
  498. klog.V(1).Infof("Error parsing response from \"%s\": %v", pricingURL, err.Error())
  499. break
  500. }
  501. if product.Attributes.PreInstalledSw == "NA" &&
  502. (strings.HasPrefix(product.Attributes.UsageType, "BoxUsage") || strings.Contains(product.Attributes.UsageType, "-BoxUsage")) {
  503. key := aws.KubeAttrConversion(product.Attributes.Location, product.Attributes.InstanceType, product.Attributes.OperatingSystem)
  504. spotKey := key + ",preemptible"
  505. if inputkeys[key] || inputkeys[spotKey] { // Just grab the sku even if spot, and change the price later.
  506. productTerms := &AWSProductTerms{
  507. Sku: product.Sku,
  508. Memory: product.Attributes.Memory,
  509. Storage: product.Attributes.Storage,
  510. VCpu: product.Attributes.VCpu,
  511. GPU: product.Attributes.GPU,
  512. }
  513. aws.Pricing[key] = productTerms
  514. aws.Pricing[spotKey] = productTerms
  515. skusToKeys[product.Sku] = key
  516. }
  517. aws.ValidPricingKeys[key] = true
  518. aws.ValidPricingKeys[spotKey] = true
  519. } else if strings.Contains(product.Attributes.UsageType, "EBS:Volume") {
  520. // UsageTypes may be prefixed with a region code - we're removing this when using
  521. // volTypes to keep lookups generic
  522. usageTypeRegx := regexp.MustCompile(".*(-|^)(EBS.+)")
  523. usageTypeMatch := usageTypeRegx.FindStringSubmatch(product.Attributes.UsageType)
  524. usageTypeNoRegion := usageTypeMatch[len(usageTypeMatch)-1]
  525. key := locationToRegion[product.Attributes.Location] + "," + usageTypeNoRegion
  526. spotKey := key + ",preemptible"
  527. pv := &PV{
  528. Class: volTypes[usageTypeNoRegion],
  529. Region: locationToRegion[product.Attributes.Location],
  530. }
  531. productTerms := &AWSProductTerms{
  532. Sku: product.Sku,
  533. PV: pv,
  534. }
  535. aws.Pricing[key] = productTerms
  536. aws.Pricing[spotKey] = productTerms
  537. skusToKeys[product.Sku] = key
  538. aws.ValidPricingKeys[key] = true
  539. aws.ValidPricingKeys[spotKey] = true
  540. }
  541. }
  542. }
  543. if t == "terms" {
  544. _, err := dec.Token() // this should parse the opening "{""
  545. if err != nil {
  546. return err
  547. }
  548. termType, err := dec.Token()
  549. if err != nil {
  550. return err
  551. }
  552. if termType == "OnDemand" {
  553. _, err := dec.Token()
  554. if err != nil { // again, should parse an opening "{"
  555. return err
  556. }
  557. for dec.More() {
  558. sku, err := dec.Token()
  559. if err != nil {
  560. return err
  561. }
  562. _, err = dec.Token() // another opening "{"
  563. if err != nil {
  564. return err
  565. }
  566. skuOnDemand, err := dec.Token()
  567. if err != nil {
  568. return err
  569. }
  570. offerTerm := &AWSOfferTerm{}
  571. err = dec.Decode(&offerTerm)
  572. if err != nil {
  573. klog.V(1).Infof("Error decoding AWS Offer Term: " + err.Error())
  574. }
  575. if sku.(string)+OnDemandRateCode == skuOnDemand {
  576. key, ok := skusToKeys[sku.(string)]
  577. spotKey := key + ",preemptible"
  578. if ok {
  579. aws.Pricing[key].OnDemand = offerTerm
  580. aws.Pricing[spotKey].OnDemand = offerTerm
  581. if strings.Contains(key, "EBS:VolumeP-IOPS.piops") {
  582. // If the specific UsageType is the per IO cost used on io1 volumes
  583. // we need to add the per IO cost to the io1 PV cost
  584. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  585. // Add the per IO cost to the PV object for the io1 volume type
  586. aws.Pricing[key].PV.CostPerIO = cost
  587. } else if strings.Contains(key, "EBS:Volume") {
  588. // If volume, we need to get hourly cost and add it to the PV object
  589. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  590. costFloat, _ := strconv.ParseFloat(cost, 64)
  591. hourlyPrice := costFloat / 730
  592. aws.Pricing[key].PV.Cost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  593. }
  594. }
  595. }
  596. _, err = dec.Token()
  597. if err != nil {
  598. return err
  599. }
  600. }
  601. _, err = dec.Token()
  602. if err != nil {
  603. return err
  604. }
  605. }
  606. }
  607. }
  608. sp, err := parseSpotData(aws.SpotDataBucket, aws.SpotDataPrefix, aws.ProjectID, aws.SpotDataRegion, aws.ServiceKeyName, aws.ServiceKeySecret)
  609. if err != nil {
  610. klog.V(1).Infof("Skipping AWS spot data download: %s", err.Error())
  611. } else {
  612. aws.SpotPricingByInstanceID = sp
  613. }
  614. return nil
  615. }
  616. // Stubbed NetworkPricing for AWS. Pull directly from aws.json for now
  617. func (aws *AWS) NetworkPricing() (*Network, error) {
  618. cpricing, err := aws.Config.GetCustomPricingData()
  619. if err != nil {
  620. return nil, err
  621. }
  622. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  623. if err != nil {
  624. return nil, err
  625. }
  626. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  627. if err != nil {
  628. return nil, err
  629. }
  630. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  631. if err != nil {
  632. return nil, err
  633. }
  634. return &Network{
  635. ZoneNetworkEgressCost: znec,
  636. RegionNetworkEgressCost: rnec,
  637. InternetNetworkEgressCost: inec,
  638. }, nil
  639. }
  640. // AllNodePricing returns all the billing data fetched.
  641. func (aws *AWS) AllNodePricing() (interface{}, error) {
  642. aws.DownloadPricingDataLock.RLock()
  643. defer aws.DownloadPricingDataLock.RUnlock()
  644. return aws.Pricing, nil
  645. }
  646. func (aws *AWS) createNode(terms *AWSProductTerms, usageType string, k Key) (*Node, error) {
  647. key := k.Features()
  648. if aws.isPreemptible(key) {
  649. if spotInfo, ok := aws.SpotPricingByInstanceID[k.ID()]; ok { // try and match directly to an ID for pricing. We'll still need the features
  650. var spotcost string
  651. arr := strings.Split(spotInfo.Charge, " ")
  652. if len(arr) == 2 {
  653. spotcost = arr[0]
  654. } else {
  655. klog.V(2).Infof("Spot data for node %s is missing", k.ID())
  656. }
  657. return &Node{
  658. Cost: spotcost,
  659. VCPU: terms.VCpu,
  660. RAM: terms.Memory,
  661. GPU: terms.GPU,
  662. Storage: terms.Storage,
  663. BaseCPUPrice: aws.BaseCPUPrice,
  664. BaseRAMPrice: aws.BaseRAMPrice,
  665. BaseGPUPrice: aws.BaseGPUPrice,
  666. UsageType: usageType,
  667. }, nil
  668. }
  669. return &Node{
  670. VCPU: terms.VCpu,
  671. VCPUCost: aws.BaseSpotCPUPrice,
  672. RAM: terms.Memory,
  673. GPU: terms.GPU,
  674. RAMCost: aws.BaseSpotRAMPrice,
  675. Storage: terms.Storage,
  676. BaseCPUPrice: aws.BaseCPUPrice,
  677. BaseRAMPrice: aws.BaseRAMPrice,
  678. BaseGPUPrice: aws.BaseGPUPrice,
  679. UsageType: usageType,
  680. }, nil
  681. }
  682. c, ok := terms.OnDemand.PriceDimensions[terms.Sku+OnDemandRateCode+HourlyRateCode]
  683. if !ok {
  684. return nil, fmt.Errorf("Could not fetch data for \"%s\"", k.ID())
  685. }
  686. cost := c.PricePerUnit.USD
  687. return &Node{
  688. Cost: cost,
  689. VCPU: terms.VCpu,
  690. RAM: terms.Memory,
  691. GPU: terms.GPU,
  692. Storage: terms.Storage,
  693. BaseCPUPrice: aws.BaseCPUPrice,
  694. BaseRAMPrice: aws.BaseRAMPrice,
  695. BaseGPUPrice: aws.BaseGPUPrice,
  696. UsageType: usageType,
  697. }, nil
  698. }
  699. // NodePricing takes in a key from GetKey and returns a Node object for use in building the cost model.
  700. func (aws *AWS) NodePricing(k Key) (*Node, error) {
  701. aws.DownloadPricingDataLock.RLock()
  702. defer aws.DownloadPricingDataLock.RUnlock()
  703. key := k.Features()
  704. usageType := "ondemand"
  705. if aws.isPreemptible(key) {
  706. usageType = "preemptible"
  707. }
  708. terms, ok := aws.Pricing[key]
  709. if ok {
  710. return aws.createNode(terms, usageType, k)
  711. } else if _, ok := aws.ValidPricingKeys[key]; ok {
  712. aws.DownloadPricingDataLock.RUnlock()
  713. err := aws.DownloadPricingData()
  714. aws.DownloadPricingDataLock.RLock()
  715. if err != nil {
  716. return &Node{
  717. Cost: aws.BaseCPUPrice,
  718. BaseCPUPrice: aws.BaseCPUPrice,
  719. BaseRAMPrice: aws.BaseRAMPrice,
  720. BaseGPUPrice: aws.BaseGPUPrice,
  721. UsageType: usageType,
  722. UsesBaseCPUPrice: true,
  723. }, err
  724. }
  725. terms, termsOk := aws.Pricing[key]
  726. if !termsOk {
  727. return &Node{
  728. Cost: aws.BaseCPUPrice,
  729. BaseCPUPrice: aws.BaseCPUPrice,
  730. BaseRAMPrice: aws.BaseRAMPrice,
  731. BaseGPUPrice: aws.BaseGPUPrice,
  732. UsageType: usageType,
  733. UsesBaseCPUPrice: true,
  734. }, fmt.Errorf("Unable to find any Pricing data for \"%s\"", key)
  735. }
  736. return aws.createNode(terms, usageType, k)
  737. } else { // Fall back to base pricing if we can't find the key.
  738. klog.V(1).Infof("Invalid Pricing Key \"%s\"", key)
  739. return &Node{
  740. Cost: aws.BaseCPUPrice,
  741. BaseCPUPrice: aws.BaseCPUPrice,
  742. BaseRAMPrice: aws.BaseRAMPrice,
  743. BaseGPUPrice: aws.BaseGPUPrice,
  744. UsageType: usageType,
  745. UsesBaseCPUPrice: true,
  746. }, nil
  747. }
  748. }
  749. // ClusterInfo returns an object that represents the cluster. TODO: actually return the name of the cluster. Blocked on cluster federation.
  750. func (awsProvider *AWS) ClusterInfo() (map[string]string, error) {
  751. defaultClusterName := "AWS Cluster #1"
  752. c, err := awsProvider.GetConfig()
  753. if err != nil {
  754. return nil, err
  755. }
  756. remote := os.Getenv(remoteEnabled)
  757. remoteEnabled := false
  758. if os.Getenv(remote) == "true" {
  759. remoteEnabled = true
  760. }
  761. if c.ClusterName != "" {
  762. m := make(map[string]string)
  763. m["name"] = c.ClusterName
  764. m["provider"] = "AWS"
  765. m["id"] = os.Getenv(clusterIDKey)
  766. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  767. return m, nil
  768. }
  769. makeStructure := func(clusterName string) (map[string]string, error) {
  770. klog.V(2).Infof("Returning \"%s\" as ClusterName", clusterName)
  771. m := make(map[string]string)
  772. m["name"] = clusterName
  773. m["provider"] = "AWS"
  774. m["id"] = os.Getenv(clusterIDKey)
  775. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  776. return m, nil
  777. }
  778. maybeClusterId := os.Getenv(ClusterIdEnvVar)
  779. if len(maybeClusterId) != 0 {
  780. return makeStructure(maybeClusterId)
  781. }
  782. // TODO: This should be cached, it can take a long time to hit the API
  783. //provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)")
  784. //clusterIdRx := regexp.MustCompile("^kubernetes\\.io/cluster/([^/]+)")
  785. //klog.Infof("nodelist get here %s", time.Now())
  786. //nodeList := awsProvider.Clientset.GetAllNodes()
  787. //klog.Infof("nodelist done here %s", time.Now())
  788. /*for _, n := range nodeList {
  789. region := ""
  790. instanceId := ""
  791. providerId := n.Spec.ProviderID
  792. for matchNum, group := range provIdRx.FindStringSubmatch(providerId) {
  793. if matchNum == 1 {
  794. region = group
  795. } else if matchNum == 2 {
  796. instanceId = group
  797. }
  798. }
  799. if len(instanceId) == 0 {
  800. klog.V(2).Infof("Unable to decode Node.ProviderID \"%s\", skipping it", providerId)
  801. continue
  802. }
  803. c := &aws.Config{
  804. Region: aws.String(region),
  805. }
  806. s := session.Must(session.NewSession(c))
  807. ec2Svc := ec2.New(s)
  808. di, diErr := ec2Svc.DescribeInstances(&ec2.DescribeInstancesInput{
  809. InstanceIds: []*string{
  810. aws.String(instanceId),
  811. },
  812. })
  813. if diErr != nil {
  814. klog.Infof("Error describing instances: %s", diErr)
  815. continue
  816. }
  817. if len(di.Reservations) != 1 {
  818. klog.V(2).Infof("Expected 1 Reservation back from DescribeInstances(%s), received %d", instanceId, len(di.Reservations))
  819. continue
  820. }
  821. res := di.Reservations[0]
  822. if len(res.Instances) != 1 {
  823. klog.V(2).Infof("Expected 1 Instance back from DescribeInstances(%s), received %d", instanceId, len(res.Instances))
  824. continue
  825. }
  826. inst := res.Instances[0]
  827. for _, tag := range inst.Tags {
  828. tagKey := *tag.Key
  829. for matchNum, group := range clusterIdRx.FindStringSubmatch(tagKey) {
  830. if matchNum != 1 {
  831. continue
  832. }
  833. return makeStructure(group)
  834. }
  835. }
  836. }*/
  837. klog.V(2).Infof("Unable to sniff out cluster ID, perhaps set $%s to force one", ClusterIdEnvVar)
  838. return makeStructure(defaultClusterName)
  839. }
  840. // AddServiceKey adds an AWS service key, useful for pulling down out-of-cluster costs. Optional-- the container this runs in can be directly authorized.
  841. func (*AWS) AddServiceKey(formValues url.Values) error {
  842. keyID := formValues.Get("access_key_ID")
  843. key := formValues.Get("secret_access_key")
  844. m := make(map[string]string)
  845. m["access_key_ID"] = keyID
  846. m["secret_access_key"] = key
  847. result, err := json.Marshal(m)
  848. if err != nil {
  849. return err
  850. }
  851. return ioutil.WriteFile("/var/configs/key.json", result, 0644)
  852. }
  853. func configureAWSAuth(keyFile string) error {
  854. jsonFile, err := os.Open(keyFile)
  855. if err != nil {
  856. if os.IsNotExist(err) {
  857. klog.V(2).Infof("Using Default Credentials")
  858. return nil
  859. }
  860. return err
  861. }
  862. defer jsonFile.Close()
  863. byteValue, _ := ioutil.ReadAll(jsonFile)
  864. var result map[string]string
  865. err = json.Unmarshal([]byte(byteValue), &result)
  866. if err != nil {
  867. return err
  868. }
  869. err = os.Setenv(awsAccessKeyIDEnvVar, result["awsServiceKeyName"])
  870. if err != nil {
  871. return err
  872. }
  873. err = os.Setenv(awsAccessKeySecretEnvVar, result["awsServiceKeySecret"])
  874. if err != nil {
  875. return err
  876. }
  877. return nil
  878. }
  879. func getClusterConfig(ccFile string) (map[string]string, error) {
  880. clusterConfig, err := os.Open(ccFile)
  881. if err != nil {
  882. return nil, err
  883. }
  884. defer clusterConfig.Close()
  885. b, err := ioutil.ReadAll(clusterConfig)
  886. if err != nil {
  887. return nil, err
  888. }
  889. var clusterConf map[string]string
  890. err = json.Unmarshal([]byte(b), &clusterConf)
  891. if err != nil {
  892. return nil, err
  893. }
  894. return clusterConf, nil
  895. }
  896. // GetDisks returns the AWS disks backing PVs. Useful because sometimes k8s will not clean up PVs correctly. Requires a json config in /var/configs with key region.
  897. func (*AWS) GetDisks() ([]byte, error) {
  898. err := configureAWSAuth("/var/configs/key.json")
  899. if err != nil {
  900. return nil, err
  901. }
  902. clusterConfig, err := getClusterConfig("/var/configs/cluster.json")
  903. if err != nil {
  904. return nil, err
  905. }
  906. region := aws.String(clusterConfig["region"])
  907. c := &aws.Config{
  908. Region: region,
  909. }
  910. s := session.Must(session.NewSession(c))
  911. ec2Svc := ec2.New(s)
  912. input := &ec2.DescribeVolumesInput{}
  913. volumeResult, err := ec2Svc.DescribeVolumes(input)
  914. if err != nil {
  915. if aerr, ok := err.(awserr.Error); ok {
  916. switch aerr.Code() {
  917. default:
  918. return nil, aerr
  919. }
  920. } else {
  921. return nil, err
  922. }
  923. }
  924. return json.Marshal(volumeResult)
  925. }
  926. // ConvertToGlueColumnFormat takes a string and runs through various regex
  927. // and string replacement statements to convert it to a format compatible
  928. // with AWS Glue and Athena column names.
  929. // Following guidance from AWS provided here ('Column Names' section):
  930. // https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/run-athena-sql.html
  931. // It returns a string containing the column name in proper column name format and length.
  932. func ConvertToGlueColumnFormat(column_name string) string {
  933. klog.V(5).Infof("Converting string \"%s\" to proper AWS Glue column name.", column_name)
  934. // An underscore is added in front of uppercase letters
  935. capital_underscore := regexp.MustCompile(`[A-Z]`)
  936. final := capital_underscore.ReplaceAllString(column_name, `_$0`)
  937. // Any non-alphanumeric characters are replaced with an underscore
  938. no_space_punc := regexp.MustCompile(`[\s]{1,}|[^A-Za-z0-9]`)
  939. final = no_space_punc.ReplaceAllString(final, "_")
  940. // Duplicate underscores are removed
  941. no_dup_underscore := regexp.MustCompile(`_{2,}`)
  942. final = no_dup_underscore.ReplaceAllString(final, "_")
  943. // Any leading and trailing underscores are removed
  944. no_front_end_underscore := regexp.MustCompile(`(^\_|\_$)`)
  945. final = no_front_end_underscore.ReplaceAllString(final, "")
  946. // Uppercase to lowercase
  947. final = strings.ToLower(final)
  948. // Longer column name than expected - remove _ left to right
  949. allowed_col_len := 128
  950. undersc_to_remove := len(final) - allowed_col_len
  951. if undersc_to_remove > 0 {
  952. final = strings.Replace(final, "_", "", undersc_to_remove)
  953. }
  954. // If removing all of the underscores still didn't
  955. // make the column name < 128 characters, trim it!
  956. if len(final) > allowed_col_len {
  957. final = final[:allowed_col_len]
  958. }
  959. klog.V(5).Infof("Column name being returned: \"%s\". Length: \"%d\".", final, len(final))
  960. return final
  961. }
  962. func generateAWSGroupBy(lastIdx int) string {
  963. sequence := []string{}
  964. for i := 1; i < lastIdx+1; i++ {
  965. sequence = append(sequence, strconv.Itoa(i))
  966. }
  967. return strings.Join(sequence, ",")
  968. }
  969. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  970. // "start" and "end" are dates of the format YYYY-MM-DD
  971. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  972. func (a *AWS) ExternalAllocations(start string, end string, aggregators []string, filterType string, filterValue string) ([]*OutOfClusterAllocation, error) {
  973. customPricing, err := a.GetConfig()
  974. if err != nil {
  975. return nil, err
  976. }
  977. formattedAggregators := []string{}
  978. for _, agg := range aggregators {
  979. aggregator_column_name := "resource_tags_user_" + agg
  980. aggregator_column_name = ConvertToGlueColumnFormat(aggregator_column_name)
  981. formattedAggregators = append(formattedAggregators, aggregator_column_name)
  982. }
  983. aggregatorNames := strings.Join(formattedAggregators, ",")
  984. filter_column_name := "resource_tags_user_" + filterType
  985. filter_column_name = ConvertToGlueColumnFormat(filter_column_name)
  986. var query string
  987. var lastIdx int
  988. if filterType != "kubernetes_" { // This gets appended upstream and is equivalent to no filter.
  989. lastIdx = len(formattedAggregators) + 3
  990. groupby := generateAWSGroupBy(lastIdx)
  991. query = fmt.Sprintf(`SELECT
  992. CAST(line_item_usage_start_date AS DATE) as start_date,
  993. %s,
  994. line_item_product_code,
  995. %s,
  996. SUM(line_item_blended_cost) as blended_cost
  997. FROM %s as cost_data
  998. WHERE (%s='%s') AND line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  999. GROUP BY %s`, aggregatorNames, filter_column_name, customPricing.AthenaTable, filter_column_name, filterValue, start, end, groupby)
  1000. } else {
  1001. lastIdx = len(formattedAggregators) + 2
  1002. groupby := generateAWSGroupBy(lastIdx)
  1003. query = fmt.Sprintf(`SELECT
  1004. CAST(line_item_usage_start_date AS DATE) as start_date,
  1005. %s,
  1006. line_item_product_code,
  1007. SUM(line_item_blended_cost) as blended_cost
  1008. FROM %s as cost_data
  1009. WHERE line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1010. GROUP BY %s`, aggregatorNames, customPricing.AthenaTable, start, end, groupby)
  1011. }
  1012. klog.V(3).Infof("Running Query: %s", query)
  1013. if customPricing.ServiceKeyName != "" {
  1014. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1015. if err != nil {
  1016. return nil, err
  1017. }
  1018. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1019. if err != nil {
  1020. return nil, err
  1021. }
  1022. }
  1023. region := aws.String(customPricing.AthenaRegion)
  1024. resultsBucket := customPricing.AthenaBucketName
  1025. database := customPricing.AthenaDatabase
  1026. c := &aws.Config{
  1027. Region: region,
  1028. }
  1029. s := session.Must(session.NewSession(c))
  1030. svc := athena.New(s)
  1031. var e athena.StartQueryExecutionInput
  1032. var r athena.ResultConfiguration
  1033. r.SetOutputLocation(resultsBucket)
  1034. e.SetResultConfiguration(&r)
  1035. e.SetQueryString(query)
  1036. var q athena.QueryExecutionContext
  1037. q.SetDatabase(database)
  1038. e.SetQueryExecutionContext(&q)
  1039. res, err := svc.StartQueryExecution(&e)
  1040. if err != nil {
  1041. return nil, err
  1042. }
  1043. klog.V(2).Infof("StartQueryExecution result:")
  1044. klog.V(2).Infof(res.GoString())
  1045. var qri athena.GetQueryExecutionInput
  1046. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1047. var qrop *athena.GetQueryExecutionOutput
  1048. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1049. for {
  1050. qrop, err = svc.GetQueryExecution(&qri)
  1051. if err != nil {
  1052. return nil, err
  1053. }
  1054. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1055. break
  1056. }
  1057. time.Sleep(duration)
  1058. }
  1059. var oocAllocs []*OutOfClusterAllocation
  1060. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1061. var ip athena.GetQueryResultsInput
  1062. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1063. op, err := svc.GetQueryResults(&ip)
  1064. if err != nil {
  1065. return nil, err
  1066. }
  1067. if len(op.ResultSet.Rows) > 1 {
  1068. for _, r := range op.ResultSet.Rows[1:(len(op.ResultSet.Rows) - 1)] {
  1069. cost, err := strconv.ParseFloat(*r.Data[lastIdx].VarCharValue, 64)
  1070. if err != nil {
  1071. return nil, err
  1072. }
  1073. environment := ""
  1074. for _, d := range r.Data[1 : len(formattedAggregators)+1] {
  1075. if *d.VarCharValue != "" {
  1076. environment = *d.VarCharValue // just set to the first nonempty match
  1077. }
  1078. break
  1079. }
  1080. ooc := &OutOfClusterAllocation{
  1081. Aggregator: strings.Join(aggregators, ","),
  1082. Environment: environment,
  1083. Service: *r.Data[len(formattedAggregators)+1].VarCharValue,
  1084. Cost: cost,
  1085. }
  1086. oocAllocs = append(oocAllocs, ooc)
  1087. }
  1088. } else {
  1089. klog.V(1).Infof("No results available for %s at database %s between %s and %s", strings.Join(formattedAggregators, ","), customPricing.AthenaTable, start, end)
  1090. }
  1091. }
  1092. if customPricing.BillingDataDataset != "" { // There is GCP data, meaning someone has tried to configure a GCP out-of-cluster allocation.
  1093. gcp, err := NewCrossClusterProvider("gcp", "gcp.json", a.Clientset)
  1094. if err != nil {
  1095. klog.Infof("Could not instantiate cross-cluster provider %s", err.Error())
  1096. }
  1097. gcpOOC, err := gcp.ExternalAllocations(start, end, aggregators, filterType, filterValue)
  1098. if err != nil {
  1099. klog.Infof("Could not fetch cross-cluster costs %s", err.Error())
  1100. }
  1101. oocAllocs = append(oocAllocs, gcpOOC...)
  1102. }
  1103. return oocAllocs, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  1104. }
  1105. // QuerySQL can query a properly configured Athena database.
  1106. // Used to fetch billing data.
  1107. // Requires a json config in /var/configs with key region, output, and database.
  1108. func (a *AWS) QuerySQL(query string) ([]byte, error) {
  1109. customPricing, err := a.GetConfig()
  1110. if err != nil {
  1111. return nil, err
  1112. }
  1113. if customPricing.ServiceKeyName != "" {
  1114. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1115. if err != nil {
  1116. return nil, err
  1117. }
  1118. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1119. if err != nil {
  1120. return nil, err
  1121. }
  1122. }
  1123. athenaConfigs, err := os.Open("/var/configs/athena.json")
  1124. if err != nil {
  1125. return nil, err
  1126. }
  1127. defer athenaConfigs.Close()
  1128. b, err := ioutil.ReadAll(athenaConfigs)
  1129. if err != nil {
  1130. return nil, err
  1131. }
  1132. var athenaConf map[string]string
  1133. json.Unmarshal([]byte(b), &athenaConf)
  1134. region := aws.String(customPricing.AthenaRegion)
  1135. resultsBucket := customPricing.AthenaBucketName
  1136. database := customPricing.AthenaDatabase
  1137. c := &aws.Config{
  1138. Region: region,
  1139. }
  1140. s := session.Must(session.NewSession(c))
  1141. svc := athena.New(s)
  1142. var e athena.StartQueryExecutionInput
  1143. var r athena.ResultConfiguration
  1144. r.SetOutputLocation(resultsBucket)
  1145. e.SetResultConfiguration(&r)
  1146. e.SetQueryString(query)
  1147. var q athena.QueryExecutionContext
  1148. q.SetDatabase(database)
  1149. e.SetQueryExecutionContext(&q)
  1150. res, err := svc.StartQueryExecution(&e)
  1151. if err != nil {
  1152. return nil, err
  1153. }
  1154. klog.V(2).Infof("StartQueryExecution result:")
  1155. klog.V(2).Infof(res.GoString())
  1156. var qri athena.GetQueryExecutionInput
  1157. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1158. var qrop *athena.GetQueryExecutionOutput
  1159. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1160. for {
  1161. qrop, err = svc.GetQueryExecution(&qri)
  1162. if err != nil {
  1163. return nil, err
  1164. }
  1165. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1166. break
  1167. }
  1168. time.Sleep(duration)
  1169. }
  1170. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1171. var ip athena.GetQueryResultsInput
  1172. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1173. op, err := svc.GetQueryResults(&ip)
  1174. if err != nil {
  1175. return nil, err
  1176. }
  1177. b, err := json.Marshal(op.ResultSet)
  1178. if err != nil {
  1179. return nil, err
  1180. }
  1181. return b, nil
  1182. }
  1183. return nil, fmt.Errorf("Error getting query results : %s", *qrop.QueryExecution.Status.State)
  1184. }
  1185. type spotInfo struct {
  1186. Timestamp string `csv:"Timestamp"`
  1187. UsageType string `csv:"UsageType"`
  1188. Operation string `csv:"Operation"`
  1189. InstanceID string `csv:"InstanceID"`
  1190. MyBidID string `csv:"MyBidID"`
  1191. MyMaxPrice string `csv:"MyMaxPrice"`
  1192. MarketPrice string `csv:"MarketPrice"`
  1193. Charge string `csv:"Charge"`
  1194. Version string `csv:"Version"`
  1195. }
  1196. type fnames []*string
  1197. func (f fnames) Len() int {
  1198. return len(f)
  1199. }
  1200. func (f fnames) Swap(i, j int) {
  1201. f[i], f[j] = f[j], f[i]
  1202. }
  1203. func (f fnames) Less(i, j int) bool {
  1204. key1 := strings.Split(*f[i], ".")
  1205. key2 := strings.Split(*f[j], ".")
  1206. t1, err := time.Parse("2006-01-02-15", key1[1])
  1207. if err != nil {
  1208. klog.V(1).Info("Unable to parse timestamp" + key1[1])
  1209. return false
  1210. }
  1211. t2, err := time.Parse("2006-01-02-15", key2[1])
  1212. if err != nil {
  1213. klog.V(1).Info("Unable to parse timestamp" + key2[1])
  1214. return false
  1215. }
  1216. return t1.Before(t2)
  1217. }
  1218. func parseSpotData(bucket string, prefix string, projectID string, region string, accessKeyID string, accessKeySecret string) (map[string]*spotInfo, error) {
  1219. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  1220. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  1221. if err != nil {
  1222. return nil, err
  1223. }
  1224. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  1225. if err != nil {
  1226. return nil, err
  1227. }
  1228. }
  1229. s3Prefix := projectID
  1230. if len(prefix) != 0 {
  1231. s3Prefix = prefix + "/" + s3Prefix
  1232. }
  1233. c := aws.NewConfig().WithRegion(region)
  1234. s := session.Must(session.NewSession(c))
  1235. s3Svc := s3.New(s)
  1236. downloader := s3manager.NewDownloaderWithClient(s3Svc)
  1237. tNow := time.Now()
  1238. tOneDayAgo := tNow.Add(time.Duration(-24) * time.Hour) // Also get files from one day ago to avoid boundary conditions
  1239. ls := &s3.ListObjectsInput{
  1240. Bucket: aws.String(bucket),
  1241. Prefix: aws.String(s3Prefix + "." + tOneDayAgo.Format("2006-01-02")),
  1242. }
  1243. ls2 := &s3.ListObjectsInput{
  1244. Bucket: aws.String(bucket),
  1245. Prefix: aws.String(s3Prefix + "." + tNow.Format("2006-01-02")),
  1246. }
  1247. lso, err := s3Svc.ListObjects(ls)
  1248. if err != nil {
  1249. return nil, err
  1250. }
  1251. lsoLen := len(lso.Contents)
  1252. klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
  1253. if lsoLen == 0 {
  1254. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
  1255. }
  1256. lso2, err := s3Svc.ListObjects(ls2)
  1257. if err != nil {
  1258. return nil, err
  1259. }
  1260. lso2Len := len(lso2.Contents)
  1261. klog.V(2).Infof("Found %d spot data files from today", lso2Len)
  1262. if lso2Len == 0 {
  1263. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
  1264. }
  1265. var keys []*string
  1266. for _, obj := range lso.Contents {
  1267. keys = append(keys, obj.Key)
  1268. }
  1269. for _, obj := range lso2.Contents {
  1270. keys = append(keys, obj.Key)
  1271. }
  1272. versionRx := regexp.MustCompile("^#Version: (\\d+)\\.\\d+$")
  1273. header, err := csvutil.Header(spotInfo{}, "csv")
  1274. if err != nil {
  1275. return nil, err
  1276. }
  1277. fieldsPerRecord := len(header)
  1278. spots := make(map[string]*spotInfo)
  1279. for _, key := range keys {
  1280. getObj := &s3.GetObjectInput{
  1281. Bucket: aws.String(bucket),
  1282. Key: key,
  1283. }
  1284. buf := aws.NewWriteAtBuffer([]byte{})
  1285. _, err := downloader.Download(buf, getObj)
  1286. if err != nil {
  1287. return nil, err
  1288. }
  1289. r := bytes.NewReader(buf.Bytes())
  1290. gr, err := gzip.NewReader(r)
  1291. if err != nil {
  1292. return nil, err
  1293. }
  1294. csvReader := csv.NewReader(gr)
  1295. csvReader.Comma = '\t'
  1296. csvReader.FieldsPerRecord = fieldsPerRecord
  1297. dec, err := csvutil.NewDecoder(csvReader, header...)
  1298. if err != nil {
  1299. return nil, err
  1300. }
  1301. var foundVersion string
  1302. for {
  1303. spot := spotInfo{}
  1304. err := dec.Decode(&spot)
  1305. csvParseErr, isCsvParseErr := err.(*csv.ParseError)
  1306. if err == io.EOF {
  1307. break
  1308. } else if err == csvutil.ErrFieldCount || (isCsvParseErr && csvParseErr.Err == csv.ErrFieldCount) {
  1309. rec := dec.Record()
  1310. // the first two "Record()" will be the comment lines
  1311. // and they show up as len() == 1
  1312. // the first of which is "#Version"
  1313. // the second of which is "#Fields: "
  1314. if len(rec) != 1 {
  1315. klog.V(2).Infof("Expected %d spot info fields but received %d: %s", fieldsPerRecord, len(rec), rec)
  1316. continue
  1317. }
  1318. if len(foundVersion) == 0 {
  1319. spotFeedVersion := rec[0]
  1320. klog.V(4).Infof("Spot feed version is \"%s\"", spotFeedVersion)
  1321. matches := versionRx.FindStringSubmatch(spotFeedVersion)
  1322. if matches != nil {
  1323. foundVersion = matches[1]
  1324. if foundVersion != supportedSpotFeedVersion {
  1325. klog.V(2).Infof("Unsupported spot info feed version: wanted \"%s\" got \"%s\"", supportedSpotFeedVersion, foundVersion)
  1326. break
  1327. }
  1328. }
  1329. continue
  1330. } else if strings.Index(rec[0], "#") == 0 {
  1331. continue
  1332. } else {
  1333. klog.V(3).Infof("skipping non-TSV line: %s", rec)
  1334. continue
  1335. }
  1336. } else if err != nil {
  1337. klog.V(2).Infof("Error during spot info decode: %+v", err)
  1338. continue
  1339. }
  1340. klog.V(4).Infof("Found spot info %+v", spot)
  1341. spots[spot.InstanceID] = &spot
  1342. }
  1343. gr.Close()
  1344. }
  1345. return spots, nil
  1346. }
  1347. func (a *AWS) ApplyReservedInstancePricing(nodes map[string]*Node) {
  1348. numReserved := len(a.ReservedInstances)
  1349. // Early return if no reserved instance data loaded
  1350. if numReserved == 0 {
  1351. klog.V(4).Infof("[Reserved] No Reserved Instances")
  1352. return
  1353. }
  1354. cfg, err := a.GetConfig()
  1355. defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
  1356. if err != nil {
  1357. klog.V(3).Infof("Could not parse default cpu price")
  1358. defaultCPU = 0.031611
  1359. }
  1360. defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
  1361. if err != nil {
  1362. klog.V(3).Infof("Could not parse default ram price")
  1363. defaultRAM = 0.004237
  1364. }
  1365. cpuToRAMRatio := defaultCPU / defaultRAM
  1366. now := time.Now()
  1367. instances := make(map[string][]*AWSReservedInstance)
  1368. for _, r := range a.ReservedInstances {
  1369. if now.Before(r.StartDate) || now.After(r.EndDate) {
  1370. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  1371. continue
  1372. }
  1373. _, ok := instances[r.Region]
  1374. if !ok {
  1375. instances[r.Region] = []*AWSReservedInstance{r}
  1376. } else {
  1377. instances[r.Region] = append(instances[r.Region], r)
  1378. }
  1379. }
  1380. awsNodes := make(map[string]*v1.Node)
  1381. currentNodes := a.Clientset.GetAllNodes()
  1382. // Create a node name -> node map
  1383. for _, awsNode := range currentNodes {
  1384. awsNodes[awsNode.GetName()] = awsNode
  1385. }
  1386. // go through all provider nodes using k8s nodes for region
  1387. for nodeName, node := range nodes {
  1388. // Reset reserved allocation to prevent double allocation
  1389. node.Reserved = nil
  1390. kNode, ok := awsNodes[nodeName]
  1391. if !ok {
  1392. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  1393. continue
  1394. }
  1395. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  1396. if !ok {
  1397. klog.V(1).Infof("[Reserved] Could not find node region")
  1398. continue
  1399. }
  1400. reservedInstances, ok := instances[nodeRegion]
  1401. if !ok {
  1402. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  1403. continue
  1404. }
  1405. // Determine the InstanceType of the node
  1406. instanceType, ok := kNode.Labels["beta.kubernetes.io/instance-type"]
  1407. if !ok {
  1408. continue
  1409. }
  1410. ramBytes, err := strconv.ParseFloat(node.RAMBytes, 64)
  1411. if err != nil {
  1412. continue
  1413. }
  1414. ramGB := ramBytes / 1024 / 1024 / 1024
  1415. cpu, err := strconv.ParseFloat(node.VCPU, 64)
  1416. if err != nil {
  1417. continue
  1418. }
  1419. ramMultiple := cpu*cpuToRAMRatio + ramGB
  1420. node.Reserved = &ReservedInstanceData{
  1421. ReservedCPU: 0,
  1422. ReservedRAM: 0,
  1423. }
  1424. for i, reservedInstance := range reservedInstances {
  1425. if reservedInstance.InstanceType == instanceType {
  1426. // Use < 0 to mark as ALL
  1427. node.Reserved.ReservedCPU = -1
  1428. node.Reserved.ReservedRAM = -1
  1429. // Set Costs based on CPU/RAM ratios
  1430. ramPrice := reservedInstance.PricePerHour / ramMultiple
  1431. node.Reserved.CPUCost = ramPrice * cpuToRAMRatio
  1432. node.Reserved.RAMCost = ramPrice
  1433. // Remove the reserve from the temporary slice to prevent
  1434. // being reallocated
  1435. instances[nodeRegion] = append(reservedInstances[:i], reservedInstances[i+1:]...)
  1436. break
  1437. }
  1438. }
  1439. }
  1440. }
  1441. type AWSReservedInstance struct {
  1442. Zone string
  1443. Region string
  1444. InstanceType string
  1445. InstanceCount int64
  1446. InstanceTenacy string
  1447. StartDate time.Time
  1448. EndDate time.Time
  1449. PricePerHour float64
  1450. }
  1451. func (ari *AWSReservedInstance) String() string {
  1452. return fmt.Sprintf("[Zone: %s, Region: %s, Type: %s, Count: %d, Tenacy: %s, Start: %+v, End: %+v, Price: %f]", ari.Zone, ari.Region, ari.InstanceType, ari.InstanceCount, ari.InstanceTenacy, ari.StartDate, ari.EndDate, ari.PricePerHour)
  1453. }
  1454. func isReservedInstanceHourlyPrice(rc *ec2.RecurringCharge) bool {
  1455. return rc != nil && rc.Frequency != nil && *rc.Frequency == "Hourly"
  1456. }
  1457. func getReservedInstancePrice(ri *ec2.ReservedInstances) (float64, error) {
  1458. var pricePerHour float64
  1459. if len(ri.RecurringCharges) > 0 {
  1460. for _, rc := range ri.RecurringCharges {
  1461. if isReservedInstanceHourlyPrice(rc) {
  1462. pricePerHour = *rc.Amount
  1463. break
  1464. }
  1465. }
  1466. }
  1467. // If we're still unable to resolve hourly price, try fixed -> hourly
  1468. if pricePerHour == 0 {
  1469. if ri.Duration != nil && ri.FixedPrice != nil {
  1470. var durHours float64
  1471. durSeconds := float64(*ri.Duration)
  1472. fixedPrice := float64(*ri.FixedPrice)
  1473. if durSeconds != 0 && fixedPrice != 0 {
  1474. durHours = durSeconds / 60 / 60
  1475. pricePerHour = fixedPrice / durHours
  1476. }
  1477. }
  1478. }
  1479. if pricePerHour == 0 {
  1480. return 0, fmt.Errorf("Failed to resolve an hourly price from FixedPrice or Recurring Costs")
  1481. }
  1482. return pricePerHour, nil
  1483. }
  1484. func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
  1485. c := &aws.Config{
  1486. Region: aws.String(region),
  1487. }
  1488. s := session.Must(session.NewSession(c))
  1489. svc := ec2.New(s)
  1490. response, err := svc.DescribeReservedInstances(&ec2.DescribeReservedInstancesInput{})
  1491. if err != nil {
  1492. return nil, err
  1493. }
  1494. var reservedInstances []*AWSReservedInstance
  1495. for _, ri := range response.ReservedInstances {
  1496. var zone string
  1497. if ri.AvailabilityZone != nil {
  1498. zone = *ri.AvailabilityZone
  1499. }
  1500. pricePerHour, err := getReservedInstancePrice(ri)
  1501. if err != nil {
  1502. klog.V(1).Infof("Error Resolving Price: %s", err.Error())
  1503. continue
  1504. }
  1505. reservedInstances = append(reservedInstances, &AWSReservedInstance{
  1506. Zone: zone,
  1507. Region: region,
  1508. InstanceType: *ri.InstanceType,
  1509. InstanceCount: *ri.InstanceCount,
  1510. InstanceTenacy: *ri.InstanceTenancy,
  1511. StartDate: *ri.Start,
  1512. EndDate: *ri.End,
  1513. PricePerHour: pricePerHour,
  1514. })
  1515. }
  1516. return reservedInstances, nil
  1517. }
  1518. func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
  1519. err := configureAWSAuth("/var/configs/aws.json")
  1520. if err != nil {
  1521. return nil, err
  1522. }
  1523. var reservedInstances []*AWSReservedInstance
  1524. nodes := a.Clientset.GetAllNodes()
  1525. regionsSeen := make(map[string]bool)
  1526. for _, node := range nodes {
  1527. region, ok := node.Labels[v1.LabelZoneRegion]
  1528. if !ok {
  1529. continue
  1530. }
  1531. if regionsSeen[region] {
  1532. continue
  1533. }
  1534. ris, err := getRegionReservedInstances(region)
  1535. if err != nil {
  1536. klog.V(3).Infof("Error getting reserved instances: %s", err.Error())
  1537. continue
  1538. }
  1539. regionsSeen[region] = true
  1540. reservedInstances = append(reservedInstances, ris...)
  1541. }
  1542. return reservedInstances, nil
  1543. }