provider.go 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. package alibaba
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "math"
  7. "os"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  13. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  14. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/signers"
  15. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  16. "github.com/opencost/opencost/core/pkg/clustercache"
  17. coreenv "github.com/opencost/opencost/core/pkg/env"
  18. "github.com/opencost/opencost/core/pkg/log"
  19. "github.com/opencost/opencost/core/pkg/opencost"
  20. "github.com/opencost/opencost/core/pkg/util/fileutil"
  21. "github.com/opencost/opencost/core/pkg/util/json"
  22. "github.com/opencost/opencost/core/pkg/util/stringutil"
  23. "github.com/opencost/opencost/pkg/cloud/models"
  24. "github.com/opencost/opencost/pkg/cloud/utils"
  25. "github.com/opencost/opencost/pkg/env"
  26. "golang.org/x/exp/slices"
  27. )
  28. const (
  29. ALIBABA_ECS_PRODUCT_CODE = "ecs"
  30. ALIBABA_ECS_VERSION = "2014-05-26"
  31. ALIBABA_ECS_DOMAIN = "ecs.aliyuncs.com"
  32. ALIBABA_DESCRIBE_PRICE_API_ACTION = "DescribePrice"
  33. ALIBABA_DESCRIBE_DISK_API_ACTION = "DescribeDisks"
  34. ALIBABA_INSTANCE_RESOURCE_TYPE = "instance"
  35. ALIBABA_DISK_RESOURCE_TYPE = "disk"
  36. ALIBABA_PAY_AS_YOU_GO_BILLING = "Pay-As-You-Go"
  37. ALIBABA_SUBSCRIPTION_BILLING = "Subscription"
  38. ALIBABA_PREEMPTIBLE_BILLING = "Preemptible"
  39. ALIBABA_OPTIMIZE_KEYWORD = "optimize"
  40. ALIBABA_NON_OPTIMIZE_KEYWORD = "nonoptimize"
  41. ALIBABA_HOUR_PRICE_UNIT = "Hour"
  42. ALIBABA_MONTH_PRICE_UNIT = "Month"
  43. ALIBABA_YEAR_PRICE_UNIT = "Year"
  44. ALIBABA_UNKNOWN_INSTANCE_FAMILY_TYPE = "unknown"
  45. ALIBABA_NOT_SUPPORTED_INSTANCE_FAMILY_TYPE = "unsupported"
  46. ALIBABA_DISK_CLOUD_ESSD_CATEGORY = "cloud_essd"
  47. ALIBABA_DISK_CLOUD_CATEGORY = "cloud"
  48. ALIBABA_DATA_DISK_CATEGORY = "data"
  49. ALIBABA_SYSTEM_DISK_CATEGORY = "system"
  50. ALIBABA_DATA_DISK_PREFIX = "DataDisk"
  51. ALIBABA_PV_CLOUD_DISK_TYPE = "CloudDisk"
  52. ALIBABA_PV_NAS_TYPE = "NAS"
  53. ALIBABA_PV_OSS_TYPE = "OSS"
  54. ALIBABA_DEFAULT_DATADISK_SIZE = "2000"
  55. ALIBABA_DISK_TOPOLOGY_REGION_LABEL = "topology.diskplugin.csi.alibabacloud.com/region"
  56. ALIBABA_DISK_TOPOLOGY_ZONE_LABEL = "topology.diskplugin.csi.alibabacloud.com/zone"
  57. )
  58. var (
  59. // sizeRegEx parses a PV capacity string into a numeric part and an optional binary SI suffix (Ki, Mi, Gi, Ti).
  60. sizeRegEx = regexp.MustCompile(`^(\d+(?:\.\d+)?)(Ki|Mi|Gi|Ti)?$`)
  61. )
  62. // Variable to keep track of instance families that fail in DescribePrice API due improper defaulting of systemDisk if the information is not available
  63. var alibabaDefaultToCloudEssd = []string{"g6e", "r6e"}
  64. var alibabaRegions = []string{
  65. "cn-qingdao",
  66. "cn-beijing",
  67. "cn-zhangjiakou",
  68. "cn-huhehaote",
  69. "cn-wulanchabu",
  70. "cn-hangzhou",
  71. "cn-shanghai",
  72. "cn-nanjing",
  73. "cn-shenzhen",
  74. "cn-heyuan",
  75. "cn-guangzhou",
  76. "cn-fuzhou",
  77. "cn-wuhan-lr",
  78. "cn-chengdu",
  79. "cn-hongkong",
  80. "ap-northeast-1",
  81. "ap-northeast-2",
  82. "ap-southeast-1",
  83. "ap-southeast-2",
  84. "ap-southeast-3",
  85. "ap-southeast-6",
  86. "ap-southeast-5",
  87. "ap-south-1",
  88. "ap-southeast-7",
  89. "us-east-1",
  90. "us-west-1",
  91. "eu-west-1",
  92. "me-east-1",
  93. "me-central-1",
  94. "eu-central-1",
  95. }
  96. // AlibabaInfo contains configuration for Alibaba's CUR integration
  97. // Deprecated: v1.104 Use BOAConfiguration instead
  98. type AlibabaInfo struct {
  99. AlibabaClusterRegion string `json:"ClusterRegion"`
  100. AlibabaServiceKeyName string `json:"serviceKeyName"`
  101. AlibabaServiceKeySecret string `json:"serviceKeySecret"`
  102. AlibabaAccountID string `json:"accountID"`
  103. }
  104. // IsEmpty returns true if all fields in config are empty, false if not.
  105. func (ai *AlibabaInfo) IsEmpty() bool {
  106. return ai.AlibabaClusterRegion == "" &&
  107. ai.AlibabaServiceKeyName == "" &&
  108. ai.AlibabaServiceKeySecret == "" &&
  109. ai.AlibabaAccountID == ""
  110. }
  111. // AlibabaAccessKey holds Alibaba credentials parsing from the service-key.json file.
  112. // Deprecated: v1.104 Use AccessKey instead
  113. type AlibabaAccessKey struct {
  114. AccessKeyID string `json:"alibaba_access_key_id"`
  115. SecretAccessKey string `json:"alibaba_secret_access_key"`
  116. }
  117. // Slim Version of k8s disk assigned to a node or PV.
  118. type SlimK8sDisk struct {
  119. DiskType string
  120. RegionID string
  121. PriceUnit string
  122. SizeInGiB string
  123. DiskCategory string
  124. PerformanceLevel string
  125. ProviderID string
  126. StorageClass string
  127. }
  128. func NewSlimK8sDisk(diskType, regionID, priceUnit, diskCategory, performanceLevel, providerID, storageClass, sizeInGiB string) *SlimK8sDisk {
  129. return &SlimK8sDisk{
  130. DiskType: diskType,
  131. RegionID: regionID,
  132. PriceUnit: priceUnit,
  133. SizeInGiB: sizeInGiB,
  134. DiskCategory: diskCategory,
  135. PerformanceLevel: performanceLevel,
  136. ProviderID: providerID,
  137. StorageClass: storageClass,
  138. }
  139. }
  140. // Slim version of a k8s v1.node just to pass along the object of this struct instead of constant getting the labels from within v1.Node & unit testing.
  141. type SlimK8sNode struct {
  142. InstanceType string
  143. RegionID string
  144. PriceUnit string
  145. MemorySizeInKiB string // TO-DO : Possible to convert to float?
  146. IsIoOptimized bool
  147. OSType string
  148. ProviderID string
  149. SystemDisk *SlimK8sDisk
  150. InstanceTypeFamily string // Bug in DescribePrice, doesn't default to enhanced type correctly and you get an error in DescribePrice to get around need the family of the InstanceType.
  151. }
  152. func NewSlimK8sNode(instanceType, regionID, priceUnit, memorySizeInKiB, osType, providerID, instanceTypeFamily string, isIOOptimized bool, systemDiskInfo *SlimK8sDisk) *SlimK8sNode {
  153. return &SlimK8sNode{
  154. InstanceType: instanceType,
  155. RegionID: regionID,
  156. PriceUnit: priceUnit,
  157. MemorySizeInKiB: memorySizeInKiB,
  158. IsIoOptimized: isIOOptimized,
  159. OSType: osType,
  160. SystemDisk: systemDiskInfo,
  161. ProviderID: providerID,
  162. InstanceTypeFamily: instanceTypeFamily,
  163. }
  164. }
  165. // AlibabaNodeAttributes represents metadata about the Node in its pricing information.
  166. // Basic Attributes needed atleast to get the key, Some attributes from k8s Node response
  167. // be populated directly into *Node object.
  168. type AlibabaNodeAttributes struct {
  169. // InstanceType represents the type of instance.
  170. InstanceType string `json:"instanceType"`
  171. // MemorySizeInKiB represents the size of memory of instance.
  172. MemorySizeInKiB string `json:"memorySizeInKiB"`
  173. // IsIoOptimized represents the if instance is I/O optimized.
  174. IsIoOptimized bool `json:"isIoOptimized"`
  175. // OSType represents the OS installed in the Instance.
  176. OSType string `json:"osType"`
  177. // SystemDiskCategory represents the exact category of the system disk attached to the node.
  178. SystemDiskCategory string `json:"systemDiskCategory"`
  179. // SystemDiskSizeInGiB represents the size of the system disk attached to the node.
  180. SystemDiskSizeInGiB string `json:"systemDiskSizeInGiB"`
  181. // SystemDiskPerformanceLevel represents the performance level of the system disk attached to the node.
  182. SystemDiskPerformanceLevel string `json:"systemPerformanceLevel"`
  183. }
  184. func NewAlibabaNodeAttributes(node *SlimK8sNode) *AlibabaNodeAttributes {
  185. if node == nil {
  186. return nil
  187. }
  188. var diskCategory, sizeInGiB, performanceLevel string
  189. if node.SystemDisk != nil {
  190. diskCategory = node.SystemDisk.DiskCategory
  191. sizeInGiB = node.SystemDisk.SizeInGiB
  192. performanceLevel = node.SystemDisk.PerformanceLevel
  193. }
  194. return &AlibabaNodeAttributes{
  195. InstanceType: node.InstanceType,
  196. MemorySizeInKiB: node.MemorySizeInKiB,
  197. IsIoOptimized: node.IsIoOptimized,
  198. OSType: node.OSType,
  199. SystemDiskCategory: diskCategory,
  200. SystemDiskSizeInGiB: sizeInGiB,
  201. SystemDiskPerformanceLevel: performanceLevel,
  202. }
  203. }
  204. // AlibabaPVAttributes represents metadata the PV in its pricing information.
  205. // Basic Attributes needed atleast to get the keys. Some attributes from k8s PV response
  206. // be populated directly into *PV object.
  207. type AlibabaPVAttributes struct {
  208. // PVType can be Cloud Disk, NetWork Attached Storage(NAS) or Object Storage Service (OSS).
  209. // Represents the way the PV was attached
  210. PVType string `json:"pvType"`
  211. // PVSubType represent the sub category of PVType. This is Data in case of Cloud Disk.
  212. PVSubType string `json:"pvSubType"`
  213. // Example for PVCategory with cloudDisk PVType are cloud, cloud_efficiency, cloud_ssd,
  214. // ephemeral_ssd and cloud_essd. If not present returns empty.
  215. PVCategory string `json:"pvCategory"`
  216. // Example for PerformanceLevel with cloudDisk PVType are PL0,PL1,PL2 &PL3. If not present returns empty.
  217. PVPerformanceLevel string `json:"performanceLevel"`
  218. // The Size of the PV in terms of GiB
  219. SizeInGiB string `json:"sizeInGiB"`
  220. }
  221. // TO-Do: next iteration of Alibaba provider support NetWork Attached Storage(NAS) and Object Storage Service (OSS type PVs).
  222. // Currently defaulting to cloudDisk with provision to add work in future.
  223. func NewAlibabaPVAttributes(disk *SlimK8sDisk) *AlibabaPVAttributes {
  224. if disk == nil {
  225. return nil
  226. }
  227. return &AlibabaPVAttributes{
  228. PVType: ALIBABA_PV_CLOUD_DISK_TYPE,
  229. PVSubType: disk.DiskType,
  230. PVCategory: disk.DiskCategory,
  231. PVPerformanceLevel: disk.PerformanceLevel,
  232. SizeInGiB: disk.SizeInGiB,
  233. }
  234. }
  235. // Stage 1 support will be Pay-As-You-Go with HourlyPrice equal to TradePrice with PriceUnit as Hour
  236. // TO-DO: Subscription and Premptible support, Information can be gathered from describing instance for subscription type
  237. // and spotprice can be gather from DescribeSpotPriceHistory API.
  238. // TO-DO: how would you calculate hourly price for subscription type, is it PRICE_YEARLY/HOURS_IN_THE_YEAR|MONTH?
  239. type AlibabaPricingDetails struct {
  240. // Represents hourly price for the given Alibaba cloud Product.
  241. HourlyPrice float32 `json:"hourlyPrice"`
  242. // Represents the unit in which Alibaba Product is billed can be Hour, Month or Year based on the billingMethod.
  243. PriceUnit string `json:"priceUnit"`
  244. // Original Price paid to acquire the Alibaba Product.
  245. TradePrice float32 `json:"tradePrice"`
  246. // Represents the currency unit of the price for billing Alibaba Product.
  247. CurrencyCode string `json:"currencyCode"`
  248. }
  249. func NewAlibabaPricingDetails(hourlyPrice float32, priceUnit string, tradePrice float32, currencyCode string) *AlibabaPricingDetails {
  250. return &AlibabaPricingDetails{
  251. HourlyPrice: hourlyPrice,
  252. PriceUnit: priceUnit,
  253. TradePrice: tradePrice,
  254. CurrencyCode: currencyCode,
  255. }
  256. }
  257. // AlibabaPricingTerms can have three types of supported billing method Pay-As-You-Go, Subscription and Premptible
  258. type AlibabaPricingTerms struct {
  259. BillingMethod string `json:"billingMethod"`
  260. PricingDetails *AlibabaPricingDetails `json:"pricingDetails"`
  261. }
  262. func NewAlibabaPricingTerms(billingMethod string, pricingDetails *AlibabaPricingDetails) *AlibabaPricingTerms {
  263. return &AlibabaPricingTerms{
  264. BillingMethod: billingMethod,
  265. PricingDetails: pricingDetails,
  266. }
  267. }
  268. // Alibaba Pricing struct carry the Attributes and pricing information for Node or PV
  269. type AlibabaPricing struct {
  270. NodeAttributes *AlibabaNodeAttributes
  271. PVAttributes *AlibabaPVAttributes
  272. PricingTerms *AlibabaPricingTerms
  273. Node *models.Node
  274. PV *models.PV
  275. }
  276. // Alibaba cloud's Provider struct
  277. type Alibaba struct {
  278. // Data to store Alibaba cloud's pricing struct, key in the map represents exact match to
  279. // node.features() or pv.features for easy lookup
  280. Pricing map[string]*AlibabaPricing
  281. // Lock Needed to provide thread safe
  282. DownloadPricingDataLock sync.RWMutex
  283. Clientset clustercache.ClusterCache
  284. Config models.ProviderConfig
  285. ServiceAccountChecks *models.ServiceAccountChecks
  286. ClusterAccountId string
  287. ClusterRegion string
  288. // The following fields are unexported because of avoiding any leak of secrets of these keys.
  289. // Alibaba Access key used specifically in signer interface used to sign API calls
  290. accessKey *credentials.AccessKeyCredential
  291. // Map of regionID to sdk.client to call API for that region
  292. clients map[string]*sdk.Client
  293. }
  294. // GetAlibabaAccessKey return the Access Key used to interact with the Alibaba cloud, if not set it
  295. // set it first by looking at env variables else load it from secret files.
  296. func (alibaba *Alibaba) GetAlibabaAccessKey() (*credentials.AccessKeyCredential, error) {
  297. if alibaba.accessKeyisLoaded() {
  298. return alibaba.accessKey, nil
  299. }
  300. config, err := alibaba.GetConfig()
  301. if err != nil {
  302. return nil, fmt.Errorf("error getting the default config for Alibaba Cloud provider: %w", err)
  303. }
  304. if config.AlibabaServiceKeyName == "" {
  305. config.AlibabaServiceKeyName = env.GetAlibabaAccessKeyID()
  306. }
  307. if config.AlibabaServiceKeySecret == "" {
  308. config.AlibabaServiceKeySecret = env.GetAlibabaAccessKeySecret()
  309. }
  310. if config.AlibabaServiceKeyName == "" && config.AlibabaServiceKeySecret == "" {
  311. log.Debugf("missing service key values for Alibaba cloud integration attempting to use service account integration")
  312. err := alibaba.loadAlibabaAuthSecretAndSetEnv(true)
  313. if err != nil {
  314. return nil, fmt.Errorf("unable to set the Alibaba Cloud key/secret from config file %w", err)
  315. }
  316. config.AlibabaServiceKeyName = env.GetAlibabaAccessKeyID()
  317. config.AlibabaServiceKeySecret = env.GetAlibabaAccessKeySecret()
  318. }
  319. if config.AlibabaServiceKeyName == "" && config.AlibabaServiceKeySecret == "" {
  320. return nil, fmt.Errorf("failed to get the access key for the current alibaba account")
  321. }
  322. // At this point either user is using the alibaba key and secret from secret passed in helm config if not he will use the secret that is passed in custom pricing
  323. // There's no check at this time for if the custom pricing key and secret is valid and that's on the user else there will be errors recorded.
  324. // Key and secret passed in config will supersede key and secret passed while installing Closed source helm chart.
  325. alibaba.accessKey = &credentials.AccessKeyCredential{AccessKeyId: config.AlibabaServiceKeyName, AccessKeySecret: config.AlibabaServiceKeySecret}
  326. return alibaba.accessKey, nil
  327. }
  328. func (alibaba *Alibaba) GetAlibabaCloudInfo() (*AlibabaInfo, error) {
  329. config, err := alibaba.GetConfig()
  330. if err != nil {
  331. return nil, fmt.Errorf("could not retrieve AlibabaCloudInfo %s", err)
  332. }
  333. aak, err := alibaba.GetAlibabaAccessKey()
  334. if err != nil {
  335. return nil, err
  336. }
  337. return &AlibabaInfo{
  338. AlibabaClusterRegion: config.AlibabaClusterRegion,
  339. AlibabaServiceKeyName: aak.AccessKeyId,
  340. AlibabaServiceKeySecret: aak.AccessKeySecret,
  341. AlibabaAccountID: config.ProjectID,
  342. }, nil
  343. }
  344. // DownloadPricingData satisfies the provider interface and downloads the prices for Node instances and PVs.
  345. func (alibaba *Alibaba) DownloadPricingData() error {
  346. alibaba.DownloadPricingDataLock.Lock()
  347. defer alibaba.DownloadPricingDataLock.Unlock()
  348. var aak *credentials.AccessKeyCredential
  349. var err error
  350. if !alibaba.accessKeyisLoaded() {
  351. aak, err = alibaba.GetAlibabaAccessKey()
  352. if err != nil {
  353. return fmt.Errorf("unable to get the access key information: %w", err)
  354. }
  355. } else {
  356. aak = alibaba.accessKey
  357. }
  358. c, err := alibaba.Config.GetCustomPricingData()
  359. if err != nil {
  360. return fmt.Errorf("error downloading default pricing data: %w", err)
  361. }
  362. // Get all the nodes from Alibaba cluster.
  363. nodeList := alibaba.Clientset.GetAllNodes()
  364. var client *sdk.Client
  365. var signer *signers.AccessKeySigner
  366. var ok bool
  367. var lookupKey string
  368. alibaba.clients = make(map[string]*sdk.Client)
  369. alibaba.Pricing = make(map[string]*AlibabaPricing)
  370. for _, node := range nodeList {
  371. pricingObj := &AlibabaPricing{}
  372. slimK8sNode := generateSlimK8sNodeFromV1Node(node)
  373. if client, ok = alibaba.clients[slimK8sNode.RegionID]; !ok {
  374. client, err = sdk.NewClientWithAccessKey(slimK8sNode.RegionID, aak.AccessKeyId, aak.AccessKeySecret)
  375. if err != nil {
  376. return fmt.Errorf("unable to initiate alibaba cloud sdk client for region %s : %w", slimK8sNode.RegionID, err)
  377. }
  378. alibaba.clients[slimK8sNode.RegionID] = client
  379. }
  380. signer = signers.NewAccessKeySigner(aak)
  381. // Adjust the system Disk information of a Node by retrieving the details of associated disk. If unable to retrieve set it to empty
  382. // system disk to pass through and use defaults with Alibaba pricing API.
  383. instanceID := getInstanceIDFromProviderID(slimK8sNode.ProviderID)
  384. slimK8sNode.SystemDisk = getSystemDiskInfoOfANode(instanceID, slimK8sNode.RegionID, client, signer)
  385. lookupKey, err = determineKeyForPricing(slimK8sNode)
  386. if err != nil {
  387. return fmt.Errorf("unable to determine key for pricing: %w", err)
  388. }
  389. if _, ok := alibaba.Pricing[lookupKey]; ok {
  390. log.Debugf("Pricing information for node with same features %s already exists hence skipping", lookupKey)
  391. continue
  392. }
  393. pricingObj, err = processDescribePriceAndCreateAlibabaPricing(client, slimK8sNode, signer, c)
  394. if err != nil {
  395. return fmt.Errorf("failed to create pricing information for node with type %s with error: %w", slimK8sNode.InstanceType, err)
  396. }
  397. alibaba.Pricing[lookupKey] = pricingObj
  398. }
  399. // set the first occurrence of region from the node
  400. if alibaba.ClusterRegion == "" {
  401. for _, node := range nodeList {
  402. if regionID, ok := node.Labels["topology.kubernetes.io/region"]; ok {
  403. alibaba.ClusterRegion = regionID
  404. break
  405. }
  406. }
  407. }
  408. // PV pricing for only Cloud Disk for now.
  409. // TO-DO: Support both NAS(Network Attached storage) and OSS(Object Storage Service) type PVs
  410. pvList := alibaba.Clientset.GetAllPersistentVolumes()
  411. for _, pv := range pvList {
  412. pvRegion := determinePVRegion(pv)
  413. if pvRegion == "" {
  414. pvRegion = alibaba.ClusterRegion
  415. }
  416. pricingObj := &AlibabaPricing{}
  417. slimK8sDisk := generateSlimK8sDiskFromV1PV(pv, pvRegion)
  418. lookupKey, err = determineKeyForPricing(slimK8sDisk)
  419. if err != nil {
  420. return fmt.Errorf("unable to determine key for pricing: %w", err)
  421. }
  422. if _, ok := alibaba.Pricing[lookupKey]; ok {
  423. log.Debugf("Pricing information for pv with same features %s already exists hence skipping", lookupKey)
  424. continue
  425. }
  426. if client, ok = alibaba.clients[slimK8sDisk.RegionID]; !ok {
  427. client, err = sdk.NewClientWithAccessKey(slimK8sDisk.RegionID, aak.AccessKeyId, aak.AccessKeySecret)
  428. if err != nil {
  429. return fmt.Errorf("unable to initiate alibaba cloud sdk client for region %s : %w", slimK8sDisk.RegionID, err)
  430. }
  431. alibaba.clients[slimK8sDisk.RegionID] = client
  432. }
  433. signer = signers.NewAccessKeySigner(aak)
  434. pricingObj, err = processDescribePriceAndCreateAlibabaPricing(client, slimK8sDisk, signer, c)
  435. if err != nil {
  436. return fmt.Errorf("failed to create pricing information for pv with category %s with error: %w", slimK8sDisk.DiskCategory, err)
  437. }
  438. alibaba.Pricing[lookupKey] = pricingObj
  439. }
  440. return nil
  441. }
  442. // AllNodePricing returns all the pricing data for all nodes and pvs
  443. func (alibaba *Alibaba) AllNodePricing() (interface{}, error) {
  444. alibaba.DownloadPricingDataLock.RLock()
  445. defer alibaba.DownloadPricingDataLock.RUnlock()
  446. return alibaba.Pricing, nil
  447. }
  448. // NodePricing gives pricing information of a specific node given by the key
  449. func (alibaba *Alibaba) NodePricing(key models.Key) (*models.Node, models.PricingMetadata, error) {
  450. alibaba.DownloadPricingDataLock.RLock()
  451. defer alibaba.DownloadPricingDataLock.RUnlock()
  452. // Get node features for the key
  453. keyFeature := key.Features()
  454. meta := models.PricingMetadata{}
  455. pricing, ok := alibaba.Pricing[keyFeature]
  456. if !ok {
  457. keys := make([]string, 0, len(alibaba.Pricing))
  458. for k := range alibaba.Pricing {
  459. keys = append(keys, k)
  460. }
  461. kf := key.(*AlibabaNodeKey)
  462. // Try to look up pricing with no disk attached
  463. pricing, ok = alibaba.Pricing[kf.FeaturesWithOtherDisk("")]
  464. if !ok {
  465. log.Errorf("Node pricing information not found for node with feature: %s . Existing keys are: %+v", keyFeature, keys)
  466. return nil, meta, fmt.Errorf("Node pricing information not found for node with feature: %s letting it use default values", keyFeature)
  467. }
  468. }
  469. log.Debugf("returning the node price for the node with feature: %s", keyFeature)
  470. returnNode := pricing.Node
  471. return returnNode, meta, nil
  472. }
  473. func (alibaba *Alibaba) GpuPricing(nodeLabels map[string]string) (string, error) {
  474. return "", nil
  475. }
  476. // PVPricing gives a pricing information of a specific PV given by PVkey
  477. func (alibaba *Alibaba) PVPricing(pvk models.PVKey) (*models.PV, error) {
  478. alibaba.DownloadPricingDataLock.RLock()
  479. defer alibaba.DownloadPricingDataLock.RUnlock()
  480. keyFeature := pvk.Features()
  481. pricing, ok := alibaba.Pricing[keyFeature]
  482. if !ok {
  483. log.Debugf("Persistent Volume pricing not found for PV with feature: %s", keyFeature)
  484. return nil, fmt.Errorf("Persistent Volume pricing not found for PV with feature: %s letting it use default values", keyFeature)
  485. }
  486. log.Debugf("returning the PV price for the node with feature: %s", keyFeature)
  487. return pricing.PV, nil
  488. }
  489. // Inter zone and Inter region network cost are defaulted based on https://www.alibabacloud.com/help/en/cloud-data-transmission/latest/cross-region-data-transfers
  490. // Internet cost is default based on https://www.alibabacloud.com/help/en/elastic-compute-service/latest/public-bandwidth to $0.123
  491. func (alibaba *Alibaba) NetworkPricing() (*models.Network, error) {
  492. cpricing, err := alibaba.Config.GetCustomPricingData()
  493. if err != nil {
  494. return nil, err
  495. }
  496. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  497. if err != nil {
  498. return nil, err
  499. }
  500. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  501. if err != nil {
  502. return nil, err
  503. }
  504. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  505. if err != nil {
  506. return nil, err
  507. }
  508. nge, err := strconv.ParseFloat(cpricing.NatGatewayEgress, 64)
  509. if err != nil {
  510. return nil, err
  511. }
  512. ngi, err := strconv.ParseFloat(cpricing.NatGatewayIngress, 64)
  513. if err != nil {
  514. return nil, err
  515. }
  516. return &models.Network{
  517. ZoneNetworkEgressCost: znec,
  518. RegionNetworkEgressCost: rnec,
  519. InternetNetworkEgressCost: inec,
  520. NatGatewayEgressCost: nge,
  521. NatGatewayIngressCost: ngi,
  522. }, nil
  523. }
  524. // Alibaba loadbalancer has three different types https://www.alibabacloud.com/product/server-load-balancer,
  525. // defaulted price to classic load balancer https://www.alibabacloud.com/help/en/server-load-balancer/latest/pay-as-you-go.
  526. func (alibaba *Alibaba) LoadBalancerPricing() (*models.LoadBalancer, error) {
  527. cpricing, err := alibaba.Config.GetCustomPricingData()
  528. if err != nil {
  529. return nil, err
  530. }
  531. lbPricing, err := strconv.ParseFloat(cpricing.DefaultLBPrice, 64)
  532. if err != nil {
  533. return nil, err
  534. }
  535. return &models.LoadBalancer{
  536. Cost: lbPricing,
  537. }, nil
  538. }
  539. func (alibaba *Alibaba) GetConfig() (*models.CustomPricing, error) {
  540. c, err := alibaba.Config.GetCustomPricingData()
  541. if err != nil {
  542. return nil, err
  543. }
  544. if c.Discount == "" {
  545. c.Discount = "0%"
  546. }
  547. if c.NegotiatedDiscount == "" {
  548. c.NegotiatedDiscount = "0%"
  549. }
  550. return c, nil
  551. }
  552. // Load once and cache the result (even on failure). This is an install time secret, so
  553. // we don't expect the secret to change. If it does, however, we can force reload using
  554. // the input parameter.
  555. func (alibaba *Alibaba) loadAlibabaAuthSecretAndSetEnv(force bool) error {
  556. if !force && alibaba.accessKeyisLoaded() {
  557. return nil
  558. }
  559. exists, err := fileutil.FileExists(models.AuthSecretPath)
  560. if !exists || err != nil {
  561. return fmt.Errorf("failed to locate service account file: %s with err: %w", models.AuthSecretPath, err)
  562. }
  563. result, err := os.ReadFile(models.AuthSecretPath)
  564. if err != nil {
  565. return fmt.Errorf("failed to read service account file: %s with err: %w", models.AuthSecretPath, err)
  566. }
  567. var ak *AlibabaAccessKey
  568. err = json.Unmarshal(result, &ak)
  569. if err != nil {
  570. return fmt.Errorf("failed to unmarshall access key id and access key secret with err: %w", err)
  571. }
  572. err = coreenv.Set(env.AlibabaAccessKeyIDEnvVar, ak.AccessKeyID)
  573. if err != nil {
  574. return fmt.Errorf("failed to set environment variable: %s with err: %w", env.AlibabaAccessKeyIDEnvVar, err)
  575. }
  576. err = coreenv.Set(env.AlibabaAccessKeySecretEnvVar, ak.SecretAccessKey)
  577. if err != nil {
  578. return fmt.Errorf("failed to set environment variable: %s with err: %w", env.AlibabaAccessKeySecretEnvVar, err)
  579. }
  580. alibaba.accessKey = &credentials.AccessKeyCredential{
  581. AccessKeyId: ak.AccessKeyID,
  582. AccessKeySecret: ak.SecretAccessKey,
  583. }
  584. return nil
  585. }
  586. // Regions returns a current supported list of Alibaba regions
  587. func (alibaba *Alibaba) Regions() []string {
  588. regionOverrides := env.GetRegionOverrideList()
  589. if len(regionOverrides) > 0 {
  590. log.Debugf("Overriding Alibaba regions with configured region list: %+v", regionOverrides)
  591. return regionOverrides
  592. }
  593. return alibabaRegions
  594. }
  595. // ClusterInfo returns information about Alibaba Cloud cluster, as provided by metadata.
  596. func (alibaba *Alibaba) ClusterInfo() (map[string]string, error) {
  597. c, err := alibaba.GetConfig()
  598. if err != nil {
  599. return nil, fmt.Errorf("failed to getConfig with err: %w", err)
  600. }
  601. var clusterName string
  602. if c.ClusterName != "" {
  603. clusterName = c.ClusterName
  604. }
  605. // Set it to environment clusterID if not set at this point
  606. if clusterName == "" {
  607. clusterName = coreenv.GetClusterID()
  608. }
  609. m := make(map[string]string)
  610. m["name"] = clusterName
  611. m["provider"] = opencost.AlibabaProvider
  612. m["project"] = alibaba.ClusterAccountId
  613. m["region"] = alibaba.ClusterRegion
  614. m["id"] = coreenv.GetClusterID()
  615. return m, nil
  616. }
  617. // Will look at this in Next PR if needed
  618. func (alibaba *Alibaba) GetAddresses() ([]byte, error) {
  619. return nil, nil
  620. }
  621. // Will look at this in Next PR if needed
  622. func (alibaba *Alibaba) GetDisks() ([]byte, error) {
  623. return nil, nil
  624. }
  625. func (alibaba *Alibaba) GetOrphanedResources() ([]models.OrphanedResource, error) {
  626. return nil, errors.New("not implemented")
  627. }
  628. func (alibaba *Alibaba) UpdateConfig(r io.Reader, updateType string) (*models.CustomPricing, error) {
  629. return alibaba.Config.Update(func(c *models.CustomPricing) error {
  630. if updateType != "" {
  631. return fmt.Errorf("UpdateConfig for Alibaba Provider doesn't support updateType %s at this time", updateType)
  632. } else {
  633. a := make(map[string]interface{})
  634. err := json.NewDecoder(r).Decode(&a)
  635. if err != nil {
  636. return err
  637. }
  638. for k, v := range a {
  639. kUpper := utils.ToTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  640. vstr, ok := v.(string)
  641. if ok {
  642. err := models.SetCustomPricingField(c, kUpper, vstr)
  643. if err != nil {
  644. return fmt.Errorf("error setting custom pricing field: %w", err)
  645. }
  646. } else {
  647. return fmt.Errorf("type error while updating config for %s", kUpper)
  648. }
  649. }
  650. }
  651. if env.IsRemoteEnabled() {
  652. err := utils.UpdateClusterMeta(coreenv.GetClusterID(), c.ClusterName)
  653. if err != nil {
  654. return err
  655. }
  656. }
  657. return nil
  658. })
  659. }
  660. func (alibaba *Alibaba) UpdateConfigFromConfigMap(cm map[string]string) (*models.CustomPricing, error) {
  661. return alibaba.Config.UpdateFromMap(cm)
  662. }
  663. // Will look at this in Next PR if needed
  664. func (alibaba *Alibaba) GetManagementPlatform() (string, error) {
  665. return "", nil
  666. }
  667. // Will look at this in Next PR if needed
  668. func (alibaba *Alibaba) ApplyReservedInstancePricing(nodes map[string]*models.Node) {
  669. }
  670. // Will look at this in Next PR if needed
  671. func (alibaba *Alibaba) ServiceAccountStatus() *models.ServiceAccountStatus {
  672. return &models.ServiceAccountStatus{}
  673. }
  674. // Will look at this in Next PR if needed
  675. func (alibaba *Alibaba) PricingSourceStatus() map[string]*models.PricingSource {
  676. return map[string]*models.PricingSource{}
  677. }
  678. // Will look at this in Next PR if needed
  679. func (alibaba *Alibaba) ClusterManagementPricing() (string, float64, error) {
  680. return "", 0.0, nil
  681. }
  682. // Will look at this in Next PR if needed
  683. func (alibaba *Alibaba) CombinedDiscountForNode(string, bool, float64, float64) float64 {
  684. return 0.0
  685. }
  686. func (alibaba *Alibaba) accessKeyisLoaded() bool {
  687. if alibaba.accessKey == nil {
  688. return false
  689. }
  690. if alibaba.accessKey.AccessKeyId == "" {
  691. return false
  692. }
  693. if alibaba.accessKey.AccessKeySecret == "" {
  694. return false
  695. }
  696. return true
  697. }
  698. type AlibabaNodeKey struct {
  699. ProviderID string
  700. RegionID string
  701. InstanceType string
  702. OSType string
  703. OptimizedKeyword string //If IsIoOptimized is true use the word optimize in the Node key and if its not optimized use the word nonoptimize
  704. SystemDiskCategory string
  705. SystemDiskSizeInGiB string
  706. SystemDiskPerformanceLevel string
  707. }
  708. func NewAlibabaNodeKey(node *SlimK8sNode, optimizedKeyword, systemDiskCategory, systemDiskSizeInGiB, systemDiskPerfromanceLevel string) *AlibabaNodeKey {
  709. var providerID, regionID, instanceType, osType string
  710. if node != nil {
  711. providerID = node.ProviderID
  712. regionID = node.RegionID
  713. instanceType = node.InstanceType
  714. osType = node.OSType
  715. }
  716. return &AlibabaNodeKey{
  717. ProviderID: providerID,
  718. RegionID: regionID,
  719. InstanceType: instanceType,
  720. OSType: osType,
  721. OptimizedKeyword: optimizedKeyword,
  722. SystemDiskCategory: systemDiskCategory,
  723. SystemDiskSizeInGiB: systemDiskSizeInGiB,
  724. SystemDiskPerformanceLevel: systemDiskPerfromanceLevel,
  725. }
  726. }
  727. func (alibabaNodeKey *AlibabaNodeKey) ID() string {
  728. return alibabaNodeKey.ProviderID
  729. }
  730. func (alibabaNodeKey *AlibabaNodeKey) Features() string {
  731. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaNodeKey.RegionID, alibabaNodeKey.InstanceType, alibabaNodeKey.OSType,
  732. alibabaNodeKey.OptimizedKeyword, alibabaNodeKey.SystemDiskCategory, alibabaNodeKey.SystemDiskSizeInGiB, alibabaNodeKey.SystemDiskPerformanceLevel})
  733. return strings.Join(keyLookup, "::")
  734. }
  735. func (alibabaNodeKey *AlibabaNodeKey) FeaturesWithOtherDisk(overrideDiskCategory string) string {
  736. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaNodeKey.RegionID, alibabaNodeKey.InstanceType, alibabaNodeKey.OSType,
  737. alibabaNodeKey.OptimizedKeyword, overrideDiskCategory, alibabaNodeKey.SystemDiskSizeInGiB, alibabaNodeKey.SystemDiskPerformanceLevel})
  738. return strings.Join(keyLookup, "::")
  739. }
  740. func (alibabaNodeKey *AlibabaNodeKey) GPUType() string {
  741. return ""
  742. }
  743. func (alibabaNodeKey *AlibabaNodeKey) GPUCount() int {
  744. return 0
  745. }
  746. // Get's the key for the k8s node input
  747. func (alibaba *Alibaba) GetKey(mapValue map[string]string, node *clustercache.Node) models.Key {
  748. slimK8sNode := generateSlimK8sNodeFromV1Node(node)
  749. var aak *credentials.AccessKeyCredential
  750. var err error
  751. var ok bool
  752. var client *sdk.Client
  753. var signer *signers.AccessKeySigner
  754. optimizedKeyword := ""
  755. if slimK8sNode.IsIoOptimized {
  756. optimizedKeyword = ALIBABA_OPTIMIZE_KEYWORD
  757. } else {
  758. optimizedKeyword = ALIBABA_NON_OPTIMIZE_KEYWORD
  759. }
  760. var diskCategory, diskSizeInGiB, diskPerformanceLevel string
  761. if !alibaba.accessKeyisLoaded() {
  762. aak, err = alibaba.GetAlibabaAccessKey()
  763. if err != nil {
  764. log.Warnf("unable to set the signer for node with providerID %s to retrieve the key skipping SystemDisk Retrieval with err: %v", slimK8sNode.ProviderID, err)
  765. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  766. }
  767. } else {
  768. aak = alibaba.accessKey
  769. }
  770. signer = signers.NewAccessKeySigner(aak)
  771. if aak == nil {
  772. log.Warnf("unable to retrieve the Alibaba API keys for node with providerID %s hence skipping SystemDisk Retrieval", slimK8sNode.ProviderID)
  773. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  774. }
  775. if client, ok = alibaba.clients[slimK8sNode.RegionID]; !ok {
  776. client, err = sdk.NewClientWithAccessKey(slimK8sNode.RegionID, aak.AccessKeyId, aak.AccessKeySecret)
  777. if err != nil {
  778. log.Warnf("unable to set the client for node with providerID %s to retrieve the key skipping SystemDisk Retrieval with err: %v", slimK8sNode.ProviderID, err)
  779. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  780. }
  781. alibaba.clients[slimK8sNode.RegionID] = client
  782. }
  783. instanceID := getInstanceIDFromProviderID(slimK8sNode.ProviderID)
  784. slimK8sNode.SystemDisk = getSystemDiskInfoOfANode(instanceID, slimK8sNode.RegionID, client, signer)
  785. if slimK8sNode.SystemDisk != nil {
  786. diskCategory = slimK8sNode.SystemDisk.DiskCategory
  787. diskSizeInGiB = slimK8sNode.SystemDisk.SizeInGiB
  788. diskPerformanceLevel = slimK8sNode.SystemDisk.PerformanceLevel
  789. }
  790. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  791. }
  792. type AlibabaPVKey struct {
  793. ProviderID string
  794. RegionID string
  795. PVType string
  796. PVSubType string
  797. PVCategory string
  798. PVPerformaceLevel string
  799. StorageClassName string
  800. SizeInGiB string
  801. }
  802. func (alibaba *Alibaba) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
  803. regionID := defaultRegion
  804. // If default Region is not passed default it to cluster region ID.
  805. if defaultRegion == "" {
  806. regionID = alibaba.ClusterRegion
  807. }
  808. slimK8sDisk := generateSlimK8sDiskFromV1PV(pv, defaultRegion)
  809. return &AlibabaPVKey{
  810. ProviderID: slimK8sDisk.ProviderID,
  811. RegionID: regionID,
  812. PVType: ALIBABA_PV_CLOUD_DISK_TYPE,
  813. PVSubType: slimK8sDisk.DiskType,
  814. PVCategory: slimK8sDisk.DiskCategory,
  815. PVPerformaceLevel: slimK8sDisk.PerformanceLevel,
  816. StorageClassName: pv.Spec.StorageClassName,
  817. SizeInGiB: slimK8sDisk.SizeInGiB,
  818. }
  819. }
  820. func (alibabaPVKey *AlibabaPVKey) Features() string {
  821. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaPVKey.RegionID, alibabaPVKey.PVSubType, alibabaPVKey.PVCategory, alibabaPVKey.PVPerformaceLevel, alibabaPVKey.SizeInGiB})
  822. return strings.Join(keyLookup, "::")
  823. }
  824. func (alibabaPVKey *AlibabaPVKey) ID() string {
  825. return alibabaPVKey.ProviderID
  826. }
  827. // Get storage class information for PV.
  828. func (alibabaPVKey *AlibabaPVKey) GetStorageClass() string {
  829. return alibabaPVKey.StorageClassName
  830. }
  831. // Helper functions for alibabaprovider.go
  832. // createDescribePriceACSRequest creates the HTTP GET request for the required resources' Price information,
  833. // When supporting subscription and Premptible resources this HTTP call needs to be modified with PriceUnit information
  834. // When supporting different new type of instances like Compute Optimized, Memory Optimized etc make sure you add the instance type
  835. // in unit test and check if it works or not to create the ack request and processDescribePriceAndCreateAlibabaPricing function
  836. // else more parameters need to be pulled from kubernetes node response or gather information from elsewhere and function modified.
  837. func createDescribePriceACSRequest(i interface{}) (*requests.CommonRequest, error) {
  838. request := requests.NewCommonRequest()
  839. request.Method = requests.GET
  840. request.Product = ALIBABA_ECS_PRODUCT_CODE
  841. request.Domain = ALIBABA_ECS_DOMAIN
  842. request.Version = ALIBABA_ECS_VERSION
  843. request.Scheme = requests.HTTPS
  844. request.ApiName = ALIBABA_DESCRIBE_PRICE_API_ACTION
  845. switch i.(type) {
  846. case *SlimK8sNode:
  847. node := i.(*SlimK8sNode)
  848. request.QueryParams["RegionId"] = node.RegionID
  849. request.QueryParams["ResourceType"] = ALIBABA_INSTANCE_RESOURCE_TYPE
  850. request.QueryParams["InstanceType"] = node.InstanceType
  851. request.QueryParams["PriceUnit"] = node.PriceUnit
  852. if node.SystemDisk != nil {
  853. // Only if the required information is present it should be overridden else default it via the API
  854. if node.SystemDisk.DiskCategory != "" {
  855. request.QueryParams["SystemDisk.Category"] = node.SystemDisk.DiskCategory
  856. }
  857. if node.SystemDisk.SizeInGiB != "" {
  858. request.QueryParams["SystemDisk.Size"] = node.SystemDisk.SizeInGiB
  859. }
  860. if node.SystemDisk.PerformanceLevel != "" {
  861. request.QueryParams["SystemDisk.PerformanceLevel"] = node.SystemDisk.PerformanceLevel
  862. }
  863. } else {
  864. // When the system disk information is not available, and the instance family is g6e or r6e,
  865. // or the instance generation is 6 or above, the default disk category in DescribePrice should be cloud_essd.
  866. if slices.Contains(alibabaDefaultToCloudEssd, node.InstanceTypeFamily) || getInstanceFamilyGenerationFromType(node.InstanceType) > 6 {
  867. request.QueryParams["SystemDisk.Category"] = ALIBABA_DISK_CLOUD_ESSD_CATEGORY
  868. }
  869. }
  870. request.TransToAcsRequest()
  871. return request, nil
  872. case *SlimK8sDisk:
  873. disk := i.(*SlimK8sDisk)
  874. request.QueryParams["RegionId"] = disk.RegionID
  875. request.QueryParams["PriceUnit"] = disk.PriceUnit
  876. request.QueryParams["ResourceType"] = ALIBABA_DISK_RESOURCE_TYPE
  877. request.QueryParams[fmt.Sprintf("%s.%d.Size", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.SizeInGiB
  878. request.QueryParams[fmt.Sprintf("%s.%d.Category", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.DiskCategory
  879. // Performance level defaults to PL1 if not present in volume attribute.
  880. if disk.PerformanceLevel != "" {
  881. request.QueryParams[fmt.Sprintf("%s.%d.PerformanceLevel", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.PerformanceLevel
  882. }
  883. request.TransToAcsRequest()
  884. return request, nil
  885. default:
  886. return nil, fmt.Errorf("unsupported ECS type (%T) for DescribePrice at this time", i)
  887. }
  888. }
  889. // createDescribeDisksCSRequest creates the HTTP GET Request to map the system disk to the InstanceID
  890. func createDescribeDisksACSRequest(instanceID, regionID, diskType string) (*requests.CommonRequest, error) {
  891. request := requests.NewCommonRequest()
  892. request.Method = requests.GET
  893. request.Product = ALIBABA_ECS_PRODUCT_CODE
  894. request.Domain = ALIBABA_ECS_DOMAIN
  895. request.Version = ALIBABA_ECS_VERSION
  896. request.Scheme = requests.HTTPS
  897. request.ApiName = ALIBABA_DESCRIBE_DISK_API_ACTION
  898. request.QueryParams["RegionId"] = regionID
  899. request.QueryParams["InstanceId"] = instanceID
  900. request.QueryParams["DiskType"] = diskType
  901. request.TransToAcsRequest()
  902. return request, nil
  903. }
  904. // determineKeyForPricing generate a unique key from SlimK8sNode object that is constructed from v1.Node object and
  905. // SlimK8sDisk that is constructed from v1.PersistentVolume.
  906. func determineKeyForPricing(i interface{}) (string, error) {
  907. if i == nil {
  908. return "", fmt.Errorf("nil component passed to determine key")
  909. }
  910. switch i.(type) {
  911. case *SlimK8sNode:
  912. node := i.(*SlimK8sNode)
  913. var diskCategory, diskSizeInGiB, diskPerformanceLevel string
  914. if node.SystemDisk != nil {
  915. diskCategory = node.SystemDisk.DiskCategory
  916. diskSizeInGiB = node.SystemDisk.SizeInGiB
  917. diskPerformanceLevel = node.SystemDisk.PerformanceLevel
  918. }
  919. if node.IsIoOptimized {
  920. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{node.RegionID, node.InstanceType, node.OSType, ALIBABA_OPTIMIZE_KEYWORD, diskCategory, diskSizeInGiB, diskPerformanceLevel})
  921. return strings.Join(keyLookup, "::"), nil
  922. } else {
  923. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{node.RegionID, node.InstanceType, node.OSType, ALIBABA_NON_OPTIMIZE_KEYWORD, diskCategory, diskSizeInGiB, diskPerformanceLevel})
  924. return strings.Join(keyLookup, "::"), nil
  925. }
  926. case *SlimK8sDisk:
  927. disk := i.(*SlimK8sDisk)
  928. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{disk.RegionID, disk.DiskType, disk.DiskCategory, disk.PerformanceLevel, disk.SizeInGiB})
  929. return strings.Join(keyLookup, "::"), nil
  930. default:
  931. return "", fmt.Errorf("unsupported ECS type (%T) at this time", i)
  932. }
  933. }
  934. // Below structs are used to unmarshal json response of Alibaba cloud's API DescribePrice
  935. type Price struct {
  936. OriginalPrice float32 `json:"OriginalPrice"`
  937. ReservedInstanceHourPrice float32 `json:"ReservedInstanceHourPrice"`
  938. DiscountPrice float32 `json:"DiscountPrice"`
  939. Currency string `json:"Currency"`
  940. TradePrice float32 `json:"TradePrice"`
  941. }
  942. type PriceInfo struct {
  943. Price Price `json:"Price"`
  944. }
  945. type DescribePriceResponse struct {
  946. RequestId string `json:"RequestId"`
  947. PriceInfo PriceInfo `json:"PriceInfo"`
  948. }
  949. // processDescribePriceAndCreateAlibabaPricing processes the DescribePrice API and generates the pricing information for alibaba node resource and alibaba pv resource that's backed by cloud disk.
  950. func processDescribePriceAndCreateAlibabaPricing(client *sdk.Client, i interface{}, signer *signers.AccessKeySigner, custom *models.CustomPricing) (pricing *AlibabaPricing, err error) {
  951. pricing = &AlibabaPricing{}
  952. var response DescribePriceResponse
  953. if client == nil {
  954. return nil, fmt.Errorf("nil client passed to process the pricing information")
  955. }
  956. if i == nil {
  957. return nil, fmt.Errorf("nil component passed to process the pricing information")
  958. }
  959. switch i.(type) {
  960. case *SlimK8sNode:
  961. node := i.(*SlimK8sNode)
  962. req, err := createDescribePriceACSRequest(node)
  963. if err != nil {
  964. return nil, err
  965. }
  966. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  967. pricing.NodeAttributes = NewAlibabaNodeAttributes(node)
  968. if err != nil || resp.GetHttpStatus() != 200 {
  969. // Try again but default the disk to something else
  970. return nil, fmt.Errorf("unable to fetch information for node with InstanceType: %v", node.InstanceType)
  971. } else {
  972. // This is where population of Pricing happens
  973. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  974. if err != nil {
  975. return nil, fmt.Errorf("unable to unmarshall json response to custom struct with err: %w", err)
  976. }
  977. // TO-DO : Ask in PR How to get the defaults is it equal to AWS/GCP defaults? And what needs to be returned
  978. pricing.Node = &models.Node{
  979. Cost: fmt.Sprintf("%f", response.PriceInfo.Price.TradePrice),
  980. BaseCPUPrice: custom.CPU,
  981. BaseRAMPrice: custom.RAM,
  982. BaseGPUPrice: custom.GPU,
  983. }
  984. // TO-DO : Currently with Pay-As-You-go Offering TradePrice = HourlyPrice , When support happens to other type HourlyPrice Need to be determined.
  985. pricing.PricingTerms = NewAlibabaPricingTerms(ALIBABA_PAY_AS_YOU_GO_BILLING, NewAlibabaPricingDetails(response.PriceInfo.Price.TradePrice, ALIBABA_HOUR_PRICE_UNIT, response.PriceInfo.Price.TradePrice, response.PriceInfo.Price.Currency))
  986. }
  987. case *SlimK8sDisk:
  988. disk := i.(*SlimK8sDisk)
  989. req, err := createDescribePriceACSRequest(disk)
  990. if err != nil {
  991. return nil, err
  992. }
  993. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  994. if err != nil || resp.GetHttpStatus() != 200 {
  995. return nil, fmt.Errorf("unable to fetch information for disk with DiskType: %v with err: %w", disk.DiskCategory, err)
  996. } else {
  997. // This is where population of Pricing happens
  998. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  999. if err != nil {
  1000. return nil, fmt.Errorf("unable to unmarshall json response to custom struct with err: %w", err)
  1001. }
  1002. pricing.PVAttributes = NewAlibabaPVAttributes(disk)
  1003. pricing.PV = &models.PV{
  1004. Cost: fmt.Sprintf("%f", response.PriceInfo.Price.TradePrice),
  1005. }
  1006. // TO-DO : Disk has support for Hour and Month but pricing API is failing for month for disk(Research why?) and same challenge as node pricing no prepaid/postpaid distinction in v1.PersistentVolume object have to look at APIs for th information.
  1007. pricing.PricingTerms = NewAlibabaPricingTerms(ALIBABA_PAY_AS_YOU_GO_BILLING, NewAlibabaPricingDetails(response.PriceInfo.Price.TradePrice, ALIBABA_HOUR_PRICE_UNIT, response.PriceInfo.Price.TradePrice, response.PriceInfo.Price.Currency))
  1008. }
  1009. default:
  1010. return nil, fmt.Errorf("unsupported ECS Pricing component of type (%T) at this time", i)
  1011. }
  1012. return pricing, nil
  1013. }
  1014. // This function is to get the InstanceFamily from the InstanceType , convention followed in
  1015. // instance type is ecs.[FamilyName].[DifferentSize], it gets the familyName , if it is unable to get it
  1016. // it lists the instance family name as Unknown.
  1017. func getInstanceFamilyFromType(instanceType string) string {
  1018. splitinstanceType := strings.Split(instanceType, ".")
  1019. if len(splitinstanceType) != 3 {
  1020. log.Warnf("unable to find the family of the instance type %s, returning its family type unknown", instanceType)
  1021. return ALIBABA_UNKNOWN_INSTANCE_FAMILY_TYPE
  1022. }
  1023. return splitinstanceType[1]
  1024. }
  1025. // This function is used to obtain the generation of the instance family from the InstanceType,
  1026. // because when the generation is higher than or equal to 7, the instance disk type will not support cloud_efficiency.
  1027. // In such cases, when calling the DescribePrice interface, the system disk type will default to cloud_essd.
  1028. func getInstanceFamilyGenerationFromType(instanceType string) int {
  1029. // FamilyName format: g7ne or g7 or r7 or r6e,
  1030. familyName := getInstanceFamilyFromType(instanceType)
  1031. re := regexp.MustCompile(`(\d+)`)
  1032. match := re.FindString(familyName)
  1033. if match != "" {
  1034. generation, err := strconv.Atoi(match)
  1035. if err != nil {
  1036. log.Errorf("unable to convert the generation of the instance type %s to integer", instanceType)
  1037. } else {
  1038. return generation
  1039. }
  1040. }
  1041. log.Warnf("unable to find the generation of the instance type %s,", instanceType)
  1042. return -1
  1043. }
  1044. // getInstanceIDFromProviderID returns the instance ID associated with the Node. A *v1.Node providerID in Alibaba cloud
  1045. // is of <REGION-ID>.<INSTANCE-ID>. This function returns the Instance ID for the given ProviderID. if its unable to interpret
  1046. // it defaults to empty string.
  1047. func getInstanceIDFromProviderID(providerID string) string {
  1048. if providerID == "" {
  1049. return ""
  1050. }
  1051. splitStrings := strings.Split(providerID, ".")
  1052. if len(splitStrings) < 2 {
  1053. return ""
  1054. }
  1055. return splitStrings[1]
  1056. }
  1057. type Disk struct {
  1058. Category string `json:"Category"`
  1059. Size int `json:"Size"`
  1060. PerformanceLevel string `json:"PerformanceLevel"`
  1061. Type string `json:"Type"`
  1062. RegionId string `json:"RegionId"`
  1063. DiskId string `json:"DiskId"`
  1064. DiskChargeType string `json:"DiskChargeType"`
  1065. }
  1066. type Disks struct {
  1067. Disk []*Disk `json:"Disk"`
  1068. }
  1069. type DescribeDiskResponse struct {
  1070. TotalCount int `json:"TotalCount"`
  1071. Disks *Disks `json:"Disks"`
  1072. }
  1073. // getSystemDiskInfoOfANode gets the relevant System disk information associated with the Node given by the instanceID
  1074. // in form of a SlimK8sDisk with only relevant information that can adjust the node pricing. If any error occurs return
  1075. // an empty disk to not impact any default set at the price retrieval of the node.
  1076. func getSystemDiskInfoOfANode(instanceID, regionID string, client *sdk.Client, signer *signers.AccessKeySigner) (systemDisk *SlimK8sDisk) {
  1077. systemDisk = &SlimK8sDisk{}
  1078. var response DescribeDiskResponse
  1079. // if instanceID is empty string return an empty k8s
  1080. if instanceID == "" {
  1081. return
  1082. }
  1083. // if client is nil return an empty disk to not impact default pricing
  1084. if client == nil {
  1085. log.Warnf("unable to set the signer for node with providerID %s to retrieve the key skipping SystemDisk Retrieval with err: nil client", instanceID)
  1086. return
  1087. }
  1088. req, err := createDescribeDisksACSRequest(instanceID, regionID, ALIBABA_SYSTEM_DISK_CATEGORY)
  1089. // if any error occurs return an empty disk to not impact default pricing.
  1090. if err != nil {
  1091. log.Warnf("Unable to create Describe Disk Request with err: %v for node with InstanceID: %s, hence defaulting it to an empty system disk to pass through to defaults", err, instanceID)
  1092. return
  1093. }
  1094. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  1095. if err != nil || resp.GetHttpStatus() != 200 {
  1096. log.Warnf("Unable to process Describe Disk request with err: %v and errcode: %d for the node with InstanceID: %s, hence defaulting it to an empty system disk to pass through to defaults", err, resp.GetHttpStatus(), instanceID)
  1097. return
  1098. } else {
  1099. // This is where population of Pricing happens
  1100. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  1101. if err != nil {
  1102. log.Warnf("Unable to unmarshall Describe Disk response with err: %v for the node with InstanceID: %s, hence defaulting it to an empty system disk to pass through to defaults", err, instanceID)
  1103. return
  1104. }
  1105. // Every instance should only have one system disk per Alibaba Cloud documentation https://www.alibabacloud.com/help/en/elastic-compute-service/latest/block-storage-overview-disks,
  1106. // if TotalCount is not 1 just return empty and let it not impact default pricing.
  1107. if response.TotalCount != 1 {
  1108. log.Warnf("Total count of system disk for node with InstanceID: %s is not 1, hence defaulting it to an empty system disk to pass through to defaults", instanceID)
  1109. return
  1110. }
  1111. if response.Disks == nil {
  1112. log.Warnf("Disks information missing for node with InstanceID: %s, hence defaulting it to an empty system disk to pass through to defaults", instanceID)
  1113. return
  1114. }
  1115. if len(response.Disks.Disk) < 1 {
  1116. log.Warnf("Total number of system disk for node with InstanceID: %s is less than 1, hence defaulting it to an empty system disk to pass through to defaults", instanceID)
  1117. return
  1118. }
  1119. // TO-DO: When supporting Subscription type disk, you can leverge the disk.DiskChargeType here to map it to subscription type.
  1120. systemDisk := response.Disks.Disk[0]
  1121. return NewSlimK8sDisk(systemDisk.Type, systemDisk.RegionId, ALIBABA_HOUR_PRICE_UNIT, systemDisk.Category, systemDisk.PerformanceLevel, systemDisk.DiskId, "", fmt.Sprintf("%d", systemDisk.Size))
  1122. }
  1123. }
  1124. // generateSlimK8sNodeFromV1Node generates SlimK8sNode struct from v1.Node to fetch pricing information and call alibaba API.
  1125. func generateSlimK8sNodeFromV1Node(node *clustercache.Node) *SlimK8sNode {
  1126. var regionID, osType, instanceType, providerID, priceUnit, instanceFamily string
  1127. var memorySizeInKiB string // TO-DO: try to convert it into float
  1128. var ok, IsIoOptimized bool
  1129. if regionID, ok = node.Labels["topology.kubernetes.io/region"]; !ok {
  1130. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1131. log.Debugf("No RegionID label for the node: %s", node.Name)
  1132. }
  1133. if osType, ok = node.Labels["beta.kubernetes.io/os"]; !ok {
  1134. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1135. log.Debugf("OS type undetected for the node: %s", node.Name)
  1136. }
  1137. if instanceType, ok = node.Labels["node.kubernetes.io/instance-type"]; !ok {
  1138. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1139. log.Debugf("Instance Type undetected for the node: %s", node.Name)
  1140. }
  1141. instanceFamily = getInstanceFamilyFromType(instanceType)
  1142. memorySizeInKiB = fmt.Sprintf("%s", node.Status.Capacity.Memory())
  1143. providerID = node.SpecProviderID // Alibaba Cloud provider doesnt follow convention of prefix with cloud provider name
  1144. // Looking at current Instance offering , all of the Instances seem to be I/O optimized - https://www.alibabacloud.com/help/en/elastic-compute-service/latest/instance-family
  1145. // Basic price Json has it as part of the key so defaulting to true.
  1146. IsIoOptimized = true
  1147. priceUnit = ALIBABA_HOUR_PRICE_UNIT
  1148. systemDisk := &SlimK8sDisk{}
  1149. return NewSlimK8sNode(instanceType, regionID, priceUnit, memorySizeInKiB, osType, providerID, instanceFamily, IsIoOptimized, systemDisk)
  1150. }
  1151. // getNumericalValueFromResourceQuantity converts a Kubernetes PV capacity string (e.g. "20Gi", "48828125Ki")
  1152. // into a whole GiB integer string, as required by the Alibaba DescribePrice API.
  1153. // Returns ALIBABA_DEFAULT_DATADISK_SIZE if the quantity cannot be parsed.
  1154. func getNumericalValueFromResourceQuantity(quantity string) (value string) {
  1155. defer func() {
  1156. if err := recover(); err != nil {
  1157. log.Debugf("panic while parsing PV capacity %q, defaulting to %s: %v", quantity, ALIBABA_DEFAULT_DATADISK_SIZE, err)
  1158. value = ALIBABA_DEFAULT_DATADISK_SIZE
  1159. }
  1160. if value == "" {
  1161. log.Debugf("unable to determine the size of the PV from quantity %q, defaulting to %s", quantity, ALIBABA_DEFAULT_DATADISK_SIZE)
  1162. value = ALIBABA_DEFAULT_DATADISK_SIZE
  1163. }
  1164. }()
  1165. res := sizeRegEx.FindStringSubmatch(strings.TrimSpace(quantity))
  1166. if len(res) < 2 || res[1] == "" {
  1167. return
  1168. }
  1169. numericPart, err := strconv.ParseFloat(res[1], 64)
  1170. if err != nil || numericPart <= 0 {
  1171. return
  1172. }
  1173. unit := ""
  1174. if len(res) >= 3 {
  1175. unit = res[2]
  1176. }
  1177. var sizeInGiB float64
  1178. switch unit {
  1179. case "Ki":
  1180. sizeInGiB = numericPart / (1024 * 1024)
  1181. case "Mi":
  1182. sizeInGiB = numericPart / 1024
  1183. case "Gi":
  1184. sizeInGiB = numericPart
  1185. case "Ti":
  1186. sizeInGiB = numericPart * 1024
  1187. default:
  1188. sizeInGiB = numericPart / (1024 * 1024 * 1024)
  1189. }
  1190. // ceil so we never underreport disk size to the DescribePrice API.
  1191. sizeInGiBInt := int64(math.Ceil(sizeInGiB))
  1192. if sizeInGiBInt <= 0 {
  1193. return
  1194. }
  1195. value = strconv.FormatInt(sizeInGiBInt, 10)
  1196. return
  1197. }
  1198. // generateSlimK8sDiskFromV1PV function generates SlimK8sDisk from v1.PersistentVolume
  1199. // to generate slim disk type that can be used to fetch pricing information for Data disk type.
  1200. func generateSlimK8sDiskFromV1PV(pv *clustercache.PersistentVolume, regionID string) *SlimK8sDisk {
  1201. // All PVs are data disks while local disk are categorized as system disk
  1202. diskType := ALIBABA_DATA_DISK_CATEGORY
  1203. //TO-DO: Disk supports month and hour prices , defaulting to hour
  1204. priceUnit := ALIBABA_HOUR_PRICE_UNIT
  1205. sizeQuantity := fmt.Sprintf("%s", pv.Spec.Capacity.Storage())
  1206. // res := sizeRegEx.FindAllStringSubmatch(sizeQuantity, 1)
  1207. sizeInGiB := getNumericalValueFromResourceQuantity(sizeQuantity)
  1208. providerID := ""
  1209. if pv.Spec.CSI != nil {
  1210. providerID = pv.Spec.CSI.VolumeHandle
  1211. } else {
  1212. providerID = pv.Name // Looks like pv name is same as providerID in Alibaba k8s cluster
  1213. }
  1214. // Performance level being empty string gets defaulted in describePrice to PL1.
  1215. performanceLevel := ""
  1216. diskCategory := ""
  1217. if pv.Spec.CSI != nil {
  1218. if val, ok := pv.Spec.CSI.VolumeAttributes["performanceLevel"]; ok {
  1219. performanceLevel = val
  1220. }
  1221. if val, ok := pv.Spec.CSI.VolumeAttributes["type"]; ok {
  1222. diskCategory = val
  1223. }
  1224. }
  1225. // Highly unlikely that label pv.Spec.CSI.VolumeAttributes["type"] doesn't exist but if occurred default to cloud (most basic disk type)
  1226. if diskCategory == "" {
  1227. diskCategory = ALIBABA_DISK_CLOUD_CATEGORY
  1228. }
  1229. return NewSlimK8sDisk(diskType, regionID, priceUnit, diskCategory, performanceLevel, providerID, pv.Spec.StorageClassName, sizeInGiB)
  1230. }
  1231. // determinePVRegion determines associated region for a particular PV based on the following priority, which can be changed and any other path to determine region can be added!
  1232. // if topology.diskplugin.csi.alibabacloud.com/region label/annotation is passed during PV creation return that as the PV region.
  1233. // if topology.diskplugin.csi.alibabacloud.com/zone label/annotation is passed during PV creation determine the region based on this pv label.
  1234. // if neither of the above label/annotation is present check node affinity for the zone affinity and determine the region based on this zone.
  1235. // if nether of the above yields a region , return empty string to default it to cluster region.
  1236. func determinePVRegion(pv *clustercache.PersistentVolume) string {
  1237. // if "topology.diskplugin.csi.alibabacloud.com/region" is present as a label or annotation return that as the PV region
  1238. if val, ok := pv.Labels[ALIBABA_DISK_TOPOLOGY_REGION_LABEL]; ok {
  1239. log.Debugf("determinePVRegion returned a region value of: %s through label: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_REGION_LABEL, pv.Name)
  1240. return val
  1241. }
  1242. if val, ok := pv.Annotations[ALIBABA_DISK_TOPOLOGY_REGION_LABEL]; ok {
  1243. log.Debugf("determinePVRegion returned a region value of: %s through annotation: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_REGION_LABEL, pv.Name)
  1244. return val
  1245. }
  1246. // if "topology.diskplugin.csi.alibabacloud.com/zone" is present as a label or annotation set it as the PV zone before looking at node affinity to determine the region PV belongs too
  1247. var pvZone string
  1248. if val, ok := pv.Labels[ALIBABA_DISK_TOPOLOGY_ZONE_LABEL]; ok {
  1249. log.Debugf("determinePVRegion will set zone value to: %s through label: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_ZONE_LABEL, pv.Name)
  1250. pvZone = val
  1251. }
  1252. if pvZone == "" {
  1253. if val, ok := pv.Annotations[ALIBABA_DISK_TOPOLOGY_ZONE_LABEL]; ok {
  1254. log.Debugf("determinePVRegion will set zone value to: %s through annotation: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_ZONE_LABEL, pv.Name)
  1255. pvZone = val
  1256. }
  1257. }
  1258. if pvZone == "" {
  1259. // zone and regionID labels are optional in Alibaba PV creation, while PV through UI creation put's a zone PV is associated with and the region
  1260. // can be determined from this information. If pv is provision via yaml and the block is missing that's the only time it gets defaulted to ClusterRegion.
  1261. if pv.Spec.NodeAffinity != nil {
  1262. nodeAffinity := pv.Spec.NodeAffinity
  1263. if nodeAffinity.Required != nil && nodeAffinity.Required.NodeSelectorTerms != nil {
  1264. for _, nodeSelectorTerm := range nodeAffinity.Required.NodeSelectorTerms {
  1265. matchExpression := nodeSelectorTerm.MatchExpressions
  1266. for _, nodeSelectorRequirement := range matchExpression {
  1267. if nodeSelectorRequirement.Key == ALIBABA_DISK_TOPOLOGY_ZONE_LABEL {
  1268. log.Debugf("determinePVRegion will set zone value to: %s through node affinity label: %s for PV name: %s", nodeSelectorRequirement.Values[0], ALIBABA_DISK_TOPOLOGY_ZONE_LABEL, pv.Name)
  1269. pvZone = nodeSelectorRequirement.Values[0]
  1270. }
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. regionOverrides := env.GetRegionOverrideList()
  1277. regions := alibabaRegions
  1278. if len(regionOverrides) > 0 {
  1279. regions = regionOverrides
  1280. }
  1281. for _, region := range regions {
  1282. if strings.Contains(pvZone, region) {
  1283. log.Debugf("determinePVRegion determined region of %s through zone affiliation of the PV %s\n", region, pvZone)
  1284. return region
  1285. }
  1286. }
  1287. return ""
  1288. }
  1289. // PricingSourceSummary returns the pricing source summary for the provider.
  1290. // The summary represents what was _parsed_ from the pricing source, not
  1291. // everything that was _available_ in the pricing source.
  1292. func (a *Alibaba) PricingSourceSummary() interface{} {
  1293. return a.Pricing
  1294. }