customprovider.go 6.4 KB

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