provider.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package cloud
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "k8s.io/klog"
  11. "cloud.google.com/go/compute/metadata"
  12. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  13. "k8s.io/client-go/kubernetes"
  14. )
  15. // Node is the interface by which the provider and cost model communicate.
  16. // The provider will best-effort try to fill out this struct.
  17. type Node struct {
  18. Cost string `json:"hourlyCost"`
  19. VCPU string `json:"CPU"`
  20. VCPUCost string `json:"CPUHourlyCost"`
  21. RAM string `json:"RAM"`
  22. RAMBytes string `json:"RAMBytes"`
  23. RAMCost string `json:"RAMGBHourlyCost"`
  24. Storage string `json:"storage"`
  25. StorageCost string `json:"storageHourlyCost"`
  26. UsesBaseCPUPrice bool `json:"usesDefaultPrice"`
  27. BaseCPUPrice string `json:"baseCPUPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  28. UsageType string `json:"usageType"`
  29. GPU string `json:"gpu"`
  30. GPUName string `json:"gpuName"`
  31. GPUCost string `json:"gpuCost"`
  32. }
  33. // Key represents a way for nodes to match between the k8s API and a pricing API
  34. type Key interface {
  35. ID() string // ID represents an exact match
  36. Features() string // Features are a comma separated string of node metadata that could match pricing
  37. GPUType() string // GPUType returns "" if no GPU exists, but the name of the GPU otherwise
  38. }
  39. // OutOfClusterAllocation represents a cloud provider cost not associated with kubernetes
  40. type OutOfClusterAllocation struct {
  41. Aggregator string `json:"aggregator"`
  42. Environment string `json:"environment"`
  43. Service string `json:"service"`
  44. Cost float64 `json:"cost"`
  45. Cluster string `json:"cluster"`
  46. }
  47. // Provider represents a k8s provider.
  48. type Provider interface {
  49. ClusterName() ([]byte, error)
  50. AddServiceKey(url.Values) error
  51. GetDisks() ([]byte, error)
  52. NodePricing(Key) (*Node, error)
  53. AllNodePricing() (interface{}, error)
  54. DownloadPricingData() error
  55. GetKey(map[string]string) Key
  56. UpdateConfig(r io.Reader) (*CustomPricing, error)
  57. GetConfig() (*CustomPricing, error)
  58. ExternalAllocations(string, string) ([]*OutOfClusterAllocation, error)
  59. }
  60. // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
  61. func GetDefaultPricingData(fname string) (*CustomPricing, error) {
  62. jsonFile, err := os.Open("/models/" + fname)
  63. if err != nil {
  64. return nil, err
  65. }
  66. defer jsonFile.Close()
  67. byteValue, err := ioutil.ReadAll(jsonFile)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var customPricing = &CustomPricing{}
  72. err = json.Unmarshal([]byte(byteValue), customPricing)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return customPricing, nil
  77. }
  78. type CustomPricing struct {
  79. Provider string `json:"provider"`
  80. Description string `json:"description"`
  81. CPU string `json:"CPU"`
  82. SpotCPU string `json:"spotCPU"`
  83. RAM string `json:"RAM"`
  84. SpotRAM string `json:"spotRAM"`
  85. SpotLabel string `json:"spotLabel,omitempty"`
  86. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  87. ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
  88. ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
  89. SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
  90. SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
  91. SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
  92. ProjectID string `json:"projectID,omitempty"`
  93. BillingDataDataset string `json:"billingDataDataset,omitempty"`
  94. }
  95. type NodePrice struct {
  96. CPU string
  97. RAM string
  98. }
  99. type CustomProvider struct {
  100. Clientset *kubernetes.Clientset
  101. Pricing map[string]*NodePrice
  102. SpotLabel string
  103. SpotLabelValue string
  104. }
  105. func (*CustomProvider) GetConfig() (*CustomPricing, error) {
  106. return nil, nil
  107. }
  108. func (*CustomProvider) UpdateConfig(r io.Reader) (*CustomPricing, error) {
  109. return nil, nil
  110. }
  111. func (*CustomProvider) ClusterName() ([]byte, error) {
  112. return nil, nil
  113. }
  114. func (*CustomProvider) AddServiceKey(url.Values) error {
  115. return nil
  116. }
  117. func (*CustomProvider) GetDisks() ([]byte, error) {
  118. return nil, nil
  119. }
  120. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  121. return c.Pricing, nil
  122. }
  123. func (c *CustomProvider) NodePricing(key Key) (*Node, error) {
  124. k := key.Features()
  125. if _, ok := c.Pricing[k]; !ok {
  126. k = "default"
  127. }
  128. return &Node{
  129. VCPUCost: c.Pricing[k].CPU,
  130. RAMCost: c.Pricing[k].RAM,
  131. }, nil
  132. }
  133. func (c *CustomProvider) DownloadPricingData() error {
  134. if c.Pricing == nil {
  135. m := make(map[string]*NodePrice)
  136. c.Pricing = m
  137. }
  138. p, err := GetDefaultPricingData("default.json")
  139. if err != nil {
  140. return err
  141. }
  142. c.Pricing["default"] = &NodePrice{
  143. CPU: p.CPU,
  144. RAM: p.RAM,
  145. }
  146. c.Pricing["default,spot"] = &NodePrice{
  147. CPU: p.SpotCPU,
  148. RAM: p.SpotRAM,
  149. }
  150. return nil
  151. }
  152. type customProviderKey struct {
  153. SpotLabel string
  154. SpotLabelValue string
  155. Labels map[string]string
  156. }
  157. func (c *customProviderKey) GPUType() string {
  158. return ""
  159. }
  160. func (c *customProviderKey) ID() string {
  161. return ""
  162. }
  163. func (c *customProviderKey) Features() string {
  164. if c.Labels[c.SpotLabel] != "" && c.Labels[c.SpotLabel] == c.SpotLabelValue {
  165. return "default,spot"
  166. }
  167. return "default" // TODO: multiple custom pricing support.
  168. }
  169. func (c *CustomProvider) GetKey(labels map[string]string) Key {
  170. return &customProviderKey{
  171. SpotLabel: c.SpotLabel,
  172. SpotLabelValue: c.SpotLabelValue,
  173. Labels: labels,
  174. }
  175. }
  176. func (*CustomProvider) ExternalAllocations(start string, end string) ([]*OutOfClusterAllocation, error) {
  177. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  178. }
  179. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  180. return nil, nil
  181. }
  182. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  183. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  184. if metadata.OnGCE() {
  185. klog.V(3).Info("metadata reports we are in GCE")
  186. if apiKey == "" {
  187. return nil, errors.New("Supply a GCP Key to start getting data")
  188. }
  189. return &GCP{
  190. Clientset: clientset,
  191. APIKey: apiKey,
  192. }, nil
  193. }
  194. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  195. if err != nil {
  196. return nil, err
  197. }
  198. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  199. if strings.HasPrefix(provider, "aws") {
  200. klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
  201. return &AWS{
  202. Clientset: clientset,
  203. }, nil
  204. } else if strings.HasPrefix(provider, "azure") {
  205. klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
  206. return &Azure{
  207. CustomProvider: &CustomProvider{
  208. Clientset: clientset,
  209. },
  210. }, nil
  211. } else {
  212. klog.V(2).Info("Unsupported provider, falling back to default")
  213. return &CustomProvider{
  214. Clientset: clientset,
  215. }, nil
  216. }
  217. }