Просмотр исходного кода

fixes build issues, adds allocation metrics

AjayTripathy 7 лет назад
Родитель
Сommit
c43dec36e8
5 измененных файлов с 37 добавлено и 2 удалено
  1. 5 0
      Dockerfile
  2. 3 0
      costmodel/costmodel.go
  3. 2 0
      go.sum
  4. 27 1
      main.go
  5. 0 1
      test/costmodel_test.go

+ 5 - 0
Dockerfile

@@ -5,6 +5,11 @@ WORKDIR /app
 COPY go.mod .
 COPY go.sum .
 
+RUN mkdir ./test
+RUN mkdir ./test/mocks
+COPY ./test/mocks/go.mod ./test/mocks/go.mod
+COPY ./test/mocks/go.sum ./test/mocks/go.sum
+
 # Get dependencies - will also be cached if we won't change mod/sum
 RUN go mod download
 # COPY the source code as the last step

+ 3 - 0
costmodel/costmodel.go

@@ -355,6 +355,9 @@ func getContainerAllocation(req []*Vector, used []*Vector) []*Vector {
 	}
 	usedMap := make(map[float64]float64)
 	for _, usedV := range used {
+		if usedV.Timestamp == 0 {
+			continue
+		}
 		usedV.Timestamp = math.Round(usedV.Timestamp/10) * 10
 		usedMap[usedV.Timestamp] = usedV.Value
 		if _, ok := reqMap[usedV.Timestamp]; !ok { // no need to double add, since we'll range over sorted timestamps and check.

+ 2 - 0
go.sum

@@ -164,6 +164,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db h1:6/JqlYfC1CCaLnGceQTI+sDGhC9UBSPAsBqI0Gun6kU=
 golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/time v0.0.0-20161028155119-f51c12702a4d h1:TnM+PKb3ylGmZvyPXmo9m/wktg7Jn/a/fNmr33HSj8g=
 golang.org/x/time v0.0.0-20161028155119-f51c12702a4d/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
 golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -219,6 +220,7 @@ k8s.io/klog v0.0.0-20190306015804-8e90cee79f82/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUc
 k8s.io/klog v0.3.0 h1:0VPpR+sizsiivjIfIAQH/rl8tan6jvWkS7lU+0di3lE=
 k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
 k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
+k8s.io/utils v0.0.0-20190221042446-c2654d5206da h1:ElyM7RPonbKnQqOcw7dG2IK5uvQQn3b/WPHqD5mBvP4=
 k8s.io/utils v0.0.0-20190221042446-c2654d5206da/go.mod h1:8k8uAuAQ0rXslZKaEWd0c3oVhZz7sSzSiPnVZayjIX0=
 k8s.io/utils v0.0.0-20190308190857-21c4ce38f2a7 h1:8r+l4bNWjRlsFYlQJnKJ2p7s1YQPj4XyXiJVqDHRx7c=
 k8s.io/utils v0.0.0-20190308190857-21c4ce38f2a7/go.mod h1:8k8uAuAQ0rXslZKaEWd0c3oVhZz7sSzSiPnVZayjIX0=

+ 27 - 1
main.go

@@ -32,6 +32,8 @@ type Accesses struct {
 	CPUPriceRecorder       *prometheus.GaugeVec
 	RAMPriceRecorder       *prometheus.GaugeVec
 	NodeTotalPriceRecorder *prometheus.GaugeVec
+	RAMAllocationRecorder  *prometheus.GaugeVec
+	CPUAllocationRecorder  *prometheus.GaugeVec
 }
 
 type DataEnvelope struct {
@@ -103,7 +105,7 @@ func (a *Accesses) recordPrices() {
 	go func() {
 		for {
 			klog.V(3).Info("Recording prices...")
-			data, err := costModel.ComputeCostData(a.PrometheusClient, a.KubeClientSet, a.Cloud, "1h")
+			data, err := costModel.ComputeCostData(a.PrometheusClient, a.KubeClientSet, a.Cloud, "1m")
 			if err != nil {
 				klog.V(1).Info("Error in price recording: " + err.Error())
 				// zero the for loop so the time.Sleep will still work
@@ -126,6 +128,16 @@ func (a *Accesses) recordPrices() {
 				a.CPUPriceRecorder.WithLabelValues(nodeName).Set(cpuCost)
 				a.RAMPriceRecorder.WithLabelValues(nodeName).Set(ramCost)
 				a.NodeTotalPriceRecorder.WithLabelValues(nodeName).Set(totalCost)
+
+				namespace := costs.Namespace
+				podName := costs.PodName
+				containerName := costs.Name
+				if len(costs.RAMAllocation) > 0 {
+					a.RAMAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName).Set(costs.RAMAllocation[0].Value)
+				}
+				if len(costs.CPUAllocation) > 0 {
+					a.CPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName).Set(costs.CPUAllocation[0].Value)
+				}
 			}
 			time.Sleep(time.Minute)
 		}
@@ -185,9 +197,21 @@ func main() {
 		Help: "node_total_hourly_cost Total node cost per hour",
 	}, []string{"instance"})
 
+	RAMAllocation := prometheus.NewGaugeVec(prometheus.GaugeOpts{
+		Name: "container_memory_allocation_bytes",
+		Help: "container_memory_allocation_bytes Bytes of RAM used",
+	}, []string{"namespace", "pod", "container", "instance"})
+
+	CPUAllocation := prometheus.NewGaugeVec(prometheus.GaugeOpts{
+		Name: "container_cpu_allocation",
+		Help: "container_cpu_allocation Percent of a single CPU used in a minute",
+	}, []string{"namespace", "pod", "container", "instance"})
+
 	prometheus.MustRegister(cpuGv)
 	prometheus.MustRegister(ramGv)
 	prometheus.MustRegister(totalGv)
+	prometheus.MustRegister(RAMAllocation)
+	prometheus.MustRegister(CPUAllocation)
 
 	a := Accesses{
 		PrometheusClient:       promCli,
@@ -196,6 +220,8 @@ func main() {
 		CPUPriceRecorder:       cpuGv,
 		RAMPriceRecorder:       ramGv,
 		NodeTotalPriceRecorder: totalGv,
+		RAMAllocationRecorder:  RAMAllocation,
+		CPUAllocationRecorder:  CPUAllocation,
 	}
 
 	err = a.Cloud.DownloadPricingData()

+ 0 - 1
test/costmodel_test.go

@@ -12,7 +12,6 @@ import (
 func TestCostModel(t *testing.T) {
 	ctrl := gomock.NewController(t)
 
-	// Assert that Bar() is invoked.
 	defer ctrl.Finish()
 
 	u, _ := url.Parse("http://localhost:9003")