provider.go 8.4 KB

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