customprovider.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) GetAddresses() ([]byte, error) {
  94. return nil, nil
  95. }
  96. func (*CustomProvider) GetDisks() ([]byte, error) {
  97. return nil, nil
  98. }
  99. func (cp *CustomProvider) AllNodePricing() (interface{}, error) {
  100. cp.DownloadPricingDataLock.RLock()
  101. defer cp.DownloadPricingDataLock.RUnlock()
  102. return cp.Pricing, nil
  103. }
  104. func (cp *CustomProvider) NodePricing(key Key) (*Node, error) {
  105. cp.DownloadPricingDataLock.RLock()
  106. defer cp.DownloadPricingDataLock.RUnlock()
  107. k := key.Features()
  108. var gpuCount string
  109. if _, ok := cp.Pricing[k]; !ok {
  110. k = "default"
  111. }
  112. if key.GPUType() != "" {
  113. k += ",gpu" // TODO: support multiple custom gpu types.
  114. gpuCount = "1" // TODO: support more than one gpu.
  115. }
  116. return &Node{
  117. VCPUCost: cp.Pricing[k].CPU,
  118. RAMCost: cp.Pricing[k].RAM,
  119. GPUCost: cp.Pricing[k].GPU,
  120. GPU: gpuCount,
  121. }, nil
  122. }
  123. func (cp *CustomProvider) DownloadPricingData() error {
  124. cp.DownloadPricingDataLock.Lock()
  125. defer cp.DownloadPricingDataLock.Unlock()
  126. if cp.Pricing == nil {
  127. m := make(map[string]*NodePrice)
  128. cp.Pricing = m
  129. }
  130. p, err := cp.Config.GetCustomPricingData()
  131. if err != nil {
  132. return err
  133. }
  134. cp.SpotLabel = p.SpotLabel
  135. cp.SpotLabelValue = p.SpotLabelValue
  136. cp.GPULabel = p.GpuLabel
  137. cp.GPULabelValue = p.GpuLabelValue
  138. cp.Pricing["default"] = &NodePrice{
  139. CPU: p.CPU,
  140. RAM: p.RAM,
  141. }
  142. cp.Pricing["default,spot"] = &NodePrice{
  143. CPU: p.SpotCPU,
  144. RAM: p.SpotRAM,
  145. }
  146. cp.Pricing["default,gpu"] = &NodePrice{
  147. CPU: p.CPU,
  148. RAM: p.RAM,
  149. GPU: p.GPU,
  150. }
  151. return nil
  152. }
  153. func (cp *CustomProvider) GetKey(labels map[string]string, n *v1.Node) Key {
  154. return &customProviderKey{
  155. SpotLabel: cp.SpotLabel,
  156. SpotLabelValue: cp.SpotLabelValue,
  157. GPULabel: cp.GPULabel,
  158. GPULabelValue: cp.GPULabelValue,
  159. Labels: labels,
  160. }
  161. }
  162. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  163. // "start" and "end" are dates of the format YYYY-MM-DD
  164. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  165. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  166. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  167. }
  168. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  169. return nil, nil
  170. }
  171. func (cp *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  172. cpricing, err := cp.Config.GetCustomPricingData()
  173. if err != nil {
  174. return nil, err
  175. }
  176. return &PV{
  177. Cost: cpricing.Storage,
  178. }, nil
  179. }
  180. func (cp *CustomProvider) NetworkPricing() (*Network, error) {
  181. cpricing, err := cp.Config.GetCustomPricingData()
  182. if err != nil {
  183. return nil, err
  184. }
  185. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  186. if err != nil {
  187. return nil, err
  188. }
  189. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  190. if err != nil {
  191. return nil, err
  192. }
  193. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  194. if err != nil {
  195. return nil, err
  196. }
  197. return &Network{
  198. ZoneNetworkEgressCost: znec,
  199. RegionNetworkEgressCost: rnec,
  200. InternetNetworkEgressCost: inec,
  201. }, nil
  202. }
  203. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  204. return &awsPVKey{
  205. Labels: pv.Labels,
  206. StorageClassName: pv.Spec.StorageClassName,
  207. StorageClassParameters: parameters,
  208. DefaultRegion: defaultRegion,
  209. }
  210. }
  211. func (cpk *customProviderKey) GPUType() string {
  212. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  213. return t
  214. }
  215. return ""
  216. }
  217. func (cpk *customProviderKey) ID() string {
  218. return ""
  219. }
  220. func (cpk *customProviderKey) Features() string {
  221. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  222. return "default,spot"
  223. }
  224. return "default" // TODO: multiple custom pricing support.
  225. }