customprovider.go 8.6 KB

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