2
0

provider.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package oracle
  2. import (
  3. "fmt"
  4. "io"
  5. "strconv"
  6. "sync"
  7. "github.com/opencost/opencost/core/pkg/clustercache"
  8. coreenv "github.com/opencost/opencost/core/pkg/env"
  9. "github.com/opencost/opencost/core/pkg/log"
  10. "github.com/opencost/opencost/core/pkg/opencost"
  11. "github.com/opencost/opencost/core/pkg/util"
  12. "github.com/opencost/opencost/core/pkg/util/json"
  13. "github.com/opencost/opencost/pkg/cloud/models"
  14. "github.com/opencost/opencost/pkg/cloud/utils"
  15. "github.com/opencost/opencost/pkg/env"
  16. )
  17. const nodePoolIdAnnotation = "oci.oraclecloud.com/node-pool-id"
  18. const virtualPoolIdAnnotation = "oci.oraclecloud.com/virtual-node-pool-id"
  19. const virtualNodeLabel = "node-role.kubernetes.io/virtual-node"
  20. const preemptibleLabel = "oci.oraclecloud.com/oke-is-preemptible"
  21. const managementPlatformOKE = "oke"
  22. const currencyCodeUSD = "USD"
  23. type Oracle struct {
  24. Config models.ProviderConfig
  25. Clientset clustercache.ClusterCache
  26. ClusterRegion string
  27. ClusterAccountID string
  28. DownloadPricingDataLock sync.RWMutex
  29. OSEnvLock sync.Mutex
  30. RateCardStore *RateCardStore
  31. ServiceAccountChecks *models.ServiceAccountChecks
  32. DefaultPricing DefaultPricing
  33. }
  34. func (o *Oracle) ClusterInfo() (map[string]string, error) {
  35. c, err := o.GetConfig()
  36. if err != nil {
  37. return nil, err
  38. }
  39. m := make(map[string]string)
  40. m["name"] = "Oracle Cluster #1"
  41. if clusterName := o.getClusterName(c); clusterName != "" {
  42. m["name"] = clusterName
  43. }
  44. m["provider"] = opencost.OracleProvider
  45. m["account"] = o.ClusterAccountID
  46. m["region"] = o.ClusterRegion
  47. m["remoteReadEnabled"] = strconv.FormatBool(env.IsRemoteEnabled())
  48. m["id"] = coreenv.GetClusterID()
  49. return m, nil
  50. }
  51. func (o *Oracle) NodePricing(key models.Key) (*models.Node, models.PricingMetadata, error) {
  52. if err := o.ensurePricingData(); err != nil {
  53. return nil, models.PricingMetadata{}, err
  54. }
  55. o.DownloadPricingDataLock.RLock()
  56. defer o.DownloadPricingDataLock.RUnlock()
  57. node, metadata, err := o.RateCardStore.ForKey(key, o.DefaultPricing)
  58. if err != nil {
  59. return node, metadata, err
  60. }
  61. // Check if the node is preemptible based on the label
  62. if oracleKey, ok := key.(*oracleKey); ok {
  63. if val, exists := oracleKey.labels[preemptibleLabel]; exists && val == "true" {
  64. node.UsageType = "preemptible"
  65. }
  66. }
  67. return node, metadata, nil
  68. }
  69. func (o *Oracle) GpuPricing(nodeLabels map[string]string) (string, error) {
  70. return "", nil
  71. }
  72. func (o *Oracle) PVPricing(pvk models.PVKey) (*models.PV, error) {
  73. if err := o.ensurePricingData(); err != nil {
  74. return nil, err
  75. }
  76. o.DownloadPricingDataLock.RLock()
  77. defer o.DownloadPricingDataLock.RUnlock()
  78. return o.RateCardStore.ForPVK(pvk, o.DefaultPricing)
  79. }
  80. func (o *Oracle) NetworkPricing() (*models.Network, error) {
  81. if err := o.ensurePricingData(); err != nil {
  82. return nil, err
  83. }
  84. o.DownloadPricingDataLock.RLock()
  85. defer o.DownloadPricingDataLock.RUnlock()
  86. return o.RateCardStore.ForEgressRegion(o.ClusterRegion, o.DefaultPricing)
  87. }
  88. func (o *Oracle) LoadBalancerPricing() (*models.LoadBalancer, error) {
  89. if err := o.ensurePricingData(); err != nil {
  90. return nil, err
  91. }
  92. o.DownloadPricingDataLock.RLock()
  93. defer o.DownloadPricingDataLock.RUnlock()
  94. return o.RateCardStore.ForLB(o.DefaultPricing)
  95. }
  96. func (o *Oracle) AllNodePricing() (interface{}, error) {
  97. if err := o.ensurePricingData(); err != nil {
  98. return nil, err
  99. }
  100. o.DownloadPricingDataLock.RLock()
  101. defer o.DownloadPricingDataLock.RUnlock()
  102. return o.RateCardStore.Store(), nil
  103. }
  104. // DownloadPricingData refreshes the RateCardStore pricing data.
  105. func (o *Oracle) DownloadPricingData() error {
  106. o.DownloadPricingDataLock.Lock()
  107. defer o.DownloadPricingDataLock.Unlock()
  108. cfg, err := o.GetConfig()
  109. if err != nil {
  110. return err
  111. }
  112. if o.RateCardStore == nil {
  113. url := env.GetOCIPricingURL()
  114. o.RateCardStore = NewRateCardStore(url, cfg.CurrencyCode)
  115. }
  116. if _, err := o.RateCardStore.Refresh(); err != nil {
  117. return err
  118. }
  119. o.DefaultPricing = DefaultPricing{
  120. OCPU: cfg.CPU,
  121. Memory: cfg.RAM,
  122. GPU: cfg.GPU,
  123. Storage: cfg.Storage,
  124. Egress: cfg.InternetNetworkEgress,
  125. LB: cfg.DefaultLBPrice,
  126. }
  127. return nil
  128. }
  129. func (o *Oracle) GetKey(labels map[string]string, n *clustercache.Node) models.Key {
  130. var gpuCount int
  131. var gpuType string
  132. if gpuc, ok := n.Status.Capacity["nvidia.com/gpu"]; ok {
  133. gpuCount = int(gpuc.Value())
  134. gpuType = "nvidia.com/gpu"
  135. }
  136. instanceType, _ := util.GetInstanceType(labels)
  137. return &oracleKey{
  138. providerID: n.SpecProviderID,
  139. instanceType: instanceType,
  140. labels: labels,
  141. gpuCount: gpuCount,
  142. gpuType: gpuType,
  143. }
  144. }
  145. func (o *Oracle) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, _ string) models.PVKey {
  146. var providerID string
  147. var driver string
  148. if pv.Spec.CSI != nil {
  149. providerID = pv.Spec.CSI.VolumeHandle
  150. driver = pv.Spec.CSI.Driver
  151. }
  152. return &oraclePVKey{
  153. storageClass: pv.Spec.StorageClassName,
  154. providerID: providerID,
  155. driver: driver,
  156. parameters: parameters,
  157. }
  158. }
  159. func (o *Oracle) UpdateConfig(r io.Reader, _ string) (*models.CustomPricing, error) {
  160. return o.Config.Update(func(pricing *models.CustomPricing) error {
  161. a := make(map[string]interface{})
  162. err := json.NewDecoder(r).Decode(&a)
  163. if err != nil {
  164. return err
  165. }
  166. for k, v := range a {
  167. kUpper := utils.ToTitle.String(k)
  168. vstr, ok := v.(string)
  169. if ok {
  170. err := models.SetCustomPricingField(pricing, kUpper, vstr)
  171. if err != nil {
  172. return fmt.Errorf("error setting custom pricing field: %w", err)
  173. }
  174. } else {
  175. return fmt.Errorf("type error while updating config for %s", kUpper)
  176. }
  177. }
  178. if env.IsRemoteEnabled() {
  179. err := utils.UpdateClusterMeta(coreenv.GetClusterID(), o.getClusterName(pricing))
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. return nil
  185. })
  186. }
  187. func (o *Oracle) UpdateConfigFromConfigMap(m map[string]string) (*models.CustomPricing, error) {
  188. return o.Config.UpdateFromMap(m)
  189. }
  190. func (o *Oracle) GetConfig() (*models.CustomPricing, error) {
  191. c, err := o.Config.GetCustomPricingData()
  192. if err != nil {
  193. return nil, err
  194. }
  195. if c.Discount == "" {
  196. c.Discount = "0%"
  197. }
  198. if c.NegotiatedDiscount == "" {
  199. c.NegotiatedDiscount = "0%"
  200. }
  201. if c.CurrencyCode == "" {
  202. c.CurrencyCode = currencyCodeUSD
  203. }
  204. return c, nil
  205. }
  206. func (o *Oracle) GetManagementPlatform() (string, error) {
  207. nodes := o.Clientset.GetAllNodes()
  208. for _, node := range nodes {
  209. if _, ok := node.Annotations[nodePoolIdAnnotation]; ok {
  210. return managementPlatformOKE, nil
  211. }
  212. if _, ok := node.Annotations[virtualPoolIdAnnotation]; ok {
  213. return managementPlatformOKE, nil
  214. }
  215. }
  216. return "", nil
  217. }
  218. func (o *Oracle) PricingSourceStatus() map[string]*models.PricingSource {
  219. listPricing := "List Pricing"
  220. return map[string]*models.PricingSource{
  221. listPricing: {
  222. Name: listPricing,
  223. Enabled: true,
  224. Available: true,
  225. },
  226. }
  227. }
  228. func (o *Oracle) ClusterManagementPricing() (string, float64, error) {
  229. if err := o.ensurePricingData(); err != nil {
  230. return "", 0.0, err
  231. }
  232. managementPlatform, _ := o.GetManagementPlatform()
  233. if managementPlatform != managementPlatformOKE {
  234. return "", 0.0, nil // Self-managed cluster.
  235. }
  236. o.DownloadPricingDataLock.Lock()
  237. defer o.DownloadPricingDataLock.Unlock()
  238. // TODO: Support lookup of cluster type, as BASIC_CLUSTER types are free.
  239. return managementPlatformOKE, o.RateCardStore.ForManagedCluster("ENHANCED_CLUSTER"), nil
  240. }
  241. func (o *Oracle) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
  242. return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
  243. }
  244. func (o *Oracle) Regions() []string {
  245. regionOverrides := env.GetRegionOverrideList()
  246. if len(regionOverrides) > 0 {
  247. log.Debugf("Overriding Oracle regions with configured region list: %+v", regionOverrides)
  248. return regionOverrides
  249. }
  250. return oracleRegions()
  251. }
  252. func (o *Oracle) PricingSourceSummary() interface{} {
  253. if err := o.ensurePricingData(); err != nil {
  254. return err
  255. }
  256. o.DownloadPricingDataLock.Lock()
  257. defer o.DownloadPricingDataLock.Unlock()
  258. return o.RateCardStore.Store()
  259. }
  260. func (o *Oracle) getClusterName(cfg *models.CustomPricing) string {
  261. if cfg.ClusterName != "" {
  262. return cfg.ClusterName
  263. }
  264. for _, node := range o.Clientset.GetAllNodes() {
  265. if clusterName, ok := node.Labels["name"]; ok {
  266. return clusterName
  267. }
  268. }
  269. return ""
  270. }
  271. func (o *Oracle) ensurePricingData() error {
  272. if o.RateCardStore == nil {
  273. return o.DownloadPricingData()
  274. }
  275. return nil
  276. }
  277. func (o *Oracle) GetAddresses() ([]byte, error) {
  278. return nil, nil
  279. }
  280. func (o *Oracle) GetDisks() ([]byte, error) {
  281. return nil, nil
  282. }
  283. func (o *Oracle) GetOrphanedResources() ([]models.OrphanedResource, error) {
  284. return nil, nil
  285. }
  286. func (o *Oracle) ApplyReservedInstancePricing(m map[string]*models.Node) {}
  287. func (o *Oracle) ServiceAccountStatus() *models.ServiceAccountStatus {
  288. return o.ServiceAccountChecks.GetStatus()
  289. }