awsprovider.go 66 KB

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