provider.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package cloud
  2. import (
  3. "encoding/json"
  4. "fmt"
  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. if apiKey == "" {
  131. return nil, fmt.Errorf("Supply a GCP Key to start getting data")
  132. }
  133. return &GCP{
  134. Clientset: clientset,
  135. APIKey: apiKey,
  136. }, nil
  137. }
  138. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  139. if err != nil {
  140. return nil, err
  141. }
  142. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  143. if strings.HasPrefix(provider, "aws") {
  144. return &AWS{
  145. Clientset: clientset,
  146. }, nil
  147. } else if strings.HasPrefix(provider, "azure") {
  148. return &Azure{
  149. CustomProvider: &CustomProvider{
  150. Clientset: clientset,
  151. },
  152. }, nil
  153. } else {
  154. log.Printf("Unsupported provider, falling back to default")
  155. return &CustomProvider{
  156. Clientset: clientset,
  157. }, nil
  158. }
  159. }