customprovider.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package cloud
  2. import (
  3. "io"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/kubecost/cost-model/pkg/clustercache"
  9. "github.com/kubecost/cost-model/pkg/env"
  10. "github.com/kubecost/cost-model/pkg/util/json"
  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. Config *ProviderConfig
  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) ClusterManagementPricing() (string, float64, error) {
  36. return "", 0.0, nil
  37. }
  38. func (*CustomProvider) GetLocalStorageQuery(window, offset time.Duration, rate bool, used bool) string {
  39. return ""
  40. }
  41. func (cp *CustomProvider) GetConfig() (*CustomPricing, error) {
  42. return cp.Config.GetCustomPricingData()
  43. }
  44. func (*CustomProvider) GetManagementPlatform() (string, error) {
  45. return "", nil
  46. }
  47. func (*CustomProvider) ApplyReservedInstancePricing(nodes map[string]*Node) {
  48. }
  49. func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  50. return cp.Config.UpdateFromMap(a)
  51. }
  52. func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  53. // Parse config updates from reader
  54. a := make(map[string]interface{})
  55. err := json.NewDecoder(r).Decode(&a)
  56. if err != nil {
  57. return nil, err
  58. }
  59. // Update Config
  60. c, err := cp.Config.Update(func(c *CustomPricing) error {
  61. for k, v := range a {
  62. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  63. vstr, ok := v.(string)
  64. if ok {
  65. err := SetCustomPricingField(c, kUpper, vstr)
  66. if err != nil {
  67. return err
  68. }
  69. } else {
  70. sci := v.(map[string]interface{})
  71. sc := make(map[string]string)
  72. for k, val := range sci {
  73. sc[k] = val.(string)
  74. }
  75. c.SharedCosts = sc //todo: support reflection/multiple map fields
  76. }
  77. }
  78. return nil
  79. })
  80. if err != nil {
  81. return nil, err
  82. }
  83. defer cp.DownloadPricingData()
  84. return c, nil
  85. }
  86. func (cp *CustomProvider) ClusterInfo() (map[string]string, error) {
  87. conf, err := cp.GetConfig()
  88. if err != nil {
  89. return nil, err
  90. }
  91. m := make(map[string]string)
  92. if conf.ClusterName != "" {
  93. m["name"] = conf.ClusterName
  94. }
  95. m["provider"] = "custom"
  96. m["id"] = env.GetClusterID()
  97. return m, nil
  98. }
  99. func (*CustomProvider) GetAddresses() ([]byte, error) {
  100. return nil, nil
  101. }
  102. func (*CustomProvider) GetDisks() ([]byte, error) {
  103. return nil, nil
  104. }
  105. func (cp *CustomProvider) AllNodePricing() (interface{}, error) {
  106. cp.DownloadPricingDataLock.RLock()
  107. defer cp.DownloadPricingDataLock.RUnlock()
  108. return cp.Pricing, nil
  109. }
  110. func (cp *CustomProvider) NodePricing(key Key) (*Node, error) {
  111. cp.DownloadPricingDataLock.RLock()
  112. defer cp.DownloadPricingDataLock.RUnlock()
  113. k := key.Features()
  114. var gpuCount string
  115. if _, ok := cp.Pricing[k]; !ok {
  116. k = "default"
  117. }
  118. if key.GPUType() != "" {
  119. k += ",gpu" // TODO: support multiple custom gpu types.
  120. gpuCount = "1" // TODO: support more than one gpu.
  121. }
  122. return &Node{
  123. VCPUCost: cp.Pricing[k].CPU,
  124. RAMCost: cp.Pricing[k].RAM,
  125. GPUCost: cp.Pricing[k].GPU,
  126. GPU: gpuCount,
  127. }, nil
  128. }
  129. func (cp *CustomProvider) DownloadPricingData() error {
  130. cp.DownloadPricingDataLock.Lock()
  131. defer cp.DownloadPricingDataLock.Unlock()
  132. if cp.Pricing == nil {
  133. m := make(map[string]*NodePrice)
  134. cp.Pricing = m
  135. }
  136. p, err := cp.Config.GetCustomPricingData()
  137. if err != nil {
  138. return err
  139. }
  140. cp.SpotLabel = p.SpotLabel
  141. cp.SpotLabelValue = p.SpotLabelValue
  142. cp.GPULabel = p.GpuLabel
  143. cp.GPULabelValue = p.GpuLabelValue
  144. cp.Pricing["default"] = &NodePrice{
  145. CPU: p.CPU,
  146. RAM: p.RAM,
  147. }
  148. cp.Pricing["default,spot"] = &NodePrice{
  149. CPU: p.SpotCPU,
  150. RAM: p.SpotRAM,
  151. }
  152. cp.Pricing["default,gpu"] = &NodePrice{
  153. CPU: p.CPU,
  154. RAM: p.RAM,
  155. GPU: p.GPU,
  156. }
  157. return nil
  158. }
  159. func (cp *CustomProvider) GetKey(labels map[string]string, n *v1.Node) Key {
  160. return &customProviderKey{
  161. SpotLabel: cp.SpotLabel,
  162. SpotLabelValue: cp.SpotLabelValue,
  163. GPULabel: cp.GPULabel,
  164. GPULabelValue: cp.GPULabelValue,
  165. Labels: labels,
  166. }
  167. }
  168. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  169. // "start" and "end" are dates of the format YYYY-MM-DD
  170. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  171. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  172. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  173. }
  174. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  175. return nil, nil
  176. }
  177. func (cp *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  178. cpricing, err := cp.Config.GetCustomPricingData()
  179. if err != nil {
  180. return nil, err
  181. }
  182. return &PV{
  183. Cost: cpricing.Storage,
  184. }, nil
  185. }
  186. func (cp *CustomProvider) NetworkPricing() (*Network, error) {
  187. cpricing, err := cp.Config.GetCustomPricingData()
  188. if err != nil {
  189. return nil, err
  190. }
  191. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  192. if err != nil {
  193. return nil, err
  194. }
  195. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  196. if err != nil {
  197. return nil, err
  198. }
  199. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return &Network{
  204. ZoneNetworkEgressCost: znec,
  205. RegionNetworkEgressCost: rnec,
  206. InternetNetworkEgressCost: inec,
  207. }, nil
  208. }
  209. func (cp *CustomProvider) LoadBalancerPricing() (*LoadBalancer, error) {
  210. cpricing, err := cp.Config.GetCustomPricingData()
  211. if err != nil {
  212. return nil, err
  213. }
  214. fffrc, err := strconv.ParseFloat(cpricing.FirstFiveForwardingRulesCost, 64)
  215. if err != nil {
  216. return nil, err
  217. }
  218. afrc, err := strconv.ParseFloat(cpricing.AdditionalForwardingRuleCost, 64)
  219. if err != nil {
  220. return nil, err
  221. }
  222. lbidc, err := strconv.ParseFloat(cpricing.LBIngressDataCost, 64)
  223. if err != nil {
  224. return nil, err
  225. }
  226. var totalCost float64
  227. numForwardingRules := 1.0 // hard-code at 1 for now
  228. dataIngressGB := 0.0 // hard-code at 0 for now
  229. if numForwardingRules < 5 {
  230. totalCost = fffrc*numForwardingRules + lbidc*dataIngressGB
  231. } else {
  232. totalCost = fffrc*5 + afrc*(numForwardingRules-5) + lbidc*dataIngressGB
  233. }
  234. return &LoadBalancer{
  235. Cost: totalCost,
  236. }, nil
  237. }
  238. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  239. return &awsPVKey{
  240. Labels: pv.Labels,
  241. StorageClassName: pv.Spec.StorageClassName,
  242. StorageClassParameters: parameters,
  243. DefaultRegion: defaultRegion,
  244. }
  245. }
  246. func (cpk *customProviderKey) GPUType() string {
  247. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  248. return t
  249. }
  250. return ""
  251. }
  252. func (cpk *customProviderKey) ID() string {
  253. return ""
  254. }
  255. func (cpk *customProviderKey) Features() string {
  256. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  257. return "default,spot"
  258. }
  259. return "default" // TODO: multiple custom pricing support.
  260. }
  261. func (cp *CustomProvider) ServiceAccountStatus() *ServiceAccountStatus {
  262. return &ServiceAccountStatus{
  263. Checks: []*ServiceAccountCheck{},
  264. }
  265. }
  266. func (cp *CustomProvider) PricingSourceStatus() map[string]*PricingSource {
  267. return make(map[string]*PricingSource)
  268. }
  269. func (cp *CustomProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
  270. return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
  271. }