awsprovider.go 63 KB

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