provider.go 16 KB

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