func addPodInfo(key string, podMs *core.MetricSet, pod *kube_api.Pod, batch *core.DataBatch, newMs map[string]*core.MetricSet) { // Add UID to pod podMs.Labels[core.LabelPodId.Key] = string(pod.UID) podMs.Labels[core.LabelLabels.Key] = util.LabelsToString(pod.Labels, ",") // Add cpu/mem requests and limits to containers for _, container := range pod.Spec.Containers { containerKey := core.PodContainerKey(pod.Namespace, pod.Name, container.Name) if _, found := batch.MetricSets[containerKey]; !found { if _, found := newMs[containerKey]; !found { glog.V(2).Infof("Container %s not found, creating a stub", containerKey) containerMs := &core.MetricSet{ MetricValues: make(map[string]core.MetricValue), Labels: map[string]string{ core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, core.LabelNamespaceName.Key: pod.Namespace, core.LabelPodNamespace.Key: pod.Namespace, core.LabelPodName.Key: pod.Name, core.LabelContainerName.Key: container.Name, core.LabelContainerBaseImage.Key: container.Image, core.LabelPodId.Key: string(pod.UID), core.LabelLabels.Key: util.LabelsToString(pod.Labels, ","), core.LabelNodename.Key: podMs.Labels[core.LabelNodename.Key], core.LabelHostname.Key: podMs.Labels[core.LabelHostname.Key], core.LabelHostID.Key: podMs.Labels[core.LabelHostID.Key], }, } updateContainerResourcesAndLimits(containerMs, container) newMs[containerKey] = containerMs } } } }
func (this *NodeAutoscalingEnricher) Process(batch *core.DataBatch) (*core.DataBatch, error) { nodes, err := this.nodeLister.List() if err != nil { return nil, err } for _, node := range nodes.Items { if metricSet, found := batch.MetricSets[core.NodeKey(node.Name)]; found { metricSet.Labels[core.LabelLabels.Key] = util.LabelsToString(node.Labels, ",") availableCpu, _ := node.Status.Capacity[kube_api.ResourceCPU] availableMem, _ := node.Status.Capacity[kube_api.ResourceMemory] cpuRequested := getInt(metricSet, &core.MetricCpuRequest) cpuUsed := getInt(metricSet, &core.MetricCpuUsageRate) memRequested := getInt(metricSet, &core.MetricMemoryRequest) memUsed := getInt(metricSet, &core.MetricMemoryUsage) if availableCpu.MilliValue() != 0 { setFloat(metricSet, &core.MetricNodeCpuUtilization, float32(cpuUsed)/float32(availableCpu.MilliValue())) setFloat(metricSet, &core.MetricNodeCpuReservation, float32(cpuRequested)/float32(availableCpu.MilliValue())) setFloat(metricSet, &core.MetricNodeCpuCapacity, float32(availableCpu.MilliValue())) } if availableMem.Value() != 0 { setFloat(metricSet, &core.MetricNodeMemoryUtilization, float32(memUsed)/float32(availableMem.Value())) setFloat(metricSet, &core.MetricNodeMemoryReservation, float32(memRequested)/float32(availableMem.Value())) setFloat(metricSet, &core.MetricNodeMemoryCapacity, float32(availableMem.Value())) } } } return batch, nil }
func addContainerInfo(key string, containerMs *core.MetricSet, pod *kube_api.Pod, batch *core.DataBatch, newMs map[string]*core.MetricSet) { for _, container := range pod.Spec.Containers { if key == core.PodContainerKey(pod.Namespace, pod.Name, container.Name) { updateContainerResourcesAndLimits(containerMs, container) if _, ok := containerMs.Labels[core.LabelContainerBaseImage.Key]; !ok { containerMs.Labels[core.LabelContainerBaseImage.Key] = container.Image } break } } containerMs.Labels[core.LabelPodId.Key] = string(pod.UID) containerMs.Labels[core.LabelLabels.Key] = util.LabelsToString(pod.Labels, ",") namespace := containerMs.Labels[core.LabelNamespaceName.Key] podName := containerMs.Labels[core.LabelPodName.Key] podKey := core.PodKey(namespace, podName) _, oldfound := batch.MetricSets[podKey] if !oldfound { _, newfound := newMs[podKey] if !newfound { glog.V(2).Infof("Pod %s not found, creating a stub", podKey) podMs := &core.MetricSet{ MetricValues: make(map[string]core.MetricValue), Labels: map[string]string{ core.LabelMetricSetType.Key: core.MetricSetTypePod, core.LabelNamespaceName.Key: namespace, core.LabelPodNamespace.Key: namespace, core.LabelPodName.Key: podName, core.LabelNodename.Key: containerMs.Labels[core.LabelNodename.Key], core.LabelHostname.Key: containerMs.Labels[core.LabelHostname.Key], core.LabelHostID.Key: containerMs.Labels[core.LabelHostID.Key], }, } newMs[podKey] = podMs addPodInfo(podKey, podMs, pod, batch, newMs) } } }