awsprovider.go 62 KB

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