azureprovider.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. package cloud
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "os"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "github.com/kubecost/cost-model/clustercache"
  14. "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-09-01/skus"
  15. "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
  16. "github.com/Azure/azure-sdk-for-go/services/preview/commerce/mgmt/2015-06-01-preview/commerce"
  17. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
  18. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
  19. "github.com/Azure/go-autorest/autorest"
  20. "github.com/Azure/go-autorest/autorest/azure"
  21. "github.com/Azure/go-autorest/autorest/azure/auth"
  22. v1 "k8s.io/api/core/v1"
  23. "k8s.io/klog"
  24. )
  25. var (
  26. regionCodeMappings = map[string]string{
  27. "ap": "asia",
  28. "au": "australia",
  29. "br": "brazil",
  30. "ca": "canada",
  31. "eu": "europe",
  32. "fr": "france",
  33. "in": "india",
  34. "ja": "japan",
  35. "kr": "korea",
  36. "uk": "uk",
  37. "us": "us",
  38. "za": "southafrica",
  39. }
  40. //mtBasic, _ = regexp.Compile("^BASIC.A\\d+[_Promo]*$")
  41. //mtStandardA, _ = regexp.Compile("^A\\d+[_Promo]*$")
  42. mtStandardB, _ = regexp.Compile(`^Standard_B\d+m?[_v\d]*[_Promo]*$`)
  43. mtStandardD, _ = regexp.Compile(`^Standard_D\d[_v\d]*[_Promo]*$`)
  44. mtStandardE, _ = regexp.Compile(`^Standard_E\d+i?[_v\d]*[_Promo]*$`)
  45. mtStandardF, _ = regexp.Compile(`^Standard_F\d+[_v\d]*[_Promo]*$`)
  46. mtStandardG, _ = regexp.Compile(`^Standard_G\d+[_v\d]*[_Promo]*$`)
  47. mtStandardL, _ = regexp.Compile(`^Standard_L\d+[_v\d]*[_Promo]*$`)
  48. mtStandardM, _ = regexp.Compile(`^Standard_M\d+[m|t|l]*s[_v\d]*[_Promo]*$`)
  49. mtStandardN, _ = regexp.Compile(`^Standard_N[C|D|V]\d+r?[_v\d]*[_Promo]*$`)
  50. )
  51. type regionParts []string
  52. func (r regionParts) String() string {
  53. var result string
  54. for _, p := range r {
  55. result += p
  56. }
  57. return result
  58. }
  59. func getRegions(service string, subscriptionsClient subscriptions.Client, providersClient resources.ProvidersClient, subscriptionID string) (map[string]string, error) {
  60. allLocations := make(map[string]string)
  61. supLocations := make(map[string]string)
  62. // retrieve all locations for the subscription id (some of them may not be supported by the required provider)
  63. if locations, err := subscriptionsClient.ListLocations(context.TODO(), subscriptionID); err == nil {
  64. // fill up the map: DisplayName - > Name
  65. for _, loc := range *locations.Value {
  66. allLocations[*loc.DisplayName] = *loc.Name
  67. }
  68. } else {
  69. return nil, err
  70. }
  71. // identify supported locations for the namespace and resource type
  72. const (
  73. providerNamespaceForCompute = "Microsoft.Compute"
  74. resourceTypeForCompute = "locations/vmSizes"
  75. providerNamespaceForAks = "Microsoft.ContainerService"
  76. resourceTypeForAks = "managedClusters"
  77. )
  78. switch service {
  79. case "aks":
  80. if providers, err := providersClient.Get(context.TODO(), providerNamespaceForAks, ""); err == nil {
  81. for _, pr := range *providers.ResourceTypes {
  82. if *pr.ResourceType == resourceTypeForAks {
  83. for _, displName := range *pr.Locations {
  84. if loc, ok := allLocations[displName]; ok {
  85. supLocations[loc] = displName
  86. } else {
  87. klog.V(1).Infof("unsupported cloud region %s", loc)
  88. }
  89. }
  90. break
  91. }
  92. }
  93. } else {
  94. return nil, err
  95. }
  96. return supLocations, nil
  97. default:
  98. if providers, err := providersClient.Get(context.TODO(), providerNamespaceForCompute, ""); err == nil {
  99. for _, pr := range *providers.ResourceTypes {
  100. if *pr.ResourceType == resourceTypeForCompute {
  101. for _, displName := range *pr.Locations {
  102. if loc, ok := allLocations[displName]; ok {
  103. supLocations[loc] = displName
  104. } else {
  105. klog.V(1).Infof("unsupported cloud region %s", loc)
  106. }
  107. }
  108. break
  109. }
  110. }
  111. } else {
  112. return nil, err
  113. }
  114. return supLocations, nil
  115. }
  116. }
  117. func toRegionID(meterRegion string, regions map[string]string) (string, error) {
  118. var rp regionParts = strings.Split(strings.ToLower(meterRegion), " ")
  119. regionCode := regionCodeMappings[rp[0]]
  120. lastPart := rp[len(rp)-1]
  121. var regionIds []string
  122. if _, err := strconv.Atoi(lastPart); err == nil {
  123. regionIds = []string{
  124. fmt.Sprintf("%s%s%s", regionCode, rp[1:len(rp)-1], lastPart),
  125. fmt.Sprintf("%s%s%s", rp[1:len(rp)-1], regionCode, lastPart),
  126. }
  127. } else {
  128. regionIds = []string{
  129. fmt.Sprintf("%s%s", regionCode, rp[1:]),
  130. fmt.Sprintf("%s%s", rp[1:], regionCode),
  131. }
  132. }
  133. for _, regionID := range regionIds {
  134. if checkRegionID(regionID, regions) {
  135. return regionID, nil
  136. }
  137. }
  138. return "", fmt.Errorf("Couldn't find region")
  139. }
  140. func checkRegionID(regionID string, regions map[string]string) bool {
  141. for region := range regions {
  142. if regionID == region {
  143. return true
  144. }
  145. }
  146. return false
  147. }
  148. type Azure struct {
  149. allPrices map[string]*Node
  150. DownloadPricingDataLock sync.RWMutex
  151. Clientset clustercache.ClusterCache
  152. Config *ProviderConfig
  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 (az *Azure) NetworkPricing() (*Network, error) {
  413. cpricing, err := az.Config.GetCustomPricingData()
  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. return az.Config.UpdateFromMap(a)
  487. }
  488. func (az *Azure) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  489. defer az.DownloadPricingData()
  490. return az.Config.Update(func(c *CustomPricing) error {
  491. a := make(map[string]interface{})
  492. err := json.NewDecoder(r).Decode(&a)
  493. if err != nil {
  494. return err
  495. }
  496. for k, v := range a {
  497. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  498. vstr, ok := v.(string)
  499. if ok {
  500. err := SetCustomPricingField(c, kUpper, vstr)
  501. if err != nil {
  502. return err
  503. }
  504. } else {
  505. sci := v.(map[string]interface{})
  506. sc := make(map[string]string)
  507. for k, val := range sci {
  508. sc[k] = val.(string)
  509. }
  510. c.SharedCosts = sc //todo: support reflection/multiple map fields
  511. }
  512. }
  513. remoteEnabled := os.Getenv(remoteEnabled)
  514. if remoteEnabled == "true" {
  515. err := UpdateClusterMeta(os.Getenv(clusterIDKey), c.ClusterName)
  516. if err != nil {
  517. return err
  518. }
  519. }
  520. return nil
  521. })
  522. }
  523. func (az *Azure) GetConfig() (*CustomPricing, error) {
  524. c, err := az.Config.GetCustomPricingData()
  525. if c.Discount == "" {
  526. c.Discount = "0%"
  527. }
  528. if c.NegotiatedDiscount == "" {
  529. c.NegotiatedDiscount = "0%"
  530. }
  531. if c.CurrencyCode == "" {
  532. c.CurrencyCode = "USD"
  533. }
  534. if c.AzureBillingRegion == "" {
  535. c.AzureBillingRegion = "US"
  536. }
  537. if err != nil {
  538. return nil, err
  539. }
  540. return c, nil
  541. }
  542. func (az *Azure) ExternalAllocations(string, string, string, string, string) ([]*OutOfClusterAllocation, error) {
  543. return nil, nil
  544. }
  545. func (az *Azure) ApplyReservedInstancePricing(nodes map[string]*Node) {
  546. }
  547. func (az *Azure) PVPricing(PVKey) (*PV, error) {
  548. return nil, nil
  549. }
  550. func (az *Azure) GetLocalStorageQuery(window, offset string, rate bool) string {
  551. return ""
  552. }