awsprovider.go 62 KB

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