customprovider.go 6.0 KB

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