customprovider.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package cloud
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/opencost/opencost/pkg/kubecost"
  6. "io"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/opencost/opencost/pkg/clustercache"
  12. "github.com/opencost/opencost/pkg/env"
  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 := strings.Title(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. k = "default"
  118. }
  119. if key.GPUType() != "" {
  120. k += ",gpu" // TODO: support multiple custom gpu types.
  121. gpuCount = "1" // TODO: support more than one gpu.
  122. }
  123. return &Node{
  124. VCPUCost: cp.Pricing[k].CPU,
  125. RAMCost: cp.Pricing[k].RAM,
  126. GPUCost: cp.Pricing[k].GPU,
  127. GPU: gpuCount,
  128. }, nil
  129. }
  130. func (cp *CustomProvider) DownloadPricingData() error {
  131. cp.DownloadPricingDataLock.Lock()
  132. defer cp.DownloadPricingDataLock.Unlock()
  133. if cp.Pricing == nil {
  134. m := make(map[string]*NodePrice)
  135. cp.Pricing = m
  136. }
  137. p, err := cp.Config.GetCustomPricingData()
  138. if err != nil {
  139. return err
  140. }
  141. cp.SpotLabel = p.SpotLabel
  142. cp.SpotLabelValue = p.SpotLabelValue
  143. cp.GPULabel = p.GpuLabel
  144. cp.GPULabelValue = p.GpuLabelValue
  145. cp.Pricing["default"] = &NodePrice{
  146. CPU: p.CPU,
  147. RAM: p.RAM,
  148. }
  149. cp.Pricing["default,spot"] = &NodePrice{
  150. CPU: p.SpotCPU,
  151. RAM: p.SpotRAM,
  152. }
  153. cp.Pricing["default,gpu"] = &NodePrice{
  154. CPU: p.CPU,
  155. RAM: p.RAM,
  156. GPU: p.GPU,
  157. }
  158. return nil
  159. }
  160. func (cp *CustomProvider) GetKey(labels map[string]string, n *v1.Node) Key {
  161. return &customProviderKey{
  162. SpotLabel: cp.SpotLabel,
  163. SpotLabelValue: cp.SpotLabelValue,
  164. GPULabel: cp.GPULabel,
  165. GPULabelValue: cp.GPULabelValue,
  166. Labels: labels,
  167. }
  168. }
  169. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  170. // "start" and "end" are dates of the format YYYY-MM-DD
  171. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  172. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  173. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  174. }
  175. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  176. return nil, nil
  177. }
  178. func (cp *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  179. cpricing, err := cp.Config.GetCustomPricingData()
  180. if err != nil {
  181. return nil, err
  182. }
  183. return &PV{
  184. Cost: cpricing.Storage,
  185. }, nil
  186. }
  187. func (cp *CustomProvider) NetworkPricing() (*Network, error) {
  188. cpricing, err := cp.Config.GetCustomPricingData()
  189. if err != nil {
  190. return nil, err
  191. }
  192. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  193. if err != nil {
  194. return nil, err
  195. }
  196. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  197. if err != nil {
  198. return nil, err
  199. }
  200. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return &Network{
  205. ZoneNetworkEgressCost: znec,
  206. RegionNetworkEgressCost: rnec,
  207. InternetNetworkEgressCost: inec,
  208. }, nil
  209. }
  210. func (cp *CustomProvider) LoadBalancerPricing() (*LoadBalancer, error) {
  211. cpricing, err := cp.Config.GetCustomPricingData()
  212. if err != nil {
  213. return nil, err
  214. }
  215. fffrc, err := strconv.ParseFloat(cpricing.FirstFiveForwardingRulesCost, 64)
  216. if err != nil {
  217. return nil, err
  218. }
  219. afrc, err := strconv.ParseFloat(cpricing.AdditionalForwardingRuleCost, 64)
  220. if err != nil {
  221. return nil, err
  222. }
  223. lbidc, err := strconv.ParseFloat(cpricing.LBIngressDataCost, 64)
  224. if err != nil {
  225. return nil, err
  226. }
  227. var totalCost float64
  228. numForwardingRules := 1.0 // hard-code at 1 for now
  229. dataIngressGB := 0.0 // hard-code at 0 for now
  230. if numForwardingRules < 5 {
  231. totalCost = fffrc*numForwardingRules + lbidc*dataIngressGB
  232. } else {
  233. totalCost = fffrc*5 + afrc*(numForwardingRules-5) + lbidc*dataIngressGB
  234. }
  235. return &LoadBalancer{
  236. Cost: totalCost,
  237. }, nil
  238. }
  239. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  240. return &awsPVKey{
  241. Labels: pv.Labels,
  242. StorageClassName: pv.Spec.StorageClassName,
  243. StorageClassParameters: parameters,
  244. DefaultRegion: defaultRegion,
  245. }
  246. }
  247. func (k *customProviderKey) GPUCount() int {
  248. return 0
  249. }
  250. func (cpk *customProviderKey) GPUType() string {
  251. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  252. return t
  253. }
  254. return ""
  255. }
  256. func (cpk *customProviderKey) ID() string {
  257. return ""
  258. }
  259. func (cpk *customProviderKey) Features() string {
  260. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  261. return "default,spot"
  262. }
  263. return "default" // TODO: multiple custom pricing support.
  264. }
  265. func (cp *CustomProvider) ServiceAccountStatus() *ServiceAccountStatus {
  266. return &ServiceAccountStatus{
  267. Checks: []*ServiceAccountCheck{},
  268. }
  269. }
  270. func (cp *CustomProvider) PricingSourceStatus() map[string]*PricingSource {
  271. return make(map[string]*PricingSource)
  272. }
  273. func (cp *CustomProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
  274. return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
  275. }
  276. func (cp *CustomProvider) Regions() []string {
  277. return []string{}
  278. }