provider.go 84 KB

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