awsprovider.go 61 KB

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