github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/processors/node_autoscaling_enricher.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package processors 16 17 import ( 18 kube_config "k8s.io/heapster/common/kubernetes" 19 "k8s.io/heapster/metrics/core" 20 "k8s.io/heapster/metrics/util" 21 kube_api "k8s.io/kubernetes/pkg/api" 22 "k8s.io/kubernetes/pkg/client/cache" 23 kube_client "k8s.io/kubernetes/pkg/client/unversioned" 24 "net/url" 25 ) 26 27 type NodeAutoscalingEnricher struct { 28 nodeLister *cache.StoreToNodeLister 29 reflector *cache.Reflector 30 } 31 32 func (this *NodeAutoscalingEnricher) Name() string { 33 return "node_autoscaling_enricher" 34 } 35 36 func (this *NodeAutoscalingEnricher) Process(batch *core.DataBatch) (*core.DataBatch, error) { 37 nodes, err := this.nodeLister.List() 38 if err != nil { 39 return nil, err 40 } 41 for _, node := range nodes.Items { 42 if metricSet, found := batch.MetricSets[core.NodeKey(node.Name)]; found { 43 metricSet.Labels[core.LabelLabels.Key] = util.LabelsToString(node.Labels) 44 capacityCpu, _ := node.Status.Capacity[kube_api.ResourceCPU] 45 capacityMem, _ := node.Status.Capacity[kube_api.ResourceMemory] 46 allocatableCpu, _ := node.Status.Allocatable[kube_api.ResourceCPU] 47 allocatableMem, _ := node.Status.Allocatable[kube_api.ResourceMemory] 48 49 cpuRequested := getInt(metricSet, &core.MetricCpuRequest) 50 cpuUsed := getInt(metricSet, &core.MetricCpuUsageRate) 51 memRequested := getInt(metricSet, &core.MetricMemoryRequest) 52 memUsed := getInt(metricSet, &core.MetricMemoryUsage) 53 54 if allocatableCpu.MilliValue() != 0 { 55 setFloat(metricSet, &core.MetricNodeCpuUtilization, float32(cpuUsed)/float32(allocatableCpu.MilliValue())) 56 setFloat(metricSet, &core.MetricNodeCpuReservation, float32(cpuRequested)/float32(allocatableCpu.MilliValue())) 57 } 58 setFloat(metricSet, &core.MetricNodeCpuCapacity, float32(capacityCpu.MilliValue())) 59 setFloat(metricSet, &core.MetricNodeCpuAllocatable, float32(allocatableCpu.MilliValue())) 60 61 if allocatableMem.Value() != 0 { 62 setFloat(metricSet, &core.MetricNodeMemoryUtilization, float32(memUsed)/float32(allocatableMem.Value())) 63 setFloat(metricSet, &core.MetricNodeMemoryReservation, float32(memRequested)/float32(allocatableMem.Value())) 64 } 65 setFloat(metricSet, &core.MetricNodeMemoryCapacity, float32(capacityMem.Value())) 66 setFloat(metricSet, &core.MetricNodeMemoryAllocatable, float32(allocatableMem.Value())) 67 } 68 } 69 return batch, nil 70 } 71 72 func getInt(metricSet *core.MetricSet, metric *core.Metric) int64 { 73 if value, found := metricSet.MetricValues[metric.MetricDescriptor.Name]; found { 74 return value.IntValue 75 } 76 return 0 77 } 78 79 func setFloat(metricSet *core.MetricSet, metric *core.Metric, value float32) { 80 metricSet.MetricValues[metric.MetricDescriptor.Name] = core.MetricValue{ 81 MetricType: core.MetricGauge, 82 ValueType: core.ValueFloat, 83 FloatValue: value, 84 } 85 } 86 87 func NewNodeAutoscalingEnricher(url *url.URL) (*NodeAutoscalingEnricher, error) { 88 kubeConfig, err := kube_config.GetKubeClientConfig(url) 89 if err != nil { 90 return nil, err 91 } 92 kubeClient := kube_client.NewOrDie(kubeConfig) 93 94 // watch nodes 95 nodeLister, reflector, _ := util.GetNodeLister(kubeClient) 96 97 return &NodeAutoscalingEnricher{ 98 nodeLister: nodeLister, 99 reflector: reflector, 100 }, nil 101 }