provider.go 75 KB

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