awsprovider.go 49 KB

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