awsprovider.go 61 KB

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