awsprovider.go 63 KB

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