customprovider.go 8.5 KB

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