awsprovider.go 66 KB

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