customprovider.go 7.7 KB

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