customprovider.go 7.9 KB

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