awsprovider.go 61 KB

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