github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/pod_based_enricher_test.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 "testing" 19 "time" 20 21 "github.com/stretchr/testify/assert" 22 23 "k8s.io/heapster/metrics/core" 24 "k8s.io/heapster/metrics/util" 25 26 kube_api "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/api/resource" 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 v1listers "k8s.io/client-go/listers/core/v1" 30 "k8s.io/client-go/tools/cache" 31 ) 32 33 var batches = []*core.DataBatch{ 34 { 35 Timestamp: time.Now(), 36 MetricSets: map[string]*core.MetricSet{ 37 core.PodContainerKey("ns1", "pod1", "c1"): { 38 Labels: map[string]string{ 39 core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, 40 core.LabelPodName.Key: "pod1", 41 core.LabelNamespaceName.Key: "ns1", 42 core.LabelContainerName.Key: "c1", 43 }, 44 MetricValues: map[string]core.MetricValue{}, 45 }, 46 47 core.PodKey("ns1", "pod1"): { 48 Labels: map[string]string{ 49 core.LabelMetricSetType.Key: core.MetricSetTypePod, 50 core.LabelPodName.Key: "pod1", 51 core.LabelNamespaceName.Key: "ns1", 52 }, 53 MetricValues: map[string]core.MetricValue{}, 54 }, 55 }, 56 }, 57 { 58 Timestamp: time.Now(), 59 MetricSets: map[string]*core.MetricSet{ 60 core.PodContainerKey("ns1", "pod1", "c1"): { 61 Labels: map[string]string{ 62 core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, 63 core.LabelPodName.Key: "pod1", 64 core.LabelNamespaceName.Key: "ns1", 65 core.LabelContainerName.Key: "c1", 66 }, 67 MetricValues: map[string]core.MetricValue{}, 68 }, 69 }, 70 }, 71 { 72 Timestamp: time.Now(), 73 MetricSets: map[string]*core.MetricSet{ 74 core.PodKey("ns1", "pod1"): { 75 Labels: map[string]string{ 76 core.LabelMetricSetType.Key: core.MetricSetTypePod, 77 core.LabelPodName.Key: "pod1", 78 core.LabelNamespaceName.Key: "ns1", 79 }, 80 MetricValues: map[string]core.MetricValue{}, 81 }, 82 }, 83 }, 84 } 85 86 const otherResource = "example.com/resource1" 87 88 func TestPodEnricher(t *testing.T) { 89 pod := kube_api.Pod{ 90 ObjectMeta: metav1.ObjectMeta{ 91 Name: "pod1", 92 Namespace: "ns1", 93 }, 94 Spec: kube_api.PodSpec{ 95 NodeName: "node1", 96 Containers: []kube_api.Container{ 97 { 98 Name: "c1", 99 Image: "k8s.gcr.io/pause:2.0", 100 Resources: kube_api.ResourceRequirements{ 101 Requests: kube_api.ResourceList{ 102 kube_api.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI), 103 kube_api.ResourceMemory: *resource.NewQuantity(555, resource.DecimalSI), 104 kube_api.ResourceEphemeralStorage: *resource.NewQuantity(1000, resource.DecimalSI), 105 }, 106 }, 107 }, 108 { 109 Name: "nginx", 110 Image: "k8s.gcr.io/pause:2.0", 111 Resources: kube_api.ResourceRequirements{ 112 Requests: kube_api.ResourceList{ 113 kube_api.ResourceCPU: *resource.NewMilliQuantity(333, resource.DecimalSI), 114 kube_api.ResourceMemory: *resource.NewQuantity(1000, resource.DecimalSI), 115 kube_api.ResourceEphemeralStorage: *resource.NewQuantity(2000, resource.DecimalSI), 116 otherResource: *resource.NewQuantity(2, resource.DecimalSI), 117 }, 118 Limits: kube_api.ResourceList{ 119 kube_api.ResourceCPU: *resource.NewMilliQuantity(2222, resource.DecimalSI), 120 kube_api.ResourceMemory: *resource.NewQuantity(3333, resource.DecimalSI), 121 kube_api.ResourceEphemeralStorage: *resource.NewQuantity(5000, resource.DecimalSI), 122 otherResource: *resource.NewQuantity(2, resource.DecimalSI), 123 }, 124 }, 125 }, 126 }, 127 }, 128 } 129 130 store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) 131 podLister := v1listers.NewPodLister(store) 132 store.Add(&pod) 133 labelCopier, err := util.NewLabelCopier(",", []string{}, []string{}) 134 assert.NoError(t, err) 135 136 podBasedEnricher := PodBasedEnricher{ 137 podLister: podLister, 138 labelCopier: labelCopier, 139 } 140 141 for _, batch := range batches { 142 batch, err = podBasedEnricher.Process(batch) 143 assert.NoError(t, err) 144 145 podAggregator := PodAggregator{} 146 batch, err = podAggregator.Process(batch) 147 assert.NoError(t, err) 148 149 podMs, found := batch.MetricSets[core.PodKey("ns1", "pod1")] 150 assert.True(t, found) 151 checkRequests(t, podMs, 433, 1555, 3000, 2) 152 checkLimits(t, podMs, 2222, 3333, 5000) 153 154 containerMs, found := batch.MetricSets[core.PodContainerKey("ns1", "pod1", "c1")] 155 assert.True(t, found) 156 checkRequests(t, containerMs, 100, 555, 1000, -1) 157 checkLimits(t, containerMs, 0, 0, 0) 158 } 159 } 160 161 func checkRequests(t *testing.T, ms *core.MetricSet, cpu, mem, storage, other int64) { 162 cpuVal, found := ms.MetricValues[core.MetricCpuRequest.Name] 163 assert.True(t, found) 164 assert.Equal(t, cpu, cpuVal.IntValue) 165 166 memVal, found := ms.MetricValues[core.MetricMemoryRequest.Name] 167 assert.True(t, found) 168 assert.Equal(t, mem, memVal.IntValue) 169 170 storageVal, found := ms.MetricValues[core.MetricEphemeralStorageRequest.Name] 171 assert.True(t, found) 172 assert.Equal(t, storage, storageVal.IntValue) 173 174 if other > 0 { 175 val, found := ms.MetricValues[otherResource+"/request"] 176 assert.True(t, found) 177 assert.Equal(t, other, val.IntValue) 178 } 179 } 180 181 func checkLimits(t *testing.T, ms *core.MetricSet, cpu, mem int64, storage int64) { 182 cpuVal, found := ms.MetricValues[core.MetricCpuLimit.Name] 183 assert.True(t, found) 184 assert.Equal(t, cpu, cpuVal.IntValue) 185 186 memVal, found := ms.MetricValues[core.MetricMemoryLimit.Name] 187 assert.True(t, found) 188 assert.Equal(t, mem, memVal.IntValue) 189 190 storageVal, found := ms.MetricValues[core.MetricEphemeralStorageLimit.Name] 191 assert.True(t, found) 192 assert.Equal(t, storage, storageVal.IntValue) 193 }