provider.go 84 KB

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