123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package noderesources
- import (
- "context"
- "database/sql"
- "fmt"
- _ "github.com/go-sql-driver/mysql"
- v1 "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/klog"
- framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
- "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
- )
- type CustomAllocated struct {
- handle framework.FrameworkHandle
- resourceAllocationScorer
-
- }
- type nodeMetrics struct {
- L3cacheMisses int64
- memoryReads int64
- memoryWrites int64
- averageIPC float32
- }
- type System struct {
- ID int `json:"id"`
- Uuid string `json:"uuid"`
- numSockets int `json:"num_sockets"`
- numCores int `json:"num_cores`
- }
- type wrongValueError struct{}
- var _ = framework.ScorePlugin(&CustomAllocated{})
- var nodes = map[string]string{
- "kube-01": "c4766d29-4dc1-11ea-9d98-0242ac110002",
- "kube-02": "2",
- "kube-03": "2",
- "kube-04": "2",
- }
- const CustomAllocatedName = "NodeResourcesCustomAllocated"
- func (ca *CustomAllocated) Name() string {
- return CustomAllocatedName
- }
- func getCustomMetrics(name string) *nodeMetrics {
-
- db, err := sql.Open("mysql", "client:password@tcp(147.102.37.161:3306)/evolve")
- if err != nil {
- panic(err.Error())
- }
- defer db.Close()
- curr_uuid, ok := nodes[name]
- nd := nodeMetrics{}
- if ok {
- results, err := db.Query("SELECT id, num_sockets, num_cores FROM systems WHERE uuid = ?", curr_uuid)
- if err != nil {
- panic(err.Error())
- }
- sys := System{}
- sys.Uuid = curr_uuid
- for results.Next() {
-
- err = results.Scan(&sys.ID, &sys.numSockets, &sys.numCores)
- if err != nil {
- panic(err.Error())
- }
-
- klog.Infof("This is the system with name: %s, id: %d and number of cores: %d", name, sys.ID, sys.numCores)
- }
- }
-
- if name == "kube-01" {
- nd.L3cacheMisses = 1
- nd.memoryReads = 1
- nd.memoryWrites = 1
- nd.averageIPC = 2.0
- } else {
-
- nd.L3cacheMisses = 200
- nd.memoryReads = 50
- nd.memoryWrites = 60
- nd.averageIPC = 1.2
- }
-
- if nd.L3cacheMisses < 0 || nd.memoryReads < 0 || nd.memoryWrites < 0 || nd.averageIPC < 0 {
-
- return nil
- }
- return &nd
- }
- func (ca *CustomAllocated) scoringFunction(pod *v1.Pod, node *nodeMetrics, nodeInfo *nodeinfo.NodeInfo) int64 {
-
- intScore := int64(float32(node.memoryReads+node.memoryWrites) / node.averageIPC)
-
- klog.Infof("Score: %d", intScore)
- return intScore
- }
- func (ca *CustomAllocated) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
- nodeInfo, err := ca.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
- if err != nil {
- return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
- }
-
- nodeMetrics := getCustomMetrics(nodeName)
-
-
-
-
-
-
- klog.Infof("Node: %s", nodeName)
- return ca.scoringFunction(pod, nodeMetrics, nodeInfo), nil
-
- }
- func (ca *CustomAllocated) ScoreExtensions() framework.ScoreExtensions {
- return nil
- }
- func NewCustomAllocated(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
- return &CustomAllocated{
- handle: h,
- resourceAllocationScorer: resourceAllocationScorer{
- CustomAllocatedName,
- customResourceScorer,
- defaultRequestedRatioResources,
- },
- }, nil
- }
- func customResourceScorer(requested, allocable resourceToValueMap, includeVolumes bool, requestedVolumes int, allocatableVolumes int) int64 {
-
- var nodeScore, weightSum int64
- for resource, weight := range defaultRequestedRatioResources {
- resourceScore := customRequestedScore(requested[resource], allocable[resource])
- nodeScore += resourceScore * weight
- weightSum += weight
- }
- return nodeScore / weightSum
- }
- func customRequestedScore(requested, capacity int64) int64 {
- if capacity == 0 {
- return 0
- }
- if requested > capacity {
- return 0
- }
- return ((capacity - requested) * int64(framework.MaxNodeScore)) / capacity
- }
|