customprovider.go 6.2 KB

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