customprovider.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package cloud
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "github.com/opencost/opencost/pkg/clustercache"
  10. "github.com/opencost/opencost/pkg/env"
  11. "github.com/opencost/opencost/pkg/kubecost"
  12. "github.com/opencost/opencost/pkg/log"
  13. "github.com/opencost/opencost/pkg/util/json"
  14. v1 "k8s.io/api/core/v1"
  15. )
  16. type NodePrice struct {
  17. CPU string
  18. RAM string
  19. GPU string
  20. }
  21. type CustomProvider struct {
  22. Clientset clustercache.ClusterCache
  23. Pricing map[string]*NodePrice
  24. SpotLabel string
  25. SpotLabelValue string
  26. GPULabel string
  27. GPULabelValue string
  28. clusterRegion string
  29. clusterAccountID string
  30. DownloadPricingDataLock sync.RWMutex
  31. Config *ProviderConfig
  32. }
  33. // PricingSourceSummary returns the pricing source summary for the provider.
  34. // The summary represents what was _parsed_ from the pricing source, not what
  35. // was returned from the relevant API.
  36. func (cp *CustomProvider) PricingSourceSummary() interface{} {
  37. return cp.Pricing
  38. }
  39. type customProviderKey struct {
  40. SpotLabel string
  41. SpotLabelValue string
  42. GPULabel string
  43. GPULabelValue string
  44. Labels map[string]string
  45. }
  46. func (*CustomProvider) ClusterManagementPricing() (string, float64, error) {
  47. return "", 0.0, nil
  48. }
  49. func (*CustomProvider) GetLocalStorageQuery(window, offset time.Duration, rate bool, used bool) string {
  50. return ""
  51. }
  52. func (cp *CustomProvider) GetConfig() (*CustomPricing, error) {
  53. return cp.Config.GetCustomPricingData()
  54. }
  55. func (*CustomProvider) GetManagementPlatform() (string, error) {
  56. return "", nil
  57. }
  58. func (*CustomProvider) ApplyReservedInstancePricing(nodes map[string]*Node) {
  59. }
  60. func (cp *CustomProvider) UpdateConfigFromConfigMap(a map[string]string) (*CustomPricing, error) {
  61. return cp.Config.UpdateFromMap(a)
  62. }
  63. func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  64. // Parse config updates from reader
  65. a := make(map[string]interface{})
  66. err := json.NewDecoder(r).Decode(&a)
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Update Config
  71. c, err := cp.Config.Update(func(c *CustomPricing) error {
  72. for k, v := range a {
  73. kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  74. vstr, ok := v.(string)
  75. if ok {
  76. err := SetCustomPricingField(c, kUpper, vstr)
  77. if err != nil {
  78. return err
  79. }
  80. } else {
  81. return fmt.Errorf("type error while updating config for %s", kUpper)
  82. }
  83. }
  84. return nil
  85. })
  86. if err != nil {
  87. return nil, err
  88. }
  89. defer cp.DownloadPricingData()
  90. return c, nil
  91. }
  92. func (cp *CustomProvider) ClusterInfo() (map[string]string, error) {
  93. conf, err := cp.GetConfig()
  94. if err != nil {
  95. return nil, err
  96. }
  97. m := make(map[string]string)
  98. if conf.ClusterName != "" {
  99. m["name"] = conf.ClusterName
  100. }
  101. m["provider"] = kubecost.CustomProvider
  102. m["region"] = cp.clusterRegion
  103. m["account"] = cp.clusterAccountID
  104. m["id"] = env.GetClusterID()
  105. return m, nil
  106. }
  107. func (*CustomProvider) GetAddresses() ([]byte, error) {
  108. return nil, nil
  109. }
  110. func (*CustomProvider) GetDisks() ([]byte, error) {
  111. return nil, nil
  112. }
  113. func (*CustomProvider) GetOrphanedResources() ([]OrphanedResource, error) {
  114. return nil, errors.New("not implemented")
  115. }
  116. func (cp *CustomProvider) AllNodePricing() (interface{}, error) {
  117. cp.DownloadPricingDataLock.RLock()
  118. defer cp.DownloadPricingDataLock.RUnlock()
  119. return cp.Pricing, nil
  120. }
  121. func (cp *CustomProvider) NodePricing(key Key) (*Node, error) {
  122. cp.DownloadPricingDataLock.RLock()
  123. defer cp.DownloadPricingDataLock.RUnlock()
  124. k := key.Features()
  125. var gpuCount string
  126. if _, ok := cp.Pricing[k]; !ok {
  127. // Default is saying that there is no pricing info for the cluster and we should fall back to the defualt values.
  128. // An interesting case is if the default values weren't loaded.
  129. k = "default"
  130. }
  131. if key.GPUType() != "" {
  132. k += ",gpu" // TODO: support multiple custom gpu types.
  133. gpuCount = "1" // TODO: support more than one gpu.
  134. }
  135. var cpuCost, ramCost, gpuCost string
  136. if pricing, ok := cp.Pricing[k]; !ok {
  137. log.Warnf("No pricing found for key=%s, setting values to 0", k)
  138. cpuCost = "0.0"
  139. ramCost = "0.0"
  140. gpuCost = "0.0"
  141. } else {
  142. cpuCost = pricing.CPU
  143. ramCost = pricing.RAM
  144. gpuCost = pricing.GPU
  145. }
  146. return &Node{
  147. VCPUCost: cpuCost,
  148. RAMCost: ramCost,
  149. GPUCost: gpuCost,
  150. GPU: gpuCount,
  151. }, nil
  152. }
  153. func (cp *CustomProvider) DownloadPricingData() error {
  154. cp.DownloadPricingDataLock.Lock()
  155. defer cp.DownloadPricingDataLock.Unlock()
  156. if cp.Pricing == nil {
  157. m := make(map[string]*NodePrice)
  158. cp.Pricing = m
  159. }
  160. p, err := cp.Config.GetCustomPricingData()
  161. if err != nil {
  162. return err
  163. }
  164. cp.SpotLabel = p.SpotLabel
  165. cp.SpotLabelValue = p.SpotLabelValue
  166. cp.GPULabel = p.GpuLabel
  167. cp.GPULabelValue = p.GpuLabelValue
  168. cp.Pricing["default"] = &NodePrice{
  169. CPU: p.CPU,
  170. RAM: p.RAM,
  171. }
  172. cp.Pricing["default,spot"] = &NodePrice{
  173. CPU: p.SpotCPU,
  174. RAM: p.SpotRAM,
  175. }
  176. cp.Pricing["default,gpu"] = &NodePrice{
  177. CPU: p.CPU,
  178. RAM: p.RAM,
  179. GPU: p.GPU,
  180. }
  181. return nil
  182. }
  183. func (cp *CustomProvider) GetKey(labels map[string]string, n *v1.Node) Key {
  184. return &customProviderKey{
  185. SpotLabel: cp.SpotLabel,
  186. SpotLabelValue: cp.SpotLabelValue,
  187. GPULabel: cp.GPULabel,
  188. GPULabelValue: cp.GPULabelValue,
  189. Labels: labels,
  190. }
  191. }
  192. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  193. // "start" and "end" are dates of the format YYYY-MM-DD
  194. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  195. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator []string, filterType string, filterValue string, crossCluster bool) ([]*OutOfClusterAllocation, error) {
  196. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  197. }
  198. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  199. return nil, nil
  200. }
  201. func (cp *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  202. cpricing, err := cp.Config.GetCustomPricingData()
  203. if err != nil {
  204. return nil, err
  205. }
  206. return &PV{
  207. Cost: cpricing.Storage,
  208. }, nil
  209. }
  210. func (cp *CustomProvider) NetworkPricing() (*Network, error) {
  211. cpricing, err := cp.Config.GetCustomPricingData()
  212. if err != nil {
  213. return nil, err
  214. }
  215. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  216. if err != nil {
  217. return nil, err
  218. }
  219. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  220. if err != nil {
  221. return nil, err
  222. }
  223. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return &Network{
  228. ZoneNetworkEgressCost: znec,
  229. RegionNetworkEgressCost: rnec,
  230. InternetNetworkEgressCost: inec,
  231. }, nil
  232. }
  233. func (cp *CustomProvider) LoadBalancerPricing() (*LoadBalancer, error) {
  234. cpricing, err := cp.Config.GetCustomPricingData()
  235. if err != nil {
  236. return nil, err
  237. }
  238. fffrc, err := strconv.ParseFloat(cpricing.FirstFiveForwardingRulesCost, 64)
  239. if err != nil {
  240. return nil, err
  241. }
  242. afrc, err := strconv.ParseFloat(cpricing.AdditionalForwardingRuleCost, 64)
  243. if err != nil {
  244. return nil, err
  245. }
  246. lbidc, err := strconv.ParseFloat(cpricing.LBIngressDataCost, 64)
  247. if err != nil {
  248. return nil, err
  249. }
  250. var totalCost float64
  251. numForwardingRules := 1.0 // hard-code at 1 for now
  252. dataIngressGB := 0.0 // hard-code at 0 for now
  253. if numForwardingRules < 5 {
  254. totalCost = fffrc*numForwardingRules + lbidc*dataIngressGB
  255. } else {
  256. totalCost = fffrc*5 + afrc*(numForwardingRules-5) + lbidc*dataIngressGB
  257. }
  258. return &LoadBalancer{
  259. Cost: totalCost,
  260. }, nil
  261. }
  262. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) PVKey {
  263. return &awsPVKey{
  264. Labels: pv.Labels,
  265. StorageClassName: pv.Spec.StorageClassName,
  266. StorageClassParameters: parameters,
  267. DefaultRegion: defaultRegion,
  268. }
  269. }
  270. func (k *customProviderKey) GPUCount() int {
  271. return 0
  272. }
  273. func (cpk *customProviderKey) GPUType() string {
  274. if t, ok := cpk.Labels[cpk.GPULabel]; ok {
  275. return t
  276. }
  277. return ""
  278. }
  279. func (cpk *customProviderKey) ID() string {
  280. return ""
  281. }
  282. func (cpk *customProviderKey) Features() string {
  283. if cpk.Labels[cpk.SpotLabel] != "" && cpk.Labels[cpk.SpotLabel] == cpk.SpotLabelValue {
  284. return "default,spot"
  285. }
  286. return "default" // TODO: multiple custom pricing support.
  287. }
  288. func (cp *CustomProvider) ServiceAccountStatus() *ServiceAccountStatus {
  289. return &ServiceAccountStatus{
  290. Checks: []*ServiceAccountCheck{},
  291. }
  292. }
  293. func (cp *CustomProvider) PricingSourceStatus() map[string]*PricingSource {
  294. return make(map[string]*PricingSource)
  295. }
  296. func (cp *CustomProvider) CombinedDiscountForNode(instanceType string, isPreemptible bool, defaultDiscount, negotiatedDiscount float64) float64 {
  297. return 1.0 - ((1.0 - defaultDiscount) * (1.0 - negotiatedDiscount))
  298. }
  299. func (cp *CustomProvider) Regions() []string {
  300. return []string{}
  301. }