customprovider.go 5.7 KB

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