awsprovider.go 63 KB

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