awsprovider.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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. "net/url"
  12. "os"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. "k8s.io/klog"
  19. "github.com/kubecost/cost-model/pkg/clustercache"
  20. "github.com/aws/aws-sdk-go/aws"
  21. "github.com/aws/aws-sdk-go/aws/awserr"
  22. "github.com/aws/aws-sdk-go/aws/session"
  23. "github.com/aws/aws-sdk-go/service/athena"
  24. "github.com/aws/aws-sdk-go/service/ec2"
  25. "github.com/aws/aws-sdk-go/service/s3"
  26. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  27. "github.com/jszwec/csvutil"
  28. v1 "k8s.io/api/core/v1"
  29. )
  30. const awsAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  31. const awsAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
  32. const awsReservedInstancePricePerHour = 0.0287
  33. const supportedSpotFeedVersion = "1"
  34. const SpotInfoUpdateType = "spotinfo"
  35. const AthenaInfoUpdateType = "athenainfo"
  36. // AWS represents an Amazon Provider
  37. type AWS struct {
  38. Pricing map[string]*AWSProductTerms
  39. SpotPricingByInstanceID map[string]*spotInfo
  40. ValidPricingKeys map[string]bool
  41. Clientset clustercache.ClusterCache
  42. BaseCPUPrice string
  43. BaseRAMPrice string
  44. BaseGPUPrice string
  45. BaseSpotCPUPrice string
  46. BaseSpotRAMPrice string
  47. SpotLabelName string
  48. SpotLabelValue string
  49. ServiceKeyName string
  50. ServiceKeySecret string
  51. SpotDataRegion string
  52. SpotDataBucket string
  53. SpotDataPrefix string
  54. ProjectID string
  55. DownloadPricingDataLock sync.RWMutex
  56. ReservedInstances []*AWSReservedInstance
  57. Config *ProviderConfig
  58. *CustomProvider
  59. }
  60. // AWSPricing maps a k8s node to an AWS Pricing "product"
  61. type AWSPricing struct {
  62. Products map[string]*AWSProduct `json:"products"`
  63. Terms AWSPricingTerms `json:"terms"`
  64. }
  65. // AWSProduct represents a purchased SKU
  66. type AWSProduct struct {
  67. Sku string `json:"sku"`
  68. Attributes AWSProductAttributes `json:"attributes"`
  69. }
  70. // AWSProductAttributes represents metadata about the product used to map to a node.
  71. type AWSProductAttributes struct {
  72. Location string `json:"location"`
  73. InstanceType string `json:"instanceType"`
  74. Memory string `json:"memory"`
  75. Storage string `json:"storage"`
  76. VCpu string `json:"vcpu"`
  77. UsageType string `json:"usagetype"`
  78. OperatingSystem string `json:"operatingSystem"`
  79. PreInstalledSw string `json:"preInstalledSw"`
  80. InstanceFamily string `json:"instanceFamily"`
  81. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  82. }
  83. // AWSPricingTerms are how you pay for the node: OnDemand, Reserved, or (TODO) Spot
  84. type AWSPricingTerms struct {
  85. OnDemand map[string]map[string]*AWSOfferTerm `json:"OnDemand"`
  86. Reserved map[string]map[string]*AWSOfferTerm `json:"Reserved"`
  87. }
  88. // AWSOfferTerm is a sku extension used to pay for the node.
  89. type AWSOfferTerm struct {
  90. Sku string `json:"sku"`
  91. PriceDimensions map[string]*AWSRateCode `json:"priceDimensions"`
  92. }
  93. // AWSRateCode encodes data about the price of a product
  94. type AWSRateCode struct {
  95. Unit string `json:"unit"`
  96. PricePerUnit AWSCurrencyCode `json:"pricePerUnit"`
  97. }
  98. // AWSCurrencyCode is the localized currency. (TODO: support non-USD)
  99. type AWSCurrencyCode struct {
  100. USD string `json:"USD"`
  101. }
  102. // AWSProductTerms represents the full terms of the product
  103. type AWSProductTerms struct {
  104. Sku string `json:"sku"`
  105. OnDemand *AWSOfferTerm `json:"OnDemand"`
  106. Reserved *AWSOfferTerm `json:"Reserved"`
  107. Memory string `json:"memory"`
  108. Storage string `json:"storage"`
  109. VCpu string `json:"vcpu"`
  110. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  111. PV *PV `json:"pv"`
  112. }
  113. // ClusterIdEnvVar is the environment variable in which one can manually set the ClusterId
  114. const ClusterIdEnvVar = "AWS_CLUSTER_ID"
  115. // OnDemandRateCode is appended to an node sku
  116. const OnDemandRateCode = ".JRTCKXETXF"
  117. // ReservedRateCode is appended to a node sku
  118. const ReservedRateCode = ".38NPMPTW36"
  119. // HourlyRateCode is appended to a node sku
  120. const HourlyRateCode = ".6YS6EN2CT7"
  121. // volTypes are used to map between AWS UsageTypes and
  122. // EBS volume types, as they would appear in K8s storage class
  123. // name and the EC2 API.
  124. var volTypes = map[string]string{
  125. "EBS:VolumeUsage.gp2": "gp2",
  126. "EBS:VolumeUsage": "standard",
  127. "EBS:VolumeUsage.sc1": "sc1",
  128. "EBS:VolumeP-IOPS.piops": "io1",
  129. "EBS:VolumeUsage.st1": "st1",
  130. "EBS:VolumeUsage.piops": "io1",
  131. "gp2": "EBS:VolumeUsage.gp2",
  132. "standard": "EBS:VolumeUsage",
  133. "sc1": "EBS:VolumeUsage.sc1",
  134. "io1": "EBS:VolumeUsage.piops",
  135. "st1": "EBS:VolumeUsage.st1",
  136. }
  137. // locationToRegion maps AWS region names (As they come from Billing)
  138. // to actual region identifiers
  139. var locationToRegion = map[string]string{
  140. "US East (Ohio)": "us-east-2",
  141. "US East (N. Virginia)": "us-east-1",
  142. "US West (N. California)": "us-west-1",
  143. "US West (Oregon)": "us-west-2",
  144. "Asia Pacific (Hong Kong)": "ap-east-1",
  145. "Asia Pacific (Mumbai)": "ap-south-1",
  146. "Asia Pacific (Osaka-Local)": "ap-northeast-3",
  147. "Asia Pacific (Seoul)": "ap-northeast-2",
  148. "Asia Pacific (Singapore)": "ap-southeast-1",
  149. "Asia Pacific (Sydney)": "ap-southeast-2",
  150. "Asia Pacific (Tokyo)": "ap-northeast-1",
  151. "Canada (Central)": "ca-central-1",
  152. "China (Beijing)": "cn-north-1",
  153. "China (Ningxia)": "cn-northwest-1",
  154. "EU (Frankfurt)": "eu-central-1",
  155. "EU (Ireland)": "eu-west-1",
  156. "EU (London)": "eu-west-2",
  157. "EU (Paris)": "eu-west-3",
  158. "EU (Stockholm)": "eu-north-1",
  159. "South America (Sao Paulo)": "sa-east-1",
  160. "AWS GovCloud (US-East)": "us-gov-east-1",
  161. "AWS GovCloud (US)": "us-gov-west-1",
  162. }
  163. var regionToBillingRegionCode = map[string]string{
  164. "us-east-2": "USE2",
  165. "us-east-1": "",
  166. "us-west-1": "USW1",
  167. "us-west-2": "USW2",
  168. "ap-east-1": "APE1",
  169. "ap-south-1": "APS3",
  170. "ap-northeast-3": "APN3",
  171. "ap-northeast-2": "APN2",
  172. "ap-southeast-1": "APS1",
  173. "ap-southeast-2": "APS2",
  174. "ap-northeast-1": "APN1",
  175. "ca-central-1": "CAN1",
  176. "cn-north-1": "",
  177. "cn-northwest-1": "",
  178. "eu-central-1": "EUC1",
  179. "eu-west-1": "EU",
  180. "eu-west-2": "EUW2",
  181. "eu-west-3": "EUW3",
  182. "eu-north-1": "EUN1",
  183. "sa-east-1": "SAE1",
  184. "us-gov-east-1": "UGE1",
  185. "us-gov-west-1": "UGW1",
  186. }
  187. func (aws *AWS) GetLocalStorageQuery(window, offset string, rate bool) string {
  188. return ""
  189. }
  190. // KubeAttrConversion maps the k8s labels for region to an aws region
  191. func (aws *AWS) KubeAttrConversion(location, instanceType, operatingSystem string) string {
  192. operatingSystem = strings.ToLower(operatingSystem)
  193. region := locationToRegion[location]
  194. return region + "," + instanceType + "," + operatingSystem
  195. }
  196. type AwsSpotFeedInfo struct {
  197. BucketName string `json:"bucketName"`
  198. Prefix string `json:"prefix"`
  199. Region string `json:"region"`
  200. AccountID string `json:"projectID"`
  201. ServiceKeyName string `json:"serviceKeyName"`
  202. ServiceKeySecret string `json:"serviceKeySecret"`
  203. SpotLabel string `json:"spotLabel"`
  204. SpotLabelValue string `json:"spotLabelValue"`
  205. }
  206. type AwsAthenaInfo struct {
  207. AthenaBucketName string `json:"athenaBucketName"`
  208. AthenaRegion string `json:"athenaRegion"`
  209. AthenaDatabase string `json:"athenaDatabase"`
  210. AthenaTable string `json:"athenaTable"`
  211. ServiceKeyName string `json:"serviceKeyName"`
  212. ServiceKeySecret string `json:"serviceKeySecret"`
  213. AccountID string `json:"projectID"`
  214. }
  215. func (aws *AWS) GetManagementPlatform() (string, error) {
  216. nodes := aws.Clientset.GetAllNodes()
  217. if len(nodes) > 0 {
  218. n := nodes[0]
  219. version := n.Status.NodeInfo.KubeletVersion
  220. if strings.Contains(version, "eks") {
  221. return "eks", nil
  222. }
  223. if _, ok := n.Labels["kops.k8s.io/instancegroup"]; ok {
  224. return "kops", nil
  225. }
  226. }
  227. return "", nil
  228. }
  229. func (aws *AWS) GetConfig() (*CustomPricing, error) {
  230. c, err := aws.Config.GetCustomPricingData()
  231. if c.Discount == "" {
  232. c.Discount = "0%"
  233. }
  234. if c.NegotiatedDiscount == "" {
  235. c.NegotiatedDiscount = "0%"
  236. }
  237. if err != nil {
  238. return nil, err
  239. }
  240. return c, nil
  241. }
  242. func (aws *AWS) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  243. return aws.Config.UpdateFromMap(a)
  244. }
  245. func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  246. return aws.Config.Update(func(c *CustomPricing) error {
  247. if updateType == SpotInfoUpdateType {
  248. a := AwsSpotFeedInfo{}
  249. err := json.NewDecoder(r).Decode(&a)
  250. if err != nil {
  251. return err
  252. }
  253. c.ServiceKeyName = a.ServiceKeyName
  254. c.ServiceKeySecret = a.ServiceKeySecret
  255. c.SpotDataPrefix = a.Prefix
  256. c.SpotDataBucket = a.BucketName
  257. c.ProjectID = a.AccountID
  258. c.SpotDataRegion = a.Region
  259. c.SpotLabel = a.SpotLabel
  260. c.SpotLabelValue = a.SpotLabelValue
  261. } else if updateType == AthenaInfoUpdateType {
  262. a := AwsAthenaInfo{}
  263. err := json.NewDecoder(r).Decode(&a)
  264. if err != nil {
  265. return err
  266. }
  267. c.AthenaBucketName = a.AthenaBucketName
  268. c.AthenaRegion = a.AthenaRegion
  269. c.AthenaDatabase = a.AthenaDatabase
  270. c.AthenaTable = a.AthenaTable
  271. c.ServiceKeyName = a.ServiceKeyName
  272. c.ServiceKeySecret = a.ServiceKeySecret
  273. c.AthenaProjectID = a.AccountID
  274. } else {
  275. a := make(map[string]interface{})
  276. err := json.NewDecoder(r).Decode(&a)
  277. if err != nil {
  278. return err
  279. }
  280. for k, v := range a {
  281. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  282. vstr, ok := v.(string)
  283. if ok {
  284. err := SetCustomPricingField(c, kUpper, vstr)
  285. if err != nil {
  286. return err
  287. }
  288. } else {
  289. sci := v.(map[string]interface{})
  290. sc := make(map[string]string)
  291. for k, val := range sci {
  292. sc[k] = val.(string)
  293. }
  294. c.SharedCosts = sc //todo: support reflection/multiple map fields
  295. }
  296. }
  297. }
  298. remoteEnabled := os.Getenv(remoteEnabled)
  299. if remoteEnabled == "true" {
  300. err := UpdateClusterMeta(os.Getenv(clusterIDKey), c.ClusterName)
  301. if err != nil {
  302. return err
  303. }
  304. }
  305. return nil
  306. })
  307. }
  308. type awsKey struct {
  309. SpotLabelName string
  310. SpotLabelValue string
  311. Labels map[string]string
  312. ProviderID string
  313. }
  314. func (k *awsKey) GPUType() string {
  315. return ""
  316. }
  317. func (k *awsKey) ID() string {
  318. provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)") // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
  319. for matchNum, group := range provIdRx.FindStringSubmatch(k.ProviderID) {
  320. if matchNum == 2 {
  321. return group
  322. }
  323. }
  324. klog.V(3).Infof("Could not find instance ID in \"%s\"", k.ProviderID)
  325. return ""
  326. }
  327. func (k *awsKey) Features() string {
  328. instanceType := k.Labels[v1.LabelInstanceType]
  329. var operatingSystem string
  330. operatingSystem, ok := k.Labels[v1.LabelOSStable]
  331. if !ok {
  332. operatingSystem = k.Labels["beta.kubernetes.io/os"]
  333. }
  334. region := k.Labels[v1.LabelZoneRegion]
  335. key := region + "," + instanceType + "," + operatingSystem
  336. usageType := "preemptible"
  337. spotKey := key + "," + usageType
  338. if l, ok := k.Labels["lifecycle"]; ok && l == "EC2Spot" {
  339. return spotKey
  340. }
  341. if l, ok := k.Labels[k.SpotLabelName]; ok && l == k.SpotLabelValue {
  342. return spotKey
  343. }
  344. return key
  345. }
  346. func (aws *AWS) PVPricing(pvk PVKey) (*PV, error) {
  347. pricing, ok := aws.Pricing[pvk.Features()]
  348. if !ok {
  349. klog.V(4).Infof("Persistent Volume pricing not found for %s: %s", pvk.GetStorageClass(), pvk.Features())
  350. return &PV{}, nil
  351. }
  352. return pricing.PV, nil
  353. }
  354. type awsPVKey struct {
  355. Labels map[string]string
  356. StorageClassParameters map[string]string
  357. StorageClassName string
  358. Name string
  359. DefaultRegion string
  360. }
  361. func (aws *AWS) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  362. return &awsPVKey{
  363. Labels: pv.Labels,
  364. StorageClassName: pv.Spec.StorageClassName,
  365. StorageClassParameters: parameters,
  366. Name: pv.Name,
  367. DefaultRegion: defaultRegion,
  368. }
  369. }
  370. func (key *awsPVKey) GetStorageClass() string {
  371. return key.StorageClassName
  372. }
  373. func (key *awsPVKey) Features() string {
  374. storageClass := key.StorageClassParameters["type"]
  375. if storageClass == "standard" {
  376. storageClass = "gp2"
  377. }
  378. // Storage class names are generally EBS volume types (gp2)
  379. // Keys in Pricing are based on UsageTypes (EBS:VolumeType.gp2)
  380. // Converts between the 2
  381. region := key.Labels[v1.LabelZoneRegion]
  382. //if region == "" {
  383. // region = "us-east-1"
  384. //}
  385. class, ok := volTypes[storageClass]
  386. if !ok {
  387. klog.V(4).Infof("No voltype mapping for %s's storageClass: %s", key.Name, storageClass)
  388. }
  389. return region + "," + class
  390. }
  391. // GetKey maps node labels to information needed to retrieve pricing data
  392. func (aws *AWS) GetKey(labels map[string]string) Key {
  393. return &awsKey{
  394. SpotLabelName: aws.SpotLabelName,
  395. SpotLabelValue: aws.SpotLabelValue,
  396. Labels: labels,
  397. ProviderID: labels["providerID"],
  398. }
  399. }
  400. func (aws *AWS) isPreemptible(key string) bool {
  401. s := strings.Split(key, ",")
  402. if len(s) == 4 && s[3] == "preemptible" {
  403. return true
  404. }
  405. return false
  406. }
  407. // DownloadPricingData fetches data from the AWS Pricing API
  408. func (aws *AWS) DownloadPricingData() error {
  409. aws.DownloadPricingDataLock.Lock()
  410. defer aws.DownloadPricingDataLock.Unlock()
  411. c, err := aws.Config.GetCustomPricingData()
  412. if err != nil {
  413. klog.V(1).Infof("Error downloading default pricing data: %s", err.Error())
  414. }
  415. aws.BaseCPUPrice = c.CPU
  416. aws.BaseRAMPrice = c.RAM
  417. aws.BaseGPUPrice = c.GPU
  418. aws.BaseSpotCPUPrice = c.SpotCPU
  419. aws.BaseSpotRAMPrice = c.SpotRAM
  420. aws.SpotLabelName = c.SpotLabel
  421. aws.SpotLabelValue = c.SpotLabelValue
  422. aws.SpotDataBucket = c.SpotDataBucket
  423. aws.SpotDataPrefix = c.SpotDataPrefix
  424. aws.ProjectID = c.ProjectID
  425. aws.SpotDataRegion = c.SpotDataRegion
  426. aws.ServiceKeyName = c.ServiceKeyName
  427. aws.ServiceKeySecret = c.ServiceKeySecret
  428. if len(aws.SpotDataBucket) != 0 && len(aws.ProjectID) == 0 {
  429. klog.V(1).Infof("using SpotDataBucket \"%s\" without ProjectID will not end well", aws.SpotDataBucket)
  430. }
  431. nodeList := aws.Clientset.GetAllNodes()
  432. inputkeys := make(map[string]bool)
  433. for _, n := range nodeList {
  434. labels := n.GetObjectMeta().GetLabels()
  435. key := aws.GetKey(labels)
  436. inputkeys[key.Features()] = true
  437. }
  438. pvList := aws.Clientset.GetAllPersistentVolumes()
  439. storageClasses := aws.Clientset.GetAllStorageClasses()
  440. storageClassMap := make(map[string]map[string]string)
  441. for _, storageClass := range storageClasses {
  442. params := storageClass.Parameters
  443. storageClassMap[storageClass.ObjectMeta.Name] = params
  444. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  445. storageClassMap["default"] = params
  446. storageClassMap[""] = params
  447. }
  448. }
  449. pvkeys := make(map[string]PVKey)
  450. for _, pv := range pvList {
  451. params, ok := storageClassMap[pv.Spec.StorageClassName]
  452. if !ok {
  453. klog.V(2).Infof("Unable to find params for storageClassName %s, falling back to default pricing", pv.Spec.StorageClassName)
  454. continue
  455. }
  456. key := aws.GetPVKey(pv, params, "")
  457. pvkeys[key.Features()] = key
  458. }
  459. reserved, err := aws.getReservedInstances()
  460. if err != nil {
  461. klog.V(1).Infof("Failed to lookup reserved instance data: %s", err.Error())
  462. } else {
  463. klog.V(1).Infof("Found %d reserved instances", len(reserved))
  464. aws.ReservedInstances = reserved
  465. for _, r := range reserved {
  466. klog.V(1).Infof("%s", r)
  467. }
  468. }
  469. aws.Pricing = make(map[string]*AWSProductTerms)
  470. aws.ValidPricingKeys = make(map[string]bool)
  471. skusToKeys := make(map[string]string)
  472. pricingURL := "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.json"
  473. klog.V(2).Infof("starting download of \"%s\", which is quite large ...", pricingURL)
  474. resp, err := http.Get(pricingURL)
  475. if err != nil {
  476. klog.V(2).Infof("Bogus fetch of \"%s\": %v", pricingURL, err)
  477. return err
  478. }
  479. klog.V(2).Infof("Finished downloading \"%s\"", pricingURL)
  480. dec := json.NewDecoder(resp.Body)
  481. for {
  482. t, err := dec.Token()
  483. if err == io.EOF {
  484. klog.V(2).Infof("done loading \"%s\"\n", pricingURL)
  485. break
  486. }
  487. if t == "products" {
  488. _, err := dec.Token() // this should parse the opening "{""
  489. if err != nil {
  490. return err
  491. }
  492. for dec.More() {
  493. _, err := dec.Token() // the sku token
  494. if err != nil {
  495. return err
  496. }
  497. product := &AWSProduct{}
  498. err = dec.Decode(&product)
  499. if err != nil {
  500. klog.V(1).Infof("Error parsing response from \"%s\": %v", pricingURL, err.Error())
  501. break
  502. }
  503. if product.Attributes.PreInstalledSw == "NA" &&
  504. (strings.HasPrefix(product.Attributes.UsageType, "BoxUsage") || strings.Contains(product.Attributes.UsageType, "-BoxUsage")) {
  505. key := aws.KubeAttrConversion(product.Attributes.Location, product.Attributes.InstanceType, product.Attributes.OperatingSystem)
  506. spotKey := key + ",preemptible"
  507. if inputkeys[key] || inputkeys[spotKey] { // Just grab the sku even if spot, and change the price later.
  508. productTerms := &AWSProductTerms{
  509. Sku: product.Sku,
  510. Memory: product.Attributes.Memory,
  511. Storage: product.Attributes.Storage,
  512. VCpu: product.Attributes.VCpu,
  513. GPU: product.Attributes.GPU,
  514. }
  515. aws.Pricing[key] = productTerms
  516. aws.Pricing[spotKey] = productTerms
  517. skusToKeys[product.Sku] = key
  518. }
  519. aws.ValidPricingKeys[key] = true
  520. aws.ValidPricingKeys[spotKey] = true
  521. } else if strings.Contains(product.Attributes.UsageType, "EBS:Volume") {
  522. // UsageTypes may be prefixed with a region code - we're removing this when using
  523. // volTypes to keep lookups generic
  524. usageTypeRegx := regexp.MustCompile(".*(-|^)(EBS.+)")
  525. usageTypeMatch := usageTypeRegx.FindStringSubmatch(product.Attributes.UsageType)
  526. usageTypeNoRegion := usageTypeMatch[len(usageTypeMatch)-1]
  527. key := locationToRegion[product.Attributes.Location] + "," + usageTypeNoRegion
  528. spotKey := key + ",preemptible"
  529. pv := &PV{
  530. Class: volTypes[usageTypeNoRegion],
  531. Region: locationToRegion[product.Attributes.Location],
  532. }
  533. productTerms := &AWSProductTerms{
  534. Sku: product.Sku,
  535. PV: pv,
  536. }
  537. aws.Pricing[key] = productTerms
  538. aws.Pricing[spotKey] = productTerms
  539. skusToKeys[product.Sku] = key
  540. aws.ValidPricingKeys[key] = true
  541. aws.ValidPricingKeys[spotKey] = true
  542. }
  543. }
  544. }
  545. if t == "terms" {
  546. _, err := dec.Token() // this should parse the opening "{""
  547. if err != nil {
  548. return err
  549. }
  550. termType, err := dec.Token()
  551. if err != nil {
  552. return err
  553. }
  554. if termType == "OnDemand" {
  555. _, err := dec.Token()
  556. if err != nil { // again, should parse an opening "{"
  557. return err
  558. }
  559. for dec.More() {
  560. sku, err := dec.Token()
  561. if err != nil {
  562. return err
  563. }
  564. _, err = dec.Token() // another opening "{"
  565. if err != nil {
  566. return err
  567. }
  568. skuOnDemand, err := dec.Token()
  569. if err != nil {
  570. return err
  571. }
  572. offerTerm := &AWSOfferTerm{}
  573. err = dec.Decode(&offerTerm)
  574. if err != nil {
  575. klog.V(1).Infof("Error decoding AWS Offer Term: " + err.Error())
  576. }
  577. if sku.(string)+OnDemandRateCode == skuOnDemand {
  578. key, ok := skusToKeys[sku.(string)]
  579. spotKey := key + ",preemptible"
  580. if ok {
  581. aws.Pricing[key].OnDemand = offerTerm
  582. aws.Pricing[spotKey].OnDemand = offerTerm
  583. if strings.Contains(key, "EBS:VolumeP-IOPS.piops") {
  584. // If the specific UsageType is the per IO cost used on io1 volumes
  585. // we need to add the per IO cost to the io1 PV cost
  586. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  587. // Add the per IO cost to the PV object for the io1 volume type
  588. aws.Pricing[key].PV.CostPerIO = cost
  589. } else if strings.Contains(key, "EBS:Volume") {
  590. // If volume, we need to get hourly cost and add it to the PV object
  591. cost := offerTerm.PriceDimensions[sku.(string)+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
  592. costFloat, _ := strconv.ParseFloat(cost, 64)
  593. hourlyPrice := costFloat / 730
  594. aws.Pricing[key].PV.Cost = strconv.FormatFloat(hourlyPrice, 'f', -1, 64)
  595. }
  596. }
  597. }
  598. _, err = dec.Token()
  599. if err != nil {
  600. return err
  601. }
  602. }
  603. _, err = dec.Token()
  604. if err != nil {
  605. return err
  606. }
  607. }
  608. }
  609. }
  610. sp, err := parseSpotData(aws.SpotDataBucket, aws.SpotDataPrefix, aws.ProjectID, aws.SpotDataRegion, aws.ServiceKeyName, aws.ServiceKeySecret)
  611. if err != nil {
  612. klog.V(1).Infof("Skipping AWS spot data download: %s", err.Error())
  613. } else {
  614. aws.SpotPricingByInstanceID = sp
  615. }
  616. return nil
  617. }
  618. // Stubbed NetworkPricing for AWS. Pull directly from aws.json for now
  619. func (aws *AWS) NetworkPricing() (*Network, error) {
  620. cpricing, err := aws.Config.GetCustomPricingData()
  621. if err != nil {
  622. return nil, err
  623. }
  624. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  625. if err != nil {
  626. return nil, err
  627. }
  628. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  629. if err != nil {
  630. return nil, err
  631. }
  632. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  633. if err != nil {
  634. return nil, err
  635. }
  636. return &Network{
  637. ZoneNetworkEgressCost: znec,
  638. RegionNetworkEgressCost: rnec,
  639. InternetNetworkEgressCost: inec,
  640. }, nil
  641. }
  642. // AllNodePricing returns all the billing data fetched.
  643. func (aws *AWS) AllNodePricing() (interface{}, error) {
  644. aws.DownloadPricingDataLock.RLock()
  645. defer aws.DownloadPricingDataLock.RUnlock()
  646. return aws.Pricing, nil
  647. }
  648. func (aws *AWS) createNode(terms *AWSProductTerms, usageType string, k Key) (*Node, error) {
  649. key := k.Features()
  650. if aws.isPreemptible(key) {
  651. if spotInfo, ok := aws.SpotPricingByInstanceID[k.ID()]; ok { // try and match directly to an ID for pricing. We'll still need the features
  652. var spotcost string
  653. arr := strings.Split(spotInfo.Charge, " ")
  654. if len(arr) == 2 {
  655. spotcost = arr[0]
  656. } else {
  657. klog.V(2).Infof("Spot data for node %s is missing", k.ID())
  658. }
  659. return &Node{
  660. Cost: spotcost,
  661. VCPU: terms.VCpu,
  662. RAM: terms.Memory,
  663. GPU: terms.GPU,
  664. Storage: terms.Storage,
  665. BaseCPUPrice: aws.BaseCPUPrice,
  666. BaseRAMPrice: aws.BaseRAMPrice,
  667. BaseGPUPrice: aws.BaseGPUPrice,
  668. UsageType: usageType,
  669. }, nil
  670. }
  671. return &Node{
  672. VCPU: terms.VCpu,
  673. VCPUCost: aws.BaseSpotCPUPrice,
  674. RAM: terms.Memory,
  675. GPU: terms.GPU,
  676. RAMCost: aws.BaseSpotRAMPrice,
  677. Storage: terms.Storage,
  678. BaseCPUPrice: aws.BaseCPUPrice,
  679. BaseRAMPrice: aws.BaseRAMPrice,
  680. BaseGPUPrice: aws.BaseGPUPrice,
  681. UsageType: usageType,
  682. }, nil
  683. }
  684. c, ok := terms.OnDemand.PriceDimensions[terms.Sku+OnDemandRateCode+HourlyRateCode]
  685. if !ok {
  686. return nil, fmt.Errorf("Could not fetch data for \"%s\"", k.ID())
  687. }
  688. cost := c.PricePerUnit.USD
  689. return &Node{
  690. Cost: cost,
  691. VCPU: terms.VCpu,
  692. RAM: terms.Memory,
  693. GPU: terms.GPU,
  694. Storage: terms.Storage,
  695. BaseCPUPrice: aws.BaseCPUPrice,
  696. BaseRAMPrice: aws.BaseRAMPrice,
  697. BaseGPUPrice: aws.BaseGPUPrice,
  698. UsageType: usageType,
  699. }, nil
  700. }
  701. // NodePricing takes in a key from GetKey and returns a Node object for use in building the cost model.
  702. func (aws *AWS) NodePricing(k Key) (*Node, error) {
  703. aws.DownloadPricingDataLock.RLock()
  704. defer aws.DownloadPricingDataLock.RUnlock()
  705. key := k.Features()
  706. usageType := "ondemand"
  707. if aws.isPreemptible(key) {
  708. usageType = "preemptible"
  709. }
  710. terms, ok := aws.Pricing[key]
  711. if ok {
  712. return aws.createNode(terms, usageType, k)
  713. } else if _, ok := aws.ValidPricingKeys[key]; ok {
  714. aws.DownloadPricingDataLock.RUnlock()
  715. err := aws.DownloadPricingData()
  716. aws.DownloadPricingDataLock.RLock()
  717. if err != nil {
  718. return &Node{
  719. Cost: aws.BaseCPUPrice,
  720. BaseCPUPrice: aws.BaseCPUPrice,
  721. BaseRAMPrice: aws.BaseRAMPrice,
  722. BaseGPUPrice: aws.BaseGPUPrice,
  723. UsageType: usageType,
  724. UsesBaseCPUPrice: true,
  725. }, err
  726. }
  727. terms, termsOk := aws.Pricing[key]
  728. if !termsOk {
  729. return &Node{
  730. Cost: aws.BaseCPUPrice,
  731. BaseCPUPrice: aws.BaseCPUPrice,
  732. BaseRAMPrice: aws.BaseRAMPrice,
  733. BaseGPUPrice: aws.BaseGPUPrice,
  734. UsageType: usageType,
  735. UsesBaseCPUPrice: true,
  736. }, fmt.Errorf("Unable to find any Pricing data for \"%s\"", key)
  737. }
  738. return aws.createNode(terms, usageType, k)
  739. } else { // Fall back to base pricing if we can't find the key.
  740. klog.V(1).Infof("Invalid Pricing Key \"%s\"", key)
  741. return &Node{
  742. Cost: aws.BaseCPUPrice,
  743. BaseCPUPrice: aws.BaseCPUPrice,
  744. BaseRAMPrice: aws.BaseRAMPrice,
  745. BaseGPUPrice: aws.BaseGPUPrice,
  746. UsageType: usageType,
  747. UsesBaseCPUPrice: true,
  748. }, nil
  749. }
  750. }
  751. // ClusterInfo returns an object that represents the cluster. TODO: actually return the name of the cluster. Blocked on cluster federation.
  752. func (awsProvider *AWS) ClusterInfo() (map[string]string, error) {
  753. defaultClusterName := "AWS Cluster #1"
  754. c, err := awsProvider.GetConfig()
  755. if err != nil {
  756. return nil, err
  757. }
  758. remote := os.Getenv(remoteEnabled)
  759. remoteEnabled := false
  760. if os.Getenv(remote) == "true" {
  761. remoteEnabled = true
  762. }
  763. if c.ClusterName != "" {
  764. m := make(map[string]string)
  765. m["name"] = c.ClusterName
  766. m["provider"] = "AWS"
  767. m["id"] = os.Getenv(clusterIDKey)
  768. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  769. return m, nil
  770. }
  771. makeStructure := func(clusterName string) (map[string]string, error) {
  772. klog.V(2).Infof("Returning \"%s\" as ClusterName", clusterName)
  773. m := make(map[string]string)
  774. m["name"] = clusterName
  775. m["provider"] = "AWS"
  776. m["id"] = os.Getenv(clusterIDKey)
  777. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  778. return m, nil
  779. }
  780. maybeClusterId := os.Getenv(ClusterIdEnvVar)
  781. if len(maybeClusterId) != 0 {
  782. return makeStructure(maybeClusterId)
  783. }
  784. // TODO: This should be cached, it can take a long time to hit the API
  785. //provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)")
  786. //clusterIdRx := regexp.MustCompile("^kubernetes\\.io/cluster/([^/]+)")
  787. //klog.Infof("nodelist get here %s", time.Now())
  788. //nodeList := awsProvider.Clientset.GetAllNodes()
  789. //klog.Infof("nodelist done here %s", time.Now())
  790. /*for _, n := range nodeList {
  791. region := ""
  792. instanceId := ""
  793. providerId := n.Spec.ProviderID
  794. for matchNum, group := range provIdRx.FindStringSubmatch(providerId) {
  795. if matchNum == 1 {
  796. region = group
  797. } else if matchNum == 2 {
  798. instanceId = group
  799. }
  800. }
  801. if len(instanceId) == 0 {
  802. klog.V(2).Infof("Unable to decode Node.ProviderID \"%s\", skipping it", providerId)
  803. continue
  804. }
  805. c := &aws.Config{
  806. Region: aws.String(region),
  807. }
  808. s := session.Must(session.NewSession(c))
  809. ec2Svc := ec2.New(s)
  810. di, diErr := ec2Svc.DescribeInstances(&ec2.DescribeInstancesInput{
  811. InstanceIds: []*string{
  812. aws.String(instanceId),
  813. },
  814. })
  815. if diErr != nil {
  816. klog.Infof("Error describing instances: %s", diErr)
  817. continue
  818. }
  819. if len(di.Reservations) != 1 {
  820. klog.V(2).Infof("Expected 1 Reservation back from DescribeInstances(%s), received %d", instanceId, len(di.Reservations))
  821. continue
  822. }
  823. res := di.Reservations[0]
  824. if len(res.Instances) != 1 {
  825. klog.V(2).Infof("Expected 1 Instance back from DescribeInstances(%s), received %d", instanceId, len(res.Instances))
  826. continue
  827. }
  828. inst := res.Instances[0]
  829. for _, tag := range inst.Tags {
  830. tagKey := *tag.Key
  831. for matchNum, group := range clusterIdRx.FindStringSubmatch(tagKey) {
  832. if matchNum != 1 {
  833. continue
  834. }
  835. return makeStructure(group)
  836. }
  837. }
  838. }*/
  839. klog.V(2).Infof("Unable to sniff out cluster ID, perhaps set $%s to force one", ClusterIdEnvVar)
  840. return makeStructure(defaultClusterName)
  841. }
  842. // AddServiceKey adds an AWS service key, useful for pulling down out-of-cluster costs. Optional-- the container this runs in can be directly authorized.
  843. func (*AWS) AddServiceKey(formValues url.Values) error {
  844. keyID := formValues.Get("access_key_ID")
  845. key := formValues.Get("secret_access_key")
  846. m := make(map[string]string)
  847. m["access_key_ID"] = keyID
  848. m["secret_access_key"] = key
  849. result, err := json.Marshal(m)
  850. if err != nil {
  851. return err
  852. }
  853. return ioutil.WriteFile("/var/configs/key.json", result, 0644)
  854. }
  855. func configureAWSAuth(keyFile string) error {
  856. jsonFile, err := os.Open(keyFile)
  857. if err != nil {
  858. if os.IsNotExist(err) {
  859. klog.V(2).Infof("Using Default Credentials")
  860. return nil
  861. }
  862. return err
  863. }
  864. defer jsonFile.Close()
  865. byteValue, _ := ioutil.ReadAll(jsonFile)
  866. var result map[string]string
  867. err = json.Unmarshal([]byte(byteValue), &result)
  868. if err != nil {
  869. return err
  870. }
  871. err = os.Setenv(awsAccessKeyIDEnvVar, result["awsServiceKeyName"])
  872. if err != nil {
  873. return err
  874. }
  875. err = os.Setenv(awsAccessKeySecretEnvVar, result["awsServiceKeySecret"])
  876. if err != nil {
  877. return err
  878. }
  879. return nil
  880. }
  881. func getClusterConfig(ccFile string) (map[string]string, error) {
  882. clusterConfig, err := os.Open(ccFile)
  883. if err != nil {
  884. return nil, err
  885. }
  886. defer clusterConfig.Close()
  887. b, err := ioutil.ReadAll(clusterConfig)
  888. if err != nil {
  889. return nil, err
  890. }
  891. var clusterConf map[string]string
  892. err = json.Unmarshal([]byte(b), &clusterConf)
  893. if err != nil {
  894. return nil, err
  895. }
  896. return clusterConf, nil
  897. }
  898. // 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.
  899. func (*AWS) GetDisks() ([]byte, error) {
  900. err := configureAWSAuth("/var/configs/key.json")
  901. if err != nil {
  902. return nil, err
  903. }
  904. clusterConfig, err := getClusterConfig("/var/configs/cluster.json")
  905. if err != nil {
  906. return nil, err
  907. }
  908. region := aws.String(clusterConfig["region"])
  909. c := &aws.Config{
  910. Region: region,
  911. }
  912. s := session.Must(session.NewSession(c))
  913. ec2Svc := ec2.New(s)
  914. input := &ec2.DescribeVolumesInput{}
  915. volumeResult, err := ec2Svc.DescribeVolumes(input)
  916. if err != nil {
  917. if aerr, ok := err.(awserr.Error); ok {
  918. switch aerr.Code() {
  919. default:
  920. return nil, aerr
  921. }
  922. } else {
  923. return nil, err
  924. }
  925. }
  926. return json.Marshal(volumeResult)
  927. }
  928. // ConvertToGlueColumnFormat takes a string and runs through various regex
  929. // and string replacement statements to convert it to a format compatible
  930. // with AWS Glue and Athena column names.
  931. // Following guidance from AWS provided here ('Column Names' section):
  932. // https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/run-athena-sql.html
  933. // It returns a string containing the column name in proper column name format and length.
  934. func ConvertToGlueColumnFormat(column_name string) string {
  935. klog.V(5).Infof("Converting string \"%s\" to proper AWS Glue column name.", column_name)
  936. // An underscore is added in front of uppercase letters
  937. capital_underscore := regexp.MustCompile(`[A-Z]`)
  938. final := capital_underscore.ReplaceAllString(column_name, `_$0`)
  939. // Any non-alphanumeric characters are replaced with an underscore
  940. no_space_punc := regexp.MustCompile(`[\s]{1,}|[^A-Za-z0-9]`)
  941. final = no_space_punc.ReplaceAllString(final, "_")
  942. // Duplicate underscores are removed
  943. no_dup_underscore := regexp.MustCompile(`_{2,}`)
  944. final = no_dup_underscore.ReplaceAllString(final, "_")
  945. // Any leading and trailing underscores are removed
  946. no_front_end_underscore := regexp.MustCompile(`(^\_|\_$)`)
  947. final = no_front_end_underscore.ReplaceAllString(final, "")
  948. // Uppercase to lowercase
  949. final = strings.ToLower(final)
  950. // Longer column name than expected - remove _ left to right
  951. allowed_col_len := 128
  952. undersc_to_remove := len(final) - allowed_col_len
  953. if undersc_to_remove > 0 {
  954. final = strings.Replace(final, "_", "", undersc_to_remove)
  955. }
  956. // If removing all of the underscores still didn't
  957. // make the column name < 128 characters, trim it!
  958. if len(final) > allowed_col_len {
  959. final = final[:allowed_col_len]
  960. }
  961. klog.V(5).Infof("Column name being returned: \"%s\". Length: \"%d\".", final, len(final))
  962. return final
  963. }
  964. func generateAWSGroupBy(lastIdx int) string {
  965. sequence := []string{}
  966. for i := 1; i < lastIdx+1; i++ {
  967. sequence = append(sequence, strconv.Itoa(i))
  968. }
  969. return strings.Join(sequence, ",")
  970. }
  971. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  972. // "start" and "end" are dates of the format YYYY-MM-DD
  973. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  974. func (a *AWS) ExternalAllocations(start string, end string, aggregators []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  975. customPricing, err := a.GetConfig()
  976. if err != nil {
  977. return nil, err
  978. }
  979. formattedAggregators := []string{}
  980. for _, agg := range aggregators {
  981. aggregator_column_name := "resource_tags_user_" + agg
  982. aggregator_column_name = ConvertToGlueColumnFormat(aggregator_column_name)
  983. formattedAggregators = append(formattedAggregators, aggregator_column_name)
  984. }
  985. aggregatorNames := strings.Join(formattedAggregators, ",")
  986. filter_column_name := "resource_tags_user_" + filterType
  987. filter_column_name = ConvertToGlueColumnFormat(filter_column_name)
  988. var query string
  989. var lastIdx int
  990. if filterType != "kubernetes_" { // This gets appended upstream and is equivalent to no filter.
  991. lastIdx = len(formattedAggregators) + 3
  992. groupby := generateAWSGroupBy(lastIdx)
  993. query = fmt.Sprintf(`SELECT
  994. CAST(line_item_usage_start_date AS DATE) as start_date,
  995. %s,
  996. line_item_product_code,
  997. %s,
  998. SUM(line_item_blended_cost) as blended_cost
  999. FROM %s as cost_data
  1000. WHERE (%s='%s') AND line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1001. GROUP BY %s`, aggregatorNames, filter_column_name, customPricing.AthenaTable, filter_column_name, filterValue, start, end, groupby)
  1002. } else {
  1003. lastIdx = len(formattedAggregators) + 2
  1004. groupby := generateAWSGroupBy(lastIdx)
  1005. query = fmt.Sprintf(`SELECT
  1006. CAST(line_item_usage_start_date AS DATE) as start_date,
  1007. %s,
  1008. line_item_product_code,
  1009. SUM(line_item_blended_cost) as blended_cost
  1010. FROM %s as cost_data
  1011. WHERE line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1012. GROUP BY %s`, aggregatorNames, customPricing.AthenaTable, start, end, groupby)
  1013. }
  1014. klog.V(3).Infof("Running Query: %s", query)
  1015. if customPricing.ServiceKeyName != "" {
  1016. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1017. if err != nil {
  1018. return nil, err
  1019. }
  1020. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1021. if err != nil {
  1022. return nil, err
  1023. }
  1024. }
  1025. region := aws.String(customPricing.AthenaRegion)
  1026. resultsBucket := customPricing.AthenaBucketName
  1027. database := customPricing.AthenaDatabase
  1028. c := &aws.Config{
  1029. Region: region,
  1030. }
  1031. s := session.Must(session.NewSession(c))
  1032. svc := athena.New(s)
  1033. var e athena.StartQueryExecutionInput
  1034. var r athena.ResultConfiguration
  1035. r.SetOutputLocation(resultsBucket)
  1036. e.SetResultConfiguration(&r)
  1037. e.SetQueryString(query)
  1038. var q athena.QueryExecutionContext
  1039. q.SetDatabase(database)
  1040. e.SetQueryExecutionContext(&q)
  1041. res, err := svc.StartQueryExecution(&e)
  1042. if err != nil {
  1043. return nil, err
  1044. }
  1045. klog.V(2).Infof("StartQueryExecution result:")
  1046. klog.V(2).Infof(res.GoString())
  1047. var qri athena.GetQueryExecutionInput
  1048. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1049. var qrop *athena.GetQueryExecutionOutput
  1050. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1051. for {
  1052. qrop, err = svc.GetQueryExecution(&qri)
  1053. if err != nil {
  1054. return nil, err
  1055. }
  1056. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1057. break
  1058. }
  1059. time.Sleep(duration)
  1060. }
  1061. var oocAllocs []*OutOfClusterAllocation
  1062. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1063. var ip athena.GetQueryResultsInput
  1064. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1065. op, err := svc.GetQueryResults(&ip)
  1066. if err != nil {
  1067. return nil, err
  1068. }
  1069. if len(op.ResultSet.Rows) > 1 {
  1070. for _, r := range op.ResultSet.Rows[1:(len(op.ResultSet.Rows) - 1)] {
  1071. cost, err := strconv.ParseFloat(*r.Data[lastIdx].VarCharValue, 64)
  1072. if err != nil {
  1073. return nil, err
  1074. }
  1075. environment := ""
  1076. for _, d := range r.Data[1 : len(formattedAggregators)+1] {
  1077. if *d.VarCharValue != "" {
  1078. environment = *d.VarCharValue // just set to the first nonempty match
  1079. }
  1080. break
  1081. }
  1082. ooc := &OutOfClusterAllocation{
  1083. Aggregator: strings.Join(aggregators, ","),
  1084. Environment: environment,
  1085. Service: *r.Data[len(formattedAggregators)+1].VarCharValue,
  1086. Cost: cost,
  1087. }
  1088. oocAllocs = append(oocAllocs, ooc)
  1089. }
  1090. } else {
  1091. klog.V(1).Infof("No results available for %s at database %s between %s and %s", strings.Join(formattedAggregators, ","), customPricing.AthenaTable, start, end)
  1092. }
  1093. }
  1094. if customPricing.BillingDataDataset != "" && !crossCluster { // There is GCP data, meaning someone has tried to configure a GCP out-of-cluster allocation.
  1095. gcp, err := NewCrossClusterProvider("gcp", "aws.json", a.Clientset)
  1096. if err != nil {
  1097. klog.Infof("Could not instantiate cross-cluster provider %s", err.Error())
  1098. }
  1099. gcpOOC, err := gcp.ExternalAllocations(start, end, aggregators, filterType, filterValue, true)
  1100. if err != nil {
  1101. klog.Infof("Could not fetch cross-cluster costs %s", err.Error())
  1102. }
  1103. oocAllocs = append(oocAllocs, gcpOOC...)
  1104. }
  1105. return oocAllocs, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  1106. }
  1107. // QuerySQL can query a properly configured Athena database.
  1108. // Used to fetch billing data.
  1109. // Requires a json config in /var/configs with key region, output, and database.
  1110. func (a *AWS) QuerySQL(query string) ([]byte, error) {
  1111. customPricing, err := a.GetConfig()
  1112. if err != nil {
  1113. return nil, err
  1114. }
  1115. if customPricing.ServiceKeyName != "" {
  1116. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1117. if err != nil {
  1118. return nil, err
  1119. }
  1120. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1121. if err != nil {
  1122. return nil, err
  1123. }
  1124. }
  1125. athenaConfigs, err := os.Open("/var/configs/athena.json")
  1126. if err != nil {
  1127. return nil, err
  1128. }
  1129. defer athenaConfigs.Close()
  1130. b, err := ioutil.ReadAll(athenaConfigs)
  1131. if err != nil {
  1132. return nil, err
  1133. }
  1134. var athenaConf map[string]string
  1135. json.Unmarshal([]byte(b), &athenaConf)
  1136. region := aws.String(customPricing.AthenaRegion)
  1137. resultsBucket := customPricing.AthenaBucketName
  1138. database := customPricing.AthenaDatabase
  1139. c := &aws.Config{
  1140. Region: region,
  1141. }
  1142. s := session.Must(session.NewSession(c))
  1143. svc := athena.New(s)
  1144. var e athena.StartQueryExecutionInput
  1145. var r athena.ResultConfiguration
  1146. r.SetOutputLocation(resultsBucket)
  1147. e.SetResultConfiguration(&r)
  1148. e.SetQueryString(query)
  1149. var q athena.QueryExecutionContext
  1150. q.SetDatabase(database)
  1151. e.SetQueryExecutionContext(&q)
  1152. res, err := svc.StartQueryExecution(&e)
  1153. if err != nil {
  1154. return nil, err
  1155. }
  1156. klog.V(2).Infof("StartQueryExecution result:")
  1157. klog.V(2).Infof(res.GoString())
  1158. var qri athena.GetQueryExecutionInput
  1159. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1160. var qrop *athena.GetQueryExecutionOutput
  1161. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1162. for {
  1163. qrop, err = svc.GetQueryExecution(&qri)
  1164. if err != nil {
  1165. return nil, err
  1166. }
  1167. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1168. break
  1169. }
  1170. time.Sleep(duration)
  1171. }
  1172. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1173. var ip athena.GetQueryResultsInput
  1174. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1175. op, err := svc.GetQueryResults(&ip)
  1176. if err != nil {
  1177. return nil, err
  1178. }
  1179. b, err := json.Marshal(op.ResultSet)
  1180. if err != nil {
  1181. return nil, err
  1182. }
  1183. return b, nil
  1184. }
  1185. return nil, fmt.Errorf("Error getting query results : %s", *qrop.QueryExecution.Status.State)
  1186. }
  1187. type spotInfo struct {
  1188. Timestamp string `csv:"Timestamp"`
  1189. UsageType string `csv:"UsageType"`
  1190. Operation string `csv:"Operation"`
  1191. InstanceID string `csv:"InstanceID"`
  1192. MyBidID string `csv:"MyBidID"`
  1193. MyMaxPrice string `csv:"MyMaxPrice"`
  1194. MarketPrice string `csv:"MarketPrice"`
  1195. Charge string `csv:"Charge"`
  1196. Version string `csv:"Version"`
  1197. }
  1198. type fnames []*string
  1199. func (f fnames) Len() int {
  1200. return len(f)
  1201. }
  1202. func (f fnames) Swap(i, j int) {
  1203. f[i], f[j] = f[j], f[i]
  1204. }
  1205. func (f fnames) Less(i, j int) bool {
  1206. key1 := strings.Split(*f[i], ".")
  1207. key2 := strings.Split(*f[j], ".")
  1208. t1, err := time.Parse("2006-01-02-15", key1[1])
  1209. if err != nil {
  1210. klog.V(1).Info("Unable to parse timestamp" + key1[1])
  1211. return false
  1212. }
  1213. t2, err := time.Parse("2006-01-02-15", key2[1])
  1214. if err != nil {
  1215. klog.V(1).Info("Unable to parse timestamp" + key2[1])
  1216. return false
  1217. }
  1218. return t1.Before(t2)
  1219. }
  1220. func parseSpotData(bucket string, prefix string, projectID string, region string, accessKeyID string, accessKeySecret string) (map[string]*spotInfo, error) {
  1221. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  1222. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  1223. if err != nil {
  1224. return nil, err
  1225. }
  1226. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  1227. if err != nil {
  1228. return nil, err
  1229. }
  1230. }
  1231. s3Prefix := projectID
  1232. if len(prefix) != 0 {
  1233. s3Prefix = prefix + "/" + s3Prefix
  1234. }
  1235. c := aws.NewConfig().WithRegion(region)
  1236. s := session.Must(session.NewSession(c))
  1237. s3Svc := s3.New(s)
  1238. downloader := s3manager.NewDownloaderWithClient(s3Svc)
  1239. tNow := time.Now()
  1240. tOneDayAgo := tNow.Add(time.Duration(-24) * time.Hour) // Also get files from one day ago to avoid boundary conditions
  1241. ls := &s3.ListObjectsInput{
  1242. Bucket: aws.String(bucket),
  1243. Prefix: aws.String(s3Prefix + "." + tOneDayAgo.Format("2006-01-02")),
  1244. }
  1245. ls2 := &s3.ListObjectsInput{
  1246. Bucket: aws.String(bucket),
  1247. Prefix: aws.String(s3Prefix + "." + tNow.Format("2006-01-02")),
  1248. }
  1249. lso, err := s3Svc.ListObjects(ls)
  1250. if err != nil {
  1251. return nil, err
  1252. }
  1253. lsoLen := len(lso.Contents)
  1254. klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
  1255. if lsoLen == 0 {
  1256. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
  1257. }
  1258. lso2, err := s3Svc.ListObjects(ls2)
  1259. if err != nil {
  1260. return nil, err
  1261. }
  1262. lso2Len := len(lso2.Contents)
  1263. klog.V(2).Infof("Found %d spot data files from today", lso2Len)
  1264. if lso2Len == 0 {
  1265. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
  1266. }
  1267. var keys []*string
  1268. for _, obj := range lso.Contents {
  1269. keys = append(keys, obj.Key)
  1270. }
  1271. for _, obj := range lso2.Contents {
  1272. keys = append(keys, obj.Key)
  1273. }
  1274. versionRx := regexp.MustCompile("^#Version: (\\d+)\\.\\d+$")
  1275. header, err := csvutil.Header(spotInfo{}, "csv")
  1276. if err != nil {
  1277. return nil, err
  1278. }
  1279. fieldsPerRecord := len(header)
  1280. spots := make(map[string]*spotInfo)
  1281. for _, key := range keys {
  1282. getObj := &s3.GetObjectInput{
  1283. Bucket: aws.String(bucket),
  1284. Key: key,
  1285. }
  1286. buf := aws.NewWriteAtBuffer([]byte{})
  1287. _, err := downloader.Download(buf, getObj)
  1288. if err != nil {
  1289. return nil, err
  1290. }
  1291. r := bytes.NewReader(buf.Bytes())
  1292. gr, err := gzip.NewReader(r)
  1293. if err != nil {
  1294. return nil, err
  1295. }
  1296. csvReader := csv.NewReader(gr)
  1297. csvReader.Comma = '\t'
  1298. csvReader.FieldsPerRecord = fieldsPerRecord
  1299. dec, err := csvutil.NewDecoder(csvReader, header...)
  1300. if err != nil {
  1301. return nil, err
  1302. }
  1303. var foundVersion string
  1304. for {
  1305. spot := spotInfo{}
  1306. err := dec.Decode(&spot)
  1307. csvParseErr, isCsvParseErr := err.(*csv.ParseError)
  1308. if err == io.EOF {
  1309. break
  1310. } else if err == csvutil.ErrFieldCount || (isCsvParseErr && csvParseErr.Err == csv.ErrFieldCount) {
  1311. rec := dec.Record()
  1312. // the first two "Record()" will be the comment lines
  1313. // and they show up as len() == 1
  1314. // the first of which is "#Version"
  1315. // the second of which is "#Fields: "
  1316. if len(rec) != 1 {
  1317. klog.V(2).Infof("Expected %d spot info fields but received %d: %s", fieldsPerRecord, len(rec), rec)
  1318. continue
  1319. }
  1320. if len(foundVersion) == 0 {
  1321. spotFeedVersion := rec[0]
  1322. klog.V(4).Infof("Spot feed version is \"%s\"", spotFeedVersion)
  1323. matches := versionRx.FindStringSubmatch(spotFeedVersion)
  1324. if matches != nil {
  1325. foundVersion = matches[1]
  1326. if foundVersion != supportedSpotFeedVersion {
  1327. klog.V(2).Infof("Unsupported spot info feed version: wanted \"%s\" got \"%s\"", supportedSpotFeedVersion, foundVersion)
  1328. break
  1329. }
  1330. }
  1331. continue
  1332. } else if strings.Index(rec[0], "#") == 0 {
  1333. continue
  1334. } else {
  1335. klog.V(3).Infof("skipping non-TSV line: %s", rec)
  1336. continue
  1337. }
  1338. } else if err != nil {
  1339. klog.V(2).Infof("Error during spot info decode: %+v", err)
  1340. continue
  1341. }
  1342. klog.V(4).Infof("Found spot info %+v", spot)
  1343. spots[spot.InstanceID] = &spot
  1344. }
  1345. gr.Close()
  1346. }
  1347. return spots, nil
  1348. }
  1349. func (a *AWS) ApplyReservedInstancePricing(nodes map[string]*Node) {
  1350. numReserved := len(a.ReservedInstances)
  1351. // Early return if no reserved instance data loaded
  1352. if numReserved == 0 {
  1353. klog.V(4).Infof("[Reserved] No Reserved Instances")
  1354. return
  1355. }
  1356. cfg, err := a.GetConfig()
  1357. defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
  1358. if err != nil {
  1359. klog.V(3).Infof("Could not parse default cpu price")
  1360. defaultCPU = 0.031611
  1361. }
  1362. defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
  1363. if err != nil {
  1364. klog.V(3).Infof("Could not parse default ram price")
  1365. defaultRAM = 0.004237
  1366. }
  1367. cpuToRAMRatio := defaultCPU / defaultRAM
  1368. now := time.Now()
  1369. instances := make(map[string][]*AWSReservedInstance)
  1370. for _, r := range a.ReservedInstances {
  1371. if now.Before(r.StartDate) || now.After(r.EndDate) {
  1372. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  1373. continue
  1374. }
  1375. _, ok := instances[r.Region]
  1376. if !ok {
  1377. instances[r.Region] = []*AWSReservedInstance{r}
  1378. } else {
  1379. instances[r.Region] = append(instances[r.Region], r)
  1380. }
  1381. }
  1382. awsNodes := make(map[string]*v1.Node)
  1383. currentNodes := a.Clientset.GetAllNodes()
  1384. // Create a node name -> node map
  1385. for _, awsNode := range currentNodes {
  1386. awsNodes[awsNode.GetName()] = awsNode
  1387. }
  1388. // go through all provider nodes using k8s nodes for region
  1389. for nodeName, node := range nodes {
  1390. // Reset reserved allocation to prevent double allocation
  1391. node.Reserved = nil
  1392. kNode, ok := awsNodes[nodeName]
  1393. if !ok {
  1394. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  1395. continue
  1396. }
  1397. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  1398. if !ok {
  1399. klog.V(1).Infof("[Reserved] Could not find node region")
  1400. continue
  1401. }
  1402. reservedInstances, ok := instances[nodeRegion]
  1403. if !ok {
  1404. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  1405. continue
  1406. }
  1407. // Determine the InstanceType of the node
  1408. instanceType, ok := kNode.Labels["beta.kubernetes.io/instance-type"]
  1409. if !ok {
  1410. continue
  1411. }
  1412. ramBytes, err := strconv.ParseFloat(node.RAMBytes, 64)
  1413. if err != nil {
  1414. continue
  1415. }
  1416. ramGB := ramBytes / 1024 / 1024 / 1024
  1417. cpu, err := strconv.ParseFloat(node.VCPU, 64)
  1418. if err != nil {
  1419. continue
  1420. }
  1421. ramMultiple := cpu*cpuToRAMRatio + ramGB
  1422. node.Reserved = &ReservedInstanceData{
  1423. ReservedCPU: 0,
  1424. ReservedRAM: 0,
  1425. }
  1426. for i, reservedInstance := range reservedInstances {
  1427. if reservedInstance.InstanceType == instanceType {
  1428. // Use < 0 to mark as ALL
  1429. node.Reserved.ReservedCPU = -1
  1430. node.Reserved.ReservedRAM = -1
  1431. // Set Costs based on CPU/RAM ratios
  1432. ramPrice := reservedInstance.PricePerHour / ramMultiple
  1433. node.Reserved.CPUCost = ramPrice * cpuToRAMRatio
  1434. node.Reserved.RAMCost = ramPrice
  1435. // Remove the reserve from the temporary slice to prevent
  1436. // being reallocated
  1437. instances[nodeRegion] = append(reservedInstances[:i], reservedInstances[i+1:]...)
  1438. break
  1439. }
  1440. }
  1441. }
  1442. }
  1443. type AWSReservedInstance struct {
  1444. Zone string
  1445. Region string
  1446. InstanceType string
  1447. InstanceCount int64
  1448. InstanceTenacy string
  1449. StartDate time.Time
  1450. EndDate time.Time
  1451. PricePerHour float64
  1452. }
  1453. func (ari *AWSReservedInstance) String() string {
  1454. return fmt.Sprintf("[Zone: %s, Region: %s, Type: %s, Count: %d, Tenacy: %s, Start: %+v, End: %+v, Price: %f]", ari.Zone, ari.Region, ari.InstanceType, ari.InstanceCount, ari.InstanceTenacy, ari.StartDate, ari.EndDate, ari.PricePerHour)
  1455. }
  1456. func isReservedInstanceHourlyPrice(rc *ec2.RecurringCharge) bool {
  1457. return rc != nil && rc.Frequency != nil && *rc.Frequency == "Hourly"
  1458. }
  1459. func getReservedInstancePrice(ri *ec2.ReservedInstances) (float64, error) {
  1460. var pricePerHour float64
  1461. if len(ri.RecurringCharges) > 0 {
  1462. for _, rc := range ri.RecurringCharges {
  1463. if isReservedInstanceHourlyPrice(rc) {
  1464. pricePerHour = *rc.Amount
  1465. break
  1466. }
  1467. }
  1468. }
  1469. // If we're still unable to resolve hourly price, try fixed -> hourly
  1470. if pricePerHour == 0 {
  1471. if ri.Duration != nil && ri.FixedPrice != nil {
  1472. var durHours float64
  1473. durSeconds := float64(*ri.Duration)
  1474. fixedPrice := float64(*ri.FixedPrice)
  1475. if durSeconds != 0 && fixedPrice != 0 {
  1476. durHours = durSeconds / 60 / 60
  1477. pricePerHour = fixedPrice / durHours
  1478. }
  1479. }
  1480. }
  1481. if pricePerHour == 0 {
  1482. return 0, fmt.Errorf("Failed to resolve an hourly price from FixedPrice or Recurring Costs")
  1483. }
  1484. return pricePerHour, nil
  1485. }
  1486. func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
  1487. c := &aws.Config{
  1488. Region: aws.String(region),
  1489. }
  1490. s := session.Must(session.NewSession(c))
  1491. svc := ec2.New(s)
  1492. response, err := svc.DescribeReservedInstances(&ec2.DescribeReservedInstancesInput{})
  1493. if err != nil {
  1494. return nil, err
  1495. }
  1496. var reservedInstances []*AWSReservedInstance
  1497. for _, ri := range response.ReservedInstances {
  1498. var zone string
  1499. if ri.AvailabilityZone != nil {
  1500. zone = *ri.AvailabilityZone
  1501. }
  1502. pricePerHour, err := getReservedInstancePrice(ri)
  1503. if err != nil {
  1504. klog.V(1).Infof("Error Resolving Price: %s", err.Error())
  1505. continue
  1506. }
  1507. reservedInstances = append(reservedInstances, &AWSReservedInstance{
  1508. Zone: zone,
  1509. Region: region,
  1510. InstanceType: *ri.InstanceType,
  1511. InstanceCount: *ri.InstanceCount,
  1512. InstanceTenacy: *ri.InstanceTenancy,
  1513. StartDate: *ri.Start,
  1514. EndDate: *ri.End,
  1515. PricePerHour: pricePerHour,
  1516. })
  1517. }
  1518. return reservedInstances, nil
  1519. }
  1520. func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
  1521. err := configureAWSAuth("/var/configs/aws.json")
  1522. if err != nil {
  1523. return nil, err
  1524. }
  1525. var reservedInstances []*AWSReservedInstance
  1526. nodes := a.Clientset.GetAllNodes()
  1527. regionsSeen := make(map[string]bool)
  1528. for _, node := range nodes {
  1529. region, ok := node.Labels[v1.LabelZoneRegion]
  1530. if !ok {
  1531. continue
  1532. }
  1533. if regionsSeen[region] {
  1534. continue
  1535. }
  1536. ris, err := getRegionReservedInstances(region)
  1537. if err != nil {
  1538. klog.V(3).Infof("Error getting reserved instances: %s", err.Error())
  1539. continue
  1540. }
  1541. regionsSeen[region] = true
  1542. reservedInstances = append(reservedInstances, ris...)
  1543. }
  1544. return reservedInstances, nil
  1545. }