provider.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. CurrencyCode string `json:"currencyCode"`
  191. Discount string `json:"discount"`
  192. ClusterName string `json:"clusterName"`
  193. }
  194. func SetCustomPricingField(obj *CustomPricing, name string, value string) error {
  195. structValue := reflect.ValueOf(obj).Elem()
  196. structFieldValue := structValue.FieldByName(name)
  197. if !structFieldValue.IsValid() {
  198. return fmt.Errorf("No such field: %s in obj", name)
  199. }
  200. if !structFieldValue.CanSet() {
  201. return fmt.Errorf("Cannot set %s field value", name)
  202. }
  203. structFieldType := structFieldValue.Type()
  204. val := reflect.ValueOf(value)
  205. if structFieldType != val.Type() {
  206. return fmt.Errorf("Provided value type didn't match custom pricing field type")
  207. }
  208. structFieldValue.Set(val)
  209. return nil
  210. }
  211. type NodePrice struct {
  212. CPU string
  213. RAM string
  214. GPU string
  215. }
  216. type CustomProvider struct {
  217. Clientset *kubernetes.Clientset
  218. Pricing map[string]*NodePrice
  219. SpotLabel string
  220. SpotLabelValue string
  221. GPULabel string
  222. GPULabelValue string
  223. DownloadPricingDataLock sync.RWMutex
  224. }
  225. func (*CustomProvider) GetLocalStorageQuery() (string, error) {
  226. return "", nil
  227. }
  228. func (*CustomProvider) GetConfig() (*CustomPricing, error) {
  229. return GetDefaultPricingData("default.json")
  230. }
  231. func (*CustomProvider) GetManagementPlatform() (string, error) {
  232. return "", nil
  233. }
  234. func (cprov *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
  235. c, err := GetDefaultPricingData("default.json")
  236. if err != nil {
  237. return nil, err
  238. }
  239. path := os.Getenv("CONFIG_PATH")
  240. if path == "" {
  241. path = "/models/"
  242. }
  243. a := make(map[string]string)
  244. err = json.NewDecoder(r).Decode(&a)
  245. if err != nil {
  246. return nil, err
  247. }
  248. for k, v := range a {
  249. kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
  250. err := SetCustomPricingField(c, kUpper, v)
  251. if err != nil {
  252. return nil, err
  253. }
  254. }
  255. cj, err := json.Marshal(c)
  256. if err != nil {
  257. return nil, err
  258. }
  259. configPath := path + "default.json"
  260. err = ioutil.WriteFile(configPath, cj, 0644)
  261. if err != nil {
  262. return nil, err
  263. }
  264. defer cprov.DownloadPricingData()
  265. return c, nil
  266. }
  267. func (c *CustomProvider) ClusterInfo() (map[string]string, error) {
  268. conf, err := c.GetConfig()
  269. if err != nil {
  270. return nil, err
  271. }
  272. m := make(map[string]string)
  273. if conf.ClusterName != "" {
  274. m["name"] = conf.ClusterName
  275. }
  276. m["provider"] = "custom"
  277. return m, nil
  278. }
  279. func (*CustomProvider) AddServiceKey(url.Values) error {
  280. return nil
  281. }
  282. func (*CustomProvider) GetDisks() ([]byte, error) {
  283. return nil, nil
  284. }
  285. func (c *CustomProvider) AllNodePricing() (interface{}, error) {
  286. c.DownloadPricingDataLock.RLock()
  287. defer c.DownloadPricingDataLock.RUnlock()
  288. return c.Pricing, nil
  289. }
  290. func (c *CustomProvider) NodePricing(key Key) (*Node, error) {
  291. c.DownloadPricingDataLock.RLock()
  292. defer c.DownloadPricingDataLock.RUnlock()
  293. k := key.Features()
  294. var gpuCount string
  295. if _, ok := c.Pricing[k]; !ok {
  296. k = "default"
  297. }
  298. if key.GPUType() != "" {
  299. k += ",gpu" // TODO: support multiple custom gpu types.
  300. gpuCount = "1" // TODO: support more than one gpu.
  301. }
  302. return &Node{
  303. VCPUCost: c.Pricing[k].CPU,
  304. RAMCost: c.Pricing[k].RAM,
  305. GPUCost: c.Pricing[k].GPU,
  306. GPU: gpuCount,
  307. }, nil
  308. }
  309. func (c *CustomProvider) DownloadPricingData() error {
  310. c.DownloadPricingDataLock.Lock()
  311. defer c.DownloadPricingDataLock.Unlock()
  312. if c.Pricing == nil {
  313. m := make(map[string]*NodePrice)
  314. c.Pricing = m
  315. }
  316. p, err := GetDefaultPricingData("default.json")
  317. if err != nil {
  318. return err
  319. }
  320. c.SpotLabel = p.SpotLabel
  321. c.SpotLabelValue = p.SpotLabelValue
  322. c.GPULabel = p.GpuLabel
  323. c.GPULabelValue = p.GpuLabelValue
  324. c.Pricing["default"] = &NodePrice{
  325. CPU: p.CPU,
  326. RAM: p.RAM,
  327. }
  328. c.Pricing["default,spot"] = &NodePrice{
  329. CPU: p.SpotCPU,
  330. RAM: p.SpotRAM,
  331. }
  332. c.Pricing["default,gpu"] = &NodePrice{
  333. CPU: p.CPU,
  334. RAM: p.RAM,
  335. GPU: p.GPU,
  336. }
  337. return nil
  338. }
  339. type customProviderKey struct {
  340. SpotLabel string
  341. SpotLabelValue string
  342. GPULabel string
  343. GPULabelValue string
  344. Labels map[string]string
  345. }
  346. func (c *customProviderKey) GPUType() string {
  347. if t, ok := c.Labels[c.GPULabel]; ok {
  348. return t
  349. }
  350. return ""
  351. }
  352. func (c *customProviderKey) ID() string {
  353. return ""
  354. }
  355. func (c *customProviderKey) Features() string {
  356. if c.Labels[c.SpotLabel] != "" && c.Labels[c.SpotLabel] == c.SpotLabelValue {
  357. return "default,spot"
  358. }
  359. return "default" // TODO: multiple custom pricing support.
  360. }
  361. func (c *CustomProvider) GetKey(labels map[string]string) Key {
  362. return &customProviderKey{
  363. SpotLabel: c.SpotLabel,
  364. SpotLabelValue: c.SpotLabelValue,
  365. GPULabel: c.GPULabel,
  366. GPULabelValue: c.GPULabelValue,
  367. Labels: labels,
  368. }
  369. }
  370. // ExternalAllocations represents tagged assets outside the scope of kubernetes.
  371. // "start" and "end" are dates of the format YYYY-MM-DD
  372. // "aggregator" is the tag used to determine how to allocate those assets, ie namespace, pod, etc.
  373. func (*CustomProvider) ExternalAllocations(start string, end string, aggregator string) ([]*OutOfClusterAllocation, error) {
  374. return nil, nil // TODO: transform the QuerySQL lines into the new OutOfClusterAllocation Struct
  375. }
  376. func (*CustomProvider) QuerySQL(query string) ([]byte, error) {
  377. return nil, nil
  378. }
  379. func (c *CustomProvider) PVPricing(pvk PVKey) (*PV, error) {
  380. cpricing, err := GetDefaultPricingData("default")
  381. if err != nil {
  382. return nil, err
  383. }
  384. return &PV{
  385. Cost: cpricing.Storage,
  386. }, nil
  387. }
  388. func (c *CustomProvider) NetworkPricing() (*Network, error) {
  389. cpricing, err := GetDefaultPricingData("default")
  390. if err != nil {
  391. return nil, err
  392. }
  393. znec, err := strconv.ParseFloat(cpricing.ZoneNetworkEgress, 64)
  394. if err != nil {
  395. return nil, err
  396. }
  397. rnec, err := strconv.ParseFloat(cpricing.RegionNetworkEgress, 64)
  398. if err != nil {
  399. return nil, err
  400. }
  401. inec, err := strconv.ParseFloat(cpricing.InternetNetworkEgress, 64)
  402. if err != nil {
  403. return nil, err
  404. }
  405. return &Network{
  406. ZoneNetworkEgressCost: znec,
  407. RegionNetworkEgressCost: rnec,
  408. InternetNetworkEgressCost: inec,
  409. }, nil
  410. }
  411. func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string) PVKey {
  412. return &awsPVKey{
  413. Labels: pv.Labels,
  414. StorageClassName: pv.Spec.StorageClassName,
  415. }
  416. }
  417. // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
  418. func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, error) {
  419. if metadata.OnGCE() {
  420. klog.V(3).Info("metadata reports we are in GCE")
  421. if apiKey == "" {
  422. return nil, errors.New("Supply a GCP Key to start getting data")
  423. }
  424. return &GCP{
  425. Clientset: clientset,
  426. APIKey: apiKey,
  427. }, nil
  428. }
  429. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  430. if err != nil {
  431. return nil, err
  432. }
  433. provider := strings.ToLower(nodes.Items[0].Spec.ProviderID)
  434. if strings.HasPrefix(provider, "aws") {
  435. klog.V(2).Info("Found ProviderID starting with \"aws\", using AWS Provider")
  436. return &AWS{
  437. Clientset: clientset,
  438. }, nil
  439. } else if strings.HasPrefix(provider, "azure") {
  440. klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
  441. return &Azure{
  442. Clientset: clientset,
  443. }, nil
  444. } else {
  445. klog.V(2).Info("Unsupported provider, falling back to default")
  446. return &CustomProvider{
  447. Clientset: clientset,
  448. }, nil
  449. }
  450. }
  451. func UpdateClusterMeta(cluster_id, cluster_name 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. if err != nil {
  457. return err
  458. }
  459. defer db.Close()
  460. updateStmt := `UPDATE names SET cluster_name = $1 WHERE cluster_id = $2;`
  461. _, err = db.Exec(updateStmt, cluster_name, cluster_id)
  462. if err != nil {
  463. return err
  464. }
  465. return nil
  466. }
  467. func CreateClusterMeta(cluster_id, cluster_name string) error {
  468. pw := os.Getenv(remotePW)
  469. address := os.Getenv(sqlAddress)
  470. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  471. db, err := sql.Open("postgres", connStr)
  472. if err != nil {
  473. return err
  474. }
  475. defer db.Close()
  476. for _, stmt := range createTableStatements {
  477. _, err := db.Exec(stmt)
  478. if err != nil {
  479. return err
  480. }
  481. }
  482. insertStmt := `INSERT INTO names (cluster_id, cluster_name) VALUES ($1, $2);`
  483. _, err = db.Exec(insertStmt, cluster_id, cluster_name)
  484. if err != nil {
  485. return err
  486. }
  487. return nil
  488. }
  489. func GetClusterMeta(cluster_id string) (string, string, error) {
  490. pw := os.Getenv(remotePW)
  491. address := os.Getenv(sqlAddress)
  492. connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
  493. db, err := sql.Open("postgres", connStr)
  494. defer db.Close()
  495. query := `SELECT cluster_id, cluster_name
  496. FROM names
  497. WHERE cluster_id = ?`
  498. rows, err := db.Query(query, cluster_id)
  499. if err != nil {
  500. return "", "", err
  501. }
  502. defer rows.Close()
  503. var (
  504. sql_cluster_id string
  505. cluster_name string
  506. )
  507. for rows.Next() {
  508. if err := rows.Scan(&sql_cluster_id, &cluster_name); err != nil {
  509. return "", "", err
  510. }
  511. }
  512. return sql_cluster_id, cluster_name, nil
  513. }
  514. func GetOrCreateClusterMeta(cluster_id, cluster_name string) (string, string, error) {
  515. id, name, err := GetClusterMeta(cluster_id)
  516. if err != nil {
  517. err := CreateClusterMeta(cluster_id, cluster_name)
  518. if err != nil {
  519. return "", "", err
  520. }
  521. }
  522. if id == "" {
  523. err := CreateClusterMeta(cluster_id, cluster_name)
  524. if err != nil {
  525. return "", "", err
  526. }
  527. }
  528. return id, name, nil
  529. }