awsprovider.go 64 KB

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