customprovider.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package cloud
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "github.com/opencost/opencost/pkg/cloud/models"
  10. "github.com/opencost/opencost/pkg/cloud/utils"
  11. "github.com/opencost/opencost/pkg/clustercache"
  12. "github.com/opencost/opencost/pkg/env"
  13. "github.com/opencost/opencost/pkg/kubecost"
  14. "github.com/opencost/opencost/pkg/log"
  15. "github.com/opencost/opencost/pkg/util/json"
  16. v1 "k8s.io/api/core/v1"
  17. )
  18. type NodePrice struct {
  19. CPU string
  20. RAM string
  21. GPU string
  22. }
  23. type CustomProvider struct {
  24. Clientset clustercache.ClusterCache
  25. Pricing map[string]*NodePrice
  26. SpotLabel string
  27. SpotLabelValue string
  28. GPULabel string
  29. GPULabelValue string
  30. clusterRegion string
  31. clusterAccountID string
  32. DownloadPricingDataLock sync.RWMutex
  33. Config *ProviderConfig
  34. }
  35. // PricingSourceSummary returns the pricing source summary for the provider.
  36. // The summary represents what was _parsed_ from the pricing source, not what
  37. // was returned from the relevant API.
  38. func (cp *CustomProvider) PricingSourceSummary() interface{} {
  39. return cp.Pricing
  40. }
  41. type customProviderKey struct {
  42. SpotLabel string
  43. SpotLabelValue string
  44. GPULabel string
  45. GPULabelValue string
  46. Labels map[string]string
  47. }
  48. func (*CustomProvider) ClusterManagementPricing() (string, float64, error) {
  49. return "", 0.0, nil
  50. }
  51. func (*CustomProvider) GetLocalStorageQuery(window, offset time.Duration, rate bool, used bool) string {
  52. return ""
  53. }
  54. func (cp *CustomProvider) GetConfig() (*models.CustomPricing, error) {
  55. return cp.Config.GetCustomPricingData()
  56. }
  57. func (*CustomProvider) GetManagementPlatform() (string, error) {
  58. return "", nil
  59. }
  60. func (*CustomProvider) ApplyReservedInstancePricing(nodes map[string]*models.Node) {
  61. }
  62. func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*models.CustomPricing, error) {
  63. return cp.Config.UpdateFromMap(a)
  64. }
  65. func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*models.CustomPricing, error) {
  66. // Parse config updates from reader
  67. a := make(map[string]interface{})
  68. err := json.NewDecoder(r).Decode(&a)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // Update Config
  73. c, err := cp.Config.Update(func(c *models.CustomPricing) error {
  74. for k, v := range a {
  75. kUpper := utils.ToTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  76. vstr, ok := v.(string)
  77. if ok {
  78. err := models.SetCustomPricingField(c, kUpper, vstr)
  79. if err != nil {
  80. return err
  81. }
  82. } else {
  83. return fmt.Errorf("type error while updating config for %s", kUpper)
  84. }
  85. }
  86. return nil
  87. })
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer cp.DownloadPricingData()
  92. return c, nil
  93. }
  94. func (cp *CustomProvider) ClusterInfo() (map[string]string, error) {
  95. conf, err := cp.GetConfig()
  96. if err != nil {
  97. return nil, err
  98. }
  99. m := make(map[string]string)
  100. if conf.ClusterName != "" {
  101. m["name"] = conf.ClusterName
  102. }
  103. m["provider"] = kubecost.CustomProvider
  104. m["region"] = cp.clusterRegion
  105. m["account"] = cp.clusterAccountID
  106. m["id"] = env.GetClusterID()
  107. return m, nil
  108. }
  109. func (*CustomProvider) GetAddresses() ([]byte, error) {
  110. return nil, nil
  111. }
  112. func (*CustomProvider) GetDisks() ([]byte, error) {
  113. return nil, nil
  114. }
  115. func (*CustomProvider) GetOrphanedResources() ([]models.OrphanedResource, error) {
  116. return nil, errors.New("not implemented")
  117. }
  118. func (cp *CustomProvider) AllNodePricing() (interface{}, error) {
  119. cp.DownloadPricingDataLock.RLock()
  120. defer cp.DownloadPricingDataLock.RUnlock()
  121. return cp.Pricing, nil
  122. }
  123. func (cp *CustomProvider) NodePricing(key models.Key) (*models.Node, error) {
  124. cp.DownloadPricingDataLock.RLock()
  125. defer cp.DownloadPricingDataLock.RUnlock()
  126. k := key.Features()
  127. var gpuCount string
  128. if _, ok := cp.Pricing[k]; !ok {
  129. // Default is saying that there is no pricing info for the cluster and we should fall back to the defualt values.
  130. // An interesting case is if the default values weren't loaded.
  131. k = "default"
  132. }
  133. if key.GPUType() != "" {
  134. k += ",gpu" // TODO: support multiple custom gpu types.
  135. gpuCount = "1" // TODO: support more than one gpu.
  136. }
  137. var cpuCost, ramCost, gpuCost string
  138. if pricing, ok := cp.Pricing[k]; !ok {
  139. log.Warnf("No pricing found for key=%s, setting values to 0", k)
  140. cpuCost = "0.0"
  141. ramCost = "0.0"
  142. gpuCost = "0.0"
  143. } else {
  144. cpuCost = pricing.CPU
  145. ramCost = pricing.RAM
  146. gpuCost = pricing.GPU
  147. }
  148. return &models.Node{
  149. VCPUCost: cpuCost,
  150. RAMCost: ramCost,
  151. GPUCost: gpuCost,
  152. GPU: gpuCount,
  153. }, nil
  154. }
  155. func (cp *CustomProvider) DownloadPricingData() error {
  156. cp.DownloadPricingDataLock.Lock()
  157. defer cp.DownloadPricingDataLock.Unlock()
  158. if cp.Pricing == nil {
  159. m := make(map[string]*NodePrice)
  160. cp.Pricing = m
  161. }
  162. p, err := cp.Config.GetCustomPricingData()
  163. if err != nil {
  164. return err
  165. }
  166. cp.SpotLabel = p.SpotLabel
  167. cp.SpotLabelValue = p.SpotLabelValue
  168. cp.GPULabel = p.GpuLabel
  169. cp.GPULabelValue = p.GpuLabelValue
  170. cp.Pricing["default"] = &NodePrice{
  171. CPU: p.CPU,
  172. RAM: p.RAM,
  173. }
  174. cp.Pricing["default,spot"] = &NodePrice{
  175. CPU: p.SpotCPU,
  176. RAM: p.SpotRAM,
  177. }
  178. cp.Pricing["default,gpu"] = &NodePrice{
  179. CPU: p.CPU,
  180. RAM: p.RAM,
  181. GPU: p.GPU,
  182. }
  183. return nil
  184. }
  185. func (cp *CustomProvider) GetKey(labels map[string]string, n *v1.Node) models.Key {
  186. return &customProviderKey{
  187. SpotLabel: cp.SpotLabel,
  188. SpotLabelValue: cp.SpotLabelValue,
  189. GPULabel: cp.GPULabel,
  190. GPULabelValue: cp.GPULabelValue,
  191. Labels: labels,
  192. }
  193. }
  194. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  195. // "start" and "end" are dates of the format YYYY-MM-DD
  196. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  197. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*models.OutOfClusterAllocation, error) {
  198. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  199. }
  200. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  201. return nil, nil
  202. }
  203. func (cp *CustomProvider) PVPricing(pvk models.PVKey) (*models.PV, error) {
  204. cpricing, err := cp.Config.GetCustomPricingData()
  205. if err != nil {
  206. return nil, err
  207. }
  208. return &models.PV{
  209. Cost: cpricing.Storage,
  210. }, nil
  211. }
  212. func (cp *CustomProvider) NetworkPricing() (*models.Network, error) {
  213. cpricing, err := cp.Config.GetCustomPricingData()
  214. if err != nil {
  215. return nil, err
  216. }
  217. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  218. if err != nil {
  219. return nil, err
  220. }
  221. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  222. if err != nil {
  223. return nil, err
  224. }
  225. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  226. if err != nil {
  227. return nil, err
  228. }
  229. return &models.Network{
  230. ZoneNetworkEgressCost: znec,
  231. RegionNetworkEgressCost: rnec,
  232. InternetNetworkEgressCost: inec,
  233. }, nil
  234. }
  235. func (cp *CustomProvider) LoadBalancerPricing() (*models.LoadBalancer, error) {
  236. cpricing, err := cp.Config.GetCustomPricingData()
  237. if err != nil {
  238. return nil, err
  239. }
  240. fffrc, err := strconv.ParseFloat(cpricing.FirstFiveForwardingRulesCost, 64)
  241. if err != nil {
  242. return nil, err
  243. }
  244. afrc, err := strconv.ParseFloat(cpricing.AdditionalForwardingRuleCost, 64)
  245. if err != nil {
  246. return nil, err
  247. }
  248. lbidc, err := strconv.ParseFloat(cpricing.LBIngressDataCost, 64)
  249. if err != nil {
  250. return nil, err
  251. }
  252. var totalCost float64
  253. numForwardingRules := 1.0 // hard-code at 1 for now
  254. dataIngressGB := 0.0 // hard-code at 0 for now
  255. if numForwardingRules < 5 {
  256. totalCost = fffrc*numForwardingRules + lbidc*dataIngressGB
  257. } else {
  258. totalCost = fffrc*5 + afrc*(numForwardingRules-5) + lbidc*dataIngressGB
  259. }
  260. return &models.LoadBalancer{
  261. Cost: totalCost,
  262. }, nil
  263. }
  264. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
  265. return &awsPVKey{
  266. Labels: pv.Labels,
  267. StorageClassName: pv.Spec.StorageClassName,
  268. StorageClassParameters: parameters,
  269. DefaultRegion: defaultRegion,
  270. }
  271. }
  272. func (k *customProviderKey) GPUCount() int {
  273. return 0
  274. }
  275. func (cpk *customProviderKey) GPUType() string {
  276. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  277. return t
  278. }
  279. return ""
  280. }
  281. func (cpk *customProviderKey) ID() string {
  282. return ""
  283. }
  284. func (cpk *customProviderKey) Features() string {
  285. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  286. return "default,spot"
  287. }
  288. return "default" // TODO: multiple custom pricing support.
  289. }
  290. func (cp *CustomProvider) ServiceAccountStatus() *models.ServiceAccountStatus {
  291. return &models.ServiceAccountStatus{
  292. Checks: []*models.ServiceAccountCheck{},
  293. }
  294. }
  295. func (cp *CustomProvider) PricingSourceStatus() map[string]*models.PricingSource {
  296. return make(map[string]*models.PricingSource)
  297. }
  298. func (cp *CustomProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
  299. return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
  300. }
  301. func (cp *CustomProvider) Regions() []string {
  302. return []string{}
  303. }