custom_resource_allocation.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package priorities
  14. import (
  15. "database/sql"
  16. "fmt"
  17. "os"
  18. _ "github.com/go-sql-driver/mysql"
  19. client "github.com/influxdata/influxdb1-client/v2"
  20. "gopkg.in/yaml.v2"
  21. "k8s.io/klog"
  22. )
  23. var (
  24. customResourcePriority = &CustomAllocationPriority{"CustomResourceAllocation", customResourceScorer}
  25. // LeastRequestedPriorityMap is a priority function that favors nodes with fewer requested resources.
  26. // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and
  27. // prioritizes based on the minimum of the average of the fraction of requested to capacity.
  28. //
  29. // Details:
  30. // (cpu((capacity-sum(requested))*10/capacity) + memory((capacity-sum(requested))*10/capacity))/2
  31. CustomRequestedPriorityMap = customResourcePriority.PriorityMap
  32. )
  33. type Config struct {
  34. Server struct {
  35. Port string `yaml:"port"`
  36. Host string `yaml:"host"`
  37. } `yaml:"server"`
  38. Database struct {
  39. Type string `yaml: "type"`
  40. Name string `yaml:"name"`
  41. Username string `yaml:"username"`
  42. Password string `yaml:"password"`
  43. } `yaml:"database"`
  44. }
  45. type System struct {
  46. ID int `json:"id"`
  47. Uuid string `json:"uuid"`
  48. numSockets int `json:"num_sockets"`
  49. numCores int `json:"num_cores`
  50. }
  51. var nodes = map[string]string{
  52. "kube-01": "c4766d29-4dc1-11ea-9d98-0242ac110002",
  53. "kube-02": "2",
  54. "kube-03": "2",
  55. "kube-04": "2",
  56. "kube-05": "2",
  57. }
  58. func readFile(cfg *Config, file string) {
  59. f, err := os.Open("/etc/kubernetes/scheduler-monitoringDB.yaml")
  60. if err != nil {
  61. panic(err.Error())
  62. }
  63. defer f.Close()
  64. decoder := yaml.NewDecoder(f)
  65. err = decoder.Decode(&cfg)
  66. if err != nil {
  67. panic(err.Error())
  68. }
  69. }
  70. func customResourceScorer(nodeName string) int64 {
  71. //return (customRequestedScore(requested.MilliCPU, allocable.MilliCPU) +
  72. //customRequestedScore(requested.Memory, allocable.Memory)) / 2
  73. //read database information
  74. var cfg Config
  75. readFile(&cfg, "/etc/kubernetes/scheduler-monitoringDB.yaml")
  76. /*-------------------------------------
  77. //TODO read also nodes to uuid mappings
  78. -------------------------------------*/
  79. //Access the Database
  80. DBstring := cfg.Database.Username + ":" + cfg.Database.Password + "@tcp(" + cfg.Server.Host + ":" + cfg.Server.Port + ")/" + cfg.Database.Name
  81. db, err := sql.Open(cfg.Database.Type, DBstring)
  82. if err != nil {
  83. panic(err.Error())
  84. }
  85. //TODO InfluxDB
  86. c, err := client.NewHttpClient(client.HTTPConfig{
  87. Addr: "http://" + cfg.Server.Host + ":" + cfg.Server.Port + "",
  88. })
  89. if err != nil {
  90. fmt.Println("Error while creating InfluxDB client: ", err.Error())
  91. }
  92. // close the connection in the end of execution
  93. defer c.Close()
  94. //Get the uuid of this node in order to query in the database
  95. curr_uuid, ok := nodes[nodeName]
  96. if ok {
  97. command := fmt.Sprintf("SELECT ipc from system_metrics where uuid = %s order by time desc limit 1", curr_uuid)
  98. q := client.NewQuery(command, "evolve", "")
  99. if response, err := c.Query(q); err == nil && response.Error() == nil {
  100. fmt.Println(response.Results)
  101. }
  102. }
  103. // //Close the database connection in the end of the execution
  104. // defer db.Close()
  105. // //Get the uuid of this node in order to query in the database
  106. // curr_uuid, ok := nodes[nodeName]
  107. // //Get the metrics for the current node
  108. // if ok {
  109. // results, err := db.Query("SELECT id, num_sockets, num_cores FROM systems WHERE uuid = ?", curr_uuid)
  110. // if err != nil {
  111. // panic(err.Error()) // proper error handling instead of panic in your app
  112. // }
  113. // sys := System{}
  114. // sys.Uuid = curr_uuid
  115. // for results.Next() {
  116. // // for each row, scan the result into our tag composite object
  117. // err = results.Scan(&sys.ID, &sys.numSockets, &sys.numCores)
  118. // if err != nil {
  119. // panic(err.Error()) // proper error handling instead of panic in your app
  120. // }
  121. // // and then print out the tag's Name attribute
  122. // klog.Infof("This is the system with name: %s, id: %d and number of cores: %d", nodeName, sys.ID, sys.numCores)
  123. // }
  124. // }
  125. res := customRequestedScore(nodeName)
  126. klog.Infof("Node name %s, has score %d\n", nodeName, res)
  127. return res
  128. }
  129. func customRequestedScore(nodeName string) int64 {
  130. if nodeName == "kube-01" {
  131. return 10
  132. }
  133. return 0
  134. }