github.com/kubewharf/katalyst-core@v0.5.3/pkg/custom-metric/provider/util.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package provider 18 19 import ( 20 "time" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/labels" 24 "k8s.io/klog/v2" 25 "k8s.io/metrics/pkg/apis/custom_metrics" 26 "k8s.io/metrics/pkg/apis/external_metrics" 27 28 "github.com/kubewharf/katalyst-core/pkg/custom-metric/store/data/types" 29 ) 30 31 // findMetricValueLatest returns metric with the latest timestamp. 32 func findMetricValueLatest(metricName string, metricValueList []custom_metrics.MetricValue) *custom_metrics.MetricValue { 33 var res *custom_metrics.MetricValue 34 35 for i, metricValue := range metricValueList { 36 if res == nil || res.Timestamp.UnixMilli() < metricValue.Timestamp.UnixMilli() { 37 res = &metricValue 38 39 if i != 0 { 40 klog.Warningf("metric %v stores latest value with index %v instead of beginning", metricName, i) 41 } 42 } 43 } 44 45 return res 46 } 47 48 /* 49 those packing functions are helpers to pack out standard Item structs 50 */ 51 52 func PackMetricValueList(metricItem types.Metric, metricSelector labels.Selector) []custom_metrics.MetricValue { 53 var res []custom_metrics.MetricValue 54 for _, item := range metricItem.GetItemList() { 55 res = append(res, *PackMetricValue(metricItem, item, metricSelector)) 56 } 57 return res 58 } 59 60 func PackMetricValue(m types.Metric, item types.Item, metricSelector labels.Selector) *custom_metrics.MetricValue { 61 result := &custom_metrics.MetricValue{ 62 DescribedObject: custom_metrics.ObjectReference{ 63 Kind: m.GetObjectKind(), 64 Namespace: m.GetObjectNamespace(), 65 Name: m.GetObjectName(), 66 }, 67 Metric: custom_metrics.MetricIdentifier{ 68 Name: m.GetName(), 69 Selector: &metav1.LabelSelector{ 70 MatchLabels: m.GetLabels(), 71 }, 72 }, 73 Timestamp: metav1.NewTime(time.UnixMilli(item.GetTimestamp())), 74 WindowSeconds: item.GetWindowSeconds(), 75 Value: item.GetQuantity(), 76 } 77 // if user specifies the metric selector, return itself. 78 if len(m.GetLabels()) == 0 && !metricSelector.Empty() { 79 result.Metric.Selector = convertMetricLabelSelector(metricSelector) 80 } 81 82 return result 83 } 84 85 func convertMetricLabelSelector(metricSelector labels.Selector) *metav1.LabelSelector { 86 requirements, _ := metricSelector.Requirements() 87 selector := &metav1.LabelSelector{ 88 MatchExpressions: make([]metav1.LabelSelectorRequirement, 0), 89 } 90 91 for i := range requirements { 92 selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ 93 Key: requirements[i].Key(), 94 Operator: metav1.LabelSelectorOperator(requirements[i].Operator()), 95 Values: requirements[i].Values().List(), 96 }) 97 } 98 return selector 99 } 100 101 func PackExternalMetricValueList(metricItem types.Metric) []external_metrics.ExternalMetricValue { 102 var res []external_metrics.ExternalMetricValue 103 for _, item := range metricItem.GetItemList() { 104 res = append(res, *PackExternalMetricValue(metricItem, item)) 105 } 106 return res 107 } 108 109 func PackExternalMetricValue(metricItem types.Metric, metric types.Item) *external_metrics.ExternalMetricValue { 110 return &external_metrics.ExternalMetricValue{ 111 MetricName: metricItem.GetName(), 112 MetricLabels: metricItem.GetLabels(), 113 Timestamp: metav1.NewTime(time.UnixMilli(metric.GetTimestamp())), 114 WindowSeconds: metric.GetWindowSeconds(), 115 Value: metric.GetQuantity(), 116 } 117 }