awsprovider.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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. provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)")
  802. clusterIdRx := regexp.MustCompile("^kubernetes\\.io/cluster/([^/]+)")
  803. nodeList := awsProvider.Clientset.GetAllNodes()
  804. for _, n := range nodeList {
  805. region := ""
  806. instanceId := ""
  807. providerId := n.Spec.ProviderID
  808. for matchNum, group := range provIdRx.FindStringSubmatch(providerId) {
  809. if matchNum == 1 {
  810. region = group
  811. } else if matchNum == 2 {
  812. instanceId = group
  813. }
  814. }
  815. if len(instanceId) == 0 {
  816. klog.V(2).Infof("Unable to decode Node.ProviderID \"%s\", skipping it", providerId)
  817. continue
  818. }
  819. c := &aws.Config{
  820. Region: aws.String(region),
  821. }
  822. s := session.Must(session.NewSession(c))
  823. ec2Svc := ec2.New(s)
  824. di, diErr := ec2Svc.DescribeInstances(&ec2.DescribeInstancesInput{
  825. InstanceIds: []*string{
  826. aws.String(instanceId),
  827. },
  828. })
  829. if diErr != nil {
  830. // maybe log this?
  831. continue
  832. }
  833. if len(di.Reservations) != 1 {
  834. klog.V(2).Infof("Expected 1 Reservation back from DescribeInstances(%s), received %d", instanceId, len(di.Reservations))
  835. continue
  836. }
  837. res := di.Reservations[0]
  838. if len(res.Instances) != 1 {
  839. klog.V(2).Infof("Expected 1 Instance back from DescribeInstances(%s), received %d", instanceId, len(res.Instances))
  840. continue
  841. }
  842. inst := res.Instances[0]
  843. for _, tag := range inst.Tags {
  844. tagKey := *tag.Key
  845. for matchNum, group := range clusterIdRx.FindStringSubmatch(tagKey) {
  846. if matchNum != 1 {
  847. continue
  848. }
  849. return makeStructure(group)
  850. }
  851. }
  852. }
  853. klog.V(2).Infof("Unable to sniff out cluster ID, perhaps set $%s to force one", ClusterIdEnvVar)
  854. return makeStructure(defaultClusterName)
  855. }
  856. // AddServiceKey adds an AWS service key, useful for pulling down out-of-cluster costs. Optional-- the container this runs in can be directly authorized.
  857. func (*AWS) AddServiceKey(formValues url.Values) error {
  858. keyID := formValues.Get("access_key_ID")
  859. key := formValues.Get("secret_access_key")
  860. m := make(map[string]string)
  861. m["access_key_ID"] = keyID
  862. m["secret_access_key"] = key
  863. result, err := json.Marshal(m)
  864. if err != nil {
  865. return err
  866. }
  867. return ioutil.WriteFile("/var/configs/key.json", result, 0644)
  868. }
  869. func configureAWSAuth(keyFile string) error {
  870. jsonFile, err := os.Open(keyFile)
  871. if err != nil {
  872. if os.IsNotExist(err) {
  873. klog.V(2).Infof("Using Default Credentials")
  874. return nil
  875. }
  876. return err
  877. }
  878. defer jsonFile.Close()
  879. byteValue, _ := ioutil.ReadAll(jsonFile)
  880. var result map[string]string
  881. err = json.Unmarshal([]byte(byteValue), &result)
  882. if err != nil {
  883. return err
  884. }
  885. err = os.Setenv(awsAccessKeyIDEnvVar, result["awsServiceKeyName"])
  886. if err != nil {
  887. return err
  888. }
  889. err = os.Setenv(awsAccessKeySecretEnvVar, result["awsServiceKeySecret"])
  890. if err != nil {
  891. return err
  892. }
  893. return nil
  894. }
  895. func getClusterConfig(ccFile string) (map[string]string, error) {
  896. clusterConfig, err := os.Open(ccFile)
  897. if err != nil {
  898. return nil, err
  899. }
  900. defer clusterConfig.Close()
  901. b, err := ioutil.ReadAll(clusterConfig)
  902. if err != nil {
  903. return nil, err
  904. }
  905. var clusterConf map[string]string
  906. err = json.Unmarshal([]byte(b), &clusterConf)
  907. if err != nil {
  908. return nil, err
  909. }
  910. return clusterConf, nil
  911. }
  912. // 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.
  913. func (*AWS) GetDisks() ([]byte, error) {
  914. err := configureAWSAuth("/var/configs/key.json")
  915. if err != nil {
  916. return nil, err
  917. }
  918. clusterConfig, err := getClusterConfig("/var/configs/cluster.json")
  919. if err != nil {
  920. return nil, err
  921. }
  922. region := aws.String(clusterConfig["region"])
  923. c := &aws.Config{
  924. Region: region,
  925. }
  926. s := session.Must(session.NewSession(c))
  927. ec2Svc := ec2.New(s)
  928. input := &ec2.DescribeVolumesInput{}
  929. volumeResult, err := ec2Svc.DescribeVolumes(input)
  930. if err != nil {
  931. if aerr, ok := err.(awserr.Error); ok {
  932. switch aerr.Code() {
  933. default:
  934. return nil, aerr
  935. }
  936. } else {
  937. return nil, err
  938. }
  939. }
  940. return json.Marshal(volumeResult)
  941. }
  942. // ConvertToGlueColumnFormat takes a string and runs through various regex
  943. // and string replacement statements to convert it to a format compatible
  944. // with AWS Glue and Athena column names.
  945. // Following guidance from AWS provided here ('Column Names' section):
  946. // https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/run-athena-sql.html
  947. // It returns a string containing the column name in proper column name format and length.
  948. func ConvertToGlueColumnFormat(column_name string) string {
  949. klog.V(5).Infof("Converting string \"%s\" to proper AWS Glue column name.", column_name)
  950. // An underscore is added in front of uppercase letters
  951. capital_underscore := regexp.MustCompile(`[A-Z]`)
  952. final := capital_underscore.ReplaceAllString(column_name, `_$0`)
  953. // Any non-alphanumeric characters are replaced with an underscore
  954. no_space_punc := regexp.MustCompile(`[\s]{1,}|[^A-Za-z0-9]`)
  955. final = no_space_punc.ReplaceAllString(final, "_")
  956. // Duplicate underscores are removed
  957. no_dup_underscore := regexp.MustCompile(`_{2,}`)
  958. final = no_dup_underscore.ReplaceAllString(final, "_")
  959. // Any leading and trailing underscores are removed
  960. no_front_end_underscore := regexp.MustCompile(`(^\_|\_$)`)
  961. final = no_front_end_underscore.ReplaceAllString(final, "")
  962. // Uppercase to lowercase
  963. final = strings.ToLower(final)
  964. // Longer column name than expected - remove _ left to right
  965. allowed_col_len := 128
  966. undersc_to_remove := len(final) - allowed_col_len
  967. if undersc_to_remove > 0 {
  968. final = strings.Replace(final, "_", "", undersc_to_remove)
  969. }
  970. // If removing all of the underscores still didn't
  971. // make the column name < 128 characters, trim it!
  972. if len(final) > allowed_col_len {
  973. final = final[:allowed_col_len]
  974. }
  975. klog.V(5).Infof("Column name being returned: \"%s\". Length: \"%d\".", final, len(final))
  976. return final
  977. }
  978. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  979. // "start" and "end" are dates of the format YYYY-MM-DD
  980. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  981. func (a *AWS) ExternalAllocations(start string, end string, aggregator string, filterType string, filterValue string) ([]*OutOfClusterAllocation, error) {
  982. customPricing, err := a.GetConfig()
  983. if err != nil {
  984. return nil, err
  985. }
  986. aggregator_column_name := "resource_tags_user_" + aggregator
  987. aggregator_column_name = ConvertToGlueColumnFormat(aggregator_column_name)
  988. filter_column_name := "resource_tags_user_" + filterType
  989. filter_column_name = ConvertToGlueColumnFormat(filter_column_name)
  990. var query string
  991. var lastIdx int
  992. if filterType != "kubernetes_" { // This gets appended upstream and is equivalent to no filter.
  993. query = fmt.Sprintf(`SELECT
  994. CAST(line_item_usage_start_date AS DATE) as start_date,
  995. %s,
  996. line_item_product_code,
  997. %s,
  998. SUM(line_item_blended_cost) as blended_cost
  999. FROM %s as cost_data
  1000. WHERE (%s='%s' OR %s='') AND line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1001. GROUP BY 1,2,3,4`, aggregator_column_name, filter_column_name, customPricing.AthenaTable, filter_column_name, filterValue, filter_column_name, start, end)
  1002. lastIdx = 4
  1003. } else {
  1004. lastIdx = 3
  1005. query = fmt.Sprintf(`SELECT
  1006. CAST(line_item_usage_start_date AS DATE) as start_date,
  1007. %s,
  1008. line_item_product_code,
  1009. SUM(line_item_blended_cost) as blended_cost
  1010. FROM %s as cost_data
  1011. WHERE line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1012. GROUP BY 1,2,3`, aggregator_column_name, customPricing.AthenaTable, start, end)
  1013. }
  1014. klog.V(3).Infof("Running Query: %s", query)
  1015. if customPricing.ServiceKeyName != "" {
  1016. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1017. if err != nil {
  1018. return nil, err
  1019. }
  1020. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1021. if err != nil {
  1022. return nil, err
  1023. }
  1024. }
  1025. region := aws.String(customPricing.AthenaRegion)
  1026. resultsBucket := customPricing.AthenaBucketName
  1027. database := customPricing.AthenaDatabase
  1028. c := &aws.Config{
  1029. Region: region,
  1030. }
  1031. s := session.Must(session.NewSession(c))
  1032. svc := athena.New(s)
  1033. var e athena.StartQueryExecutionInput
  1034. var r athena.ResultConfiguration
  1035. r.SetOutputLocation(resultsBucket)
  1036. e.SetResultConfiguration(&r)
  1037. e.SetQueryString(query)
  1038. var q athena.QueryExecutionContext
  1039. q.SetDatabase(database)
  1040. e.SetQueryExecutionContext(&q)
  1041. res, err := svc.StartQueryExecution(&e)
  1042. if err != nil {
  1043. return nil, err
  1044. }
  1045. klog.V(2).Infof("StartQueryExecution result:")
  1046. klog.V(2).Infof(res.GoString())
  1047. var qri athena.GetQueryExecutionInput
  1048. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1049. var qrop *athena.GetQueryExecutionOutput
  1050. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1051. for {
  1052. qrop, err = svc.GetQueryExecution(&qri)
  1053. if err != nil {
  1054. return nil, err
  1055. }
  1056. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1057. break
  1058. }
  1059. time.Sleep(duration)
  1060. }
  1061. var oocAllocs []*OutOfClusterAllocation
  1062. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1063. var ip athena.GetQueryResultsInput
  1064. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1065. op, err := svc.GetQueryResults(&ip)
  1066. if err != nil {
  1067. return nil, err
  1068. }
  1069. if len(op.ResultSet.Rows) > 1 {
  1070. for _, r := range op.ResultSet.Rows[1:(len(op.ResultSet.Rows) - 1)] {
  1071. cost, err := strconv.ParseFloat(*r.Data[lastIdx].VarCharValue, 64)
  1072. if err != nil {
  1073. return nil, err
  1074. }
  1075. ooc := &OutOfClusterAllocation{
  1076. Aggregator: aggregator,
  1077. Environment: *r.Data[1].VarCharValue,
  1078. Service: *r.Data[2].VarCharValue,
  1079. Cost: cost,
  1080. }
  1081. oocAllocs = append(oocAllocs, ooc)
  1082. }
  1083. } else {
  1084. klog.V(1).Infof("No results available for %s at database %s between %s and %s", aggregator_column_name, customPricing.AthenaTable, start, end)
  1085. }
  1086. }
  1087. return oocAllocs, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  1088. }
  1089. // QuerySQL can query a properly configured Athena database.
  1090. // Used to fetch billing data.
  1091. // Requires a json config in /var/configs with key region, output, and database.
  1092. func (a *AWS) QuerySQL(query string) ([]byte, error) {
  1093. customPricing, err := a.GetConfig()
  1094. if err != nil {
  1095. return nil, err
  1096. }
  1097. if customPricing.ServiceKeyName != "" {
  1098. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1099. if err != nil {
  1100. return nil, err
  1101. }
  1102. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1103. if err != nil {
  1104. return nil, err
  1105. }
  1106. }
  1107. athenaConfigs, err := os.Open("/var/configs/athena.json")
  1108. if err != nil {
  1109. return nil, err
  1110. }
  1111. defer athenaConfigs.Close()
  1112. b, err := ioutil.ReadAll(athenaConfigs)
  1113. if err != nil {
  1114. return nil, err
  1115. }
  1116. var athenaConf map[string]string
  1117. json.Unmarshal([]byte(b), &athenaConf)
  1118. region := aws.String(customPricing.AthenaRegion)
  1119. resultsBucket := customPricing.AthenaBucketName
  1120. database := customPricing.AthenaDatabase
  1121. c := &aws.Config{
  1122. Region: region,
  1123. }
  1124. s := session.Must(session.NewSession(c))
  1125. svc := athena.New(s)
  1126. var e athena.StartQueryExecutionInput
  1127. var r athena.ResultConfiguration
  1128. r.SetOutputLocation(resultsBucket)
  1129. e.SetResultConfiguration(&r)
  1130. e.SetQueryString(query)
  1131. var q athena.QueryExecutionContext
  1132. q.SetDatabase(database)
  1133. e.SetQueryExecutionContext(&q)
  1134. res, err := svc.StartQueryExecution(&e)
  1135. if err != nil {
  1136. return nil, err
  1137. }
  1138. klog.V(2).Infof("StartQueryExecution result:")
  1139. klog.V(2).Infof(res.GoString())
  1140. var qri athena.GetQueryExecutionInput
  1141. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1142. var qrop *athena.GetQueryExecutionOutput
  1143. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1144. for {
  1145. qrop, err = svc.GetQueryExecution(&qri)
  1146. if err != nil {
  1147. return nil, err
  1148. }
  1149. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1150. break
  1151. }
  1152. time.Sleep(duration)
  1153. }
  1154. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1155. var ip athena.GetQueryResultsInput
  1156. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1157. op, err := svc.GetQueryResults(&ip)
  1158. if err != nil {
  1159. return nil, err
  1160. }
  1161. b, err := json.Marshal(op.ResultSet)
  1162. if err != nil {
  1163. return nil, err
  1164. }
  1165. return b, nil
  1166. }
  1167. return nil, fmt.Errorf("Error getting query results : %s", *qrop.QueryExecution.Status.State)
  1168. }
  1169. type spotInfo struct {
  1170. Timestamp string `csv:"Timestamp"`
  1171. UsageType string `csv:"UsageType"`
  1172. Operation string `csv:"Operation"`
  1173. InstanceID string `csv:"InstanceID"`
  1174. MyBidID string `csv:"MyBidID"`
  1175. MyMaxPrice string `csv:"MyMaxPrice"`
  1176. MarketPrice string `csv:"MarketPrice"`
  1177. Charge string `csv:"Charge"`
  1178. Version string `csv:"Version"`
  1179. }
  1180. type fnames []*string
  1181. func (f fnames) Len() int {
  1182. return len(f)
  1183. }
  1184. func (f fnames) Swap(i, j int) {
  1185. f[i], f[j] = f[j], f[i]
  1186. }
  1187. func (f fnames) Less(i, j int) bool {
  1188. key1 := strings.Split(*f[i], ".")
  1189. key2 := strings.Split(*f[j], ".")
  1190. t1, err := time.Parse("2006-01-02-15", key1[1])
  1191. if err != nil {
  1192. klog.V(1).Info("Unable to parse timestamp" + key1[1])
  1193. return false
  1194. }
  1195. t2, err := time.Parse("2006-01-02-15", key2[1])
  1196. if err != nil {
  1197. klog.V(1).Info("Unable to parse timestamp" + key2[1])
  1198. return false
  1199. }
  1200. return t1.Before(t2)
  1201. }
  1202. func parseSpotData(bucket string, prefix string, projectID string, region string, accessKeyID string, accessKeySecret string) (map[string]*spotInfo, error) {
  1203. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  1204. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  1205. if err != nil {
  1206. return nil, err
  1207. }
  1208. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  1209. if err != nil {
  1210. return nil, err
  1211. }
  1212. }
  1213. s3Prefix := projectID
  1214. if len(prefix) != 0 {
  1215. s3Prefix = prefix + "/" + s3Prefix
  1216. }
  1217. c := aws.NewConfig().WithRegion(region)
  1218. s := session.Must(session.NewSession(c))
  1219. s3Svc := s3.New(s)
  1220. downloader := s3manager.NewDownloaderWithClient(s3Svc)
  1221. tNow := time.Now()
  1222. tOneDayAgo := tNow.Add(time.Duration(-24) * time.Hour) // Also get files from one day ago to avoid boundary conditions
  1223. ls := &s3.ListObjectsInput{
  1224. Bucket: aws.String(bucket),
  1225. Prefix: aws.String(s3Prefix + "." + tOneDayAgo.Format("2006-01-02")),
  1226. }
  1227. ls2 := &s3.ListObjectsInput{
  1228. Bucket: aws.String(bucket),
  1229. Prefix: aws.String(s3Prefix + "." + tNow.Format("2006-01-02")),
  1230. }
  1231. lso, err := s3Svc.ListObjects(ls)
  1232. if err != nil {
  1233. return nil, err
  1234. }
  1235. lsoLen := len(lso.Contents)
  1236. klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
  1237. if lsoLen == 0 {
  1238. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
  1239. }
  1240. lso2, err := s3Svc.ListObjects(ls2)
  1241. if err != nil {
  1242. return nil, err
  1243. }
  1244. lso2Len := len(lso2.Contents)
  1245. klog.V(2).Infof("Found %d spot data files from today", lso2Len)
  1246. if lso2Len == 0 {
  1247. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
  1248. }
  1249. var keys []*string
  1250. for _, obj := range lso.Contents {
  1251. keys = append(keys, obj.Key)
  1252. }
  1253. for _, obj := range lso2.Contents {
  1254. keys = append(keys, obj.Key)
  1255. }
  1256. versionRx := regexp.MustCompile("^#Version: (\\d+)\\.\\d+$")
  1257. header, err := csvutil.Header(spotInfo{}, "csv")
  1258. if err != nil {
  1259. return nil, err
  1260. }
  1261. fieldsPerRecord := len(header)
  1262. spots := make(map[string]*spotInfo)
  1263. for _, key := range keys {
  1264. getObj := &s3.GetObjectInput{
  1265. Bucket: aws.String(bucket),
  1266. Key: key,
  1267. }
  1268. buf := aws.NewWriteAtBuffer([]byte{})
  1269. _, err := downloader.Download(buf, getObj)
  1270. if err != nil {
  1271. return nil, err
  1272. }
  1273. r := bytes.NewReader(buf.Bytes())
  1274. gr, err := gzip.NewReader(r)
  1275. if err != nil {
  1276. return nil, err
  1277. }
  1278. csvReader := csv.NewReader(gr)
  1279. csvReader.Comma = '\t'
  1280. csvReader.FieldsPerRecord = fieldsPerRecord
  1281. dec, err := csvutil.NewDecoder(csvReader, header...)
  1282. if err != nil {
  1283. return nil, err
  1284. }
  1285. var foundVersion string
  1286. for {
  1287. spot := spotInfo{}
  1288. err := dec.Decode(&spot)
  1289. csvParseErr, isCsvParseErr := err.(*csv.ParseError)
  1290. if err == io.EOF {
  1291. break
  1292. } else if err == csvutil.ErrFieldCount || (isCsvParseErr && csvParseErr.Err == csv.ErrFieldCount) {
  1293. rec := dec.Record()
  1294. // the first two "Record()" will be the comment lines
  1295. // and they show up as len() == 1
  1296. // the first of which is "#Version"
  1297. // the second of which is "#Fields: "
  1298. if len(rec) != 1 {
  1299. klog.V(2).Infof("Expected %d spot info fields but received %d: %s", fieldsPerRecord, len(rec), rec)
  1300. continue
  1301. }
  1302. if len(foundVersion) == 0 {
  1303. spotFeedVersion := rec[0]
  1304. klog.V(4).Infof("Spot feed version is \"%s\"", spotFeedVersion)
  1305. matches := versionRx.FindStringSubmatch(spotFeedVersion)
  1306. if matches != nil {
  1307. foundVersion = matches[1]
  1308. if foundVersion != supportedSpotFeedVersion {
  1309. klog.V(2).Infof("Unsupported spot info feed version: wanted \"%s\" got \"%s\"", supportedSpotFeedVersion, foundVersion)
  1310. break
  1311. }
  1312. }
  1313. continue
  1314. } else if strings.Index(rec[0], "#") == 0 {
  1315. continue
  1316. } else {
  1317. klog.V(3).Infof("skipping non-TSV line: %s", rec)
  1318. continue
  1319. }
  1320. } else if err != nil {
  1321. klog.V(2).Infof("Error during spot info decode: %+v", err)
  1322. continue
  1323. }
  1324. klog.V(4).Infof("Found spot info %+v", spot)
  1325. spots[spot.InstanceID] = &spot
  1326. }
  1327. gr.Close()
  1328. }
  1329. return spots, nil
  1330. }
  1331. func (a *AWS) ApplyReservedInstancePricing(nodes map[string]*Node) {
  1332. numReserved := len(a.ReservedInstances)
  1333. // Early return if no reserved instance data loaded
  1334. if numReserved == 0 {
  1335. klog.V(4).Infof("[Reserved] No Reserved Instances")
  1336. return
  1337. }
  1338. cfg, err := a.GetConfig()
  1339. defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
  1340. if err != nil {
  1341. klog.V(3).Infof("Could not parse default cpu price")
  1342. defaultCPU = 0.031611
  1343. }
  1344. defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
  1345. if err != nil {
  1346. klog.V(3).Infof("Could not parse default ram price")
  1347. defaultRAM = 0.004237
  1348. }
  1349. cpuToRAMRatio := defaultCPU / defaultRAM
  1350. now := time.Now()
  1351. instances := make(map[string][]*AWSReservedInstance)
  1352. for _, r := range a.ReservedInstances {
  1353. if now.Before(r.StartDate) || now.After(r.EndDate) {
  1354. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  1355. continue
  1356. }
  1357. _, ok := instances[r.Region]
  1358. if !ok {
  1359. instances[r.Region] = []*AWSReservedInstance{r}
  1360. } else {
  1361. instances[r.Region] = append(instances[r.Region], r)
  1362. }
  1363. }
  1364. awsNodes := make(map[string]*v1.Node)
  1365. currentNodes := a.Clientset.GetAllNodes()
  1366. // Create a node name -> node map
  1367. for _, awsNode := range currentNodes {
  1368. awsNodes[awsNode.GetName()] = awsNode
  1369. }
  1370. // go through all provider nodes using k8s nodes for region
  1371. for nodeName, node := range nodes {
  1372. // Reset reserved allocation to prevent double allocation
  1373. node.Reserved = nil
  1374. kNode, ok := awsNodes[nodeName]
  1375. if !ok {
  1376. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  1377. continue
  1378. }
  1379. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  1380. if !ok {
  1381. klog.V(1).Infof("[Reserved] Could not find node region")
  1382. continue
  1383. }
  1384. reservedInstances, ok := instances[nodeRegion]
  1385. if !ok {
  1386. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  1387. continue
  1388. }
  1389. // Determine the InstanceType of the node
  1390. instanceType, ok := kNode.Labels["beta.kubernetes.io/instance-type"]
  1391. if !ok {
  1392. continue
  1393. }
  1394. ramBytes, err := strconv.ParseFloat(node.RAMBytes, 64)
  1395. if err != nil {
  1396. continue
  1397. }
  1398. ramGB := ramBytes / 1024 / 1024 / 1024
  1399. cpu, err := strconv.ParseFloat(node.VCPU, 64)
  1400. if err != nil {
  1401. continue
  1402. }
  1403. ramMultiple := cpu*cpuToRAMRatio + ramGB
  1404. node.Reserved = &ReservedInstanceData{
  1405. ReservedCPU: 0,
  1406. ReservedRAM: 0,
  1407. }
  1408. for i, reservedInstance := range reservedInstances {
  1409. if reservedInstance.InstanceType == instanceType {
  1410. // Use < 0 to mark as ALL
  1411. node.Reserved.ReservedCPU = -1
  1412. node.Reserved.ReservedRAM = -1
  1413. // Set Costs based on CPU/RAM ratios
  1414. ramPrice := reservedInstance.PricePerHour / ramMultiple
  1415. node.Reserved.CPUCost = ramPrice * cpuToRAMRatio
  1416. node.Reserved.RAMCost = ramPrice
  1417. // Remove the reserve from the temporary slice to prevent
  1418. // being reallocated
  1419. instances[nodeRegion] = append(reservedInstances[:i], reservedInstances[i+1:]...)
  1420. break
  1421. }
  1422. }
  1423. }
  1424. }
  1425. type AWSReservedInstance struct {
  1426. Zone string
  1427. Region string
  1428. InstanceType string
  1429. InstanceCount int64
  1430. InstanceTenacy string
  1431. StartDate time.Time
  1432. EndDate time.Time
  1433. PricePerHour float64
  1434. }
  1435. func (ari *AWSReservedInstance) String() string {
  1436. 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)
  1437. }
  1438. func isReservedInstanceHourlyPrice(rc *ec2.RecurringCharge) bool {
  1439. return rc != nil && rc.Frequency != nil && *rc.Frequency == "Hourly"
  1440. }
  1441. func getReservedInstancePrice(ri *ec2.ReservedInstances) (float64, error) {
  1442. var pricePerHour float64
  1443. if len(ri.RecurringCharges) > 0 {
  1444. for _, rc := range ri.RecurringCharges {
  1445. if isReservedInstanceHourlyPrice(rc) {
  1446. pricePerHour = *rc.Amount
  1447. break
  1448. }
  1449. }
  1450. }
  1451. // If we're still unable to resolve hourly price, try fixed -> hourly
  1452. if pricePerHour == 0 {
  1453. if ri.Duration != nil && ri.FixedPrice != nil {
  1454. var durHours float64
  1455. durSeconds := float64(*ri.Duration)
  1456. fixedPrice := float64(*ri.FixedPrice)
  1457. if durSeconds != 0 && fixedPrice != 0 {
  1458. durHours = durSeconds / 60 / 60
  1459. pricePerHour = fixedPrice / durHours
  1460. }
  1461. }
  1462. }
  1463. if pricePerHour == 0 {
  1464. return 0, fmt.Errorf("Failed to resolve an hourly price from FixedPrice or Recurring Costs")
  1465. }
  1466. return pricePerHour, nil
  1467. }
  1468. func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
  1469. c := &aws.Config{
  1470. Region: aws.String(region),
  1471. }
  1472. s := session.Must(session.NewSession(c))
  1473. svc := ec2.New(s)
  1474. response, err := svc.DescribeReservedInstances(&ec2.DescribeReservedInstancesInput{})
  1475. if err != nil {
  1476. return nil, err
  1477. }
  1478. var reservedInstances []*AWSReservedInstance
  1479. for _, ri := range response.ReservedInstances {
  1480. var zone string
  1481. if ri.AvailabilityZone != nil {
  1482. zone = *ri.AvailabilityZone
  1483. }
  1484. pricePerHour, err := getReservedInstancePrice(ri)
  1485. if err != nil {
  1486. klog.V(1).Infof("Error Resolving Price: %s", err.Error())
  1487. continue
  1488. }
  1489. reservedInstances = append(reservedInstances, &AWSReservedInstance{
  1490. Zone: zone,
  1491. Region: region,
  1492. InstanceType: *ri.InstanceType,
  1493. InstanceCount: *ri.InstanceCount,
  1494. InstanceTenacy: *ri.InstanceTenancy,
  1495. StartDate: *ri.Start,
  1496. EndDate: *ri.End,
  1497. PricePerHour: pricePerHour,
  1498. })
  1499. }
  1500. return reservedInstances, nil
  1501. }
  1502. func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
  1503. err := configureAWSAuth("/var/configs/aws.json")
  1504. if err != nil {
  1505. return nil, err
  1506. }
  1507. var reservedInstances []*AWSReservedInstance
  1508. nodes := a.Clientset.GetAllNodes()
  1509. regionsSeen := make(map[string]bool)
  1510. for _, node := range nodes {
  1511. region, ok := node.Labels[v1.LabelZoneRegion]
  1512. if !ok {
  1513. continue
  1514. }
  1515. if regionsSeen[region] {
  1516. continue
  1517. }
  1518. ris, err := getRegionReservedInstances(region)
  1519. if err != nil {
  1520. klog.V(3).Infof("Error getting reserved instances: %s", err.Error())
  1521. continue
  1522. }
  1523. regionsSeen[region] = true
  1524. reservedInstances = append(reservedInstances, ris...)
  1525. }
  1526. return reservedInstances, nil
  1527. }