AjayTripathy 7 лет назад
Родитель
Сommit
50006ab763
1 измененных файлов с 69 добавлено и 53 удалено
  1. 69 53
      costmodel/costmodel.go

+ 69 - 53
costmodel/costmodel.go

@@ -39,25 +39,26 @@ const (
 )
 
 type CostData struct {
-	Name          string                  `json:"name"`
-	PodName       string                  `json:"podName"`
-	NodeName      string                  `json:"nodeName"`
-	NodeData      *costAnalyzerCloud.Node `json:"node"`
-	Namespace     string                  `json:"namespace"`
-	Deployments   []string                `json:"deployments"`
-	Services      []string                `json:"services"`
-	Daemonsets    []string                `json:"daemonsets"`
-	Statefulsets  []string                `json:"statefulsets"`
-	Jobs          []string                `json:"jobs"`
-	RAMReq        []*Vector               `json:"ramreq"`
-	RAMUsed       []*Vector               `json:"ramused"`
-	CPUReq        []*Vector               `json:"cpureq"`
-	CPUUsed       []*Vector               `json:"cpuused"`
-	RAMAllocation []*Vector               `json:"ramallocated"`
-	CPUAllocation []*Vector               `json:"cpuallocated"`
-	GPUReq        []*Vector               `json:"gpureq"`
-	PVData        []*PersistentVolumeData `json:"pvData"`
-	Labels        map[string]string       `json:"labels"`
+	Name            string                  `json:"name"`
+	PodName         string                  `json:"podName"`
+	NodeName        string                  `json:"nodeName"`
+	NodeData        *costAnalyzerCloud.Node `json:"node"`
+	Namespace       string                  `json:"namespace"`
+	Deployments     []string                `json:"deployments"`
+	Services        []string                `json:"services"`
+	Daemonsets      []string                `json:"daemonsets"`
+	Statefulsets    []string                `json:"statefulsets"`
+	Jobs            []string                `json:"jobs"`
+	RAMReq          []*Vector               `json:"ramreq"`
+	RAMUsed         []*Vector               `json:"ramused"`
+	CPUReq          []*Vector               `json:"cpureq"`
+	CPUUsed         []*Vector               `json:"cpuused"`
+	RAMAllocation   []*Vector               `json:"ramallocated"`
+	CPUAllocation   []*Vector               `json:"cpuallocated"`
+	GPUReq          []*Vector               `json:"gpureq"`
+	PVData          []*PersistentVolumeData `json:"pvData"`
+	Labels          map[string]string       `json:"labels"`
+	NamespaceLabels map[string]string       `json:"namespaceLabels"`
 }
 
 type Vector struct {
@@ -193,6 +194,13 @@ func ComputeCostData(cli prometheusClient.Client, clientset kubernetes.Interface
 		if pod, ok := currentContainers[key]; ok {
 			podName := pod.GetObjectMeta().GetName()
 			ns := pod.GetObjectMeta().GetNamespace()
+
+			nsInfo, err := clientset.CoreV1().Namespaces().Get(ns, metav1.GetOptions{})
+			if err != nil {
+				return nil, err
+			}
+			nsLabels := nsInfo.Labels
+
 			podLabels := pod.GetObjectMeta().GetLabels()
 			nodeName := pod.Spec.NodeName
 			var nodeData *costAnalyzerCloud.Node
@@ -266,23 +274,24 @@ func ComputeCostData(cli prometheusClient.Client, clientset kubernetes.Interface
 				}
 
 				costs := &CostData{
-					Name:         containerName,
-					PodName:      podName,
-					NodeName:     nodeName,
-					Namespace:    ns,
-					Deployments:  podDeployments,
-					Services:     podServices,
-					Daemonsets:   getDaemonsetsOfPod(pod),
-					Jobs:         getJobsOfPod(pod),
-					Statefulsets: getStatefulSetsOfPod(pod),
-					NodeData:     nodeData,
-					RAMReq:       RAMReqV,
-					RAMUsed:      RAMUsedV,
-					CPUReq:       CPUReqV,
-					CPUUsed:      CPUUsedV,
-					GPUReq:       GPUReqV,
-					PVData:       pvReq,
-					Labels:       podLabels,
+					Name:            containerName,
+					PodName:         podName,
+					NodeName:        nodeName,
+					Namespace:       ns,
+					Deployments:     podDeployments,
+					Services:        podServices,
+					Daemonsets:      getDaemonsetsOfPod(pod),
+					Jobs:            getJobsOfPod(pod),
+					Statefulsets:    getStatefulSetsOfPod(pod),
+					NodeData:        nodeData,
+					RAMReq:          RAMReqV,
+					RAMUsed:         RAMUsedV,
+					CPUReq:          CPUReqV,
+					CPUUsed:         CPUUsedV,
+					GPUReq:          GPUReqV,
+					PVData:          pvReq,
+					Labels:          podLabels,
+					NamespaceLabels: nsLabels,
 				}
 				costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
 				costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)
@@ -687,6 +696,12 @@ func ComputeCostDataRange(cli prometheusClient.Client, clientset kubernetes.Inte
 				}
 			}
 
+			nsInfo, err := clientset.CoreV1().Namespaces().Get(ns, metav1.GetOptions{})
+			if err != nil {
+				return nil, err
+			}
+			nsLabels := nsInfo.Labels
+
 			for i, container := range pod.Spec.Containers {
 				containerName := container.Name
 
@@ -724,23 +739,24 @@ func ComputeCostDataRange(cli prometheusClient.Client, clientset kubernetes.Inte
 				}
 
 				costs := &CostData{
-					Name:         containerName,
-					PodName:      podName,
-					NodeName:     nodeName,
-					Namespace:    ns,
-					Deployments:  podDeployments,
-					Services:     podServices,
-					Daemonsets:   getDaemonsetsOfPod(pod),
-					Jobs:         getJobsOfPod(pod),
-					Statefulsets: getStatefulSetsOfPod(pod),
-					NodeData:     nodeData,
-					RAMReq:       RAMReqV,
-					RAMUsed:      RAMUsedV,
-					CPUReq:       CPUReqV,
-					CPUUsed:      CPUUsedV,
-					GPUReq:       GPUReqV,
-					PVData:       pvReq,
-					Labels:       podLabels,
+					Name:            containerName,
+					PodName:         podName,
+					NodeName:        nodeName,
+					Namespace:       ns,
+					Deployments:     podDeployments,
+					Services:        podServices,
+					Daemonsets:      getDaemonsetsOfPod(pod),
+					Jobs:            getJobsOfPod(pod),
+					Statefulsets:    getStatefulSetsOfPod(pod),
+					NodeData:        nodeData,
+					RAMReq:          RAMReqV,
+					RAMUsed:         RAMUsedV,
+					CPUReq:          CPUReqV,
+					CPUUsed:         CPUUsedV,
+					GPUReq:          GPUReqV,
+					PVData:          pvReq,
+					Labels:          podLabels,
+					NamespaceLabels: nsLabels,
 				}
 				costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
 				costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)