provider.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package cloud
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "cloud.google.com/go/compute/metadata"
  11. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  12. "k8s.io/client-go/kubernetes"
  13. )
  14. // Node is the interface by which the provider and cost model communicate.
  15. // The provider will best-effort try to fill out this struct.
  16. type Node struct {
  17. Cost string `json:"hourlyCost"`
  18. VCPU string `json:"CPU"`
  19. VCPUCost string `json:"CPUHourlyCost"`
  20. RAM string `json:"RAM"`
  21. RAMBytes string `json:"RAMBytes"`
  22. RAMCost string `json:"RAMGBHourlyCost"`
  23. Storage string `json:"storage"`
  24. StorageCost string `json:"storageHourlyCost"`
  25. UsesBaseCPUPrice bool `json:"usesDefaultPrice"`
  26. BaseCPUPrice string `json:"baseCPUPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  27. UsageType string `json:"usageType"`
  28. }
  29. // Provider represents a k8s provider.
  30. type Provider interface {
  31. ClusterName() ([]byte, error)
  32. AddServiceKey(url.Values) error
  33. GetDisks() ([]byte, error)
  34. NodePricing(string) (*Node, error)
  35. AllNodePricing() (interface{}, error)
  36. DownloadPricingData() error
  37. GetKey(map[string]string) string
  38. QuerySQL(string) ([]byte, error)
  39. }
  40. // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
  41. func GetDefaultPricingData(fname string) (*CustomPricing, error) {
  42. jsonFile, err := os.Open("/models/" + fname)
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer jsonFile.Close()
  47. byteValue, err := ioutil.ReadAll(jsonFile)
  48. if err != nil {
  49. return nil, err
  50. }
  51. var customPricing = &CustomPricing{}
  52. err = json.Unmarshal([]byte(byteValue), customPricing)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return customPricing, nil
  57. }
  58. type CustomPricing struct {
  59. Provider string `json:"provider"`
  60. Description string `json:"description"`
  61. CPU string `json:"CPU"`
  62. SpotCPU string `json:"spotCPU"`
  63. RAM string `json:"RAM"`
  64. SpotRAM string `json:"spotRAM"`
  65. SpotLabel string `json:"spotLabel,omitempty"`
  66. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  67. }
  68. type NodePrice struct {
  69. CPU string
  70. RAM string
  71. }
  72. type CustomProvider struct {
  73. Clientset *kubernetes.Clientset
  74. Pricing map[string]*NodePrice
  75. SpotLabel string
  76. SpotLabelValue string
  77. }
  78. func (*CustomProvider) ClusterName() ([]byte, error) {
  79. return nil, nil
  80. }
  81. func (*CustomProvider) AddServiceKey(url.Values) error {
  82. return nil
  83. }
  84. func (*CustomProvider) GetDisks() ([]byte, error) {
  85. return nil, nil
  86. }
  87. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  88. return c.Pricing, nil
  89. }
  90. func (c *CustomProvider) NodePricing(key string) (*Node, error) {
  91. if _, ok := c.Pricing[key]; !ok {
  92. key = "default"
  93. }
  94. return &Node{
  95. VCPUCost: c.Pricing[key].CPU,
  96. RAMCost: c.Pricing[key].RAM,
  97. }, nil
  98. }
  99. func (c *CustomProvider) DownloadPricingData() error {
  100. if c.Pricing == nil {
  101. m := make(map[string]*NodePrice)
  102. c.Pricing = m
  103. }
  104. p, err := GetDefaultPricingData("default.json")
  105. if err != nil {
  106. return err
  107. }
  108. c.Pricing["default"] = &NodePrice{
  109. CPU: p.CPU,
  110. RAM: p.RAM,
  111. }
  112. c.Pricing["default,spot"] = &NodePrice{
  113. CPU: p.SpotCPU,
  114. RAM: p.SpotRAM,
  115. }
  116. return nil
  117. }
  118. func (c *CustomProvider) GetKey(labels map[string]string) string {
  119. if labels[c.SpotLabel] != "" && labels[c.SpotLabel] == c.SpotLabelValue {
  120. return "default,spot"
  121. }
  122. return "default" // TODO: multiple custom pricing support.
  123. }
  124. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  125. return nil, nil
  126. }
  127. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  128. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  129. if metadata.OnGCE() {
  130. log.Print("metadata reports we are in GCE")
  131. if apiKey == "" {
  132. return nil, errors.New("Supply a GCP Key to start getting data")
  133. }
  134. return &GCP{
  135. Clientset: clientset,
  136. APIKey: apiKey,
  137. }, nil
  138. }
  139. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  140. if err != nil {
  141. return nil, err
  142. }
  143. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  144. if strings.HasPrefix(provider, "aws") {
  145. log.Print("Found ProviderID starting with \"aws\", using AWS Provider")
  146. return &AWS{
  147. Clientset: clientset,
  148. }, nil
  149. } else if strings.HasPrefix(provider, "azure") {
  150. log.Print("Found ProviderID starting with \"azure\", using Azure Provider")
  151. return &Azure{
  152. CustomProvider: &CustomProvider{
  153. Clientset: clientset,
  154. },
  155. }, nil
  156. } else {
  157. log.Print("Unsupported provider, falling back to default")
  158. return &CustomProvider{
  159. Clientset: clientset,
  160. }, nil
  161. }
  162. }