custom_resource_allocation.go 5.1 KB

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