provider.go 80 KB

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