provider.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. package alibaba
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  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/env"
  17. "github.com/opencost/opencost/core/pkg/log"
  18. "github.com/opencost/opencost/core/pkg/opencost"
  19. "github.com/opencost/opencost/core/pkg/util/fileutil"
  20. "github.com/opencost/opencost/core/pkg/util/json"
  21. "github.com/opencost/opencost/core/pkg/util/stringutil"
  22. "github.com/opencost/opencost/pkg/cloud/models"
  23. "github.com/opencost/opencost/pkg/cloud/utils"
  24. "github.com/opencost/opencost/pkg/clustercache"
  25. ocenv "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. // Regular expression to get the numerical value of PV suffix with GiB from *v1.PersistentVolume.
  60. sizeRegEx = regexp.MustCompile("(.*?)Gi")
  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 = ocenv.GetAlibabaAccessKeyID()
  306. }
  307. if config.AlibabaServiceKeySecret == "" {
  308. config.AlibabaServiceKeySecret = ocenv.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 = ocenv.GetAlibabaAccessKeyID()
  317. config.AlibabaServiceKeySecret = ocenv.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. // PVPricing gives a pricing information of a specific PV given by PVkey
  474. func (alibaba *Alibaba) PVPricing(pvk models.PVKey) (*models.PV, error) {
  475. alibaba.DownloadPricingDataLock.RLock()
  476. defer alibaba.DownloadPricingDataLock.RUnlock()
  477. keyFeature := pvk.Features()
  478. pricing, ok := alibaba.Pricing[keyFeature]
  479. if !ok {
  480. log.Debugf("Persistent Volume pricing not found for PV with feature: %s", keyFeature)
  481. return nil, fmt.Errorf("Persistent Volume pricing not found for PV with feature: %s letting it use default values", keyFeature)
  482. }
  483. log.Debugf("returning the PV price for the node with feature: %s", keyFeature)
  484. return pricing.PV, nil
  485. }
  486. // 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
  487. // Internet cost is default based on https://www.alibabacloud.com/help/en/elastic-compute-service/latest/public-bandwidth to $0.123
  488. func (alibaba *Alibaba) NetworkPricing() (*models.Network, error) {
  489. cpricing, err := alibaba.Config.GetCustomPricingData()
  490. if err != nil {
  491. return nil, err
  492. }
  493. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  494. if err != nil {
  495. return nil, err
  496. }
  497. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  498. if err != nil {
  499. return nil, err
  500. }
  501. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  502. if err != nil {
  503. return nil, err
  504. }
  505. return &models.Network{
  506. ZoneNetworkEgressCost: znec,
  507. RegionNetworkEgressCost: rnec,
  508. InternetNetworkEgressCost: inec,
  509. }, nil
  510. }
  511. // Alibaba loadbalancer has three different types https://www.alibabacloud.com/product/server-load-balancer,
  512. // defaulted price to classic load balancer https://www.alibabacloud.com/help/en/server-load-balancer/latest/pay-as-you-go.
  513. func (alibaba *Alibaba) LoadBalancerPricing() (*models.LoadBalancer, error) {
  514. cpricing, err := alibaba.Config.GetCustomPricingData()
  515. if err != nil {
  516. return nil, err
  517. }
  518. lbPricing, err := strconv.ParseFloat(cpricing.DefaultLBPrice, 64)
  519. if err != nil {
  520. return nil, err
  521. }
  522. return &models.LoadBalancer{
  523. Cost: lbPricing,
  524. }, nil
  525. }
  526. func (alibaba *Alibaba) GetConfig() (*models.CustomPricing, error) {
  527. c, err := alibaba.Config.GetCustomPricingData()
  528. if err != nil {
  529. return nil, err
  530. }
  531. if c.Discount == "" {
  532. c.Discount = "0%"
  533. }
  534. if c.NegotiatedDiscount == "" {
  535. c.NegotiatedDiscount = "0%"
  536. }
  537. if c.ShareTenancyCosts == "" {
  538. c.ShareTenancyCosts = models.DefaultShareTenancyCost
  539. }
  540. return c, nil
  541. }
  542. // Load once and cache the result (even on failure). This is an install time secret, so
  543. // we don't expect the secret to change. If it does, however, we can force reload using
  544. // the input parameter.
  545. func (alibaba *Alibaba) loadAlibabaAuthSecretAndSetEnv(force bool) error {
  546. if !force && alibaba.accessKeyisLoaded() {
  547. return nil
  548. }
  549. exists, err := fileutil.FileExists(models.AuthSecretPath)
  550. if !exists || err != nil {
  551. return fmt.Errorf("failed to locate service account file: %s with err: %w", models.AuthSecretPath, err)
  552. }
  553. result, err := os.ReadFile(models.AuthSecretPath)
  554. if err != nil {
  555. return fmt.Errorf("failed to read service account file: %s with err: %w", models.AuthSecretPath, err)
  556. }
  557. var ak *AlibabaAccessKey
  558. err = json.Unmarshal(result, &ak)
  559. if err != nil {
  560. return fmt.Errorf("failed to unmarshall access key id and access key secret with err: %w", err)
  561. }
  562. err = env.Set(ocenv.AlibabaAccessKeyIDEnvVar, ak.AccessKeyID)
  563. if err != nil {
  564. return fmt.Errorf("failed to set environment variable: %s with err: %w", ocenv.AlibabaAccessKeyIDEnvVar, err)
  565. }
  566. err = env.Set(ocenv.AlibabaAccessKeySecretEnvVar, ak.SecretAccessKey)
  567. if err != nil {
  568. return fmt.Errorf("failed to set environment variable: %s with err: %w", ocenv.AlibabaAccessKeySecretEnvVar, err)
  569. }
  570. alibaba.accessKey = &credentials.AccessKeyCredential{
  571. AccessKeyId: ak.AccessKeyID,
  572. AccessKeySecret: ak.SecretAccessKey,
  573. }
  574. return nil
  575. }
  576. // Regions returns a current supported list of Alibaba regions
  577. func (alibaba *Alibaba) Regions() []string {
  578. regionOverrides := ocenv.GetRegionOverrideList()
  579. if len(regionOverrides) > 0 {
  580. log.Debugf("Overriding Alibaba regions with configured region list: %+v", regionOverrides)
  581. return regionOverrides
  582. }
  583. return alibabaRegions
  584. }
  585. // ClusterInfo returns information about Alibaba Cloud cluster, as provided by metadata.
  586. func (alibaba *Alibaba) ClusterInfo() (map[string]string, error) {
  587. c, err := alibaba.GetConfig()
  588. if err != nil {
  589. return nil, fmt.Errorf("failed to getConfig with err: %w", err)
  590. }
  591. var clusterName string
  592. if c.ClusterName != "" {
  593. clusterName = c.ClusterName
  594. }
  595. // Set it to environment clusterID if not set at this point
  596. if clusterName == "" {
  597. clusterName = ocenv.GetClusterID()
  598. }
  599. m := make(map[string]string)
  600. m["name"] = clusterName
  601. m["provider"] = opencost.AlibabaProvider
  602. m["project"] = alibaba.ClusterAccountId
  603. m["region"] = alibaba.ClusterRegion
  604. m["id"] = ocenv.GetClusterID()
  605. return m, nil
  606. }
  607. // Will look at this in Next PR if needed
  608. func (alibaba *Alibaba) GetAddresses() ([]byte, error) {
  609. return nil, nil
  610. }
  611. // Will look at this in Next PR if needed
  612. func (alibaba *Alibaba) GetDisks() ([]byte, error) {
  613. return nil, nil
  614. }
  615. func (alibaba *Alibaba) GetOrphanedResources() ([]models.OrphanedResource, error) {
  616. return nil, errors.New("not implemented")
  617. }
  618. func (alibaba *Alibaba) UpdateConfig(r io.Reader, updateType string) (*models.CustomPricing, error) {
  619. return alibaba.Config.Update(func(c *models.CustomPricing) error {
  620. if updateType != "" {
  621. return fmt.Errorf("UpdateConfig for Alibaba Provider doesn't support updateType %s at this time", updateType)
  622. } else {
  623. a := make(map[string]interface{})
  624. err := json.NewDecoder(r).Decode(&a)
  625. if err != nil {
  626. return err
  627. }
  628. for k, v := range a {
  629. kUpper := utils.ToTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  630. vstr, ok := v.(string)
  631. if ok {
  632. err := models.SetCustomPricingField(c, kUpper, vstr)
  633. if err != nil {
  634. return fmt.Errorf("error setting custom pricing field: %w", err)
  635. }
  636. } else {
  637. return fmt.Errorf("type error while updating config for %s", kUpper)
  638. }
  639. }
  640. }
  641. if ocenv.IsRemoteEnabled() {
  642. err := utils.UpdateClusterMeta(ocenv.GetClusterID(), c.ClusterName)
  643. if err != nil {
  644. return err
  645. }
  646. }
  647. return nil
  648. })
  649. }
  650. func (alibaba *Alibaba) UpdateConfigFromConfigMap(cm map[string]string) (*models.CustomPricing, error) {
  651. return alibaba.Config.UpdateFromMap(cm)
  652. }
  653. // Will look at this in Next PR if needed
  654. func (alibaba *Alibaba) GetManagementPlatform() (string, error) {
  655. return "", nil
  656. }
  657. // Will look at this in Next PR if needed
  658. func (alibaba *Alibaba) GetLocalStorageQuery(window, offset time.Duration, rate bool, used bool) string {
  659. return ""
  660. }
  661. // Will look at this in Next PR if needed
  662. func (alibaba *Alibaba) ApplyReservedInstancePricing(nodes map[string]*models.Node) {
  663. }
  664. // Will look at this in Next PR if needed
  665. func (alibaba *Alibaba) ServiceAccountStatus() *models.ServiceAccountStatus {
  666. return &models.ServiceAccountStatus{}
  667. }
  668. // Will look at this in Next PR if needed
  669. func (alibaba *Alibaba) PricingSourceStatus() map[string]*models.PricingSource {
  670. return map[string]*models.PricingSource{}
  671. }
  672. // Will look at this in Next PR if needed
  673. func (alibaba *Alibaba) ClusterManagementPricing() (string, float64, error) {
  674. return "", 0.0, nil
  675. }
  676. // Will look at this in Next PR if needed
  677. func (alibaba *Alibaba) CombinedDiscountForNode(string, bool, float64, float64) float64 {
  678. return 0.0
  679. }
  680. func (alibaba *Alibaba) accessKeyisLoaded() bool {
  681. if alibaba.accessKey == nil {
  682. return false
  683. }
  684. if alibaba.accessKey.AccessKeyId == "" {
  685. return false
  686. }
  687. if alibaba.accessKey.AccessKeySecret == "" {
  688. return false
  689. }
  690. return true
  691. }
  692. type AlibabaNodeKey struct {
  693. ProviderID string
  694. RegionID string
  695. InstanceType string
  696. OSType string
  697. OptimizedKeyword string //If IsIoOptimized is true use the word optimize in the Node key and if its not optimized use the word nonoptimize
  698. SystemDiskCategory string
  699. SystemDiskSizeInGiB string
  700. SystemDiskPerformanceLevel string
  701. }
  702. func NewAlibabaNodeKey(node *SlimK8sNode, optimizedKeyword, systemDiskCategory, systemDiskSizeInGiB, systemDiskPerfromanceLevel string) *AlibabaNodeKey {
  703. var providerID, regionID, instanceType, osType string
  704. if node != nil {
  705. providerID = node.ProviderID
  706. regionID = node.RegionID
  707. instanceType = node.InstanceType
  708. osType = node.OSType
  709. }
  710. return &AlibabaNodeKey{
  711. ProviderID: providerID,
  712. RegionID: regionID,
  713. InstanceType: instanceType,
  714. OSType: osType,
  715. OptimizedKeyword: optimizedKeyword,
  716. SystemDiskCategory: systemDiskCategory,
  717. SystemDiskSizeInGiB: systemDiskSizeInGiB,
  718. SystemDiskPerformanceLevel: systemDiskPerfromanceLevel,
  719. }
  720. }
  721. func (alibabaNodeKey *AlibabaNodeKey) ID() string {
  722. return alibabaNodeKey.ProviderID
  723. }
  724. func (alibabaNodeKey *AlibabaNodeKey) Features() string {
  725. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaNodeKey.RegionID, alibabaNodeKey.InstanceType, alibabaNodeKey.OSType,
  726. alibabaNodeKey.OptimizedKeyword, alibabaNodeKey.SystemDiskCategory, alibabaNodeKey.SystemDiskSizeInGiB, alibabaNodeKey.SystemDiskPerformanceLevel})
  727. return strings.Join(keyLookup, "::")
  728. }
  729. func (alibabaNodeKey *AlibabaNodeKey) FeaturesWithOtherDisk(overrideDiskCategory string) string {
  730. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaNodeKey.RegionID, alibabaNodeKey.InstanceType, alibabaNodeKey.OSType,
  731. alibabaNodeKey.OptimizedKeyword, overrideDiskCategory, alibabaNodeKey.SystemDiskSizeInGiB, alibabaNodeKey.SystemDiskPerformanceLevel})
  732. return strings.Join(keyLookup, "::")
  733. }
  734. func (alibabaNodeKey *AlibabaNodeKey) GPUType() string {
  735. return ""
  736. }
  737. func (alibabaNodeKey *AlibabaNodeKey) GPUCount() int {
  738. return 0
  739. }
  740. // Get's the key for the k8s node input
  741. func (alibaba *Alibaba) GetKey(mapValue map[string]string, node *clustercache.Node) models.Key {
  742. slimK8sNode := generateSlimK8sNodeFromV1Node(node)
  743. var aak *credentials.AccessKeyCredential
  744. var err error
  745. var ok bool
  746. var client *sdk.Client
  747. var signer *signers.AccessKeySigner
  748. optimizedKeyword := ""
  749. if slimK8sNode.IsIoOptimized {
  750. optimizedKeyword = ALIBABA_OPTIMIZE_KEYWORD
  751. } else {
  752. optimizedKeyword = ALIBABA_NON_OPTIMIZE_KEYWORD
  753. }
  754. var diskCategory, diskSizeInGiB, diskPerformanceLevel string
  755. if !alibaba.accessKeyisLoaded() {
  756. aak, err = alibaba.GetAlibabaAccessKey()
  757. if err != nil {
  758. 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)
  759. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  760. }
  761. } else {
  762. aak = alibaba.accessKey
  763. }
  764. signer = signers.NewAccessKeySigner(aak)
  765. if aak == nil {
  766. log.Warnf("unable to retrieve the Alibaba API keys for node with providerID %s hence skipping SystemDisk Retrieval", slimK8sNode.ProviderID)
  767. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  768. }
  769. if client, ok = alibaba.clients[slimK8sNode.RegionID]; !ok {
  770. client, err = sdk.NewClientWithAccessKey(slimK8sNode.RegionID, aak.AccessKeyId, aak.AccessKeySecret)
  771. if err != nil {
  772. 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)
  773. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  774. }
  775. alibaba.clients[slimK8sNode.RegionID] = client
  776. }
  777. instanceID := getInstanceIDFromProviderID(slimK8sNode.ProviderID)
  778. slimK8sNode.SystemDisk = getSystemDiskInfoOfANode(instanceID, slimK8sNode.RegionID, client, signer)
  779. if slimK8sNode.SystemDisk != nil {
  780. diskCategory = slimK8sNode.SystemDisk.DiskCategory
  781. diskSizeInGiB = slimK8sNode.SystemDisk.SizeInGiB
  782. diskPerformanceLevel = slimK8sNode.SystemDisk.PerformanceLevel
  783. }
  784. return NewAlibabaNodeKey(slimK8sNode, optimizedKeyword, diskCategory, diskSizeInGiB, diskPerformanceLevel)
  785. }
  786. type AlibabaPVKey struct {
  787. ProviderID string
  788. RegionID string
  789. PVType string
  790. PVSubType string
  791. PVCategory string
  792. PVPerformaceLevel string
  793. StorageClassName string
  794. SizeInGiB string
  795. }
  796. func (alibaba *Alibaba) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
  797. regionID := defaultRegion
  798. // If default Region is not passed default it to cluster region ID.
  799. if defaultRegion == "" {
  800. regionID = alibaba.ClusterRegion
  801. }
  802. slimK8sDisk := generateSlimK8sDiskFromV1PV(pv, defaultRegion)
  803. return &AlibabaPVKey{
  804. ProviderID: slimK8sDisk.ProviderID,
  805. RegionID: regionID,
  806. PVType: ALIBABA_PV_CLOUD_DISK_TYPE,
  807. PVSubType: slimK8sDisk.DiskType,
  808. PVCategory: slimK8sDisk.DiskCategory,
  809. PVPerformaceLevel: slimK8sDisk.PerformanceLevel,
  810. StorageClassName: pv.Spec.StorageClassName,
  811. SizeInGiB: slimK8sDisk.SizeInGiB,
  812. }
  813. }
  814. func (alibabaPVKey *AlibabaPVKey) Features() string {
  815. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{alibabaPVKey.RegionID, alibabaPVKey.PVSubType, alibabaPVKey.PVCategory, alibabaPVKey.PVPerformaceLevel, alibabaPVKey.SizeInGiB})
  816. return strings.Join(keyLookup, "::")
  817. }
  818. func (alibabaPVKey *AlibabaPVKey) ID() string {
  819. return alibabaPVKey.ProviderID
  820. }
  821. // Get storage class information for PV.
  822. func (alibabaPVKey *AlibabaPVKey) GetStorageClass() string {
  823. return alibabaPVKey.StorageClassName
  824. }
  825. // Helper functions for alibabaprovider.go
  826. // createDescribePriceACSRequest creates the HTTP GET request for the required resources' Price information,
  827. // When supporting subscription and Premptible resources this HTTP call needs to be modified with PriceUnit information
  828. // When supporting different new type of instances like Compute Optimized, Memory Optimized etc make sure you add the instance type
  829. // in unit test and check if it works or not to create the ack request and processDescribePriceAndCreateAlibabaPricing function
  830. // else more parameters need to be pulled from kubernetes node response or gather information from elsewhere and function modified.
  831. func createDescribePriceACSRequest(i interface{}) (*requests.CommonRequest, error) {
  832. request := requests.NewCommonRequest()
  833. request.Method = requests.GET
  834. request.Product = ALIBABA_ECS_PRODUCT_CODE
  835. request.Domain = ALIBABA_ECS_DOMAIN
  836. request.Version = ALIBABA_ECS_VERSION
  837. request.Scheme = requests.HTTPS
  838. request.ApiName = ALIBABA_DESCRIBE_PRICE_API_ACTION
  839. switch i.(type) {
  840. case *SlimK8sNode:
  841. node := i.(*SlimK8sNode)
  842. request.QueryParams["RegionId"] = node.RegionID
  843. request.QueryParams["ResourceType"] = ALIBABA_INSTANCE_RESOURCE_TYPE
  844. request.QueryParams["InstanceType"] = node.InstanceType
  845. request.QueryParams["PriceUnit"] = node.PriceUnit
  846. if node.SystemDisk != nil {
  847. // Only if the required information is present it should be overridden else default it via the API
  848. if node.SystemDisk.DiskCategory != "" {
  849. request.QueryParams["SystemDisk.Category"] = node.SystemDisk.DiskCategory
  850. }
  851. if node.SystemDisk.SizeInGiB != "" {
  852. request.QueryParams["SystemDisk.Size"] = node.SystemDisk.SizeInGiB
  853. }
  854. if node.SystemDisk.PerformanceLevel != "" {
  855. request.QueryParams["SystemDisk.PerformanceLevel"] = node.SystemDisk.PerformanceLevel
  856. }
  857. } else {
  858. // When the system disk information is not available, and the instance family is g6e or r6e,
  859. // or the instance generation is 6 or above, the default disk category in DescribePrice should be cloud_essd.
  860. if slices.Contains(alibabaDefaultToCloudEssd, node.InstanceTypeFamily) || getInstanceFamilyGenerationFromType(node.InstanceType) > 6 {
  861. request.QueryParams["SystemDisk.Category"] = ALIBABA_DISK_CLOUD_ESSD_CATEGORY
  862. }
  863. }
  864. request.TransToAcsRequest()
  865. return request, nil
  866. case *SlimK8sDisk:
  867. disk := i.(*SlimK8sDisk)
  868. request.QueryParams["RegionId"] = disk.RegionID
  869. request.QueryParams["PriceUnit"] = disk.PriceUnit
  870. request.QueryParams["ResourceType"] = ALIBABA_DISK_RESOURCE_TYPE
  871. request.QueryParams[fmt.Sprintf("%s.%d.Size", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.SizeInGiB
  872. request.QueryParams[fmt.Sprintf("%s.%d.Category", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.DiskCategory
  873. // Performance level defaults to PL1 if not present in volume attribute.
  874. if disk.PerformanceLevel != "" {
  875. request.QueryParams[fmt.Sprintf("%s.%d.PerformanceLevel", ALIBABA_DATA_DISK_PREFIX, 1)] = disk.PerformanceLevel
  876. }
  877. request.TransToAcsRequest()
  878. return request, nil
  879. default:
  880. return nil, fmt.Errorf("unsupported ECS type (%T) for DescribePrice at this time", i)
  881. }
  882. }
  883. // createDescribeDisksCSRequest creates the HTTP GET Request to map the system disk to the InstanceID
  884. func createDescribeDisksACSRequest(instanceID, regionID, diskType string) (*requests.CommonRequest, error) {
  885. request := requests.NewCommonRequest()
  886. request.Method = requests.GET
  887. request.Product = ALIBABA_ECS_PRODUCT_CODE
  888. request.Domain = ALIBABA_ECS_DOMAIN
  889. request.Version = ALIBABA_ECS_VERSION
  890. request.Scheme = requests.HTTPS
  891. request.ApiName = ALIBABA_DESCRIBE_DISK_API_ACTION
  892. request.QueryParams["RegionId"] = regionID
  893. request.QueryParams["InstanceId"] = instanceID
  894. request.QueryParams["DiskType"] = diskType
  895. request.TransToAcsRequest()
  896. return request, nil
  897. }
  898. // determineKeyForPricing generate a unique key from SlimK8sNode object that is constructed from v1.Node object and
  899. // SlimK8sDisk that is constructed from v1.PersistentVolume.
  900. func determineKeyForPricing(i interface{}) (string, error) {
  901. if i == nil {
  902. return "", fmt.Errorf("nil component passed to determine key")
  903. }
  904. switch i.(type) {
  905. case *SlimK8sNode:
  906. node := i.(*SlimK8sNode)
  907. var diskCategory, diskSizeInGiB, diskPerformanceLevel string
  908. if node.SystemDisk != nil {
  909. diskCategory = node.SystemDisk.DiskCategory
  910. diskSizeInGiB = node.SystemDisk.SizeInGiB
  911. diskPerformanceLevel = node.SystemDisk.PerformanceLevel
  912. }
  913. if node.IsIoOptimized {
  914. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{node.RegionID, node.InstanceType, node.OSType, ALIBABA_OPTIMIZE_KEYWORD, diskCategory, diskSizeInGiB, diskPerformanceLevel})
  915. return strings.Join(keyLookup, "::"), nil
  916. } else {
  917. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{node.RegionID, node.InstanceType, node.OSType, ALIBABA_NON_OPTIMIZE_KEYWORD, diskCategory, diskSizeInGiB, diskPerformanceLevel})
  918. return strings.Join(keyLookup, "::"), nil
  919. }
  920. case *SlimK8sDisk:
  921. disk := i.(*SlimK8sDisk)
  922. keyLookup := stringutil.DeleteEmptyStringsFromArray([]string{disk.RegionID, disk.DiskType, disk.DiskCategory, disk.PerformanceLevel, disk.SizeInGiB})
  923. return strings.Join(keyLookup, "::"), nil
  924. default:
  925. return "", fmt.Errorf("unsupported ECS type (%T) at this time", i)
  926. }
  927. }
  928. // Below structs are used to unmarshal json response of Alibaba cloud's API DescribePrice
  929. type Price struct {
  930. OriginalPrice float32 `json:"OriginalPrice"`
  931. ReservedInstanceHourPrice float32 `json:"ReservedInstanceHourPrice"`
  932. DiscountPrice float32 `json:"DiscountPrice"`
  933. Currency string `json:"Currency"`
  934. TradePrice float32 `json:"TradePrice"`
  935. }
  936. type PriceInfo struct {
  937. Price Price `json:"Price"`
  938. }
  939. type DescribePriceResponse struct {
  940. RequestId string `json:"RequestId"`
  941. PriceInfo PriceInfo `json:"PriceInfo"`
  942. }
  943. // processDescribePriceAndCreateAlibabaPricing processes the DescribePrice API and generates the pricing information for alibaba node resource and alibaba pv resource that's backed by cloud disk.
  944. func processDescribePriceAndCreateAlibabaPricing(client *sdk.Client, i interface{}, signer *signers.AccessKeySigner, custom *models.CustomPricing) (pricing *AlibabaPricing, err error) {
  945. pricing = &AlibabaPricing{}
  946. var response DescribePriceResponse
  947. if i == nil {
  948. return nil, fmt.Errorf("nil component passed to process the pricing information")
  949. }
  950. switch i.(type) {
  951. case *SlimK8sNode:
  952. node := i.(*SlimK8sNode)
  953. req, err := createDescribePriceACSRequest(node)
  954. if err != nil {
  955. return nil, err
  956. }
  957. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  958. pricing.NodeAttributes = NewAlibabaNodeAttributes(node)
  959. if err != nil || resp.GetHttpStatus() != 200 {
  960. // Try again but default the disk to something else
  961. return nil, fmt.Errorf("unable to fetch information for node with InstanceType: %v", node.InstanceType)
  962. } else {
  963. // This is where population of Pricing happens
  964. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  965. if err != nil {
  966. return nil, fmt.Errorf("unable to unmarshall json response to custom struct with err: %w", err)
  967. }
  968. // TO-DO : Ask in PR How to get the defaults is it equal to AWS/GCP defaults? And what needs to be returned
  969. pricing.Node = &models.Node{
  970. Cost: fmt.Sprintf("%f", response.PriceInfo.Price.TradePrice),
  971. BaseCPUPrice: custom.CPU,
  972. BaseRAMPrice: custom.RAM,
  973. BaseGPUPrice: custom.GPU,
  974. }
  975. // TO-DO : Currently with Pay-As-You-go Offering TradePrice = HourlyPrice , When support happens to other type HourlyPrice Need to be determined.
  976. 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))
  977. }
  978. case *SlimK8sDisk:
  979. disk := i.(*SlimK8sDisk)
  980. req, err := createDescribePriceACSRequest(disk)
  981. if err != nil {
  982. return nil, err
  983. }
  984. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  985. if err != nil || resp.GetHttpStatus() != 200 {
  986. return nil, fmt.Errorf("unable to fetch information for disk with DiskType: %v with err: %w", disk.DiskCategory, err)
  987. } else {
  988. // This is where population of Pricing happens
  989. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  990. if err != nil {
  991. return nil, fmt.Errorf("unable to unmarshall json response to custom struct with err: %w", err)
  992. }
  993. pricing.PVAttributes = NewAlibabaPVAttributes(disk)
  994. pricing.PV = &models.PV{
  995. Cost: fmt.Sprintf("%f", response.PriceInfo.Price.TradePrice),
  996. }
  997. // 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.
  998. 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))
  999. }
  1000. default:
  1001. return nil, fmt.Errorf("unsupported ECS Pricing component of type (%T) at this time", i)
  1002. }
  1003. return pricing, nil
  1004. }
  1005. // This function is to get the InstanceFamily from the InstanceType , convention followed in
  1006. // instance type is ecs.[FamilyName].[DifferentSize], it gets the familyName , if it is unable to get it
  1007. // it lists the instance family name as Unknown.
  1008. func getInstanceFamilyFromType(instanceType string) string {
  1009. splitinstanceType := strings.Split(instanceType, ".")
  1010. if len(splitinstanceType) != 3 {
  1011. log.Warnf("unable to find the family of the instance type %s, returning its family type unknown", instanceType)
  1012. return ALIBABA_UNKNOWN_INSTANCE_FAMILY_TYPE
  1013. }
  1014. return splitinstanceType[1]
  1015. }
  1016. // This function is used to obtain the generation of the instance family from the InstanceType,
  1017. // because when the generation is higher than or equal to 7, the instance disk type will not support cloud_efficiency.
  1018. // In such cases, when calling the DescribePrice interface, the system disk type will default to cloud_essd.
  1019. func getInstanceFamilyGenerationFromType(instanceType string) int {
  1020. // FamilyName format: g7ne or g7 or r7 or r6e,
  1021. familyName := getInstanceFamilyFromType(instanceType)
  1022. re := regexp.MustCompile(`(\d+)`)
  1023. match := re.FindString(familyName)
  1024. if match != "" {
  1025. generation, err := strconv.Atoi(match)
  1026. if err != nil {
  1027. log.Errorf("unable to convert the generation of the instance type %s to integer", instanceType)
  1028. } else {
  1029. return generation
  1030. }
  1031. }
  1032. log.Warnf("unable to find the generation of the instance type %s,", instanceType)
  1033. return -1
  1034. }
  1035. // getInstanceIDFromProviderID returns the instance ID associated with the Node. A *v1.Node providerID in Alibaba cloud
  1036. // is of <REGION-ID>.<INSTANCE-ID>. This function returns the Instance ID for the given ProviderID. if its unable to interpret
  1037. // it defaults to empty string.
  1038. func getInstanceIDFromProviderID(providerID string) string {
  1039. if providerID == "" {
  1040. return ""
  1041. }
  1042. splitStrings := strings.Split(providerID, ".")
  1043. if len(splitStrings) < 2 {
  1044. return ""
  1045. }
  1046. return splitStrings[1]
  1047. }
  1048. type Disk struct {
  1049. Category string `json:"Category"`
  1050. Size int `json:"Size"`
  1051. PerformanceLevel string `json:"PerformanceLevel"`
  1052. Type string `json:"Type"`
  1053. RegionId string `json:"RegionId"`
  1054. DiskId string `json:"DiskId"`
  1055. DiskChargeType string `json:"DiskChargeType"`
  1056. }
  1057. type Disks struct {
  1058. Disk []*Disk `json:"Disk"`
  1059. }
  1060. type DescribeDiskResponse struct {
  1061. TotalCount int `json:"TotalCount"`
  1062. Disks *Disks `json:"Disks"`
  1063. }
  1064. // getSystemDiskInfoOfANode gets the relevant System disk information associated with the Node given by the instanceID
  1065. // in form of a SlimK8sDisk with only relevant information that can adjust the node pricing. If any error occurs return
  1066. // an empty disk to not impact any default set at the price retrieval of the node.
  1067. func getSystemDiskInfoOfANode(instanceID, regionID string, client *sdk.Client, signer *signers.AccessKeySigner) (systemDisk *SlimK8sDisk) {
  1068. systemDisk = &SlimK8sDisk{}
  1069. var response DescribeDiskResponse
  1070. // if instanceID is empty string return an empty k8s
  1071. if instanceID == "" {
  1072. return
  1073. }
  1074. req, err := createDescribeDisksACSRequest(instanceID, regionID, ALIBABA_SYSTEM_DISK_CATEGORY)
  1075. // if any error occurs return an empty disk to not impact default pricing.
  1076. if err != nil {
  1077. 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)
  1078. return
  1079. }
  1080. resp, err := client.ProcessCommonRequestWithSigner(req, signer)
  1081. if err != nil || resp.GetHttpStatus() != 200 {
  1082. 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)
  1083. return
  1084. } else {
  1085. // This is where population of Pricing happens
  1086. err = json.Unmarshal(resp.GetHttpContentBytes(), &response)
  1087. if err != nil {
  1088. 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)
  1089. return
  1090. }
  1091. // 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,
  1092. // if TotalCount is not 1 just return empty and let it not impact default pricing.
  1093. if response.TotalCount != 1 {
  1094. 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)
  1095. return
  1096. }
  1097. if response.Disks == nil {
  1098. log.Warnf("Disks information missing for node with InstanceID: %s, hence defaulting it to an empty system disk to pass through to defaults", instanceID)
  1099. return
  1100. }
  1101. if len(response.Disks.Disk) < 1 {
  1102. 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)
  1103. return
  1104. }
  1105. // TO-DO: When supporting Subscription type disk, you can leverge the disk.DiskChargeType here to map it to subscription type.
  1106. systemDisk := response.Disks.Disk[0]
  1107. return NewSlimK8sDisk(systemDisk.Type, systemDisk.RegionId, ALIBABA_HOUR_PRICE_UNIT, systemDisk.Category, systemDisk.PerformanceLevel, systemDisk.DiskId, "", fmt.Sprintf("%d", systemDisk.Size))
  1108. }
  1109. }
  1110. // generateSlimK8sNodeFromV1Node generates SlimK8sNode struct from v1.Node to fetch pricing information and call alibaba API.
  1111. func generateSlimK8sNodeFromV1Node(node *clustercache.Node) *SlimK8sNode {
  1112. var regionID, osType, instanceType, providerID, priceUnit, instanceFamily string
  1113. var memorySizeInKiB string // TO-DO: try to convert it into float
  1114. var ok, IsIoOptimized bool
  1115. if regionID, ok = node.Labels["topology.kubernetes.io/region"]; !ok {
  1116. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1117. log.Debugf("No RegionID label for the node: %s", node.Name)
  1118. }
  1119. if osType, ok = node.Labels["beta.kubernetes.io/os"]; !ok {
  1120. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1121. log.Debugf("OS type undetected for the node: %s", node.Name)
  1122. }
  1123. if instanceType, ok = node.Labels["node.kubernetes.io/instance-type"]; !ok {
  1124. // HIGHLY UNLIKELY THAT THIS LABEL WONT BE THERE.
  1125. log.Debugf("Instance Type undetected for the node: %s", node.Name)
  1126. }
  1127. instanceFamily = getInstanceFamilyFromType(instanceType)
  1128. memorySizeInKiB = fmt.Sprintf("%s", node.Status.Capacity.Memory())
  1129. providerID = node.SpecProviderID // Alibaba Cloud provider doesnt follow convention of prefix with cloud provider name
  1130. // 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
  1131. // Basic price Json has it as part of the key so defaulting to true.
  1132. IsIoOptimized = true
  1133. priceUnit = ALIBABA_HOUR_PRICE_UNIT
  1134. systemDisk := &SlimK8sDisk{}
  1135. return NewSlimK8sNode(instanceType, regionID, priceUnit, memorySizeInKiB, osType, providerID, instanceFamily, IsIoOptimized, systemDisk)
  1136. }
  1137. // getNumericalValueFromResourceQuantity returns the numericalValue of the resourceQuantity
  1138. // An example is: 20Gi returns to 20. If any error occurs it returns the default value used in describePrice API which is 2000.
  1139. func getNumericalValueFromResourceQuantity(quantity string) (value string) {
  1140. // defaulting when any panic or empty string occurs.
  1141. defer func() {
  1142. log.Debugf("unable to determine the size of the PV so defaulting the size to %s", ALIBABA_DEFAULT_DATADISK_SIZE)
  1143. if err := recover(); err != nil {
  1144. value = ALIBABA_DEFAULT_DATADISK_SIZE
  1145. }
  1146. if value == "" {
  1147. value = ALIBABA_DEFAULT_DATADISK_SIZE
  1148. }
  1149. }()
  1150. res := sizeRegEx.FindAllStringSubmatch(quantity, 1)
  1151. value = res[0][1]
  1152. return
  1153. }
  1154. // generateSlimK8sDiskFromV1PV function generates SlimK8sDisk from v1.PersistentVolume
  1155. // to generate slim disk type that can be used to fetch pricing information for Data disk type.
  1156. func generateSlimK8sDiskFromV1PV(pv *clustercache.PersistentVolume, regionID string) *SlimK8sDisk {
  1157. // All PVs are data disks while local disk are categorized as system disk
  1158. diskType := ALIBABA_DATA_DISK_CATEGORY
  1159. //TO-DO: Disk supports month and hour prices , defaulting to hour
  1160. priceUnit := ALIBABA_HOUR_PRICE_UNIT
  1161. sizeQuantity := fmt.Sprintf("%s", pv.Spec.Capacity.Storage())
  1162. // res := sizeRegEx.FindAllStringSubmatch(sizeQuantity, 1)
  1163. sizeInGiB := getNumericalValueFromResourceQuantity(sizeQuantity)
  1164. providerID := ""
  1165. if pv.Spec.CSI != nil {
  1166. providerID = pv.Spec.CSI.VolumeHandle
  1167. } else {
  1168. providerID = pv.Name // Looks like pv name is same as providerID in Alibaba k8s cluster
  1169. }
  1170. // Performance level being empty string gets defaulted in describePrice to PL1.
  1171. performanceLevel := ""
  1172. diskCategory := ""
  1173. if pv.Spec.CSI != nil {
  1174. if val, ok := pv.Spec.CSI.VolumeAttributes["performanceLevel"]; ok {
  1175. performanceLevel = val
  1176. }
  1177. if val, ok := pv.Spec.CSI.VolumeAttributes["type"]; ok {
  1178. diskCategory = val
  1179. }
  1180. }
  1181. // Highly unlikely that label pv.Spec.CSI.VolumeAttributes["type"] doesn't exist but if occurred default to cloud (most basic disk type)
  1182. if diskCategory == "" {
  1183. diskCategory = ALIBABA_DISK_CLOUD_CATEGORY
  1184. }
  1185. return NewSlimK8sDisk(diskType, regionID, priceUnit, diskCategory, performanceLevel, providerID, pv.Spec.StorageClassName, sizeInGiB)
  1186. }
  1187. // 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!
  1188. // if topology.diskplugin.csi.alibabacloud.com/region label/annotation is passed during PV creation return that as the PV region.
  1189. // if topology.diskplugin.csi.alibabacloud.com/zone label/annotation is passed during PV creation determine the region based on this pv label.
  1190. // if neither of the above label/annotation is present check node affinity for the zone affinity and determine the region based on this zone.
  1191. // if nether of the above yields a region , return empty string to default it to cluster region.
  1192. func determinePVRegion(pv *clustercache.PersistentVolume) string {
  1193. // if "topology.diskplugin.csi.alibabacloud.com/region" is present as a label or annotation return that as the PV region
  1194. if val, ok := pv.Labels[ALIBABA_DISK_TOPOLOGY_REGION_LABEL]; ok {
  1195. log.Debugf("determinePVRegion returned a region value of: %s through label: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_REGION_LABEL, pv.Name)
  1196. return val
  1197. }
  1198. if val, ok := pv.Annotations[ALIBABA_DISK_TOPOLOGY_REGION_LABEL]; ok {
  1199. log.Debugf("determinePVRegion returned a region value of: %s through annotation: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_REGION_LABEL, pv.Name)
  1200. return val
  1201. }
  1202. // 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
  1203. var pvZone string
  1204. if val, ok := pv.Labels[ALIBABA_DISK_TOPOLOGY_ZONE_LABEL]; ok {
  1205. log.Debugf("determinePVRegion will set zone value to: %s through label: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_ZONE_LABEL, pv.Name)
  1206. pvZone = val
  1207. }
  1208. if pvZone == "" {
  1209. if val, ok := pv.Annotations[ALIBABA_DISK_TOPOLOGY_ZONE_LABEL]; ok {
  1210. log.Debugf("determinePVRegion will set zone value to: %s through annotation: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_ZONE_LABEL, pv.Name)
  1211. pvZone = val
  1212. }
  1213. }
  1214. if pvZone == "" {
  1215. // 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
  1216. // 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.
  1217. if pv.Spec.NodeAffinity != nil {
  1218. nodeAffinity := pv.Spec.NodeAffinity
  1219. if nodeAffinity.Required != nil && nodeAffinity.Required.NodeSelectorTerms != nil {
  1220. for _, nodeSelectorTerm := range nodeAffinity.Required.NodeSelectorTerms {
  1221. matchExpression := nodeSelectorTerm.MatchExpressions
  1222. for _, nodeSelectorRequirement := range matchExpression {
  1223. if nodeSelectorRequirement.Key == ALIBABA_DISK_TOPOLOGY_ZONE_LABEL {
  1224. 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)
  1225. pvZone = nodeSelectorRequirement.Values[0]
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. }
  1232. regionOverrides := ocenv.GetRegionOverrideList()
  1233. regions := alibabaRegions
  1234. if len(regionOverrides) > 0 {
  1235. regions = regionOverrides
  1236. }
  1237. for _, region := range regions {
  1238. if strings.Contains(pvZone, region) {
  1239. log.Debugf("determinePVRegion determined region of %s through zone affiliation of the PV %s\n", region, pvZone)
  1240. return region
  1241. }
  1242. }
  1243. return ""
  1244. }
  1245. // PricingSourceSummary returns the pricing source summary for the provider.
  1246. // The summary represents what was _parsed_ from the pricing source, not
  1247. // everything that was _available_ in the pricing source.
  1248. func (a *Alibaba) PricingSourceSummary() interface{} {
  1249. return a.Pricing
  1250. }