customprovider.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. v1 "k8s.io/api/core/v1"
  10. )
  11. type NodePrice struct {
  12. CPU string
  13. RAM string
  14. GPU string
  15. }
  16. type CustomProvider struct {
  17. Clientset clustercache.ClusterCache
  18. Pricing map[string]*NodePrice
  19. SpotLabel string
  20. SpotLabelValue string
  21. GPULabel string
  22. GPULabelValue string
  23. DownloadPricingDataLock sync.RWMutex
  24. Config *ProviderConfig
  25. }
  26. type customProviderKey struct {
  27. SpotLabel string
  28. SpotLabelValue string
  29. GPULabel string
  30. GPULabelValue string
  31. Labels map[string]string
  32. }
  33. func (*CustomProvider) GetLocalStorageQuery(window, offset string, rate bool, used bool) string {
  34. return ""
  35. }
  36. func (cp *CustomProvider) GetConfig() (*CustomPricing, error) {
  37. return cp.Config.GetCustomPricingData()
  38. }
  39. func (*CustomProvider) GetManagementPlatform() (string, error) {
  40. return "", nil
  41. }
  42. func (*CustomProvider) ApplyReservedInstancePricing(nodes map[string]*Node) {
  43. }
  44. func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  45. return cp.Config.UpdateFromMap(a)
  46. }
  47. func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  48. // Parse config updates from reader
  49. a := make(map[string]interface{})
  50. err := json.NewDecoder(r).Decode(&a)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // Update Config
  55. c, err := cp.Config.Update(func(c *CustomPricing) error {
  56. for k, v := range a {
  57. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  58. vstr, ok := v.(string)
  59. if ok {
  60. err := SetCustomPricingField(c, kUpper, vstr)
  61. if err != nil {
  62. return err
  63. }
  64. } else {
  65. sci := v.(map[string]interface{})
  66. sc := make(map[string]string)
  67. for k, val := range sci {
  68. sc[k] = val.(string)
  69. }
  70. c.SharedCosts = sc //todo: support reflection/multiple map fields
  71. }
  72. }
  73. return nil
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer cp.DownloadPricingData()
  79. return c, nil
  80. }
  81. func (cp *CustomProvider) ClusterInfo() (map[string]string, error) {
  82. conf, err := cp.GetConfig()
  83. if err != nil {
  84. return nil, err
  85. }
  86. m := make(map[string]string)
  87. if conf.ClusterName != "" {
  88. m["name"] = conf.ClusterName
  89. }
  90. m["provider"] = "custom"
  91. return m, nil
  92. }
  93. func (*CustomProvider) GetDisks() ([]byte, error) {
  94. return nil, nil
  95. }
  96. func (cp *CustomProvider) AllNodePricing() (interface{}, error) {
  97. cp.DownloadPricingDataLock.RLock()
  98. defer cp.DownloadPricingDataLock.RUnlock()
  99. return cp.Pricing, nil
  100. }
  101. func (cp *CustomProvider) NodePricing(key Key) (*Node, error) {
  102. cp.DownloadPricingDataLock.RLock()
  103. defer cp.DownloadPricingDataLock.RUnlock()
  104. k := key.Features()
  105. var gpuCount string
  106. if _, ok := cp.Pricing[k]; !ok {
  107. k = "default"
  108. }
  109. if key.GPUType() != "" {
  110. k += ",gpu" // TODO: support multiple custom gpu types.
  111. gpuCount = "1" // TODO: support more than one gpu.
  112. }
  113. return &Node{
  114. VCPUCost: cp.Pricing[k].CPU,
  115. RAMCost: cp.Pricing[k].RAM,
  116. GPUCost: cp.Pricing[k].GPU,
  117. GPU: gpuCount,
  118. }, nil
  119. }
  120. func (cp *CustomProvider) DownloadPricingData() error {
  121. cp.DownloadPricingDataLock.Lock()
  122. defer cp.DownloadPricingDataLock.Unlock()
  123. if cp.Pricing == nil {
  124. m := make(map[string]*NodePrice)
  125. cp.Pricing = m
  126. }
  127. p, err := cp.Config.GetCustomPricingData()
  128. if err != nil {
  129. return err
  130. }
  131. cp.SpotLabel = p.SpotLabel
  132. cp.SpotLabelValue = p.SpotLabelValue
  133. cp.GPULabel = p.GpuLabel
  134. cp.GPULabelValue = p.GpuLabelValue
  135. cp.Pricing["default"] = &NodePrice{
  136. CPU: p.CPU,
  137. RAM: p.RAM,
  138. }
  139. cp.Pricing["default,spot"] = &NodePrice{
  140. CPU: p.SpotCPU,
  141. RAM: p.SpotRAM,
  142. }
  143. cp.Pricing["default,gpu"] = &NodePrice{
  144. CPU: p.CPU,
  145. RAM: p.RAM,
  146. GPU: p.GPU,
  147. }
  148. return nil
  149. }
  150. func (cp *CustomProvider) GetKey(labels map[string]string) Key {
  151. return &customProviderKey{
  152. SpotLabel: cp.SpotLabel,
  153. SpotLabelValue: cp.SpotLabelValue,
  154. GPULabel: cp.GPULabel,
  155. GPULabelValue: cp.GPULabelValue,
  156. Labels: labels,
  157. }
  158. }
  159. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  160. // "start" and "end" are dates of the format YYYY-MM-DD
  161. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  162. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  163. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  164. }
  165. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  166. return nil, nil
  167. }
  168. func (cp *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  169. cpricing, err := cp.Config.GetCustomPricingData()
  170. if err != nil {
  171. return nil, err
  172. }
  173. return &PV{
  174. Cost: cpricing.Storage,
  175. }, nil
  176. }
  177. func (cp *CustomProvider) NetworkPricing() (*Network, error) {
  178. cpricing, err := cp.Config.GetCustomPricingData()
  179. if err != nil {
  180. return nil, err
  181. }
  182. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  183. if err != nil {
  184. return nil, err
  185. }
  186. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  187. if err != nil {
  188. return nil, err
  189. }
  190. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return &Network{
  195. ZoneNetworkEgressCost: znec,
  196. RegionNetworkEgressCost: rnec,
  197. InternetNetworkEgressCost: inec,
  198. }, nil
  199. }
  200. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  201. return &awsPVKey{
  202. Labels: pv.Labels,
  203. StorageClassName: pv.Spec.StorageClassName,
  204. StorageClassParameters: parameters,
  205. DefaultRegion: defaultRegion,
  206. }
  207. }
  208. func (cpk *customProviderKey) GPUType() string {
  209. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  210. return t
  211. }
  212. return ""
  213. }
  214. func (cpk *customProviderKey) ID() string {
  215. return ""
  216. }
  217. func (cpk *customProviderKey) Features() string {
  218. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  219. return "default,spot"
  220. }
  221. return "default" // TODO: multiple custom pricing support.
  222. }