awsprovider.go 62 KB

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