Achilleas Tzenetopoulos 5 years ago
parent
commit
205fed772f

+ 5 - 91
kubernetes-v1.15.4/pkg/scheduler/algorithm/priorities/custom_resource_allocation.go

@@ -1,5 +1,5 @@
 /*
-Copyright 2016 The Kubernetes Authors.
+Copyright 2020 Achilleas Tzenetopoulos.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -19,12 +19,10 @@ package priorities
 import (
 	"encoding/json"
 	"fmt"
-	"os"
 	"strings"
 
 	_ "github.com/go-sql-driver/mysql"
 	client "github.com/influxdata/influxdb1-client/v2"
-	"gopkg.in/yaml.v2"
 	"k8s.io/klog"
 )
 
@@ -40,78 +38,6 @@ var (
 	CustomRequestedPriorityMap = customResourcePriority.PriorityMap
 )
 
-type Config struct {
-	Server struct {
-		Port string `yaml:"port"`
-		Host string `yaml:"host"`
-	} `yaml:"server"`
-	Database struct {
-		Type     string `yaml:"type"`
-		Name     string `yaml:"name"`
-		Username string `yaml:"username"`
-		Password string `yaml:"password"`
-	} `yaml:"database"`
-	MonitoringSpecs struct {
-		TimeInterval float32 `yaml:"interval"`
-	} `yaml:"monitoring"`
-}
-
-// type Row struct {
-// 	ipc    float32
-// 	l3m    float32
-// 	reads  float32
-// 	writes float32
-// 	c6res  float32
-// }
-
-// type System struct {
-// 	ID         int    `json:"id"`
-// 	Uuid       string `json:"uuid"`
-// 	numSockets int    `json:"num_sockets"`
-// 	numCores   int    `json:"num_cores`
-// }
-
-// TODO:
-// place those maps in another file inside the package
-var nodes = map[string]string{
-	"kube-01": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-02": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-03": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-04": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-05": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-06": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-07": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-	"kube-08": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-}
-
-var sockets = map[string]int{
-	"kube-01": 0,
-	"kube-02": 0,
-	"kube-03": 0,
-	"kube-04": 0,
-	"kube-05": 0,
-	"kube-06": 1,
-	"kube-07": 0,
-	"kube-08": 1,
-}
-
-func readFile(cfg *Config, file string) error {
-	f, err := os.Open(file)
-	if err != nil {
-		klog.Infof("Config file for scheduler not found. Error: %v", err)
-		return err
-	}
-	defer f.Close()
-
-	decoder := yaml.NewDecoder(f)
-	err = decoder.Decode(&cfg)
-	if err != nil {
-		klog.Infof("Unable to decode the config file. Error: %v", err)
-		return err
-	}
-	return nil
-}
-
 func customScoreFn(si scorerInput) float64 {
 	return si.metrics["ipc"] / si.metrics["mem_read"] * si.metrics["mem_write"]
 }
@@ -152,25 +78,12 @@ func calculateWeightedAverage(response *client.Response,
 			metrics[rows.Columns[i]] += val * float64(numberOfRows-j)
 		}
 		metrics[rows.Columns[i]] = metrics[rows.Columns[i]] / float64((numberOfRows * (numberOfRows + 1) / 2))
-		klog.Infof("%v : %v", rows.Columns[i], metrics[rows.Columns[i]])
+		//klog.Infof("%v : %v", rows.Columns[i], metrics[rows.Columns[i]])
 	}
 	// TODO better handling for the returning errors
 	return metrics, nil
 }
 
-func connectToInfluxDB(cfg Config) (client.Client, error) {
-	c, err := client.NewHTTPClient(client.HTTPConfig{
-		Addr: "http://" + cfg.Server.Host + ":" + cfg.Server.Port + "",
-	})
-	if err != nil {
-		klog.Infof("Error while connecting to InfluxDB: %v ", err.Error())
-		return nil, err
-	}
-	klog.Infof("Connected Successfully to InfluxDB")
-	return c, nil
-
-}
-
 func queryInfluxDB(metrics []string, uuid string, socket,
 	time int, cfg Config, c client.Client) (map[string]float64, error) {
 
@@ -180,8 +93,9 @@ func queryInfluxDB(metrics []string, uuid string, socket,
 	// merge all the required columns
 	columns := strings.Join(metrics, ", ")
 	// build the coommand
-	command := fmt.Sprintf("SELECT %s from socket_metrics where uuid = '%s' and socket_id='%d' order by time desc limit %d", columns, uuid, socket, numberOfRows)
-	q := client.NewQuery(command, cfg.Database.Name, "")
+	var command strings.Builder
+	fmt.Fprintf(&command, "SELECT %s from socket_metrics where uuid = '%s' and socket_id='%d' order by time desc limit %d", columns, uuid, socket, numberOfRows)
+	q := client.NewQuery(command.String(), cfg.Database.Name, "")
 	response, err := c.Query(q)
 	if err != nil {
 		klog.Infof("Error while executing the query: %v", err.Error())

+ 93 - 0
kubernetes-v1.15.4/pkg/scheduler/algorithm/priorities/infrastructure.go

@@ -0,0 +1,93 @@
+package priorities
+
+import (
+	"os"
+
+	client "github.com/influxdata/influxdb1-client/v2"
+	"gopkg.in/yaml.v2"
+	"k8s.io/klog"
+)
+
+type scorerInput struct {
+	metricName string
+	metrics    map[string]float64
+}
+
+type Config struct {
+	Server struct {
+		Port string `yaml:"port"`
+		Host string `yaml:"host"`
+	} `yaml:"server"`
+	Database struct {
+		Type     string `yaml:"type"`
+		Name     string `yaml:"name"`
+		Username string `yaml:"username"`
+		Password string `yaml:"password"`
+	} `yaml:"database"`
+	MonitoringSpecs struct {
+		TimeInterval float32 `yaml:"interval"`
+	} `yaml:"monitoring"`
+}
+
+var nodes = map[string]string{
+	"kube-01": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-02": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-03": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-04": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-05": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-06": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-07": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+	"kube-08": "c4766d29-4dc1-11ea-9d98-0242ac110002",
+}
+
+var sockets = map[string]int{
+	"kube-01": 0,
+	"kube-02": 0,
+	"kube-03": 0,
+	"kube-04": 0,
+	"kube-05": 0,
+	"kube-06": 1,
+	"kube-07": 0,
+	"kube-08": 1,
+}
+
+var cores = map[string][]int{
+	"kube-01": []int{},
+	"kube-02": []int{},
+	"kube-03": []int{},
+	"kube-04": []int{},
+	"kube-05": []int{0, 1, 2, 3},
+	"kube-06": []int{12, 13, 14, 15, 16, 17, 18, 19},
+	"kube-07": []int{4, 5, 6, 7, 8, 9, 10, 11, 24, 25, 26, 27, 28, 29, 30, 31},
+	"kube-08": []int{20, 21, 22, 23, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47},
+}
+
+func readFile(cfg *Config, file string) error {
+	f, err := os.Open(file)
+	if err != nil {
+		klog.Infof("Config file for scheduler not found. Error: %v", err)
+		return err
+	}
+	defer f.Close()
+
+	decoder := yaml.NewDecoder(f)
+	err = decoder.Decode(&cfg)
+	if err != nil {
+		klog.Infof("Unable to decode the config file. Error: %v", err)
+		return err
+	}
+	return nil
+}
+
+func connectToInfluxDB(cfg Config) (client.Client, error) {
+	c, err := client.NewHTTPClient(client.HTTPConfig{
+		Addr: "http://" + cfg.Server.Host + ":" + cfg.Server.Port + "",
+	})
+	if err != nil {
+		klog.Infof("Error while connecting to InfluxDB: %v ", err.Error())
+		return nil, err
+	}
+	klog.Infof("Connected Successfully to InfluxDB")
+	return c, nil
+
+}

+ 4 - 128
kubernetes-v1.15.4/pkg/scheduler/algorithm/priorities/node_selection.go

@@ -1,5 +1,5 @@
 /*
-Copyright 2016 The Kubernetes Authors.
+Copyright 2020 Achilleas Tzenetopoulos.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -38,128 +38,17 @@ var (
 	NodeSelectionPriorityMap = nodeSelectionPriority.PriorityMap
 )
 
-// type Config struct {
-// 	Server struct {
-// 		Port string `yaml:"port"`
-// 		Host string `yaml:"host"`
-// 	} `yaml:"server"`
-// 	Database struct {
-// 		Type     string `yaml:"type"`
-// 		Name     string `yaml:"name"`
-// 		Username string `yaml:"username"`
-// 		Password string `yaml:"password"`
-// 	} `yaml:"database"`
-// 	MonitoringSpecs struct {
-// 		TimeInterval float32 `yaml:"interval"`
-// 	} `yaml:"monitoring"`
-// }
-
-// type Row struct {
-// 	ipc    float32
-// 	l3m    float32
-// 	reads  float32
-// 	writes float32
-// 	c6res  float32
-// }
-
-// type System struct {
-// 	ID         int    `json:"id"`
-// 	Uuid       string `json:"uuid"`
-// 	numSockets int    `json:"num_sockets"`
-// 	numCores   int    `json:"num_cores`
-// }
-
-// var nodes = map[string]string{
-// 	"kube-01": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-02": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-03": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-04": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-05": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-06": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-07": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// 	"kube-08": "c4766d29-4dc1-11ea-9d98-0242ac110002",
-// }
-
-// var sockets = map[string]int{
-// 	"kube-01": 0,
-// 	"kube-02": 0,
-// 	"kube-03": 0,
-// 	"kube-04": 0,
-// 	"kube-05": 0,
-// 	"kube-06": 1,
-// 	"kube-07": 0,
-// 	"kube-08": 1,
-// }
-
-var cores = map[string][]int{
-	"kube-01": []int{},
-	"kube-02": []int{},
-	"kube-03": []int{},
-	"kube-04": []int{},
-	"kube-05": []int{0, 1, 2, 3},
-	"kube-06": []int{12, 13, 14, 15, 16, 17, 18, 19},
-	"kube-07": []int{4, 5, 6, 7, 8, 9, 10, 11, 24, 25, 26, 27, 28, 29, 30, 31},
-	"kube-08": []int{20, 21, 22, 23, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47},
-}
-
-// func readFile(cfg *Config, file string) error {
-// 	f, err := os.Open(file)
-// 	if err != nil {
-// 		klog.Infof("Config file for scheduler not found. Error: %v", err)
-// 		return err
-// 	}
-// 	defer f.Close()
-
-// 	decoder := yaml.NewDecoder(f)
-// 	err = decoder.Decode(&cfg)
-// 	if err != nil {
-// 		klog.Infof("Unable to decode the config file. Error: %v", err)
-// 		return err
-// 	}
-// 	return nil
-// }
-
-type scorerInput struct {
-	metricName string
-	metrics    map[string]float64
-}
-
 func OneScorer(si scorerInput) float64 {
 	return 100 - si.metrics[si.metricName]
 }
 
-// func customScoreFn(metrics map[string]float64) float64 {
-// 	return metrics["ipc"] / metrics["mem_read"] * metrics["mem_write"]
-// }
-
-// func onlyIPC(metrics map[string]float64) float64 {
-// 	return metrics["ipc"]
-// }
-
-// func onlyL3(metrics map[string]float64) float64 {
-// 	return 1 / metrics["l3m"]
-// }
-
-// func onlyNrg(metrics map[string]float64) float64 {
-// 	return 1 / metrics["procnrg"]
-// }
-
-// func calculateScore(results map[string]float64,
-// 	logicFn func(map[string]float64) float64) float64 {
-
-// 	res := logicFn(results)
-// 	//klog.Infof("Has score (in float) %v\n", res)
-
-// 	return res
-// }
-
 func calculateWeightedAverageCores(response *client.Response,
 	numberOfRows, numberOfMetrics, numberOfCores int) (map[string]float64, error) {
 	// initialize the metrics map with a constant size
 	metrics := make(map[string]float64, numberOfMetrics)
 	rows := response.Results[0].Series[0]
 	for i := 1; i < len(rows.Columns); i++ {
-		klog.Infof("Name of column %v : %v\nrange of values: %v\nnumber of rows: %v\nnumber of cores %v\n", i, rows.Columns[i], len(rows.Values), numberOfRows, numberOfCores)
+		//klog.Infof("Name of column %v : %v\nrange of values: %v\nnumber of rows: %v\nnumber of cores %v\n", i, rows.Columns[i], len(rows.Values), numberOfRows, numberOfCores)
 		for j := 0; j < numberOfRows; j++ {
 			avg := 0.0
 			for k := 0; k < numberOfCores; k++ {
@@ -174,25 +63,12 @@ func calculateWeightedAverageCores(response *client.Response,
 			metrics[rows.Columns[i]] += avg * float64(numberOfRows-j)
 		}
 		metrics[rows.Columns[i]] = metrics[rows.Columns[i]] / float64((numberOfRows * (numberOfRows + 1) / 2))
-		klog.Infof("%v : %v", rows.Columns[i], metrics[rows.Columns[i]])
+		//klog.Infof("%v : %v", rows.Columns[i], metrics[rows.Columns[i]])
 	}
 	// TODO better handling for the returning errors
 	return metrics, nil
 }
 
-// func connectToInfluxDB(cfg Config) (client.Client, error) {
-// 	c, err := client.NewHTTPClient(client.HTTPConfig{
-// 		Addr: "http://" + cfg.Server.Host + ":" + cfg.Server.Port + "",
-// 	})
-// 	if err != nil {
-// 		klog.Infof("Error while connecting to InfluxDB: %v ", err.Error())
-// 		return nil, err
-// 	}
-// 	klog.Infof("Connected Successfully to InfluxDB")
-// 	return c, nil
-
-// }
-
 // This function does the following:
 // 1. Queries the DB with the provided metrics and cores
 // 2. Calculates and returns the weighted average of each of those metrics
@@ -274,7 +150,7 @@ func nodeSelectionScorer(nodeName string) (float64, error) {
 
 		// Select Node
 
-		klog.Infof("Node name %s, has score %v\n", nodeName, res)
+		klog.Infof("Node name %s, Score %v\n", nodeName, res)
 		return res, nil
 	} else {
 		klog.Infof("Error finding the uuid: %v", ok)