awsprovider.go 67 KB

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