customprovider.go 8.4 KB

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