provider.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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"` // GPU represents the number of GPU on the instance
  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. path := os.Getenv("CONFIG_PATH")
  63. if path == "" {
  64. path = "/models/"
  65. }
  66. path += fname
  67. if _, err := os.Stat(path); err == nil {
  68. jsonFile, err := os.Open(path)
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer jsonFile.Close()
  73. byteValue, err := ioutil.ReadAll(jsonFile)
  74. if err != nil {
  75. return nil, err
  76. }
  77. var customPricing = &CustomPricing{}
  78. err = json.Unmarshal([]byte(byteValue), customPricing)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return customPricing, nil
  83. } else if os.IsNotExist(err) {
  84. c := &CustomPricing{
  85. Provider: fname,
  86. Description: "Default prices based on GCP us-central1",
  87. CPU: "0.031611",
  88. SpotCPU: "0.006655",
  89. RAM: "0.004237",
  90. SpotRAM: "0.000892",
  91. }
  92. cj, err := json.Marshal(c)
  93. if err != nil {
  94. return nil, err
  95. }
  96. err = ioutil.WriteFile(path, cj, 0644)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return c, nil
  101. } else {
  102. return nil, err
  103. }
  104. }
  105. type CustomPricing struct {
  106. Provider string `json:"provider"`
  107. Description string `json:"description"`
  108. CPU string `json:"CPU"`
  109. SpotCPU string `json:"spotCPU"`
  110. RAM string `json:"RAM"`
  111. SpotRAM string `json:"spotRAM"`
  112. SpotLabel string `json:"spotLabel,omitempty"`
  113. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  114. ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
  115. ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
  116. SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
  117. SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
  118. SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
  119. ProjectID string `json:"projectID,omitempty"`
  120. BillingDataDataset string `json:"billingDataDataset,omitempty"`
  121. }
  122. type NodePrice struct {
  123. CPU string
  124. RAM string
  125. }
  126. type CustomProvider struct {
  127. Clientset *kubernetes.Clientset
  128. Pricing map[string]*NodePrice
  129. SpotLabel string
  130. SpotLabelValue string
  131. }
  132. func (*CustomProvider) GetConfig() (*CustomPricing, error) {
  133. return nil, nil
  134. }
  135. func (*CustomProvider) UpdateConfig(r io.Reader) (*CustomPricing, error) {
  136. return nil, nil
  137. }
  138. func (*CustomProvider) ClusterName() ([]byte, error) {
  139. return nil, nil
  140. }
  141. func (*CustomProvider) AddServiceKey(url.Values) error {
  142. return nil
  143. }
  144. func (*CustomProvider) GetDisks() ([]byte, error) {
  145. return nil, nil
  146. }
  147. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  148. return c.Pricing, nil
  149. }
  150. func (c *CustomProvider) NodePricing(key Key) (*Node, error) {
  151. k := key.Features()
  152. if _, ok := c.Pricing[k]; !ok {
  153. k = "default"
  154. }
  155. return &Node{
  156. VCPUCost: c.Pricing[k].CPU,
  157. RAMCost: c.Pricing[k].RAM,
  158. }, nil
  159. }
  160. func (c *CustomProvider) DownloadPricingData() error {
  161. if c.Pricing == nil {
  162. m := make(map[string]*NodePrice)
  163. c.Pricing = m
  164. }
  165. p, err := GetDefaultPricingData("default.json")
  166. if err != nil {
  167. return err
  168. }
  169. c.Pricing["default"] = &NodePrice{
  170. CPU: p.CPU,
  171. RAM: p.RAM,
  172. }
  173. c.Pricing["default,spot"] = &NodePrice{
  174. CPU: p.SpotCPU,
  175. RAM: p.SpotRAM,
  176. }
  177. return nil
  178. }
  179. type customProviderKey struct {
  180. SpotLabel string
  181. SpotLabelValue string
  182. Labels map[string]string
  183. }
  184. func (c *customProviderKey) GPUType() string {
  185. return ""
  186. }
  187. func (c *customProviderKey) ID() string {
  188. return ""
  189. }
  190. func (c *customProviderKey) Features() string {
  191. if c.Labels[c.SpotLabel] != "" && c.Labels[c.SpotLabel] == c.SpotLabelValue {
  192. return "default,spot"
  193. }
  194. return "default" // TODO: multiple custom pricing support.
  195. }
  196. func (c *CustomProvider) GetKey(labels map[string]string) Key {
  197. return &customProviderKey{
  198. SpotLabel: c.SpotLabel,
  199. SpotLabelValue: c.SpotLabelValue,
  200. Labels: labels,
  201. }
  202. }
  203. func (*CustomProvider) ExternalAllocations(start string, end string) ([]*OutOfClusterAllocation, error) {
  204. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  205. }
  206. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  207. return nil, nil
  208. }
  209. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  210. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  211. if metadata.OnGCE() {
  212. klog.V(3).Info("metadata reports we are in GCE")
  213. if apiKey == "" {
  214. return nil, errors.New("Supply a GCP Key to start getting data")
  215. }
  216. return &GCP{
  217. Clientset: clientset,
  218. APIKey: apiKey,
  219. }, nil
  220. }
  221. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  222. if err != nil {
  223. return nil, err
  224. }
  225. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  226. if strings.HasPrefix(provider, "aws") {
  227. klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
  228. return &AWS{
  229. Clientset: clientset,
  230. }, nil
  231. } else if strings.HasPrefix(provider, "azure") {
  232. klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
  233. return &Azure{
  234. CustomProvider: &CustomProvider{
  235. Clientset: clientset,
  236. },
  237. }, nil
  238. } else {
  239. klog.V(2).Info("Unsupported provider, falling back to default")
  240. return &CustomProvider{
  241. Clientset: clientset,
  242. }, nil
  243. }
  244. }