customprovider.go 8.0 KB

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