provider.go 6.3 KB

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