customprovider.go 5.8 KB

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