github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/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 "net/url" 19 20 kube_api "k8s.io/api/core/v1" 21 "k8s.io/apimachinery/pkg/labels" 22 kube_client "k8s.io/client-go/kubernetes" 23 v1listers "k8s.io/client-go/listers/core/v1" 24 "k8s.io/client-go/tools/cache" 25 kube_config "k8s.io/heapster/common/kubernetes" 26 "k8s.io/heapster/metrics/core" 27 "k8s.io/heapster/metrics/util" 28 ) 29 30 type NodeAutoscalingEnricher struct { 31 nodeLister v1listers.NodeLister 32 reflector *cache.Reflector 33 labelCopier *util.LabelCopier 34 } 35 36 func (this *NodeAutoscalingEnricher) Name() string { 37 return "node_autoscaling_enricher" 38 } 39 40 func (this *NodeAutoscalingEnricher) Process(batch *core.DataBatch) (*core.DataBatch, error) { 41 nodes, err := this.nodeLister.List(labels.Everything()) 42 if err != nil { 43 return nil, err 44 } 45 for _, node := range nodes { 46 if metricSet, found := batch.MetricSets[core.NodeKey(node.Name)]; found { 47 this.labelCopier.Copy(node.Labels, metricSet.Labels) 48 capacityCpu, _ := node.Status.Capacity[kube_api.ResourceCPU] 49 capacityMem, _ := node.Status.Capacity[kube_api.ResourceMemory] 50 capacityEphemeralStorage, storageExist := node.Status.Capacity[kube_api.ResourceEphemeralStorage] 51 allocatableCpu, _ := node.Status.Allocatable[kube_api.ResourceCPU] 52 allocatableMem, _ := node.Status.Allocatable[kube_api.ResourceMemory] 53 allocatableEphemeralStorage, allocatableStorageExist := node.Status.Allocatable[kube_api.ResourceEphemeralStorage] 54 55 cpuRequested := getInt(metricSet, &core.MetricCpuRequest) 56 cpuUsed := getInt(metricSet, &core.MetricCpuUsageRate) 57 memRequested := getInt(metricSet, &core.MetricMemoryRequest) 58 memUsed := getInt(metricSet, &core.MetricMemoryUsage) 59 epheRequested := getInt(metricSet, &core.MetricEphemeralStorageRequest) 60 epheUsed := getInt(metricSet, &core.MetricEphemeralStorageUsage) 61 62 if allocatableCpu.MilliValue() != 0 { 63 setFloat(metricSet, &core.MetricNodeCpuUtilization, float64(cpuUsed)/float64(allocatableCpu.MilliValue())) 64 setFloat(metricSet, &core.MetricNodeCpuReservation, float64(cpuRequested)/float64(allocatableCpu.MilliValue())) 65 } 66 setFloat(metricSet, &core.MetricNodeCpuCapacity, float64(capacityCpu.MilliValue())) 67 setFloat(metricSet, &core.MetricNodeCpuAllocatable, float64(allocatableCpu.MilliValue())) 68 69 if allocatableMem.Value() != 0 { 70 setFloat(metricSet, &core.MetricNodeMemoryUtilization, float64(memUsed)/float64(allocatableMem.Value())) 71 setFloat(metricSet, &core.MetricNodeMemoryReservation, float64(memRequested)/float64(allocatableMem.Value())) 72 } 73 setFloat(metricSet, &core.MetricNodeMemoryCapacity, float64(capacityMem.Value())) 74 setFloat(metricSet, &core.MetricNodeMemoryAllocatable, float64(allocatableMem.Value())) 75 76 if storageExist && allocatableStorageExist { 77 setFloat(metricSet, &core.MetricNodeEphemeralStorageCapacity, float64(capacityEphemeralStorage.Value())) 78 setFloat(metricSet, &core.MetricNodeEphemeralStorageAllocatable, float64(allocatableEphemeralStorage.Value())) 79 if allocatableEphemeralStorage.Value() != 0 { 80 setFloat(metricSet, &core.MetricNodeEphemeralStorageUtilization, float64(epheUsed)/float64(allocatableEphemeralStorage.Value())) 81 setFloat(metricSet, &core.MetricNodeEphemeralStorageReservation, float64(epheRequested)/float64(allocatableEphemeralStorage.Value())) 82 } 83 } 84 } 85 } 86 return batch, nil 87 } 88 89 func getInt(metricSet *core.MetricSet, metric *core.Metric) int64 { 90 if value, found := metricSet.MetricValues[metric.MetricDescriptor.Name]; found { 91 return value.IntValue 92 } 93 return 0 94 } 95 96 func setFloat(metricSet *core.MetricSet, metric *core.Metric, value float64) { 97 metricSet.MetricValues[metric.MetricDescriptor.Name] = core.MetricValue{ 98 MetricType: core.MetricGauge, 99 ValueType: core.ValueFloat, 100 FloatValue: value, 101 } 102 } 103 104 func NewNodeAutoscalingEnricher(url *url.URL, labelCopier *util.LabelCopier) (*NodeAutoscalingEnricher, error) { 105 kubeConfig, err := kube_config.GetKubeClientConfig(url) 106 if err != nil { 107 return nil, err 108 } 109 kubeClient := kube_client.NewForConfigOrDie(kubeConfig) 110 111 // watch nodes 112 nodeLister, reflector, _ := util.GetNodeLister(kubeClient) 113 114 return &NodeAutoscalingEnricher{ 115 nodeLister: nodeLister, 116 reflector: reflector, 117 labelCopier: labelCopier, 118 }, nil 119 }