awsprovider.go 67 KB

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