awsprovider.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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 (aws *AWS) configureAWSAuth() error {
  856. accessKeyID := aws.ServiceKeyName
  857. accessKeySecret := aws.ServiceKeySecret
  858. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  859. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  860. if err != nil {
  861. return err
  862. }
  863. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  864. if err != nil {
  865. return err
  866. }
  867. }
  868. return nil
  869. }
  870. func getClusterConfig(ccFile string) (map[string]string, error) {
  871. clusterConfig, err := os.Open(ccFile)
  872. if err != nil {
  873. return nil, err
  874. }
  875. defer clusterConfig.Close()
  876. b, err := ioutil.ReadAll(clusterConfig)
  877. if err != nil {
  878. return nil, err
  879. }
  880. var clusterConf map[string]string
  881. err = json.Unmarshal([]byte(b), &clusterConf)
  882. if err != nil {
  883. return nil, err
  884. }
  885. return clusterConf, nil
  886. }
  887. // 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.
  888. func (a *AWS) GetDisks() ([]byte, error) {
  889. err := a.configureAWSAuth()
  890. if err != nil {
  891. return nil, err
  892. }
  893. clusterConfig, err := getClusterConfig("/var/configs/cluster.json")
  894. if err != nil {
  895. return nil, err
  896. }
  897. region := aws.String(clusterConfig["region"])
  898. c := &aws.Config{
  899. Region: region,
  900. }
  901. s := session.Must(session.NewSession(c))
  902. ec2Svc := ec2.New(s)
  903. input := &ec2.DescribeVolumesInput{}
  904. volumeResult, err := ec2Svc.DescribeVolumes(input)
  905. if err != nil {
  906. if aerr, ok := err.(awserr.Error); ok {
  907. switch aerr.Code() {
  908. default:
  909. return nil, aerr
  910. }
  911. } else {
  912. return nil, err
  913. }
  914. }
  915. return json.Marshal(volumeResult)
  916. }
  917. // ConvertToGlueColumnFormat takes a string and runs through various regex
  918. // and string replacement statements to convert it to a format compatible
  919. // with AWS Glue and Athena column names.
  920. // Following guidance from AWS provided here ('Column Names' section):
  921. // https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/run-athena-sql.html
  922. // It returns a string containing the column name in proper column name format and length.
  923. func ConvertToGlueColumnFormat(column_name string) string {
  924. klog.V(5).Infof("Converting string \"%s\" to proper AWS Glue column name.", column_name)
  925. // An underscore is added in front of uppercase letters
  926. capital_underscore := regexp.MustCompile(`[A-Z]`)
  927. final := capital_underscore.ReplaceAllString(column_name, `_$0`)
  928. // Any non-alphanumeric characters are replaced with an underscore
  929. no_space_punc := regexp.MustCompile(`[\s]{1,}|[^A-Za-z0-9]`)
  930. final = no_space_punc.ReplaceAllString(final, "_")
  931. // Duplicate underscores are removed
  932. no_dup_underscore := regexp.MustCompile(`_{2,}`)
  933. final = no_dup_underscore.ReplaceAllString(final, "_")
  934. // Any leading and trailing underscores are removed
  935. no_front_end_underscore := regexp.MustCompile(`(^\_|\_$)`)
  936. final = no_front_end_underscore.ReplaceAllString(final, "")
  937. // Uppercase to lowercase
  938. final = strings.ToLower(final)
  939. // Longer column name than expected - remove _ left to right
  940. allowed_col_len := 128
  941. undersc_to_remove := len(final) - allowed_col_len
  942. if undersc_to_remove > 0 {
  943. final = strings.Replace(final, "_", "", undersc_to_remove)
  944. }
  945. // If removing all of the underscores still didn't
  946. // make the column name < 128 characters, trim it!
  947. if len(final) > allowed_col_len {
  948. final = final[:allowed_col_len]
  949. }
  950. klog.V(5).Infof("Column name being returned: \"%s\". Length: \"%d\".", final, len(final))
  951. return final
  952. }
  953. func generateAWSGroupBy(lastIdx int) string {
  954. sequence := []string{}
  955. for i := 1; i < lastIdx+1; i++ {
  956. sequence = append(sequence, strconv.Itoa(i))
  957. }
  958. return strings.Join(sequence, ",")
  959. }
  960. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  961. // "start" and "end" are dates of the format YYYY-MM-DD
  962. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  963. func (a *AWS) ExternalAllocations(start string, end string, aggregators []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  964. customPricing, err := a.GetConfig()
  965. if err != nil {
  966. return nil, err
  967. }
  968. formattedAggregators := []string{}
  969. for _, agg := range aggregators {
  970. aggregator_column_name := "resource_tags_user_" + agg
  971. aggregator_column_name = ConvertToGlueColumnFormat(aggregator_column_name)
  972. formattedAggregators = append(formattedAggregators, aggregator_column_name)
  973. }
  974. aggregatorNames := strings.Join(formattedAggregators, ",")
  975. filter_column_name := "resource_tags_user_" + filterType
  976. filter_column_name = ConvertToGlueColumnFormat(filter_column_name)
  977. var query string
  978. var lastIdx int
  979. if filterType != "kubernetes_" { // This gets appended upstream and is equivalent to no filter.
  980. lastIdx = len(formattedAggregators) + 3
  981. groupby := generateAWSGroupBy(lastIdx)
  982. query = fmt.Sprintf(`SELECT
  983. CAST(line_item_usage_start_date AS DATE) as start_date,
  984. %s,
  985. line_item_product_code,
  986. %s,
  987. SUM(line_item_blended_cost) as blended_cost
  988. FROM %s as cost_data
  989. WHERE (%s='%s') AND line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  990. GROUP BY %s`, aggregatorNames, filter_column_name, customPricing.AthenaTable, filter_column_name, filterValue, start, end, groupby)
  991. } else {
  992. lastIdx = len(formattedAggregators) + 2
  993. groupby := generateAWSGroupBy(lastIdx)
  994. query = fmt.Sprintf(`SELECT
  995. CAST(line_item_usage_start_date AS DATE) as start_date,
  996. %s,
  997. line_item_product_code,
  998. SUM(line_item_blended_cost) as blended_cost
  999. FROM %s as cost_data
  1000. WHERE line_item_usage_start_date BETWEEN date '%s' AND date '%s'
  1001. GROUP BY %s`, aggregatorNames, customPricing.AthenaTable, start, end, groupby)
  1002. }
  1003. klog.V(3).Infof("Running Query: %s", query)
  1004. if customPricing.ServiceKeyName != "" {
  1005. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1006. if err != nil {
  1007. return nil, err
  1008. }
  1009. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1010. if err != nil {
  1011. return nil, err
  1012. }
  1013. }
  1014. region := aws.String(customPricing.AthenaRegion)
  1015. resultsBucket := customPricing.AthenaBucketName
  1016. database := customPricing.AthenaDatabase
  1017. c := &aws.Config{
  1018. Region: region,
  1019. }
  1020. s := session.Must(session.NewSession(c))
  1021. svc := athena.New(s)
  1022. var e athena.StartQueryExecutionInput
  1023. var r athena.ResultConfiguration
  1024. r.SetOutputLocation(resultsBucket)
  1025. e.SetResultConfiguration(&r)
  1026. e.SetQueryString(query)
  1027. var q athena.QueryExecutionContext
  1028. q.SetDatabase(database)
  1029. e.SetQueryExecutionContext(&q)
  1030. res, err := svc.StartQueryExecution(&e)
  1031. if err != nil {
  1032. return nil, err
  1033. }
  1034. klog.V(2).Infof("StartQueryExecution result:")
  1035. klog.V(2).Infof(res.GoString())
  1036. var qri athena.GetQueryExecutionInput
  1037. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1038. var qrop *athena.GetQueryExecutionOutput
  1039. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1040. for {
  1041. qrop, err = svc.GetQueryExecution(&qri)
  1042. if err != nil {
  1043. return nil, err
  1044. }
  1045. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1046. break
  1047. }
  1048. time.Sleep(duration)
  1049. }
  1050. var oocAllocs []*OutOfClusterAllocation
  1051. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1052. var ip athena.GetQueryResultsInput
  1053. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1054. op, err := svc.GetQueryResults(&ip)
  1055. if err != nil {
  1056. return nil, err
  1057. }
  1058. if len(op.ResultSet.Rows) > 1 {
  1059. for _, r := range op.ResultSet.Rows[1:(len(op.ResultSet.Rows) - 1)] {
  1060. cost, err := strconv.ParseFloat(*r.Data[lastIdx].VarCharValue, 64)
  1061. if err != nil {
  1062. return nil, err
  1063. }
  1064. environment := ""
  1065. for _, d := range r.Data[1 : len(formattedAggregators)+1] {
  1066. if *d.VarCharValue != "" {
  1067. environment = *d.VarCharValue // just set to the first nonempty match
  1068. }
  1069. break
  1070. }
  1071. ooc := &OutOfClusterAllocation{
  1072. Aggregator: strings.Join(aggregators, ","),
  1073. Environment: environment,
  1074. Service: *r.Data[len(formattedAggregators)+1].VarCharValue,
  1075. Cost: cost,
  1076. }
  1077. oocAllocs = append(oocAllocs, ooc)
  1078. }
  1079. } else {
  1080. klog.V(1).Infof("No results available for %s at database %s between %s and %s", strings.Join(formattedAggregators, ","), customPricing.AthenaTable, start, end)
  1081. }
  1082. }
  1083. if customPricing.BillingDataDataset != "" && !crossCluster { // There is GCP data, meaning someone has tried to configure a GCP out-of-cluster allocation.
  1084. gcp, err := NewCrossClusterProvider("gcp", "aws.json", a.Clientset)
  1085. if err != nil {
  1086. klog.Infof("Could not instantiate cross-cluster provider %s", err.Error())
  1087. }
  1088. gcpOOC, err := gcp.ExternalAllocations(start, end, aggregators, filterType, filterValue, true)
  1089. if err != nil {
  1090. klog.Infof("Could not fetch cross-cluster costs %s", err.Error())
  1091. }
  1092. oocAllocs = append(oocAllocs, gcpOOC...)
  1093. }
  1094. return oocAllocs, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  1095. }
  1096. // QuerySQL can query a properly configured Athena database.
  1097. // Used to fetch billing data.
  1098. // Requires a json config in /var/configs with key region, output, and database.
  1099. func (a *AWS) QuerySQL(query string) ([]byte, error) {
  1100. customPricing, err := a.GetConfig()
  1101. if err != nil {
  1102. return nil, err
  1103. }
  1104. if customPricing.ServiceKeyName != "" {
  1105. err = os.Setenv(awsAccessKeyIDEnvVar, customPricing.ServiceKeyName)
  1106. if err != nil {
  1107. return nil, err
  1108. }
  1109. err = os.Setenv(awsAccessKeySecretEnvVar, customPricing.ServiceKeySecret)
  1110. if err != nil {
  1111. return nil, err
  1112. }
  1113. }
  1114. athenaConfigs, err := os.Open("/var/configs/athena.json")
  1115. if err != nil {
  1116. return nil, err
  1117. }
  1118. defer athenaConfigs.Close()
  1119. b, err := ioutil.ReadAll(athenaConfigs)
  1120. if err != nil {
  1121. return nil, err
  1122. }
  1123. var athenaConf map[string]string
  1124. json.Unmarshal([]byte(b), &athenaConf)
  1125. region := aws.String(customPricing.AthenaRegion)
  1126. resultsBucket := customPricing.AthenaBucketName
  1127. database := customPricing.AthenaDatabase
  1128. c := &aws.Config{
  1129. Region: region,
  1130. }
  1131. s := session.Must(session.NewSession(c))
  1132. svc := athena.New(s)
  1133. var e athena.StartQueryExecutionInput
  1134. var r athena.ResultConfiguration
  1135. r.SetOutputLocation(resultsBucket)
  1136. e.SetResultConfiguration(&r)
  1137. e.SetQueryString(query)
  1138. var q athena.QueryExecutionContext
  1139. q.SetDatabase(database)
  1140. e.SetQueryExecutionContext(&q)
  1141. res, err := svc.StartQueryExecution(&e)
  1142. if err != nil {
  1143. return nil, err
  1144. }
  1145. klog.V(2).Infof("StartQueryExecution result:")
  1146. klog.V(2).Infof(res.GoString())
  1147. var qri athena.GetQueryExecutionInput
  1148. qri.SetQueryExecutionId(*res.QueryExecutionId)
  1149. var qrop *athena.GetQueryExecutionOutput
  1150. duration := time.Duration(2) * time.Second // Pause for 2 seconds
  1151. for {
  1152. qrop, err = svc.GetQueryExecution(&qri)
  1153. if err != nil {
  1154. return nil, err
  1155. }
  1156. if *qrop.QueryExecution.Status.State != "RUNNING" {
  1157. break
  1158. }
  1159. time.Sleep(duration)
  1160. }
  1161. if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
  1162. var ip athena.GetQueryResultsInput
  1163. ip.SetQueryExecutionId(*res.QueryExecutionId)
  1164. op, err := svc.GetQueryResults(&ip)
  1165. if err != nil {
  1166. return nil, err
  1167. }
  1168. b, err := json.Marshal(op.ResultSet)
  1169. if err != nil {
  1170. return nil, err
  1171. }
  1172. return b, nil
  1173. }
  1174. return nil, fmt.Errorf("Error getting query results : %s", *qrop.QueryExecution.Status.State)
  1175. }
  1176. type spotInfo struct {
  1177. Timestamp string `csv:"Timestamp"`
  1178. UsageType string `csv:"UsageType"`
  1179. Operation string `csv:"Operation"`
  1180. InstanceID string `csv:"InstanceID"`
  1181. MyBidID string `csv:"MyBidID"`
  1182. MyMaxPrice string `csv:"MyMaxPrice"`
  1183. MarketPrice string `csv:"MarketPrice"`
  1184. Charge string `csv:"Charge"`
  1185. Version string `csv:"Version"`
  1186. }
  1187. type fnames []*string
  1188. func (f fnames) Len() int {
  1189. return len(f)
  1190. }
  1191. func (f fnames) Swap(i, j int) {
  1192. f[i], f[j] = f[j], f[i]
  1193. }
  1194. func (f fnames) Less(i, j int) bool {
  1195. key1 := strings.Split(*f[i], ".")
  1196. key2 := strings.Split(*f[j], ".")
  1197. t1, err := time.Parse("2006-01-02-15", key1[1])
  1198. if err != nil {
  1199. klog.V(1).Info("Unable to parse timestamp" + key1[1])
  1200. return false
  1201. }
  1202. t2, err := time.Parse("2006-01-02-15", key2[1])
  1203. if err != nil {
  1204. klog.V(1).Info("Unable to parse timestamp" + key2[1])
  1205. return false
  1206. }
  1207. return t1.Before(t2)
  1208. }
  1209. func parseSpotData(bucket string, prefix string, projectID string, region string, accessKeyID string, accessKeySecret string) (map[string]*spotInfo, error) {
  1210. if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
  1211. err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
  1212. if err != nil {
  1213. return nil, err
  1214. }
  1215. err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
  1216. if err != nil {
  1217. return nil, err
  1218. }
  1219. }
  1220. s3Prefix := projectID
  1221. if len(prefix) != 0 {
  1222. s3Prefix = prefix + "/" + s3Prefix
  1223. }
  1224. c := aws.NewConfig().WithRegion(region)
  1225. s := session.Must(session.NewSession(c))
  1226. s3Svc := s3.New(s)
  1227. downloader := s3manager.NewDownloaderWithClient(s3Svc)
  1228. tNow := time.Now()
  1229. tOneDayAgo := tNow.Add(time.Duration(-24) * time.Hour) // Also get files from one day ago to avoid boundary conditions
  1230. ls := &s3.ListObjectsInput{
  1231. Bucket: aws.String(bucket),
  1232. Prefix: aws.String(s3Prefix + "." + tOneDayAgo.Format("2006-01-02")),
  1233. }
  1234. ls2 := &s3.ListObjectsInput{
  1235. Bucket: aws.String(bucket),
  1236. Prefix: aws.String(s3Prefix + "." + tNow.Format("2006-01-02")),
  1237. }
  1238. lso, err := s3Svc.ListObjects(ls)
  1239. if err != nil {
  1240. return nil, err
  1241. }
  1242. lsoLen := len(lso.Contents)
  1243. klog.V(2).Infof("Found %d spot data files from yesterday", lsoLen)
  1244. if lsoLen == 0 {
  1245. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls.Bucket, *ls.Prefix)
  1246. }
  1247. lso2, err := s3Svc.ListObjects(ls2)
  1248. if err != nil {
  1249. return nil, err
  1250. }
  1251. lso2Len := len(lso2.Contents)
  1252. klog.V(2).Infof("Found %d spot data files from today", lso2Len)
  1253. if lso2Len == 0 {
  1254. klog.V(5).Infof("ListObjects \"s3://%s/%s\" produced no keys", *ls2.Bucket, *ls2.Prefix)
  1255. }
  1256. var keys []*string
  1257. for _, obj := range lso.Contents {
  1258. keys = append(keys, obj.Key)
  1259. }
  1260. for _, obj := range lso2.Contents {
  1261. keys = append(keys, obj.Key)
  1262. }
  1263. versionRx := regexp.MustCompile("^#Version: (\\d+)\\.\\d+$")
  1264. header, err := csvutil.Header(spotInfo{}, "csv")
  1265. if err != nil {
  1266. return nil, err
  1267. }
  1268. fieldsPerRecord := len(header)
  1269. spots := make(map[string]*spotInfo)
  1270. for _, key := range keys {
  1271. getObj := &s3.GetObjectInput{
  1272. Bucket: aws.String(bucket),
  1273. Key: key,
  1274. }
  1275. buf := aws.NewWriteAtBuffer([]byte{})
  1276. _, err := downloader.Download(buf, getObj)
  1277. if err != nil {
  1278. return nil, err
  1279. }
  1280. r := bytes.NewReader(buf.Bytes())
  1281. gr, err := gzip.NewReader(r)
  1282. if err != nil {
  1283. return nil, err
  1284. }
  1285. csvReader := csv.NewReader(gr)
  1286. csvReader.Comma = '\t'
  1287. csvReader.FieldsPerRecord = fieldsPerRecord
  1288. dec, err := csvutil.NewDecoder(csvReader, header...)
  1289. if err != nil {
  1290. return nil, err
  1291. }
  1292. var foundVersion string
  1293. for {
  1294. spot := spotInfo{}
  1295. err := dec.Decode(&spot)
  1296. csvParseErr, isCsvParseErr := err.(*csv.ParseError)
  1297. if err == io.EOF {
  1298. break
  1299. } else if err == csvutil.ErrFieldCount || (isCsvParseErr && csvParseErr.Err == csv.ErrFieldCount) {
  1300. rec := dec.Record()
  1301. // the first two "Record()" will be the comment lines
  1302. // and they show up as len() == 1
  1303. // the first of which is "#Version"
  1304. // the second of which is "#Fields: "
  1305. if len(rec) != 1 {
  1306. klog.V(2).Infof("Expected %d spot info fields but received %d: %s", fieldsPerRecord, len(rec), rec)
  1307. continue
  1308. }
  1309. if len(foundVersion) == 0 {
  1310. spotFeedVersion := rec[0]
  1311. klog.V(4).Infof("Spot feed version is \"%s\"", spotFeedVersion)
  1312. matches := versionRx.FindStringSubmatch(spotFeedVersion)
  1313. if matches != nil {
  1314. foundVersion = matches[1]
  1315. if foundVersion != supportedSpotFeedVersion {
  1316. klog.V(2).Infof("Unsupported spot info feed version: wanted \"%s\" got \"%s\"", supportedSpotFeedVersion, foundVersion)
  1317. break
  1318. }
  1319. }
  1320. continue
  1321. } else if strings.Index(rec[0], "#") == 0 {
  1322. continue
  1323. } else {
  1324. klog.V(3).Infof("skipping non-TSV line: %s", rec)
  1325. continue
  1326. }
  1327. } else if err != nil {
  1328. klog.V(2).Infof("Error during spot info decode: %+v", err)
  1329. continue
  1330. }
  1331. klog.V(4).Infof("Found spot info %+v", spot)
  1332. spots[spot.InstanceID] = &spot
  1333. }
  1334. gr.Close()
  1335. }
  1336. return spots, nil
  1337. }
  1338. func (a *AWS) ApplyReservedInstancePricing(nodes map[string]*Node) {
  1339. numReserved := len(a.ReservedInstances)
  1340. // Early return if no reserved instance data loaded
  1341. if numReserved == 0 {
  1342. klog.V(4).Infof("[Reserved] No Reserved Instances")
  1343. return
  1344. }
  1345. cfg, err := a.GetConfig()
  1346. defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
  1347. if err != nil {
  1348. klog.V(3).Infof("Could not parse default cpu price")
  1349. defaultCPU = 0.031611
  1350. }
  1351. defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
  1352. if err != nil {
  1353. klog.V(3).Infof("Could not parse default ram price")
  1354. defaultRAM = 0.004237
  1355. }
  1356. cpuToRAMRatio := defaultCPU / defaultRAM
  1357. now := time.Now()
  1358. instances := make(map[string][]*AWSReservedInstance)
  1359. for _, r := range a.ReservedInstances {
  1360. if now.Before(r.StartDate) || now.After(r.EndDate) {
  1361. klog.V(1).Infof("[Reserved] Skipped Reserved Instance due to dates")
  1362. continue
  1363. }
  1364. _, ok := instances[r.Region]
  1365. if !ok {
  1366. instances[r.Region] = []*AWSReservedInstance{r}
  1367. } else {
  1368. instances[r.Region] = append(instances[r.Region], r)
  1369. }
  1370. }
  1371. awsNodes := make(map[string]*v1.Node)
  1372. currentNodes := a.Clientset.GetAllNodes()
  1373. // Create a node name -> node map
  1374. for _, awsNode := range currentNodes {
  1375. awsNodes[awsNode.GetName()] = awsNode
  1376. }
  1377. // go through all provider nodes using k8s nodes for region
  1378. for nodeName, node := range nodes {
  1379. // Reset reserved allocation to prevent double allocation
  1380. node.Reserved = nil
  1381. kNode, ok := awsNodes[nodeName]
  1382. if !ok {
  1383. klog.V(1).Infof("[Reserved] Could not find K8s Node with name: %s", nodeName)
  1384. continue
  1385. }
  1386. nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
  1387. if !ok {
  1388. klog.V(1).Infof("[Reserved] Could not find node region")
  1389. continue
  1390. }
  1391. reservedInstances, ok := instances[nodeRegion]
  1392. if !ok {
  1393. klog.V(1).Infof("[Reserved] Could not find counters for region: %s", nodeRegion)
  1394. continue
  1395. }
  1396. // Determine the InstanceType of the node
  1397. instanceType, ok := kNode.Labels["beta.kubernetes.io/instance-type"]
  1398. if !ok {
  1399. continue
  1400. }
  1401. ramBytes, err := strconv.ParseFloat(node.RAMBytes, 64)
  1402. if err != nil {
  1403. continue
  1404. }
  1405. ramGB := ramBytes / 1024 / 1024 / 1024
  1406. cpu, err := strconv.ParseFloat(node.VCPU, 64)
  1407. if err != nil {
  1408. continue
  1409. }
  1410. ramMultiple := cpu*cpuToRAMRatio + ramGB
  1411. node.Reserved = &ReservedInstanceData{
  1412. ReservedCPU: 0,
  1413. ReservedRAM: 0,
  1414. }
  1415. for i, reservedInstance := range reservedInstances {
  1416. if reservedInstance.InstanceType == instanceType {
  1417. // Use < 0 to mark as ALL
  1418. node.Reserved.ReservedCPU = -1
  1419. node.Reserved.ReservedRAM = -1
  1420. // Set Costs based on CPU/RAM ratios
  1421. ramPrice := reservedInstance.PricePerHour / ramMultiple
  1422. node.Reserved.CPUCost = ramPrice * cpuToRAMRatio
  1423. node.Reserved.RAMCost = ramPrice
  1424. // Remove the reserve from the temporary slice to prevent
  1425. // being reallocated
  1426. instances[nodeRegion] = append(reservedInstances[:i], reservedInstances[i+1:]...)
  1427. break
  1428. }
  1429. }
  1430. }
  1431. }
  1432. type AWSReservedInstance struct {
  1433. Zone string
  1434. Region string
  1435. InstanceType string
  1436. InstanceCount int64
  1437. InstanceTenacy string
  1438. StartDate time.Time
  1439. EndDate time.Time
  1440. PricePerHour float64
  1441. }
  1442. func (ari *AWSReservedInstance) String() string {
  1443. 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)
  1444. }
  1445. func isReservedInstanceHourlyPrice(rc *ec2.RecurringCharge) bool {
  1446. return rc != nil && rc.Frequency != nil && *rc.Frequency == "Hourly"
  1447. }
  1448. func getReservedInstancePrice(ri *ec2.ReservedInstances) (float64, error) {
  1449. var pricePerHour float64
  1450. if len(ri.RecurringCharges) > 0 {
  1451. for _, rc := range ri.RecurringCharges {
  1452. if isReservedInstanceHourlyPrice(rc) {
  1453. pricePerHour = *rc.Amount
  1454. break
  1455. }
  1456. }
  1457. }
  1458. // If we're still unable to resolve hourly price, try fixed -> hourly
  1459. if pricePerHour == 0 {
  1460. if ri.Duration != nil && ri.FixedPrice != nil {
  1461. var durHours float64
  1462. durSeconds := float64(*ri.Duration)
  1463. fixedPrice := float64(*ri.FixedPrice)
  1464. if durSeconds != 0 && fixedPrice != 0 {
  1465. durHours = durSeconds / 60 / 60
  1466. pricePerHour = fixedPrice / durHours
  1467. }
  1468. }
  1469. }
  1470. if pricePerHour == 0 {
  1471. return 0, fmt.Errorf("Failed to resolve an hourly price from FixedPrice or Recurring Costs")
  1472. }
  1473. return pricePerHour, nil
  1474. }
  1475. func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
  1476. c := &aws.Config{
  1477. Region: aws.String(region),
  1478. }
  1479. s := session.Must(session.NewSession(c))
  1480. svc := ec2.New(s)
  1481. response, err := svc.DescribeReservedInstances(&ec2.DescribeReservedInstancesInput{})
  1482. if err != nil {
  1483. return nil, err
  1484. }
  1485. var reservedInstances []*AWSReservedInstance
  1486. for _, ri := range response.ReservedInstances {
  1487. var zone string
  1488. if ri.AvailabilityZone != nil {
  1489. zone = *ri.AvailabilityZone
  1490. }
  1491. pricePerHour, err := getReservedInstancePrice(ri)
  1492. if err != nil {
  1493. klog.V(1).Infof("Error Resolving Price: %s", err.Error())
  1494. continue
  1495. }
  1496. reservedInstances = append(reservedInstances, &AWSReservedInstance{
  1497. Zone: zone,
  1498. Region: region,
  1499. InstanceType: *ri.InstanceType,
  1500. InstanceCount: *ri.InstanceCount,
  1501. InstanceTenacy: *ri.InstanceTenancy,
  1502. StartDate: *ri.Start,
  1503. EndDate: *ri.End,
  1504. PricePerHour: pricePerHour,
  1505. })
  1506. }
  1507. return reservedInstances, nil
  1508. }
  1509. func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
  1510. err := a.configureAWSAuth()
  1511. if err != nil {
  1512. return nil, fmt.Errorf("Error Configuring aws auth: %s", err.Error())
  1513. }
  1514. var reservedInstances []*AWSReservedInstance
  1515. nodes := a.Clientset.GetAllNodes()
  1516. regionsSeen := make(map[string]bool)
  1517. for _, node := range nodes {
  1518. region, ok := node.Labels[v1.LabelZoneRegion]
  1519. if !ok {
  1520. continue
  1521. }
  1522. if regionsSeen[region] {
  1523. continue
  1524. }
  1525. ris, err := getRegionReservedInstances(region)
  1526. if err != nil {
  1527. klog.V(3).Infof("Error getting reserved instances: %s", err.Error())
  1528. continue
  1529. }
  1530. regionsSeen[region] = true
  1531. reservedInstances = append(reservedInstances, ris...)
  1532. }
  1533. return reservedInstances, nil
  1534. }