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. CapacityStatus string `json:"capacitystatus"`
  158. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  159. }
  160. // AWSPricingTerms are how you pay for the node: OnDemand, Reserved, or (TODO) Spot
  161. type AWSPricingTerms struct {
  162. OnDemand map[string]map[string]*AWSOfferTerm `json:"OnDemand"`
  163. Reserved map[string]map[string]*AWSOfferTerm `json:"Reserved"`
  164. }
  165. // AWSOfferTerm is a sku extension used to pay for the node.
  166. type AWSOfferTerm struct {
  167. Sku string `json:"sku"`
  168. PriceDimensions map[string]*AWSRateCode `json:"priceDimensions"`
  169. }
  170. // AWSRateCode encodes data about the price of a product
  171. type AWSRateCode struct {
  172. Unit string `json:"unit"`
  173. PricePerUnit AWSCurrencyCode `json:"pricePerUnit"`
  174. }
  175. // AWSCurrencyCode is the localized currency. (TODO: support non-USD)
  176. type AWSCurrencyCode struct {
  177. USD string `json:"USD"`
  178. }
  179. // AWSProductTerms represents the full terms of the product
  180. type AWSProductTerms struct {
  181. Sku string `json:"sku"`
  182. OnDemand *AWSOfferTerm `json:"OnDemand"`
  183. Reserved *AWSOfferTerm `json:"Reserved"`
  184. Memory string `json:"memory"`
  185. Storage string `json:"storage"`
  186. VCpu string `json:"vcpu"`
  187. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  188. PV *PV `json:"pv"`
  189. }
  190. // ClusterIdEnvVar is the environment variable in which one can manually set the ClusterId
  191. const ClusterIdEnvVar = "AWS_CLUSTER_ID"
  192. // OnDemandRateCode is appended to an node sku
  193. const OnDemandRateCode = ".JRTCKXETXF"
  194. // ReservedRateCode is appended to a node sku
  195. const ReservedRateCode = ".38NPMPTW36"
  196. // HourlyRateCode is appended to a node sku
  197. const HourlyRateCode = ".6YS6EN2CT7"
  198. // volTypes are used to map between AWS UsageTypes and
  199. // EBS volume types, as they would appear in K8s storage class
  200. // name and the EC2 API.
  201. var volTypes = map[string]string{
  202. "EBS:VolumeUsage.gp2": "gp2",
  203. "EBS:VolumeUsage": "standard",
  204. "EBS:VolumeUsage.sc1": "sc1",
  205. "EBS:VolumeP-IOPS.piops": "io1",
  206. "EBS:VolumeUsage.st1": "st1",
  207. "EBS:VolumeUsage.piops": "io1",
  208. "gp2": "EBS:VolumeUsage.gp2",
  209. "standard": "EBS:VolumeUsage",
  210. "sc1": "EBS:VolumeUsage.sc1",
  211. "io1": "EBS:VolumeUsage.piops",
  212. "st1": "EBS:VolumeUsage.st1",
  213. }
  214. // locationToRegion maps AWS region names (As they come from Billing)
  215. // to actual region identifiers
  216. var locationToRegion = map[string]string{
  217. "US East (Ohio)": "us-east-2",
  218. "US East (N. Virginia)": "us-east-1",
  219. "US West (N. California)": "us-west-1",
  220. "US West (Oregon)": "us-west-2",
  221. "Asia Pacific (Hong Kong)": "ap-east-1",
  222. "Asia Pacific (Mumbai)": "ap-south-1",
  223. "Asia Pacific (Osaka-Local)": "ap-northeast-3",
  224. "Asia Pacific (Seoul)": "ap-northeast-2",
  225. "Asia Pacific (Singapore)": "ap-southeast-1",
  226. "Asia Pacific (Sydney)": "ap-southeast-2",
  227. "Asia Pacific (Tokyo)": "ap-northeast-1",
  228. "Canada (Central)": "ca-central-1",
  229. "China (Beijing)": "cn-north-1",
  230. "China (Ningxia)": "cn-northwest-1",
  231. "EU (Frankfurt)": "eu-central-1",
  232. "EU (Ireland)": "eu-west-1",
  233. "EU (London)": "eu-west-2",
  234. "EU (Paris)": "eu-west-3",
  235. "EU (Stockholm)": "eu-north-1",
  236. "South America (Sao Paulo)": "sa-east-1",
  237. "AWS GovCloud (US-East)": "us-gov-east-1",
  238. "AWS GovCloud (US-West)": "us-gov-west-1",
  239. }
  240. var regionToBillingRegionCode = map[string]string{
  241. "us-east-2": "USE2",
  242. "us-east-1": "",
  243. "us-west-1": "USW1",
  244. "us-west-2": "USW2",
  245. "ap-east-1": "APE1",
  246. "ap-south-1": "APS3",
  247. "ap-northeast-3": "APN3",
  248. "ap-northeast-2": "APN2",
  249. "ap-southeast-1": "APS1",
  250. "ap-southeast-2": "APS2",
  251. "ap-northeast-1": "APN1",
  252. "ca-central-1": "CAN1",
  253. "cn-north-1": "",
  254. "cn-northwest-1": "",
  255. "eu-central-1": "EUC1",
  256. "eu-west-1": "EU",
  257. "eu-west-2": "EUW2",
  258. "eu-west-3": "EUW3",
  259. "eu-north-1": "EUN1",
  260. "sa-east-1": "SAE1",
  261. "us-gov-east-1": "UGE1",
  262. "us-gov-west-1": "UGW1",
  263. }
  264. var loadedAWSSecret bool = false
  265. var awsSecret *AWSAccessKey = nil
  266. func (aws *AWS) GetLocalStorageQuery(window, offset string, rate bool, used bool) string {
  267. return ""
  268. }
  269. // KubeAttrConversion maps the k8s labels for region to an aws region
  270. func (aws *AWS) KubeAttrConversion(location, instanceType, operatingSystem string) string {
  271. operatingSystem = strings.ToLower(operatingSystem)
  272. region := locationToRegion[location]
  273. return region + "," + instanceType + "," + operatingSystem
  274. }
  275. type AwsSpotFeedInfo struct {
  276. BucketName string `json:"bucketName"`
  277. Prefix string `json:"prefix"`
  278. Region string `json:"region"`
  279. AccountID string `json:"projectID"`
  280. ServiceKeyName string `json:"serviceKeyName"`
  281. ServiceKeySecret string `json:"serviceKeySecret"`
  282. SpotLabel string `json:"spotLabel"`
  283. SpotLabelValue string `json:"spotLabelValue"`
  284. }
  285. type AwsAthenaInfo struct {
  286. AthenaBucketName string `json:"athenaBucketName"`
  287. AthenaRegion string `json:"athenaRegion"`
  288. AthenaDatabase string `json:"athenaDatabase"`
  289. AthenaTable string `json:"athenaTable"`
  290. ServiceKeyName string `json:"serviceKeyName"`
  291. ServiceKeySecret string `json:"serviceKeySecret"`
  292. AccountID string `json:"projectID"`
  293. MasterPayerARN string `json:"masterPayerARN"`
  294. }
  295. func (aws *AWS) GetManagementPlatform() (string, error) {
  296. nodes := aws.Clientset.GetAllNodes()
  297. if len(nodes) > 0 {
  298. n := nodes[0]
  299. version := n.Status.NodeInfo.KubeletVersion
  300. if strings.Contains(version, "eks") {
  301. return "eks", nil
  302. }
  303. if _, ok := n.Labels["kops.k8s.io/instancegroup"]; ok {
  304. return "kops", nil
  305. }
  306. }
  307. return "", nil
  308. }
  309. func (aws *AWS) GetConfig() (*CustomPricing, error) {
  310. c, err := aws.Config.GetCustomPricingData()
  311. if c.Discount == "" {
  312. c.Discount = "0%"
  313. }
  314. if c.NegotiatedDiscount == "" {
  315. c.NegotiatedDiscount = "0%"
  316. }
  317. if err != nil {
  318. return nil, err
  319. }
  320. return c, nil
  321. }
  322. func (aws *AWS) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  323. return aws.Config.UpdateFromMap(a)
  324. }
  325. func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  326. return aws.Config.Update(func(c *CustomPricing) error {
  327. if updateType == SpotInfoUpdateType {
  328. a := AwsSpotFeedInfo{}
  329. err := json.NewDecoder(r).Decode(&a)
  330. if err != nil {
  331. return err
  332. }
  333. c.ServiceKeyName = a.ServiceKeyName
  334. if a.ServiceKeySecret != "" {
  335. c.ServiceKeySecret = a.ServiceKeySecret
  336. }
  337. c.SpotDataPrefix = a.Prefix
  338. c.SpotDataBucket = a.BucketName
  339. c.ProjectID = a.AccountID
  340. c.SpotDataRegion = a.Region
  341. c.SpotLabel = a.SpotLabel
  342. c.SpotLabelValue = a.SpotLabelValue
  343. } else if updateType == AthenaInfoUpdateType {
  344. a := AwsAthenaInfo{}
  345. err := json.NewDecoder(r).Decode(&a)
  346. if err != nil {
  347. return err
  348. }
  349. c.AthenaBucketName = a.AthenaBucketName
  350. c.AthenaRegion = a.AthenaRegion
  351. c.AthenaDatabase = a.AthenaDatabase
  352. c.AthenaTable = a.AthenaTable
  353. c.ServiceKeyName = a.ServiceKeyName
  354. if a.ServiceKeySecret != "" {
  355. c.ServiceKeySecret = a.ServiceKeySecret
  356. }
  357. if a.MasterPayerARN != "" {
  358. c.MasterPayerARN = a.MasterPayerARN
  359. }
  360. c.AthenaProjectID = a.AccountID
  361. } else {
  362. a := make(map[string]interface{})
  363. err := json.NewDecoder(r).Decode(&a)
  364. if err != nil {
  365. return err
  366. }
  367. for k, v := range a {
  368. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  369. vstr, ok := v.(string)
  370. if ok {
  371. err := SetCustomPricingField(c, kUpper, vstr)
  372. if err != nil {
  373. return err
  374. }
  375. } else {
  376. sci := v.(map[string]interface{})
  377. sc := make(map[string]string)
  378. for k, val := range sci {
  379. sc[k] = val.(string)
  380. }
  381. c.SharedCosts = sc //todo: support reflection/multiple map fields
  382. }
  383. }
  384. }
  385. if env.IsRemoteEnabled() {
  386. err := UpdateClusterMeta(env.GetClusterID(), c.ClusterName)
  387. if err != nil {
  388. return err
  389. }
  390. }
  391. return nil
  392. })
  393. }
  394. type awsKey struct {
  395. SpotLabelName string
  396. SpotLabelValue string
  397. Labels map[string]string
  398. ProviderID string
  399. }
  400. func (k *awsKey) GPUType() string {
  401. return ""
  402. }
  403. func (k *awsKey) ID() string {
  404. provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)") // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
  405. for matchNum, group := range provIdRx.FindStringSubmatch(k.ProviderID) {
  406. if matchNum == 2 {
  407. return group
  408. }
  409. }
  410. klog.V(3).Infof("Could not find instance ID in \"%s\"", k.ProviderID)
  411. return ""
  412. }
  413. func (k *awsKey) Features() string {
  414. instanceType, _ := util.GetInstanceType(k.Labels)
  415. operatingSystem, _ := util.GetOperatingSystem(k.Labels)
  416. region, _ := util.GetRegion(k.Labels)
  417. key := region + "," + instanceType + "," + operatingSystem
  418. usageType := PreemptibleType
  419. spotKey := key + "," + usageType
  420. if l, ok := k.Labels["lifecycle"]; ok && l == "EC2Spot" {
  421. return spotKey
  422. }
  423. if l, ok := k.Labels[k.SpotLabelName]; ok && l == k.SpotLabelValue {
  424. return spotKey
  425. }
  426. return key
  427. }
  428. func (aws *AWS) PVPricing(pvk PVKey) (*PV, error) {
  429. pricing, ok := aws.Pricing[pvk.Features()]
  430. if !ok {
  431. klog.V(4).Infof("Persistent Volume pricing not found for %s: %s", pvk.GetStorageClass(), pvk.Features())
  432. return &PV{}, nil
  433. }
  434. return pricing.PV, nil
  435. }
  436. type awsPVKey struct {
  437. Labels map[string]string
  438. StorageClassParameters map[string]string
  439. StorageClassName string
  440. Name string
  441. DefaultRegion string
  442. ProviderID string
  443. }
  444. func (aws *AWS) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  445. providerID := ""
  446. if pv.Spec.AWSElasticBlockStore != nil {
  447. providerID = pv.Spec.AWSElasticBlockStore.VolumeID
  448. }
  449. return &awsPVKey{
  450. Labels: pv.Labels,
  451. StorageClassName: pv.Spec.StorageClassName,
  452. StorageClassParameters: parameters,
  453. Name: pv.Name,
  454. DefaultRegion: defaultRegion,
  455. ProviderID: providerID,
  456. }
  457. }
  458. func (key *awsPVKey) ID() string {
  459. return key.ProviderID
  460. }
  461. func (key *awsPVKey) GetStorageClass() string {
  462. return key.StorageClassName
  463. }
  464. func (key *awsPVKey) Features() string {
  465. storageClass := key.StorageClassParameters["type"]
  466. if storageClass == "standard" {
  467. storageClass = "gp2"
  468. }
  469. // Storage class names are generally EBS volume types (gp2)
  470. // Keys in Pricing are based on UsageTypes (EBS:VolumeType.gp2)
  471. // Converts between the 2
  472. region, _ := util.GetRegion(key.Labels)
  473. //if region == "" {
  474. // region = "us-east-1"
  475. //}
  476. class, ok := volTypes[storageClass]
  477. if !ok {
  478. klog.V(4).Infof("No voltype mapping for %s's storageClass: %s", key.Name, storageClass)
  479. }
  480. return region + "," + class
  481. }
  482. // GetKey maps node labels to information needed to retrieve pricing data
  483. func (aws *AWS) GetKey(labels map[string]string, n *v1.Node) Key {
  484. return &awsKey{
  485. SpotLabelName: aws.SpotLabelName,
  486. SpotLabelValue: aws.SpotLabelValue,
  487. Labels: labels,
  488. ProviderID: labels["providerID"],
  489. }
  490. }
  491. func (aws *AWS) isPreemptible(key string) bool {
  492. s := strings.Split(key, ",")
  493. if len(s) == 4 && s[3] == PreemptibleType {
  494. return true
  495. }
  496. return false
  497. }
  498. func (aws *AWS) ClusterManagementPricing() (string, float64, error) {
  499. return aws.clusterProvisioner, aws.clusterManagementPrice, nil
  500. }
  501. // Use the pricing data from the current region. Fall back to using all region data if needed.
  502. func (aws *AWS) getRegionPricing(nodeList []*v1.Node) (*http.Response, string, error) {
  503. pricingURL := "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/"
  504. region := ""
  505. multiregion := false
  506. for _, n := range nodeList {
  507. labels := n.GetLabels()
  508. currentNodeRegion := ""
  509. if r, ok := util.GetRegion(labels); ok {
  510. currentNodeRegion = r
  511. } else {
  512. multiregion = true // We weren't able to detect the node's region, so pull all data.
  513. break
  514. }
  515. if region == "" { // We haven't set a region yet
  516. region = currentNodeRegion
  517. } else if region != "" && currentNodeRegion != region { // If two nodes have different regions here, we'll need to fetch all pricing data.
  518. multiregion = true
  519. break
  520. }
  521. }
  522. if region != "" && !multiregion {
  523. pricingURL += region + "/"
  524. }
  525. pricingURL += "index.json"
  526. klog.V(2).Infof("starting download of \"%s\", which is quite large ...", pricingURL)
  527. resp, err := http.Get(pricingURL)
  528. if err != nil {
  529. klog.V(2).Infof("Bogus fetch of \"%s\": %v", pricingURL, err)
  530. return nil, pricingURL, err
  531. }
  532. return resp, pricingURL, err
  533. }
  534. // DownloadPricingData fetches data from the AWS Pricing API
  535. func (aws *AWS) DownloadPricingData() error {
  536. aws.DownloadPricingDataLock.Lock()
  537. defer aws.DownloadPricingDataLock.Unlock()
  538. if aws.ServiceAccountChecks == nil {
  539. aws.ServiceAccountChecks = make(map[string]*ServiceAccountCheck)
  540. }
  541. c, err := aws.Config.GetCustomPricingData()
  542. if err != nil {
  543. klog.V(1).Infof("Error downloading default pricing data: %s", err.Error())
  544. }
  545. aws.BaseCPUPrice = c.CPU
  546. aws.BaseRAMPrice = c.RAM
  547. aws.BaseGPUPrice = c.GPU
  548. aws.BaseSpotCPUPrice = c.SpotCPU
  549. aws.BaseSpotRAMPrice = c.SpotRAM
  550. aws.BaseSpotGPUPrice = c.SpotGPU
  551. aws.SpotLabelName = c.SpotLabel
  552. aws.SpotLabelValue = c.SpotLabelValue
  553. aws.SpotDataBucket = c.SpotDataBucket
  554. aws.SpotDataPrefix = c.SpotDataPrefix
  555. aws.ProjectID = c.ProjectID
  556. aws.SpotDataRegion = c.SpotDataRegion
  557. aws.ConfigureAuthWith(c) // load aws authentication from configuration or secret
  558. if len(aws.SpotDataBucket) != 0 && len(aws.ProjectID) == 0 {
  559. klog.V(1).Infof("using SpotDataBucket \"%s\" without ProjectID will not end well", aws.SpotDataBucket)
  560. }
  561. nodeList := aws.Clientset.GetAllNodes()
  562. inputkeys := make(map[string]bool)
  563. for _, n := range nodeList {
  564. if _, ok := n.Labels["eks.amazonaws.com/nodegroup"]; ok {
  565. aws.clusterManagementPrice = 0.10
  566. aws.clusterProvisioner = "EKS"
  567. } else if _, ok := n.Labels["kops.k8s.io/instancegroup"]; ok {
  568. aws.clusterProvisioner = "KOPS"
  569. }
  570. labels := n.GetObjectMeta().GetLabels()
  571. key := aws.GetKey(labels, n)
  572. inputkeys[key.Features()] = true
  573. }
  574. pvList := aws.Clientset.GetAllPersistentVolumes()
  575. storageClasses := aws.Clientset.GetAllStorageClasses()
  576. storageClassMap := make(map[string]map[string]string)
  577. for _, storageClass := range storageClasses {
  578. params := storageClass.Parameters
  579. storageClassMap[storageClass.ObjectMeta.Name] = params
  580. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  581. storageClassMap["default"] = params
  582. storageClassMap[""] = params
  583. }
  584. }
  585. pvkeys := make(map[string]PVKey)
  586. for _, pv := range pvList {
  587. params, ok := storageClassMap[pv.Spec.StorageClassName]
  588. if !ok {
  589. klog.V(2).Infof("Unable to find params for storageClassName %s, falling back to default pricing", pv.Spec.StorageClassName)
  590. continue
  591. }
  592. key := aws.GetPVKey(pv, params, "")
  593. pvkeys[key.Features()] = key
  594. }
  595. // RIDataRunning establishes the existance of the goroutine. Since it's possible we
  596. // run multiple downloads, we don't want to create multiple go routines if one already exists
  597. if !aws.RIDataRunning && c.AthenaBucketName != "" {
  598. err = aws.GetReservationDataFromAthena() // Block until one run has completed.
  599. if err != nil {
  600. klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
  601. } else { // If we make one successful run, check on new reservation data every hour
  602. go func() {
  603. defer errors.HandlePanic()
  604. aws.RIDataRunning = true
  605. for {
  606. klog.Infof("Reserved Instance watcher running... next update in 1h")
  607. time.Sleep(time.Hour)
  608. err := aws.GetReservationDataFromAthena()
  609. if err != nil {
  610. klog.Infof("Error updating RI data: %s", err.Error())
  611. }
  612. }
  613. }()
  614. }
  615. }
  616. if !aws.SavingsPlanDataRunning && c.AthenaBucketName != "" {
  617. err = aws.GetSavingsPlanDataFromAthena()
  618. if err != nil {
  619. klog.V(1).Infof("Failed to lookup savings plan data: %s", err.Error())
  620. } else {
  621. go func() {
  622. defer errors.HandlePanic()
  623. aws.SavingsPlanDataRunning = true
  624. for {
  625. klog.Infof("Savings Plan watcher running... next update in 1h")
  626. time.Sleep(time.Hour)
  627. err := aws.GetSavingsPlanDataFromAthena()
  628. if err != nil {
  629. klog.Infof("Error updating Savings Plan data: %s", err.Error())
  630. }
  631. }
  632. }()
  633. }
  634. }
  635. aws.Pricing = make(map[string]*AWSProductTerms)
  636. aws.ValidPricingKeys = make(map[string]bool)
  637. skusToKeys := make(map[string]string)
  638. resp, pricingURL, err := aws.getRegionPricing(nodeList)
  639. if err != nil {
  640. return err
  641. }
  642. dec := json.NewDecoder(resp.Body)
  643. for {
  644. t, err := dec.Token()
  645. if err == io.EOF {
  646. klog.V(2).Infof("done loading \"%s\"\n", pricingURL)
  647. break
  648. }
  649. if t == "products" {
  650. _, err := dec.Token() // this should parse the opening "{""
  651. if err != nil {
  652. return err
  653. }
  654. for dec.More() {
  655. _, err := dec.Token() // the sku token
  656. if err != nil {
  657. return err
  658. }
  659. product := &AWSProduct{}
  660. err = dec.Decode(&product)
  661. if err != nil {
  662. klog.V(1).Infof("Error parsing response from \"%s\": %v", pricingURL, err.Error())
  663. break
  664. }
  665. if product.Attributes.PreInstalledSw == "NA" &&
  666. (strings.HasPrefix(product.Attributes.UsageType, "BoxUsage") || strings.Contains(product.Attributes.UsageType, "-BoxUsage")) &&
  667. product.Attributes.CapacityStatus == "Used" {
  668. key := aws.KubeAttrConversion(product.Attributes.Location, product.Attributes.InstanceType, product.Attributes.OperatingSystem)
  669. spotKey := key + ",preemptible"
  670. if inputkeys[key] || inputkeys[spotKey] { // Just grab the sku even if spot, and change the price later.
  671. productTerms := &AWSProductTerms{
  672. Sku: product.Sku,
  673. Memory: product.Attributes.Memory,
  674. Storage: product.Attributes.Storage,
  675. VCpu: product.Attributes.VCpu,
  676. GPU: product.Attributes.GPU,
  677. }
  678. aws.Pricing[key] = productTerms
  679. aws.Pricing[spotKey] = productTerms
  680. skusToKeys[product.Sku] = key
  681. }
  682. aws.ValidPricingKeys[key] = true
  683. aws.ValidPricingKeys[spotKey] = true
  684. } else if strings.Contains(product.Attributes.UsageType, "EBS:Volume") {
  685. // UsageTypes may be prefixed with a region code - we're removing this when using
  686. // volTypes to keep lookups generic
  687. usageTypeRegx := regexp.MustCompile(".*(-|^)(EBS.+)")
  688. usageTypeMatch := usageTypeRegx.FindStringSubmatch(product.Attributes.UsageType)
  689. usageTypeNoRegion := usageTypeMatch[len(usageTypeMatch)-1]
  690. key := locationToRegion[product.Attributes.Location] + "," + usageTypeNoRegion
  691. spotKey := key + ",preemptible"
  692. pv := &PV{
  693. Class: volTypes[usageTypeNoRegion],
  694. Region: locationToRegion[product.Attributes.Location],
  695. }
  696. productTerms := &AWSProductTerms{
  697. Sku: product.Sku,
  698. PV: pv,
  699. }
  700. aws.Pricing[key] = productTerms
  701. aws.Pricing[spotKey] = productTerms
  702. skusToKeys[product.Sku] = key
  703. aws.ValidPricingKeys[key] = true
  704. aws.ValidPricingKeys[spotKey] = true
  705. }
  706. }
  707. }
  708. if t == "terms" {
  709. _, err := dec.Token() // this should parse the opening "{""
  710. if err != nil {
  711. return err
  712. }
  713. termType, err := dec.Token()
  714. if err != nil {
  715. return err
  716. }
  717. if termType == "OnDemand" {
  718. _, err := dec.Token()
  719. if err != nil { // again, should parse an opening "{"
  720. return err
  721. }
  722. for dec.More() {
  723. sku, err := dec.Token()
  724. if err != nil {
  725. return err
  726. }
  727. _, err = dec.Token() // another opening "{"
  728. if err != nil {
  729. return err
  730. }
  731. skuOnDemand, err := dec.Token()
  732. if err != nil {
  733. return err
  734. }
  735. offerTerm := &AWSOfferTerm{}
  736. err = dec.Decode(&offerTerm)
  737. if err != nil {
  738. klog.V(1).Infof("Error decoding AWS Offer Term: " + err.Error())
  739. }
  740. if sku.(string)+OnDemandRateCode == skuOnDemand {
  741. key, ok := skusToKeys[sku.(string)]
  742. spotKey := key + ",preemptible"
  743. if ok {
  744. aws.Pricing[key].OnDemand = offerTerm
  745. aws.Pricing[spotKey].OnDemand = offerTerm
  746. if strings.Contains(key, "EBS:VolumeP-IOPS.piops") {
  747. // If the specific UsageType is the per IO cost used on io1 volumes
  748. // we need to add the per IO cost to the io1 PV cost
  749. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  750. // Add the per IO cost to the PV object for the io1 volume type
  751. aws.Pricing[key].PV.CostPerIO = cost
  752. } else if strings.Contains(key, "EBS:Volume") {
  753. // If volume, we need to get hourly cost and add it to the PV object
  754. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  755. costFloat, _ := strconv.ParseFloat(cost, 64)
  756. hourlyPrice := costFloat / 730
  757. aws.Pricing[key].PV.Cost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  758. }
  759. }
  760. }
  761. _, err = dec.Token()
  762. if err != nil {
  763. return err
  764. }
  765. }
  766. _, err = dec.Token()
  767. if err != nil {
  768. return err
  769. }
  770. }
  771. }
  772. }
  773. klog.V(2).Infof("Finished downloading \"%s\"", pricingURL)
  774. // Always run spot pricing refresh when performing download
  775. aws.refreshSpotPricing(true)
  776. // Only start a single refresh goroutine
  777. if !aws.SpotRefreshRunning {
  778. aws.SpotRefreshRunning = true
  779. go func() {
  780. defer errors.HandlePanic()
  781. for {
  782. klog.Infof("Spot Pricing Refresh scheduled in %.2f minutes.", SpotRefreshDuration.Minutes())
  783. time.Sleep(SpotRefreshDuration)
  784. // Reoccurring refresh checks update times
  785. aws.refreshSpotPricing(false)
  786. }
  787. }()
  788. }
  789. return nil
  790. }
  791. func (aws *AWS) refreshSpotPricing(force bool) {
  792. aws.SpotPricingLock.Lock()
  793. defer aws.SpotPricingLock.Unlock()
  794. now := time.Now().UTC()
  795. updateTime := now.Add(-SpotRefreshDuration)
  796. // Return if there was an update time set and an hour hasn't elapsed
  797. if !force && aws.SpotPricingUpdatedAt != nil && aws.SpotPricingUpdatedAt.After(updateTime) {
  798. return
  799. }
  800. sp, err := aws.parseSpotData(aws.SpotDataBucket, aws.SpotDataPrefix, aws.ProjectID, aws.SpotDataRegion)
  801. if err != nil {
  802. klog.V(1).Infof("Skipping AWS spot data download: %s", err.Error())
  803. aws.SpotPricingStatus = err.Error()
  804. return
  805. }
  806. aws.SpotPricingStatus = ""
  807. // update time last updated
  808. aws.SpotPricingUpdatedAt = &now
  809. aws.SpotPricingByInstanceID = sp
  810. }
  811. // Stubbed NetworkPricing for AWS. Pull directly from aws.json for now
  812. func (aws *AWS) NetworkPricing() (*Network, error) {
  813. cpricing, err := aws.Config.GetCustomPricingData()
  814. if err != nil {
  815. return nil, err
  816. }
  817. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  818. if err != nil {
  819. return nil, err
  820. }
  821. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  822. if err != nil {
  823. return nil, err
  824. }
  825. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  826. if err != nil {
  827. return nil, err
  828. }
  829. return &Network{
  830. ZoneNetworkEgressCost: znec,
  831. RegionNetworkEgressCost: rnec,
  832. InternetNetworkEgressCost: inec,
  833. }, nil
  834. }
  835. func (aws *AWS) LoadBalancerPricing() (*LoadBalancer, error) {
  836. fffrc := 0.025
  837. afrc := 0.010
  838. lbidc := 0.008
  839. numForwardingRules := 1.0
  840. dataIngressGB := 0.0
  841. var totalCost float64
  842. if numForwardingRules < 5 {
  843. totalCost = fffrc*numForwardingRules + lbidc*dataIngressGB
  844. } else {
  845. totalCost = fffrc*5 + afrc*(numForwardingRules-5) + lbidc*dataIngressGB
  846. }
  847. return &LoadBalancer{
  848. Cost: totalCost,
  849. }, nil
  850. }
  851. // AllNodePricing returns all the billing data fetched.
  852. func (aws *AWS) AllNodePricing() (interface{}, error) {
  853. aws.DownloadPricingDataLock.RLock()
  854. defer aws.DownloadPricingDataLock.RUnlock()
  855. return aws.Pricing, nil
  856. }
  857. func (aws *AWS) spotPricing(instanceID string) (*spotInfo, bool) {
  858. aws.SpotPricingLock.RLock()
  859. defer aws.SpotPricingLock.RUnlock()
  860. info, ok := aws.SpotPricingByInstanceID[instanceID]
  861. return info, ok
  862. }
  863. func (aws *AWS) reservedInstancePricing(instanceID string) (*RIData, bool) {
  864. aws.RIDataLock.RLock()
  865. defer aws.RIDataLock.RUnlock()
  866. data, ok := aws.RIPricingByInstanceID[instanceID]
  867. return data, ok
  868. }
  869. func (aws *AWS) savingsPlanPricing(instanceID string) (*SavingsPlanData, bool) {
  870. aws.SavingsPlanDataLock.RLock()
  871. defer aws.SavingsPlanDataLock.RUnlock()
  872. data, ok := aws.SavingsPlanDataByInstanceID[instanceID]
  873. return data, ok
  874. }
  875. func (aws *AWS) createNode(terms *AWSProductTerms, usageType string, k Key) (*Node, error) {
  876. key := k.Features()
  877. if spotInfo, ok := aws.spotPricing(k.ID()); ok {
  878. var spotcost string
  879. log.DedupedInfof(5, "Looking up spot data from feed for node %s", k.ID())
  880. arr := strings.Split(spotInfo.Charge, " ")
  881. if len(arr) == 2 {
  882. spotcost = arr[0]
  883. } else {
  884. klog.V(2).Infof("Spot data for node %s is missing", k.ID())
  885. }
  886. return &Node{
  887. Cost: spotcost,
  888. VCPU: terms.VCpu,
  889. RAM: terms.Memory,
  890. GPU: terms.GPU,
  891. Storage: terms.Storage,
  892. BaseCPUPrice: aws.BaseCPUPrice,
  893. BaseRAMPrice: aws.BaseRAMPrice,
  894. BaseGPUPrice: aws.BaseGPUPrice,
  895. UsageType: PreemptibleType,
  896. }, nil
  897. } else if aws.isPreemptible(key) { // Preemptible but we don't have any data in the pricing report.
  898. log.DedupedWarningf(5, "Node %s marked preemptible but we have no data in spot feed", k.ID())
  899. return &Node{
  900. VCPU: terms.VCpu,
  901. VCPUCost: aws.BaseSpotCPUPrice,
  902. RAM: terms.Memory,
  903. GPU: terms.GPU,
  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. }