awsprovider.go 67 KB

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