custom_resource_allocation.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. _, 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. klog.Infof("Error while creating InfluxDB client: %v ", err.Error())
  91. } else {
  92. klog.Infof("Connected Successfully to InfluxDB")
  93. }
  94. // close the connection in the end of execution
  95. defer c.Close()
  96. //Get the uuid of this node in order to query in the database
  97. curr_uuid, ok := nodes[nodeName]
  98. if ok {
  99. command := fmt.Sprintf("SELECT ipc from system_metrics where uuid = %s order by time desc limit 1", curr_uuid)
  100. q := client.NewQuery(command, "evolve", "")
  101. if response, err := c.Query(q); err == nil && response.Error() == nil {
  102. klog.Infof("%v", response.Results)
  103. }
  104. } else {
  105. klog.Infof("Something went wrong with InfluxDB metrics extraction: %v", ok)
  106. }
  107. // //Close the database connection in the end of the execution
  108. // defer db.Close()
  109. // //Get the uuid of this node in order to query in the database
  110. // curr_uuid, ok := nodes[nodeName]
  111. // //Get the metrics for the current node
  112. // if ok {
  113. // results, err := db.Query("SELECT id, num_sockets, num_cores FROM systems WHERE uuid = ?", curr_uuid)
  114. // if err != nil {
  115. // panic(err.Error()) // proper error handling instead of panic in your app
  116. // }
  117. // sys := System{}
  118. // sys.Uuid = curr_uuid
  119. // for results.Next() {
  120. // // for each row, scan the result into our tag composite object
  121. // err = results.Scan(&sys.ID, &sys.numSockets, &sys.numCores)
  122. // if err != nil {
  123. // panic(err.Error()) // proper error handling instead of panic in your app
  124. // }
  125. // // and then print out the tag's Name attribute
  126. // klog.Infof("This is the system with name: %s, id: %d and number of cores: %d", nodeName, sys.ID, sys.numCores)
  127. // }
  128. // }
  129. res := customRequestedScore(nodeName)
  130. klog.Infof("Node name %s, has score %d\n", nodeName, res)
  131. return res
  132. }
  133. func customRequestedScore(nodeName string) int64 {
  134. if nodeName == "kube-01" {
  135. return 10
  136. }
  137. return 0
  138. }