provider.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. package cloud
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/url"
  10. "os"
  11. "reflect"
  12. "strings"
  13. "sync"
  14. "k8s.io/klog"
  15. "cloud.google.com/go/compute/metadata"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/client-go/kubernetes"
  19. )
  20. const KC_CLUSTER_ID = "CLUSTER_ID"
  21. const remotePW = "REMOTE_WRITE_PASSWORD"
  22. const sqlAddress = "SQL_ADDRESS"
  23. const remoteEnabled = "REMOTE_WRITE_ENABLED"
  24. var createTableStatements = []string{
  25. `CREATE TABLE IF NOT EXISTS names (
  26. cluster_id VARCHAR(255) NOT NULL,
  27. cluster_name VARCHAR(255) NULL,
  28. PRIMARY KEY (cluster_id)
  29. );`,
  30. }
  31. // Node is the interface by which the provider and cost model communicate Node prices.
  32. // The provider will best-effort try to fill out this struct.
  33. type Node struct {
  34. Cost string `json:"hourlyCost"`
  35. VCPU string `json:"CPU"`
  36. VCPUCost string `json:"CPUHourlyCost"`
  37. RAM string `json:"RAM"`
  38. RAMBytes string `json:"RAMBytes"`
  39. RAMCost string `json:"RAMGBHourlyCost"`
  40. Storage string `json:"storage"`
  41. StorageCost string `json:"storageHourlyCost"`
  42. UsesBaseCPUPrice bool `json:"usesDefaultPrice"`
  43. BaseCPUPrice string `json:"baseCPUPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  44. BaseRAMPrice string `json:"baseRAMPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
  45. BaseGPUPrice string `json:"baseGPUPrice"`
  46. UsageType string `json:"usageType"`
  47. GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
  48. GPUName string `json:"gpuName"`
  49. GPUCost string `json:"gpuCost"`
  50. }
  51. // PV is the interface by which the provider and cost model communicate PV prices.
  52. // The provider will best-effort try to fill out this struct.
  53. type PV struct {
  54. Cost string `json:"hourlyCost"`
  55. CostPerIO string `json:"costPerIOOperation"`
  56. Class string `json:"storageClass"`
  57. Size string `json:"size"`
  58. Region string `json:"region"`
  59. Parameters map[string]string `json:"parameters"`
  60. }
  61. // Key represents a way for nodes to match between the k8s API and a pricing API
  62. type Key interface {
  63. ID() string // ID represents an exact match
  64. Features() string // Features are a comma separated string of node metadata that could match pricing
  65. GPUType() string // GPUType returns "" if no GPU exists, but the name of the GPU otherwise
  66. }
  67. type PVKey interface {
  68. Features() string
  69. GetStorageClass() string
  70. }
  71. // OutOfClusterAllocation represents a cloud provider cost not associated with kubernetes
  72. type OutOfClusterAllocation struct {
  73. Aggregator string `json:"aggregator"`
  74. Environment string `json:"environment"`
  75. Service string `json:"service"`
  76. Cost float64 `json:"cost"`
  77. Cluster string `json:"cluster"`
  78. }
  79. // Provider represents a k8s provider.
  80. type Provider interface {
  81. ClusterInfo() (map[string]string, error)
  82. AddServiceKey(url.Values) error
  83. GetDisks() ([]byte, error)
  84. NodePricing(Key) (*Node, error)
  85. PVPricing(PVKey) (*PV, error)
  86. AllNodePricing() (interface{}, error)
  87. DownloadPricingData() error
  88. GetKey(map[string]string) Key
  89. GetPVKey(*v1.PersistentVolume, map[string]string) PVKey
  90. UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
  91. GetConfig() (*CustomPricing, error)
  92. GetManagementPlatform() (string, error)
  93. GetLocalStorageQuery() (string, error)
  94. ExternalAllocations(string, string, string) ([]*OutOfClusterAllocation, error)
  95. }
  96. // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
  97. func GetDefaultPricingData(fname string) (*CustomPricing, error) {
  98. path := os.Getenv("CONFIG_PATH")
  99. if path == "" {
  100. path = "/models/"
  101. }
  102. path += fname
  103. if _, err := os.Stat(path); err == nil {
  104. jsonFile, err := os.Open(path)
  105. if err != nil {
  106. return nil, err
  107. }
  108. defer jsonFile.Close()
  109. byteValue, err := ioutil.ReadAll(jsonFile)
  110. if err != nil {
  111. return nil, err
  112. }
  113. var customPricing = &CustomPricing{}
  114. err = json.Unmarshal([]byte(byteValue), customPricing)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return customPricing, nil
  119. } else if os.IsNotExist(err) {
  120. c := &CustomPricing{
  121. Provider: fname,
  122. Description: "Default prices based on GCP us-central1",
  123. CPU: "0.031611",
  124. SpotCPU: "0.006655",
  125. RAM: "0.004237",
  126. SpotRAM: "0.000892",
  127. GPU: "0.95",
  128. Storage: "0.00005479452",
  129. CustomPricesEnabled: "false",
  130. }
  131. cj, err := json.Marshal(c)
  132. if err != nil {
  133. return nil, err
  134. }
  135. err = ioutil.WriteFile(path, cj, 0644)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return c, nil
  140. } else {
  141. return nil, err
  142. }
  143. }
  144. const KeyUpdateType = "athenainfo"
  145. type CustomPricing struct {
  146. Provider string `json:"provider"`
  147. Description string `json:"description"`
  148. CPU string `json:"CPU"`
  149. SpotCPU string `json:"spotCPU"`
  150. RAM string `json:"RAM"`
  151. SpotRAM string `json:"spotRAM"`
  152. GPU string `json:"GPU"`
  153. SpotGPU string `json:"spotGPU"`
  154. Storage string `json:"storage"`
  155. SpotLabel string `json:"spotLabel,omitempty"`
  156. SpotLabelValue string `json:"spotLabelValue,omitempty"`
  157. GpuLabel string `json:"gpuLabel,omitempty"`
  158. GpuLabelValue string `json:"gpuLabelValue,omitempty"`
  159. ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
  160. ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
  161. SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
  162. SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
  163. SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
  164. ProjectID string `json:"projectID,omitempty"`
  165. AthenaBucketName string `json:"athenaBucketName"`
  166. AthenaRegion string `json:"athenaRegion"`
  167. AthenaDatabase string `json:"athenaDatabase"`
  168. AthenaTable string `json:"athenaTable"`
  169. BillingDataDataset string `json:"billingDataDataset,omitempty"`
  170. CustomPricesEnabled string `json:"customPricesEnabled"`
  171. AzureSubscriptionID string `json:"azureSubscriptionID"`
  172. AzureClientID string `json:"azureClientID"`
  173. AzureClientSecret string `json:"azureClientSecret"`
  174. AzureTenantID string `json:"azureTenantID"`
  175. CurrencyCode string `json:"currencyCode"`
  176. Discount string `json:"discount"`
  177. ClusterName string `json:"clusterName"`
  178. }
  179. func SetCustomPricingField(obj *CustomPricing, name string, value string) error {
  180. structValue := reflect.ValueOf(obj).Elem()
  181. structFieldValue := structValue.FieldByName(name)
  182. if !structFieldValue.IsValid() {
  183. return fmt.Errorf("No such field: %s in obj", name)
  184. }
  185. if !structFieldValue.CanSet() {
  186. return fmt.Errorf("Cannot set %s field value", name)
  187. }
  188. structFieldType := structFieldValue.Type()
  189. val := reflect.ValueOf(value)
  190. if structFieldType != val.Type() {
  191. return fmt.Errorf("Provided value type didn't match custom pricing field type")
  192. }
  193. structFieldValue.Set(val)
  194. return nil
  195. }
  196. type NodePrice struct {
  197. CPU string
  198. RAM string
  199. GPU string
  200. }
  201. type CustomProvider struct {
  202. Clientset *kubernetes.Clientset
  203. Pricing map[string]*NodePrice
  204. SpotLabel string
  205. SpotLabelValue string
  206. GPULabel string
  207. GPULabelValue string
  208. DownloadPricingDataLock sync.RWMutex
  209. }
  210. func (*CustomProvider) GetLocalStorageQuery() (string, error) {
  211. return "", nil
  212. }
  213. func (*CustomProvider) GetConfig() (*CustomPricing, error) {
  214. return GetDefaultPricingData("default.json")
  215. }
  216. func (*CustomProvider) GetManagementPlatform() (string, error) {
  217. return "", nil
  218. }
  219. func (cprov *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  220. c, err := GetDefaultPricingData("default.json")
  221. if err != nil {
  222. return nil, err
  223. }
  224. path := os.Getenv("CONFIG_PATH")
  225. if path == "" {
  226. path = "/models/"
  227. }
  228. a := make(map[string]string)
  229. err = json.NewDecoder(r).Decode(&a)
  230. if err != nil {
  231. return nil, err
  232. }
  233. for k, v := range a {
  234. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  235. err := SetCustomPricingField(c, kUpper, v)
  236. if err != nil {
  237. return nil, err
  238. }
  239. }
  240. cj, err := json.Marshal(c)
  241. if err != nil {
  242. return nil, err
  243. }
  244. configPath := path + "default.json"
  245. err = ioutil.WriteFile(configPath, cj, 0644)
  246. if err != nil {
  247. return nil, err
  248. }
  249. defer cprov.DownloadPricingData()
  250. return c, nil
  251. }
  252. func (c *CustomProvider) ClusterInfo() (map[string]string, error) {
  253. conf, err := c.GetConfig()
  254. if err != nil {
  255. return nil, err
  256. }
  257. m := make(map[string]string)
  258. if conf.ClusterName != "" {
  259. m["name"] = conf.ClusterName
  260. }
  261. m["provider"] = "custom"
  262. return m, nil
  263. }
  264. func (*CustomProvider) AddServiceKey(url.Values) error {
  265. return nil
  266. }
  267. func (*CustomProvider) GetDisks() ([]byte, error) {
  268. return nil, nil
  269. }
  270. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  271. c.DownloadPricingDataLock.RLock()
  272. defer c.DownloadPricingDataLock.RUnlock()
  273. return c.Pricing, nil
  274. }
  275. func (c *CustomProvider) NodePricing(key Key) (*Node, error) {
  276. c.DownloadPricingDataLock.RLock()
  277. defer c.DownloadPricingDataLock.RUnlock()
  278. k := key.Features()
  279. var gpuCount string
  280. if _, ok := c.Pricing[k]; !ok {
  281. k = "default"
  282. }
  283. if key.GPUType() != "" {
  284. k += ",gpu" // TODO: support multiple custom gpu types.
  285. gpuCount = "1" // TODO: support more than one gpu.
  286. }
  287. return &Node{
  288. VCPUCost: c.Pricing[k].CPU,
  289. RAMCost: c.Pricing[k].RAM,
  290. GPUCost: c.Pricing[k].GPU,
  291. GPU: gpuCount,
  292. }, nil
  293. }
  294. func (c *CustomProvider) DownloadPricingData() error {
  295. c.DownloadPricingDataLock.Lock()
  296. defer c.DownloadPricingDataLock.Unlock()
  297. if c.Pricing == nil {
  298. m := make(map[string]*NodePrice)
  299. c.Pricing = m
  300. }
  301. p, err := GetDefaultPricingData("default.json")
  302. if err != nil {
  303. return err
  304. }
  305. c.SpotLabel = p.SpotLabel
  306. c.SpotLabelValue = p.SpotLabelValue
  307. c.GPULabel = p.GpuLabel
  308. c.GPULabelValue = p.GpuLabelValue
  309. c.Pricing["default"] = &NodePrice{
  310. CPU: p.CPU,
  311. RAM: p.RAM,
  312. }
  313. c.Pricing["default,spot"] = &NodePrice{
  314. CPU: p.SpotCPU,
  315. RAM: p.SpotRAM,
  316. }
  317. c.Pricing["default,gpu"] = &NodePrice{
  318. CPU: p.CPU,
  319. RAM: p.RAM,
  320. GPU: p.GPU,
  321. }
  322. return nil
  323. }
  324. type customProviderKey struct {
  325. SpotLabel string
  326. SpotLabelValue string
  327. GPULabel string
  328. GPULabelValue string
  329. Labels map[string]string
  330. }
  331. func (c *customProviderKey) GPUType() string {
  332. if t, ok := c.Labels[c.GPULabel]; ok {
  333. return t
  334. }
  335. return ""
  336. }
  337. func (c *customProviderKey) ID() string {
  338. return ""
  339. }
  340. func (c *customProviderKey) Features() string {
  341. if c.Labels[c.SpotLabel] != "" && c.Labels[c.SpotLabel] == c.SpotLabelValue {
  342. return "default,spot"
  343. }
  344. return "default" // TODO: multiple custom pricing support.
  345. }
  346. func (c *CustomProvider) GetKey(labels map[string]string) Key {
  347. return &customProviderKey{
  348. SpotLabel: c.SpotLabel,
  349. SpotLabelValue: c.SpotLabelValue,
  350. GPULabel: c.GPULabel,
  351. GPULabelValue: c.GPULabelValue,
  352. Labels: labels,
  353. }
  354. }
  355. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  356. // "start" and "end" are dates of the format YYYY-MM-DD
  357. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  358. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator string) ([]*OutOfClusterAllocation, error) {
  359. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  360. }
  361. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  362. return nil, nil
  363. }
  364. func (c *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  365. cpricing, err := GetDefaultPricingData("default")
  366. if err != nil {
  367. return nil, err
  368. }
  369. return &PV{
  370. Cost: cpricing.Storage,
  371. }, nil
  372. }
  373. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  374. return &awsPVKey{
  375. Labels: pv.Labels,
  376. StorageClassName: pv.Spec.StorageClassName,
  377. }
  378. }
  379. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  380. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  381. if metadata.OnGCE() {
  382. klog.V(3).Info("metadata reports we are in GCE")
  383. if apiKey == "" {
  384. return nil, errors.New("Supply a GCP Key to start getting data")
  385. }
  386. return &GCP{
  387. Clientset: clientset,
  388. APIKey: apiKey,
  389. }, nil
  390. }
  391. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  392. if err != nil {
  393. return nil, err
  394. }
  395. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  396. if strings.HasPrefix(provider, "aws") {
  397. klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
  398. return &AWS{
  399. Clientset: clientset,
  400. }, nil
  401. } else if strings.HasPrefix(provider, "azure") {
  402. klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
  403. return &Azure{
  404. Clientset: clientset,
  405. }, nil
  406. } else {
  407. klog.V(2).Info("Unsupported provider, falling back to default")
  408. return &CustomProvider{
  409. Clientset: clientset,
  410. }, nil
  411. }
  412. }
  413. func UpdateClusterMeta(cluster_id, cluster_name string) error {
  414. pw := os.Getenv(remotePW)
  415. address := os.Getenv(sqlAddress)
  416. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  417. db, err := sql.Open("postgres", connStr)
  418. if err != nil {
  419. return err
  420. }
  421. defer db.Close()
  422. updateStmt := `UPDATE names SET cluster_name = $1 WHERE cluster_id = $2;`
  423. _, err = db.Exec(updateStmt, cluster_name, cluster_id)
  424. if err != nil {
  425. return err
  426. }
  427. return nil
  428. }
  429. func CreateClusterMeta(cluster_id, cluster_name string) error {
  430. pw := os.Getenv(remotePW)
  431. address := os.Getenv(sqlAddress)
  432. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  433. db, err := sql.Open("postgres", connStr)
  434. if err != nil {
  435. return err
  436. }
  437. defer db.Close()
  438. for _, stmt := range createTableStatements {
  439. _, err := db.Exec(stmt)
  440. if err != nil {
  441. return err
  442. }
  443. }
  444. insertStmt := `INSERT INTO names (cluster_id, cluster_name) VALUES ($1, $2);`
  445. _, err = db.Exec(insertStmt, cluster_id, cluster_name)
  446. if err != nil {
  447. return err
  448. }
  449. return nil
  450. }
  451. func GetClusterMeta(cluster_id string) (string, string, error) {
  452. pw := os.Getenv(remotePW)
  453. address := os.Getenv(sqlAddress)
  454. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  455. db, err := sql.Open("postgres", connStr)
  456. defer db.Close()
  457. query := `SELECT cluster_id, cluster_name
  458. FROM names
  459. WHERE cluster_id = ?`
  460. rows, err := db.Query(query, cluster_id)
  461. if err != nil {
  462. return "", "", err
  463. }
  464. defer rows.Close()
  465. var (
  466. sql_cluster_id string
  467. cluster_name string
  468. )
  469. for rows.Next() {
  470. if err := rows.Scan(&sql_cluster_id, &cluster_name); err != nil {
  471. return "", "", err
  472. }
  473. }
  474. return sql_cluster_id, cluster_name, nil
  475. }
  476. func GetOrCreateClusterMeta(cluster_id, cluster_name string) (string, string, error) {
  477. id, name, err := GetClusterMeta(cluster_id)
  478. if err != nil {
  479. err := CreateClusterMeta(cluster_id, cluster_name)
  480. if err != nil {
  481. return "", "", err
  482. }
  483. }
  484. if id == "" {
  485. err := CreateClusterMeta(cluster_id, cluster_name)
  486. if err != nil {
  487. return "", "", err
  488. }
  489. }
  490. return id, name, nil
  491. }