awsprovider.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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/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.ProjectID = 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. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  963. // "start" and "end" are dates of the format YYYY-MM-DD
  964. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  965. func (a *AWS) ExternalAllocations(start string, end string, aggregator string, filterType string, filterValue string) ([]*OutOfClusterAllocation, error) {
  966. customPricing, err := a.GetConfig()
  967. if err != nil {
  968. return nil, err
  969. }
  970. aggregator_column_name := "resource_tags_user_" + aggregator
  971. aggregator_column_name = ConvertToGlueColumnFormat(aggregator_column_name)
  972. filter_column_name := "resource_tags_user_" + filterType
  973. filter_column_name = ConvertToGlueColumnFormat(filter_column_name)
  974. var query string
  975. var lastIdx int
  976. if filterType != "kubernetes_" { // This gets appended upstream and is equivalent to no filter.
  977. query = fmt.Sprintf(`SELECT
  978. CAST(line_item_usage_start_date AS DATE) as start_date,
  979. %s,
  980. line_item_product_code,
  981. %s,
  982. SUM(line_item_blended_cost) as blended_cost
  983. FROM %s as cost_data
  984. WHERE (%s='%s' OR %s='') AND line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  985. GROUP BY 1,2,3,4`, aggregator_column_name, filter_column_name, customPricing.AthenaTable, filter_column_name, filterValue, filter_column_name, start, end)
  986. lastIdx = 4
  987. } else {
  988. lastIdx = 3
  989. query = fmt.Sprintf(`SELECT
  990. CAST(line_item_usage_start_date AS DATE) as start_date,
  991. %s,
  992. line_item_product_code,
  993. SUM(line_item_blended_cost) as blended_cost
  994. FROM %s as cost_data
  995. WHERE line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  996. GROUP BY 1,2,3`, aggregator_column_name, customPricing.AthenaTable, start, end)
  997. }
  998. klog.V(3).Infof("Running Query: %s", query)
  999. if customPricing.ServiceKeyName != "" {
  1000. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1001. if err != nil {
  1002. return nil, err
  1003. }
  1004. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1005. if err != nil {
  1006. return nil, err
  1007. }
  1008. }
  1009. region := aws.String(customPricing.AthenaRegion)
  1010. resultsBucket := customPricing.AthenaBucketName
  1011. database := customPricing.AthenaDatabase
  1012. c := &aws.Config{
  1013. Region: region,
  1014. }
  1015. s := session.Must(session.NewSession(c))
  1016. svc := athena.New(s)
  1017. var e athena.StartQueryExecutionInput
  1018. var r athena.ResultConfiguration
  1019. r.SetOutputLocation(resultsBucket)
  1020. e.SetResultConfiguration(&r)
  1021. e.SetQueryString(query)
  1022. var q athena.QueryExecutionContext
  1023. q.SetDatabase(database)
  1024. e.SetQueryExecutionContext(&q)
  1025. res, err := svc.StartQueryExecution(&e)
  1026. if err != nil {
  1027. return nil, err
  1028. }
  1029. klog.V(2).Infof("StartQueryExecution result:")
  1030. klog.V(2).Infof(res.GoString())
  1031. var qri athena.GetQueryExecutionInput
  1032. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1033. var qrop *athena.GetQueryExecutionOutput
  1034. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1035. for {
  1036. qrop, err = svc.GetQueryExecution(&qri)
  1037. if err != nil {
  1038. return nil, err
  1039. }
  1040. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1041. break
  1042. }
  1043. time.Sleep(duration)
  1044. }
  1045. var oocAllocs []*OutOfClusterAllocation
  1046. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1047. var ip athena.GetQueryResultsInput
  1048. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1049. op, err := svc.GetQueryResults(&ip)
  1050. if err != nil {
  1051. return nil, err
  1052. }
  1053. if len(op.ResultSet.Rows) > 1 {
  1054. for _, r := range op.ResultSet.Rows[1:(len(op.ResultSet.Rows) - 1)] {
  1055. cost, err := strconv.ParseFloat(*r.Data[lastIdx].VarCharValue, 64)
  1056. if err != nil {
  1057. return nil, err
  1058. }
  1059. ooc := &OutOfClusterAllocation{
  1060. Aggregator: aggregator,
  1061. Environment: *r.Data[1].VarCharValue,
  1062. Service: *r.Data[2].VarCharValue,
  1063. Cost: cost,
  1064. }
  1065. oocAllocs = append(oocAllocs, ooc)
  1066. }
  1067. } else {
  1068. klog.V(1).Infof("No results available for %s at database %s between %s and %s", aggregator_column_name, customPricing.AthenaTable, start, end)
  1069. }
  1070. }
  1071. return oocAllocs, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  1072. }
  1073. // QuerySQL can query a properly configured Athena database.
  1074. // Used to fetch billing data.
  1075. // Requires a json config in /var/configs with key region, output, and database.
  1076. func (a *AWS) QuerySQL(query string) ([]byte, error) {
  1077. customPricing, err := a.GetConfig()
  1078. if err != nil {
  1079. return nil, err
  1080. }
  1081. if customPricing.ServiceKeyName != "" {
  1082. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1083. if err != nil {
  1084. return nil, err
  1085. }
  1086. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1087. if err != nil {
  1088. return nil, err
  1089. }
  1090. }
  1091. athenaConfigs, err := os.Open("/var/configs/athena.json")
  1092. if err != nil {
  1093. return nil, err
  1094. }
  1095. defer athenaConfigs.Close()
  1096. b, err := ioutil.ReadAll(athenaConfigs)
  1097. if err != nil {
  1098. return nil, err
  1099. }
  1100. var athenaConf map[string]string
  1101. json.Unmarshal([]byte(b), &athenaConf)
  1102. region := aws.String(customPricing.AthenaRegion)
  1103. resultsBucket := customPricing.AthenaBucketName
  1104. database := customPricing.AthenaDatabase
  1105. c := &aws.Config{
  1106. Region: region,
  1107. }
  1108. s := session.Must(session.NewSession(c))
  1109. svc := athena.New(s)
  1110. var e athena.StartQueryExecutionInput
  1111. var r athena.ResultConfiguration
  1112. r.SetOutputLocation(resultsBucket)
  1113. e.SetResultConfiguration(&r)
  1114. e.SetQueryString(query)
  1115. var q athena.QueryExecutionContext
  1116. q.SetDatabase(database)
  1117. e.SetQueryExecutionContext(&q)
  1118. res, err := svc.StartQueryExecution(&e)
  1119. if err != nil {
  1120. return nil, err
  1121. }
  1122. klog.V(2).Infof("StartQueryExecution result:")
  1123. klog.V(2).Infof(res.GoString())
  1124. var qri athena.GetQueryExecutionInput
  1125. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1126. var qrop *athena.GetQueryExecutionOutput
  1127. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1128. for {
  1129. qrop, err = svc.GetQueryExecution(&qri)
  1130. if err != nil {
  1131. return nil, err
  1132. }
  1133. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1134. break
  1135. }
  1136. time.Sleep(duration)
  1137. }
  1138. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1139. var ip athena.GetQueryResultsInput
  1140. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1141. op, err := svc.GetQueryResults(&ip)
  1142. if err != nil {
  1143. return nil, err
  1144. }
  1145. b, err := json.Marshal(op.ResultSet)
  1146. if err != nil {
  1147. return nil, err
  1148. }
  1149. return b, nil
  1150. }
  1151. return nil, fmt.Errorf("Error getting query results : %s", *qrop.QueryExecution.Status.State)
  1152. }
  1153. type spotInfo struct {
  1154. Timestamp string `csv:"Timestamp"`
  1155. UsageType string `csv:"UsageType"`
  1156. Operation string `csv:"Operation"`
  1157. InstanceID string `csv:"InstanceID"`
  1158. MyBidID string `csv:"MyBidID"`
  1159. MyMaxPrice string `csv:"MyMaxPrice"`
  1160. MarketPrice string `csv:"MarketPrice"`
  1161. Charge string `csv:"Charge"`
  1162. Version string `csv:"Version"`
  1163. }
  1164. type fnames []*string
  1165. func (f fnames) Len() int {
  1166. return len(f)
  1167. }
  1168. func (f fnames) Swap(i, j int) {
  1169. f[i], f[j] = f[j], f[i]
  1170. }
  1171. func (f fnames) Less(i, j int) bool {
  1172. key1 := strings.Split(*f[i], ".")
  1173. key2 := strings.Split(*f[j], ".")
  1174. t1, err := time.Parse("2006-01-02-15", key1[1])
  1175. if err != nil {
  1176. klog.V(1).Info("Unable to parse timestamp" + key1[1])
  1177. return false
  1178. }
  1179. t2, err := time.Parse("2006-01-02-15", key2[1])
  1180. if err != nil {
  1181. klog.V(1).Info("Unable to parse timestamp" + key2[1])
  1182. return false
  1183. }
  1184. return t1.Before(t2)
  1185. }
  1186. func parseSpotData(bucket string, prefix string, projectID string, region string, accessKeyID string, accessKeySecret string) (map[string]*spotInfo, error) {
  1187. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  1188. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  1189. if err != nil {
  1190. return nil, err
  1191. }
  1192. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  1193. if err != nil {
  1194. return nil, err
  1195. }
  1196. }
  1197. s3Prefix := projectID
  1198. if len(prefix) != 0 {
  1199. s3Prefix = prefix + "/" + s3Prefix
  1200. }
  1201. c := aws.NewConfig().WithRegion(region)
  1202. s := session.Must(session.NewSession(c))
  1203. s3Svc := s3.New(s)
  1204. downloader := s3manager.NewDownloaderWithClient(s3Svc)
  1205. tNow := time.Now()
  1206. tOneDayAgo := tNow.Add(time.Duration(-24) * time.Hour) // Also get files from one day ago to avoid boundary conditions
  1207. ls := &s3.ListObjectsInput{
  1208. Bucket: aws.String(bucket),
  1209. Prefix: aws.String(s3Prefix + "." + tOneDayAgo.Format("2006-01-02")),
  1210. }
  1211. ls2 := &s3.ListObjectsInput{
  1212. Bucket: aws.String(bucket),
  1213. Prefix: aws.String(s3Prefix + "." + tNow.Format("2006-01-02")),
  1214. }
  1215. lso, err := s3Svc.ListObjects(ls)
  1216. if err != nil {
  1217. return nil, err
  1218. }
  1219. lsoLen := len(lso.Contents)
  1220. klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
  1221. if lsoLen == 0 {
  1222. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
  1223. }
  1224. lso2, err := s3Svc.ListObjects(ls2)
  1225. if err != nil {
  1226. return nil, err
  1227. }
  1228. lso2Len := len(lso2.Contents)
  1229. klog.V(2).Infof("Found %d spot data files from today", lso2Len)
  1230. if lso2Len == 0 {
  1231. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
  1232. }
  1233. var keys []*string
  1234. for _, obj := range lso.Contents {
  1235. keys = append(keys, obj.Key)
  1236. }
  1237. for _, obj := range lso2.Contents {
  1238. keys = append(keys, obj.Key)
  1239. }
  1240. versionRx := regexp.MustCompile("^#Version: (\\d+)\\.\\d+$")
  1241. header, err := csvutil.Header(spotInfo{}, "csv")
  1242. if err != nil {
  1243. return nil, err
  1244. }
  1245. fieldsPerRecord := len(header)
  1246. spots := make(map[string]*spotInfo)
  1247. for _, key := range keys {
  1248. getObj := &s3.GetObjectInput{
  1249. Bucket: aws.String(bucket),
  1250. Key: key,
  1251. }
  1252. buf := aws.NewWriteAtBuffer([]byte{})
  1253. _, err := downloader.Download(buf, getObj)
  1254. if err != nil {
  1255. return nil, err
  1256. }
  1257. r := bytes.NewReader(buf.Bytes())
  1258. gr, err := gzip.NewReader(r)
  1259. if err != nil {
  1260. return nil, err
  1261. }
  1262. csvReader := csv.NewReader(gr)
  1263. csvReader.Comma = '\t'
  1264. csvReader.FieldsPerRecord = fieldsPerRecord
  1265. dec, err := csvutil.NewDecoder(csvReader, header...)
  1266. if err != nil {
  1267. return nil, err
  1268. }
  1269. var foundVersion string
  1270. for {
  1271. spot := spotInfo{}
  1272. err := dec.Decode(&spot)
  1273. csvParseErr, isCsvParseErr := err.(*csv.ParseError)
  1274. if err == io.EOF {
  1275. break
  1276. } else if err == csvutil.ErrFieldCount || (isCsvParseErr && csvParseErr.Err == csv.ErrFieldCount) {
  1277. rec := dec.Record()
  1278. // the first two "Record()" will be the comment lines
  1279. // and they show up as len() == 1
  1280. // the first of which is "#Version"
  1281. // the second of which is "#Fields: "
  1282. if len(rec) != 1 {
  1283. klog.V(2).Infof("Expected %d spot info fields but received %d: %s", fieldsPerRecord, len(rec), rec)
  1284. continue
  1285. }
  1286. if len(foundVersion) == 0 {
  1287. spotFeedVersion := rec[0]
  1288. klog.V(4).Infof("Spot feed version is \"%s\"", spotFeedVersion)
  1289. matches := versionRx.FindStringSubmatch(spotFeedVersion)
  1290. if matches != nil {
  1291. foundVersion = matches[1]
  1292. if foundVersion != supportedSpotFeedVersion {
  1293. klog.V(2).Infof("Unsupported spot info feed version: wanted \"%s\" got \"%s\"", supportedSpotFeedVersion, foundVersion)
  1294. break
  1295. }
  1296. }
  1297. continue
  1298. } else if strings.Index(rec[0], "#") == 0 {
  1299. continue
  1300. } else {
  1301. klog.V(3).Infof("skipping non-TSV line: %s", rec)
  1302. continue
  1303. }
  1304. } else if err != nil {
  1305. klog.V(2).Infof("Error during spot info decode: %+v", err)
  1306. continue
  1307. }
  1308. klog.V(4).Infof("Found spot info %+v", spot)
  1309. spots[spot.InstanceID] = &spot
  1310. }
  1311. gr.Close()
  1312. }
  1313. return spots, nil
  1314. }
  1315. func (a *AWS) ApplyReservedInstancePricing(nodes map[string]*Node) {
  1316. numReserved := len(a.ReservedInstances)
  1317. // Early return if no reserved instance data loaded
  1318. if numReserved == 0 {
  1319. klog.V(4).Infof("[Reserved] No Reserved Instances")
  1320. return
  1321. }
  1322. cfg, err := a.GetConfig()
  1323. defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
  1324. if err != nil {
  1325. klog.V(3).Infof("Could not parse default cpu price")
  1326. defaultCPU = 0.031611
  1327. }
  1328. defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
  1329. if err != nil {
  1330. klog.V(3).Infof("Could not parse default ram price")
  1331. defaultRAM = 0.004237
  1332. }
  1333. cpuToRAMRatio := defaultCPU / defaultRAM
  1334. now := time.Now()
  1335. instances := make(map[string][]*AWSReservedInstance)
  1336. for _, r := range a.ReservedInstances {
  1337. if now.Before(r.StartDate) || now.After(r.EndDate) {
  1338. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  1339. continue
  1340. }
  1341. _, ok := instances[r.Region]
  1342. if !ok {
  1343. instances[r.Region] = []*AWSReservedInstance{r}
  1344. } else {
  1345. instances[r.Region] = append(instances[r.Region], r)
  1346. }
  1347. }
  1348. awsNodes := make(map[string]*v1.Node)
  1349. currentNodes := a.Clientset.GetAllNodes()
  1350. // Create a node name -> node map
  1351. for _, awsNode := range currentNodes {
  1352. awsNodes[awsNode.GetName()] = awsNode
  1353. }
  1354. // go through all provider nodes using k8s nodes for region
  1355. for nodeName, node := range nodes {
  1356. // Reset reserved allocation to prevent double allocation
  1357. node.Reserved = nil
  1358. kNode, ok := awsNodes[nodeName]
  1359. if !ok {
  1360. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  1361. continue
  1362. }
  1363. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  1364. if !ok {
  1365. klog.V(1).Infof("[Reserved] Could not find node region")
  1366. continue
  1367. }
  1368. reservedInstances, ok := instances[nodeRegion]
  1369. if !ok {
  1370. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  1371. continue
  1372. }
  1373. // Determine the InstanceType of the node
  1374. instanceType, ok := kNode.Labels["beta.kubernetes.io/instance-type"]
  1375. if !ok {
  1376. continue
  1377. }
  1378. ramBytes, err := strconv.ParseFloat(node.RAMBytes, 64)
  1379. if err != nil {
  1380. continue
  1381. }
  1382. ramGB := ramBytes / 1024 / 1024 / 1024
  1383. cpu, err := strconv.ParseFloat(node.VCPU, 64)
  1384. if err != nil {
  1385. continue
  1386. }
  1387. ramMultiple := cpu*cpuToRAMRatio + ramGB
  1388. node.Reserved = &ReservedInstanceData{
  1389. ReservedCPU: 0,
  1390. ReservedRAM: 0,
  1391. }
  1392. for i, reservedInstance := range reservedInstances {
  1393. if reservedInstance.InstanceType == instanceType {
  1394. // Use < 0 to mark as ALL
  1395. node.Reserved.ReservedCPU = -1
  1396. node.Reserved.ReservedRAM = -1
  1397. // Set Costs based on CPU/RAM ratios
  1398. ramPrice := reservedInstance.PricePerHour / ramMultiple
  1399. node.Reserved.CPUCost = ramPrice * cpuToRAMRatio
  1400. node.Reserved.RAMCost = ramPrice
  1401. // Remove the reserve from the temporary slice to prevent
  1402. // being reallocated
  1403. instances[nodeRegion] = append(reservedInstances[:i], reservedInstances[i+1:]...)
  1404. break
  1405. }
  1406. }
  1407. }
  1408. }
  1409. type AWSReservedInstance struct {
  1410. Zone string
  1411. Region string
  1412. InstanceType string
  1413. InstanceCount int64
  1414. InstanceTenacy string
  1415. StartDate time.Time
  1416. EndDate time.Time
  1417. PricePerHour float64
  1418. }
  1419. func (ari *AWSReservedInstance) String() string {
  1420. 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)
  1421. }
  1422. func isReservedInstanceHourlyPrice(rc *ec2.RecurringCharge) bool {
  1423. return rc != nil && rc.Frequency != nil && *rc.Frequency == "Hourly"
  1424. }
  1425. func getReservedInstancePrice(ri *ec2.ReservedInstances) (float64, error) {
  1426. var pricePerHour float64
  1427. if len(ri.RecurringCharges) > 0 {
  1428. for _, rc := range ri.RecurringCharges {
  1429. if isReservedInstanceHourlyPrice(rc) {
  1430. pricePerHour = *rc.Amount
  1431. break
  1432. }
  1433. }
  1434. }
  1435. // If we're still unable to resolve hourly price, try fixed -> hourly
  1436. if pricePerHour == 0 {
  1437. if ri.Duration != nil && ri.FixedPrice != nil {
  1438. var durHours float64
  1439. durSeconds := float64(*ri.Duration)
  1440. fixedPrice := float64(*ri.FixedPrice)
  1441. if durSeconds != 0 && fixedPrice != 0 {
  1442. durHours = durSeconds / 60 / 60
  1443. pricePerHour = fixedPrice / durHours
  1444. }
  1445. }
  1446. }
  1447. if pricePerHour == 0 {
  1448. return 0, fmt.Errorf("Failed to resolve an hourly price from FixedPrice or Recurring Costs")
  1449. }
  1450. return pricePerHour, nil
  1451. }
  1452. func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
  1453. c := &aws.Config{
  1454. Region: aws.String(region),
  1455. }
  1456. s := session.Must(session.NewSession(c))
  1457. svc := ec2.New(s)
  1458. response, err := svc.DescribeReservedInstances(&ec2.DescribeReservedInstancesInput{})
  1459. if err != nil {
  1460. return nil, err
  1461. }
  1462. var reservedInstances []*AWSReservedInstance
  1463. for _, ri := range response.ReservedInstances {
  1464. var zone string
  1465. if ri.AvailabilityZone != nil {
  1466. zone = *ri.AvailabilityZone
  1467. }
  1468. pricePerHour, err := getReservedInstancePrice(ri)
  1469. if err != nil {
  1470. klog.V(1).Infof("Error Resolving Price: %s", err.Error())
  1471. continue
  1472. }
  1473. reservedInstances = append(reservedInstances, &AWSReservedInstance{
  1474. Zone: zone,
  1475. Region: region,
  1476. InstanceType: *ri.InstanceType,
  1477. InstanceCount: *ri.InstanceCount,
  1478. InstanceTenacy: *ri.InstanceTenancy,
  1479. StartDate: *ri.Start,
  1480. EndDate: *ri.End,
  1481. PricePerHour: pricePerHour,
  1482. })
  1483. }
  1484. return reservedInstances, nil
  1485. }
  1486. func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
  1487. err := configureAWSAuth("/var/configs/aws.json")
  1488. if err != nil {
  1489. return nil, err
  1490. }
  1491. var reservedInstances []*AWSReservedInstance
  1492. nodes := a.Clientset.GetAllNodes()
  1493. regionsSeen := make(map[string]bool)
  1494. for _, node := range nodes {
  1495. region, ok := node.Labels[v1.LabelZoneRegion]
  1496. if !ok {
  1497. continue
  1498. }
  1499. if regionsSeen[region] {
  1500. continue
  1501. }
  1502. ris, err := getRegionReservedInstances(region)
  1503. if err != nil {
  1504. klog.V(3).Infof("Error getting reserved instances: %s", err.Error())
  1505. continue
  1506. }
  1507. regionsSeen[region] = true
  1508. reservedInstances = append(reservedInstances, ris...)
  1509. }
  1510. return reservedInstances, nil
  1511. }