customprovider.go 6.3 KB

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