awsprovider.go 67 KB

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