awsprovider.go 65 KB

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