customprovider.go 9.0 KB

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