provider.go 8.3 KB

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