customprovider.go 7.8 KB

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