awsprovider.go 68 KB

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