provider.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package cloud
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/url"
  7. "os"
  8. "strings"
  9. "k8s.io/klog"
  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. // Key represents a way for nodes to match between the k8s API and a pricing API
  30. type Key interface {
  31. ID() string // ID represents an exact match
  32. Features() string // Features are a comma separated string of node metadata that could match pricing
  33. }
  34. // Provider represents a k8s provider.
  35. type Provider interface {
  36. ClusterName() ([]byte, error)
  37. AddServiceKey(url.Values) error
  38. GetDisks() ([]byte, error)
  39. NodePricing(Key) (*Node, error)
  40. AllNodePricing() (interface{}, error)
  41. DownloadPricingData() error
  42. GetKey(map[string]string) Key
  43. QuerySQL(string) ([]byte, error)
  44. }
  45. // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
  46. func GetDefaultPricingData(fname string) (*CustomPricing, error) {
  47. jsonFile, err := os.Open("/models/" + fname)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer jsonFile.Close()
  52. byteValue, err := ioutil.ReadAll(jsonFile)
  53. if err != nil {
  54. return nil, err
  55. }
  56. var customPricing = &CustomPricing{}
  57. err = json.Unmarshal([]byte(byteValue), customPricing)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return customPricing, nil
  62. }
  63. type CustomPricing struct {
  64. Provider string `json:"provider"`
  65. Description string `json:"description"`
  66. CPU string `json:"CPU"`
  67. SpotCPU string `json:"spotCPU"`
  68. RAM string `json:"RAM"`
  69. SpotRAM string `json:"spotRAM"`
  70. SpotLabel string `json:"spotLabel,omitempty"`
  71. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  72. ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
  73. ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
  74. SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
  75. SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
  76. SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
  77. ProjectID string `json:"awsProjectID,omitempty"`
  78. }
  79. type NodePrice struct {
  80. CPU string
  81. RAM string
  82. }
  83. type CustomProvider struct {
  84. Clientset *kubernetes.Clientset
  85. Pricing map[string]*NodePrice
  86. SpotLabel string
  87. SpotLabelValue string
  88. }
  89. func (*CustomProvider) ClusterName() ([]byte, error) {
  90. return nil, nil
  91. }
  92. func (*CustomProvider) AddServiceKey(url.Values) error {
  93. return nil
  94. }
  95. func (*CustomProvider) GetDisks() ([]byte, error) {
  96. return nil, nil
  97. }
  98. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  99. return c.Pricing, nil
  100. }
  101. func (c *CustomProvider) NodePricing(key Key) (*Node, error) {
  102. k := key.Features()
  103. if _, ok := c.Pricing[k]; !ok {
  104. k = "default"
  105. }
  106. return &Node{
  107. VCPUCost: c.Pricing[k].CPU,
  108. RAMCost: c.Pricing[k].RAM,
  109. }, nil
  110. }
  111. func (c *CustomProvider) DownloadPricingData() error {
  112. if c.Pricing == nil {
  113. m := make(map[string]*NodePrice)
  114. c.Pricing = m
  115. }
  116. p, err := GetDefaultPricingData("default.json")
  117. if err != nil {
  118. return err
  119. }
  120. c.Pricing["default"] = &NodePrice{
  121. CPU: p.CPU,
  122. RAM: p.RAM,
  123. }
  124. c.Pricing["default,spot"] = &NodePrice{
  125. CPU: p.SpotCPU,
  126. RAM: p.SpotRAM,
  127. }
  128. return nil
  129. }
  130. type customProviderKey struct {
  131. SpotLabel string
  132. SpotLabelValue string
  133. Labels map[string]string
  134. }
  135. func (c *customProviderKey) ID() string {
  136. return ""
  137. }
  138. func (c *customProviderKey) Features() string {
  139. if c.Labels[c.SpotLabel] != "" && c.Labels[c.SpotLabel] == c.SpotLabelValue {
  140. return "default,spot"
  141. }
  142. return "default" // TODO: multiple custom pricing support.
  143. }
  144. func (c *CustomProvider) GetKey(labels map[string]string) Key {
  145. return &customProviderKey{
  146. SpotLabel: c.SpotLabel,
  147. SpotLabelValue: c.SpotLabelValue,
  148. Labels: labels,
  149. }
  150. }
  151. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  152. return nil, nil
  153. }
  154. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  155. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  156. if metadata.OnGCE() {
  157. klog.V(3).Info("metadata reports we are in GCE")
  158. if apiKey == "" {
  159. return nil, errors.New("Supply a GCP Key to start getting data")
  160. }
  161. return &GCP{
  162. Clientset: clientset,
  163. APIKey: apiKey,
  164. }, nil
  165. }
  166. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  167. if err != nil {
  168. return nil, err
  169. }
  170. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  171. if strings.HasPrefix(provider, "aws") {
  172. klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
  173. return &AWS{
  174. Clientset: clientset,
  175. }, nil
  176. } else if strings.HasPrefix(provider, "azure") {
  177. klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
  178. return &Azure{
  179. CustomProvider: &CustomProvider{
  180. Clientset: clientset,
  181. },
  182. }, nil
  183. } else {
  184. klog.V(2).Info("Unsupported provider, falling back to default")
  185. return &CustomProvider{
  186. Clientset: clientset,
  187. }, nil
  188. }
  189. }