azureprovider.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package cloud
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/url"
  9. "os"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "github.com/kubecost/cost-model/clustercache"
  15. "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-09-01/skus"
  16. "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
  17. "github.com/Azure/azure-sdk-for-go/services/preview/commerce/mgmt/2015-06-01-preview/commerce"
  18. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
  19. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
  20. "github.com/Azure/go-autorest/autorest"
  21. "github.com/Azure/go-autorest/autorest/azure"
  22. "github.com/Azure/go-autorest/autorest/azure/auth"
  23. v1 "k8s.io/api/core/v1"
  24. "k8s.io/klog"
  25. )
  26. var (
  27. regionCodeMappings = map[string]string{
  28. "ap": "asia",
  29. "au": "australia",
  30. "br": "brazil",
  31. "ca": "canada",
  32. "eu": "europe",
  33. "fr": "france",
  34. "in": "india",
  35. "ja": "japan",
  36. "kr": "korea",
  37. "uk": "uk",
  38. "us": "us",
  39. "za": "southafrica",
  40. }
  41. //mtBasic, _ = regexp.Compile("^BASIC.A\\d+[_Promo]*$")
  42. //mtStandardA, _ = regexp.Compile("^A\\d+[_Promo]*$")
  43. mtStandardB, _ = regexp.Compile(`^Standard_B\d+m?[_v\d]*[_Promo]*$`)
  44. mtStandardD, _ = regexp.Compile(`^Standard_D\d[_v\d]*[_Promo]*$`)
  45. mtStandardE, _ = regexp.Compile(`^Standard_E\d+i?[_v\d]*[_Promo]*$`)
  46. mtStandardF, _ = regexp.Compile(`^Standard_F\d+[_v\d]*[_Promo]*$`)
  47. mtStandardG, _ = regexp.Compile(`^Standard_G\d+[_v\d]*[_Promo]*$`)
  48. mtStandardL, _ = regexp.Compile(`^Standard_L\d+[_v\d]*[_Promo]*$`)
  49. mtStandardM, _ = regexp.Compile(`^Standard_M\d+[m|t|l]*s[_v\d]*[_Promo]*$`)
  50. mtStandardN, _ = regexp.Compile(`^Standard_N[C|D|V]\d+r?[_v\d]*[_Promo]*$`)
  51. )
  52. type regionParts []string
  53. func (r regionParts) String() string {
  54. var result string
  55. for _, p := range r {
  56. result += p
  57. }
  58. return result
  59. }
  60. func getRegions(service string, subscriptionsClient subscriptions.Client, providersClient resources.ProvidersClient, subscriptionID string) (map[string]string, error) {
  61. allLocations := make(map[string]string)
  62. supLocations := make(map[string]string)
  63. // retrieve all locations for the subscription id (some of them may not be supported by the required provider)
  64. if locations, err := subscriptionsClient.ListLocations(context.TODO(), subscriptionID); err == nil {
  65. // fill up the map: DisplayName - > Name
  66. for _, loc := range *locations.Value {
  67. allLocations[*loc.DisplayName] = *loc.Name
  68. }
  69. } else {
  70. return nil, err
  71. }
  72. // identify supported locations for the namespace and resource type
  73. const (
  74. providerNamespaceForCompute = "Microsoft.Compute"
  75. resourceTypeForCompute = "locations/vmSizes"
  76. providerNamespaceForAks = "Microsoft.ContainerService"
  77. resourceTypeForAks = "managedClusters"
  78. )
  79. switch service {
  80. case "aks":
  81. if providers, err := providersClient.Get(context.TODO(), providerNamespaceForAks, ""); err == nil {
  82. for _, pr := range *providers.ResourceTypes {
  83. if *pr.ResourceType == resourceTypeForAks {
  84. for _, displName := range *pr.Locations {
  85. if loc, ok := allLocations[displName]; ok {
  86. supLocations[loc] = displName
  87. } else {
  88. klog.V(1).Infof("unsupported cloud region %s", loc)
  89. }
  90. }
  91. break
  92. }
  93. }
  94. } else {
  95. return nil, err
  96. }
  97. return supLocations, nil
  98. default:
  99. if providers, err := providersClient.Get(context.TODO(), providerNamespaceForCompute, ""); err == nil {
  100. for _, pr := range *providers.ResourceTypes {
  101. if *pr.ResourceType == resourceTypeForCompute {
  102. for _, displName := range *pr.Locations {
  103. if loc, ok := allLocations[displName]; ok {
  104. supLocations[loc] = displName
  105. } else {
  106. klog.V(1).Infof("unsupported cloud region %s", loc)
  107. }
  108. }
  109. break
  110. }
  111. }
  112. } else {
  113. return nil, err
  114. }
  115. return supLocations, nil
  116. }
  117. }
  118. func toRegionID(meterRegion string, regions map[string]string) (string, error) {
  119. var rp regionParts = strings.Split(strings.ToLower(meterRegion), " ")
  120. regionCode := regionCodeMappings[rp[0]]
  121. lastPart := rp[len(rp)-1]
  122. var regionIds []string
  123. if _, err := strconv.Atoi(lastPart); err == nil {
  124. regionIds = []string{
  125. fmt.Sprintf("%s%s%s", regionCode, rp[1:len(rp)-1], lastPart),
  126. fmt.Sprintf("%s%s%s", rp[1:len(rp)-1], regionCode, lastPart),
  127. }
  128. } else {
  129. regionIds = []string{
  130. fmt.Sprintf("%s%s", regionCode, rp[1:]),
  131. fmt.Sprintf("%s%s", rp[1:], regionCode),
  132. }
  133. }
  134. for _, regionID := range regionIds {
  135. if checkRegionID(regionID, regions) {
  136. return regionID, nil
  137. }
  138. }
  139. return "", fmt.Errorf("Couldn't find region")
  140. }
  141. func checkRegionID(regionID string, regions map[string]string) bool {
  142. for region := range regions {
  143. if regionID == region {
  144. return true
  145. }
  146. }
  147. return false
  148. }
  149. type Azure struct {
  150. allPrices map[string]*Node
  151. DownloadPricingDataLock sync.RWMutex
  152. Clientset clustercache.ClusterCache
  153. }
  154. type azureKey struct {
  155. Labels map[string]string
  156. GPULabel string
  157. GPULabelValue string
  158. }
  159. func (k *azureKey) Features() string {
  160. region := strings.ToLower(k.Labels[v1.LabelZoneRegion])
  161. instance := k.Labels[v1.LabelInstanceType]
  162. usageType := "ondemand"
  163. return fmt.Sprintf("%s,%s,%s", region, instance, usageType)
  164. }
  165. func (k *azureKey) GPUType() string {
  166. if t, ok := k.Labels[k.GPULabel]; ok {
  167. return t
  168. }
  169. return ""
  170. }
  171. func (k *azureKey) ID() string {
  172. return ""
  173. }
  174. func (az *Azure) GetKey(labels map[string]string) Key {
  175. cfg, err := az.GetConfig()
  176. if err != nil {
  177. klog.Infof("Error loading azure custom pricing information")
  178. }
  179. // azure defaults, see https://docs.microsoft.com/en-us/azure/aks/gpu-cluster
  180. gpuLabel := "accelerator"
  181. gpuLabelValue := "nvidia"
  182. if cfg.GpuLabel != "" {
  183. gpuLabel = cfg.GpuLabel
  184. }
  185. if cfg.GpuLabelValue != "" {
  186. gpuLabelValue = cfg.GpuLabelValue
  187. }
  188. return &azureKey{
  189. Labels: labels,
  190. GPULabel: gpuLabel,
  191. GPULabelValue: gpuLabelValue,
  192. }
  193. }
  194. // CreateString builds strings effectively
  195. func createString(keys ...string) string {
  196. var b strings.Builder
  197. for _, key := range keys {
  198. b.WriteString(key)
  199. }
  200. return b.String()
  201. }
  202. func transformMachineType(subCategory string, mt []string) []string {
  203. switch {
  204. case strings.Contains(subCategory, "Basic"):
  205. return []string{createString("Basic_", mt[0])}
  206. case len(mt) == 2:
  207. return []string{createString("Standard_", mt[0]), createString("Standard_", mt[1])}
  208. default:
  209. return []string{createString("Standard_", mt[0])}
  210. }
  211. }
  212. func addSuffix(mt string, suffixes ...string) []string {
  213. result := make([]string, len(suffixes))
  214. var suffix string
  215. parts := strings.Split(mt, "_")
  216. if len(parts) > 2 {
  217. for _, p := range parts[2:] {
  218. suffix = createString(suffix, "_", p)
  219. }
  220. }
  221. for i, s := range suffixes {
  222. result[i] = createString(parts[0], "_", parts[1], s, suffix)
  223. }
  224. return result
  225. }
  226. func getMachineTypeVariants(mt string) []string {
  227. switch {
  228. case mtStandardB.MatchString(mt):
  229. return []string{createString(mt, "s")}
  230. case mtStandardD.MatchString(mt):
  231. var result []string
  232. result = append(result, addSuffix(mt, "s")[0])
  233. dsType := strings.Replace(mt, "Standard_D", "Standard_DS", -1)
  234. result = append(result, dsType)
  235. result = append(result, addSuffix(dsType, "-1", "-2", "-4", "-8")...)
  236. return result
  237. case mtStandardE.MatchString(mt):
  238. return addSuffix(mt, "s", "-2s", "-4s", "-8s", "-16s", "-32s")
  239. case mtStandardF.MatchString(mt):
  240. return addSuffix(mt, "s")
  241. case mtStandardG.MatchString(mt):
  242. var result []string
  243. gsType := strings.Replace(mt, "Standard_G", "Standard_GS", -1)
  244. result = append(result, gsType)
  245. return append(result, addSuffix(gsType, "-4", "-8", "-16")...)
  246. case mtStandardL.MatchString(mt):
  247. return addSuffix(mt, "s")
  248. case mtStandardM.MatchString(mt) && strings.HasSuffix(mt, "ms"):
  249. base := strings.TrimSuffix(mt, "ms")
  250. return addSuffix(base, "-2ms", "-4ms", "-8ms", "-16ms", "-32ms", "-64ms")
  251. case mtStandardM.MatchString(mt) && (strings.HasSuffix(mt, "ls") || strings.HasSuffix(mt, "ts")):
  252. return []string{}
  253. case mtStandardM.MatchString(mt) && strings.HasSuffix(mt, "s"):
  254. base := strings.TrimSuffix(mt, "s")
  255. return addSuffix(base, "", "m")
  256. case mtStandardN.MatchString(mt):
  257. return addSuffix(mt, "s")
  258. }
  259. return []string{}
  260. }
  261. func (az *Azure) GetManagementPlatform() (string, error) {
  262. nodes := az.Clientset.GetAllNodes()
  263. if len(nodes) > 0 {
  264. n := nodes[0]
  265. providerID := n.Spec.ProviderID
  266. if strings.Contains(providerID, "aks") {
  267. return "aks", nil
  268. }
  269. }
  270. return "", nil
  271. }
  272. // DownloadPricingData uses provided azure "best guesses" for pricing
  273. func (az *Azure) DownloadPricingData() error {
  274. az.DownloadPricingDataLock.Lock()
  275. defer az.DownloadPricingDataLock.Unlock()
  276. config, err := az.GetConfig()
  277. if err != nil {
  278. return err
  279. }
  280. var authorizer autorest.Authorizer
  281. if config.AzureClientID != "" && config.AzureClientSecret != "" && config.AzureTenantID != "" {
  282. credentialsConfig := auth.NewClientCredentialsConfig(config.AzureClientID, config.AzureClientSecret, config.AzureTenantID)
  283. a, err := credentialsConfig.Authorizer()
  284. if err != nil {
  285. return err
  286. }
  287. authorizer = a
  288. }
  289. if authorizer == nil {
  290. a, err := auth.NewAuthorizerFromEnvironment()
  291. authorizer = a
  292. if err != nil { // Failed to create authorizer from environment, try from file
  293. a, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
  294. if err != nil {
  295. return err
  296. }
  297. authorizer = a
  298. }
  299. }
  300. sClient := subscriptions.NewClient()
  301. sClient.Authorizer = authorizer
  302. rcClient := commerce.NewRateCardClient(config.AzureSubscriptionID)
  303. rcClient.Authorizer = authorizer
  304. skusClient := skus.NewResourceSkusClient(config.AzureSubscriptionID)
  305. skusClient.Authorizer = authorizer
  306. providersClient := resources.NewProvidersClient(config.AzureSubscriptionID)
  307. providersClient.Authorizer = authorizer
  308. containerServiceClient := containerservice.NewContainerServicesClient(config.AzureSubscriptionID)
  309. containerServiceClient.Authorizer = authorizer
  310. rateCardFilter := fmt.Sprintf("OfferDurableId eq 'MS-AZR-0003p' and Currency eq '%s' and Locale eq 'en-US' and RegionInfo eq '%s'", config.CurrencyCode, config.AzureBillingRegion)
  311. klog.Infof("Using ratecard query %s", rateCardFilter)
  312. result, err := rcClient.Get(context.TODO(), rateCardFilter)
  313. if err != nil {
  314. return err
  315. }
  316. allPrices := make(map[string]*Node)
  317. regions, err := getRegions("compute", sClient, providersClient, config.AzureSubscriptionID)
  318. if err != nil {
  319. return err
  320. }
  321. c, err := az.GetConfig()
  322. if err != nil {
  323. return err
  324. }
  325. baseCPUPrice := c.CPU
  326. for _, v := range *result.Meters {
  327. if !strings.Contains(*v.MeterSubCategory, "Windows") && strings.Contains(*v.MeterCategory, "Virtual Machines") {
  328. region, err := toRegionID(*v.MeterRegion, regions)
  329. if err != nil {
  330. continue
  331. }
  332. meterName := *v.MeterName
  333. sc := *v.MeterSubCategory
  334. // not available now
  335. if strings.Contains(sc, "Promo") {
  336. continue
  337. }
  338. usageType := ""
  339. if !strings.Contains(meterName, "Low Priority") {
  340. usageType = "ondemand"
  341. } else {
  342. usageType = "preemptible"
  343. }
  344. var instanceTypes []string
  345. name := strings.TrimSuffix(meterName, " Low Priority")
  346. instanceType := strings.Split(name, "/")
  347. for _, it := range instanceType {
  348. instanceTypes = append(instanceTypes, strings.Replace(it, " ", "_", 1))
  349. }
  350. instanceTypes = transformMachineType(sc, instanceTypes)
  351. if strings.Contains(name, "Expired") {
  352. instanceTypes = []string{}
  353. }
  354. var priceInUsd float64
  355. if len(v.MeterRates) < 1 {
  356. klog.V(1).Infof("missing rate info %+v", map[string]interface{}{"MeterSubCategory": *v.MeterSubCategory, "region": region})
  357. continue
  358. }
  359. for _, rate := range v.MeterRates {
  360. priceInUsd += *rate
  361. }
  362. priceStr := fmt.Sprintf("%f", priceInUsd)
  363. for _, instanceType := range instanceTypes {
  364. key := fmt.Sprintf("%s,%s,%s", region, instanceType, usageType)
  365. allPrices[key] = &Node{
  366. Cost: priceStr,
  367. BaseCPUPrice: baseCPUPrice,
  368. }
  369. }
  370. }
  371. }
  372. az.allPrices = allPrices
  373. return nil
  374. }
  375. // AllNodePricing returns the Azure pricing objects stored
  376. func (az *Azure) AllNodePricing() (interface{}, error) {
  377. az.DownloadPricingDataLock.RLock()
  378. defer az.DownloadPricingDataLock.RUnlock()
  379. return az.allPrices, nil
  380. }
  381. // NodePricing returns Azure pricing data for a single node
  382. func (az *Azure) NodePricing(key Key) (*Node, error) {
  383. az.DownloadPricingDataLock.RLock()
  384. defer az.DownloadPricingDataLock.RUnlock()
  385. if n, ok := az.allPrices[key.Features()]; ok {
  386. klog.V(4).Infof("Returning pricing for node %s: %+v from key %s", key, n, key.Features())
  387. if key.GPUType() != "" {
  388. n.GPU = "1" // TODO: support multiple GPUs
  389. }
  390. return n, nil
  391. }
  392. klog.V(1).Infof("[Warning] no pricing data found for %s: %s", key.Features(), key)
  393. c, err := az.GetConfig()
  394. if err != nil {
  395. return nil, fmt.Errorf("No default pricing data available")
  396. }
  397. if key.GPUType() != "" {
  398. return &Node{
  399. VCPUCost: c.CPU,
  400. RAMCost: c.RAM,
  401. GPUCost: c.GPU,
  402. GPU: "1", // TODO: support multiple GPUs
  403. }, nil
  404. }
  405. return &Node{
  406. VCPUCost: c.CPU,
  407. RAMCost: c.RAM,
  408. UsesBaseCPUPrice: true,
  409. }, nil
  410. }
  411. // Stubbed NetworkPricing for Azure. Pull directly from azure.json for now
  412. func (c *Azure) NetworkPricing() (*Network, error) {
  413. cpricing, err := GetCustomPricingData("azure.json")
  414. if err != nil {
  415. return nil, err
  416. }
  417. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  418. if err != nil {
  419. return nil, err
  420. }
  421. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  422. if err != nil {
  423. return nil, err
  424. }
  425. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  426. if err != nil {
  427. return nil, err
  428. }
  429. return &Network{
  430. ZoneNetworkEgressCost: znec,
  431. RegionNetworkEgressCost: rnec,
  432. InternetNetworkEgressCost: inec,
  433. }, nil
  434. }
  435. type azurePvKey struct {
  436. Labels map[string]string
  437. StorageClass string
  438. StorageClassParameters map[string]string
  439. }
  440. func (az *Azure) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  441. return &azurePvKey{
  442. Labels: pv.Labels,
  443. StorageClass: pv.Spec.StorageClassName,
  444. StorageClassParameters: parameters,
  445. }
  446. }
  447. func (key *azurePvKey) GetStorageClass() string {
  448. return key.StorageClass
  449. }
  450. func (key *azurePvKey) Features() string {
  451. storageClass := key.StorageClassParameters["type"]
  452. if storageClass == "pd-ssd" {
  453. storageClass = "ssd"
  454. } else if storageClass == "pd-standard" {
  455. storageClass = "pdstandard"
  456. }
  457. return key.Labels[v1.LabelZoneRegion] + "," + storageClass
  458. }
  459. func (*Azure) GetDisks() ([]byte, error) {
  460. return nil, nil
  461. }
  462. func (az *Azure) ClusterInfo() (map[string]string, error) {
  463. remote := os.Getenv(remoteEnabled)
  464. remoteEnabled := false
  465. if os.Getenv(remote) == "true" {
  466. remoteEnabled = true
  467. }
  468. m := make(map[string]string)
  469. m["name"] = "Azure Cluster #1"
  470. c, err := az.GetConfig()
  471. if err != nil {
  472. return nil, err
  473. }
  474. if c.ClusterName != "" {
  475. m["name"] = c.ClusterName
  476. }
  477. m["provider"] = "azure"
  478. m["remoteReadEnabled"] = strconv.FormatBool(remoteEnabled)
  479. m["id"] = os.Getenv(clusterIDKey)
  480. return m, nil
  481. }
  482. func (az *Azure) AddServiceKey(url url.Values) error {
  483. return nil
  484. }
  485. func (az *Azure) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  486. c, err := GetCustomPricingData("azure.json")
  487. if err != nil {
  488. return nil, err
  489. }
  490. return configmapUpdate(c, configPathFor("azure.json"), a)
  491. }
  492. func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  493. defer az.DownloadPricingData()
  494. c, err := GetCustomPricingData("azure.json")
  495. if err != nil {
  496. return nil, err
  497. }
  498. a := make(map[string]interface{})
  499. err = json.NewDecoder(r).Decode(&a)
  500. if err != nil {
  501. return nil, err
  502. }
  503. for k, v := range a {
  504. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  505. vstr, ok := v.(string)
  506. if ok {
  507. err := SetCustomPricingField(c, kUpper, vstr)
  508. if err != nil {
  509. return nil, err
  510. }
  511. } else {
  512. sci := v.(map[string]interface{})
  513. sc := make(map[string]string)
  514. for k, val := range sci {
  515. sc[k] = val.(string)
  516. }
  517. c.SharedCosts = sc //todo: support reflection/multiple map fields
  518. }
  519. }
  520. cj, err := json.Marshal(c)
  521. if err != nil {
  522. return nil, err
  523. }
  524. remoteEnabled := os.Getenv(remoteEnabled)
  525. if remoteEnabled == "true" {
  526. err = UpdateClusterMeta(os.Getenv(clusterIDKey), c.ClusterName)
  527. if err != nil {
  528. return nil, err
  529. }
  530. }
  531. configPath := configPathFor("azure.json")
  532. configLock.Lock()
  533. err = ioutil.WriteFile(configPath, cj, 0644)
  534. configLock.Unlock()
  535. if err != nil {
  536. return nil, err
  537. }
  538. return c, nil
  539. }
  540. func (az *Azure) GetConfig() (*CustomPricing, error) {
  541. c, err := GetCustomPricingData("azure.json")
  542. if c.Discount == "" {
  543. c.Discount = "0%"
  544. }
  545. if c.NegotiatedDiscount == "" {
  546. c.NegotiatedDiscount = "0%"
  547. }
  548. if c.CurrencyCode == "" {
  549. c.CurrencyCode = "USD"
  550. }
  551. if c.AzureBillingRegion == "" {
  552. c.AzureBillingRegion = "US"
  553. }
  554. if err != nil {
  555. return nil, err
  556. }
  557. return c, nil
  558. }
  559. func (az *Azure) ExternalAllocations(string, string, string, string, string) ([]*OutOfClusterAllocation, error) {
  560. return nil, nil
  561. }
  562. func (az *Azure) ApplyReservedInstancePricing(nodes map[string]*Node) {
  563. }
  564. func (az *Azure) PVPricing(PVKey) (*PV, error) {
  565. return nil, nil
  566. }
  567. func (az *Azure) GetLocalStorageQuery(offset string) (string, error) {
  568. return "", nil
  569. }