|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes 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 metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + autoscaling "k8s.io/api/autoscaling/v2beta2" |
| 25 | + "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/labels" |
| 28 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 29 | + customapi "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2" |
| 30 | + resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1" |
| 31 | + customclient "k8s.io/metrics/pkg/client/custom_metrics" |
| 32 | + externalclient "k8s.io/metrics/pkg/client/external_metrics" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + metricServerDefaultMetricWindow = time.Minute |
| 37 | +) |
| 38 | + |
| 39 | +func NewRESTMetricsClient(resourceClient resourceclient.PodMetricsesGetter, customClient customclient.CustomMetricsClient, externalClient externalclient.ExternalMetricsClient) MetricsClient { |
| 40 | + return &restMetricsClient{ |
| 41 | + &resourceMetricsClient{resourceClient}, |
| 42 | + &customMetricsClient{customClient}, |
| 43 | + &externalMetricsClient{externalClient}, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// restMetricsClient is a client which supports fetching |
| 48 | +// metrics from both the resource metrics API and the |
| 49 | +// custom metrics API. |
| 50 | +type restMetricsClient struct { |
| 51 | + *resourceMetricsClient |
| 52 | + *customMetricsClient |
| 53 | + *externalMetricsClient |
| 54 | +} |
| 55 | + |
| 56 | +// PodMetric contains pod metric value (the metric values are expected to be the metric as a milli-value) |
| 57 | +type PodMetric struct { |
| 58 | + Timestamp time.Time |
| 59 | + Window time.Duration |
| 60 | + Value int64 |
| 61 | +} |
| 62 | + |
| 63 | +// resourceMetricsClient implements the resource-metrics-related parts of MetricsClient, |
| 64 | +// using data from the resource metrics API. |
| 65 | +type resourceMetricsClient struct { |
| 66 | + client resourceclient.PodMetricsesGetter |
| 67 | +} |
| 68 | + |
| 69 | +// PodMetricsInfo contains pod metrics as a map from pod names to PodMetricsInfo |
| 70 | +type PodMetricsInfo map[string]PodMetric |
| 71 | + |
| 72 | +// MetricsClient knows how to query a remote interface to retrieve container-level |
| 73 | +// resource metrics as well as pod-level arbitrary metrics |
| 74 | +type MetricsClient interface { |
| 75 | + // GetResourceMetric gets the given resource metric (and an associated oldest timestamp) |
| 76 | + // for all pods matching the specified selector in the given namespace |
| 77 | + GetResourceMetric(resource v1.ResourceName, namespace string, selector labels.Selector) (PodMetricsInfo, time.Time, error) |
| 78 | + |
| 79 | + // GetRawMetric gets the given metric (and an associated oldest timestamp) |
| 80 | + // for all pods matching the specified selector in the given namespace |
| 81 | + GetRawMetric(metricName string, namespace string, selector labels.Selector, metricSelector labels.Selector) (PodMetricsInfo, time.Time, error) |
| 82 | + |
| 83 | + // GetObjectMetric gets the given metric (and an associated timestamp) for the given |
| 84 | + // object in the given namespace |
| 85 | + GetObjectMetric(metricName string, namespace string, objectRef *autoscaling.CrossVersionObjectReference, metricSelector labels.Selector) (int64, time.Time, error) |
| 86 | + |
| 87 | + // GetExternalMetric gets all the values of a given external metric |
| 88 | + // that match the specified selector. |
| 89 | + GetExternalMetric(metricName string, namespace string, selector labels.Selector) ([]int64, time.Time, error) |
| 90 | +} |
| 91 | + |
| 92 | +// GetResourceMetric gets the given resource metric (and an associated oldest timestamp) |
| 93 | +// for all pods matching the specified selector in the given namespace |
| 94 | +func (c *resourceMetricsClient) GetResourceMetric(resource v1.ResourceName, namespace string, selector labels.Selector) (PodMetricsInfo, time.Time, error) { |
| 95 | + metrics, err := c.client.PodMetricses(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()}) |
| 96 | + if err != nil { |
| 97 | + return nil, time.Time{}, fmt.Errorf("unable to fetch metrics from resource metrics API: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + if len(metrics.Items) == 0 { |
| 101 | + return nil, time.Time{}, fmt.Errorf("no metrics returned from resource metrics API") |
| 102 | + } |
| 103 | + |
| 104 | + res := make(PodMetricsInfo, len(metrics.Items)) |
| 105 | + |
| 106 | + for _, m := range metrics.Items { |
| 107 | + podSum := int64(0) |
| 108 | + missing := len(m.Containers) == 0 |
| 109 | + for _, c := range m.Containers { |
| 110 | + resValue, found := c.Usage[v1.ResourceName(resource)] |
| 111 | + if !found { |
| 112 | + missing = true |
| 113 | + break // containers loop |
| 114 | + } |
| 115 | + podSum += resValue.MilliValue() |
| 116 | + } |
| 117 | + |
| 118 | + if !missing { |
| 119 | + res[m.Name] = PodMetric{ |
| 120 | + Timestamp: m.Timestamp.Time, |
| 121 | + Window: m.Window.Duration, |
| 122 | + Value: int64(podSum), |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + timestamp := metrics.Items[0].Timestamp.Time |
| 128 | + |
| 129 | + return res, timestamp, nil |
| 130 | +} |
| 131 | + |
| 132 | +// customMetricsClient implements the custom-metrics-related parts of MetricsClient, |
| 133 | +// using data from the custom metrics API. |
| 134 | +type customMetricsClient struct { |
| 135 | + client customclient.CustomMetricsClient |
| 136 | +} |
| 137 | + |
| 138 | +// GetRawMetric gets the given metric (and an associated oldest timestamp) |
| 139 | +// for all pods matching the specified selector in the given namespace |
| 140 | +func (c *customMetricsClient) GetRawMetric(metricName string, namespace string, selector labels.Selector, metricSelector labels.Selector) (PodMetricsInfo, time.Time, error) { |
| 141 | + metrics, err := c.client.NamespacedMetrics(namespace).GetForObjects(schema.GroupKind{Kind: "Pod"}, selector, metricName, metricSelector) |
| 142 | + if err != nil { |
| 143 | + return nil, time.Time{}, fmt.Errorf("unable to fetch metrics from custom metrics API: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + if len(metrics.Items) == 0 { |
| 147 | + return nil, time.Time{}, fmt.Errorf("no metrics returned from custom metrics API") |
| 148 | + } |
| 149 | + |
| 150 | + res := make(PodMetricsInfo, len(metrics.Items)) |
| 151 | + for _, m := range metrics.Items { |
| 152 | + window := metricServerDefaultMetricWindow |
| 153 | + if m.WindowSeconds != nil { |
| 154 | + window = time.Duration(*m.WindowSeconds) * time.Second |
| 155 | + } |
| 156 | + res[m.DescribedObject.Name] = PodMetric{ |
| 157 | + Timestamp: m.Timestamp.Time, |
| 158 | + Window: window, |
| 159 | + Value: int64(m.Value.MilliValue()), |
| 160 | + } |
| 161 | + |
| 162 | + m.Value.MilliValue() |
| 163 | + } |
| 164 | + |
| 165 | + timestamp := metrics.Items[0].Timestamp.Time |
| 166 | + |
| 167 | + return res, timestamp, nil |
| 168 | +} |
| 169 | + |
| 170 | +// GetObjectMetric gets the given metric (and an associated timestamp) for the given |
| 171 | +// object in the given namespace |
| 172 | +func (c *customMetricsClient) GetObjectMetric(metricName string, namespace string, objectRef *autoscaling.CrossVersionObjectReference, metricSelector labels.Selector) (int64, time.Time, error) { |
| 173 | + gvk := schema.FromAPIVersionAndKind(objectRef.APIVersion, objectRef.Kind) |
| 174 | + var metricValue *customapi.MetricValue |
| 175 | + var err error |
| 176 | + if gvk.Kind == "Namespace" && gvk.Group == "" { |
| 177 | + // handle namespace separately |
| 178 | + // NB: we ignore namespace name here, since CrossVersionObjectReference isn't |
| 179 | + // supposed to allow you to escape your namespace |
| 180 | + metricValue, err = c.client.RootScopedMetrics().GetForObject(gvk.GroupKind(), namespace, metricName, metricSelector) |
| 181 | + } else { |
| 182 | + metricValue, err = c.client.NamespacedMetrics(namespace).GetForObject(gvk.GroupKind(), objectRef.Name, metricName, metricSelector) |
| 183 | + } |
| 184 | + |
| 185 | + if err != nil { |
| 186 | + return 0, time.Time{}, fmt.Errorf("unable to fetch metrics from custom metrics API: %v", err) |
| 187 | + } |
| 188 | + |
| 189 | + return metricValue.Value.MilliValue(), metricValue.Timestamp.Time, nil |
| 190 | +} |
| 191 | + |
| 192 | +// externalMetricsClient implenets the external metrics related parts of MetricsClient, |
| 193 | +// using data from the external metrics API. |
| 194 | +type externalMetricsClient struct { |
| 195 | + client externalclient.ExternalMetricsClient |
| 196 | +} |
| 197 | + |
| 198 | +// GetExternalMetric gets all the values of a given external metric |
| 199 | +// that match the specified selector. |
| 200 | +func (c *externalMetricsClient) GetExternalMetric(metricName, namespace string, selector labels.Selector) ([]int64, time.Time, error) { |
| 201 | + metrics, err := c.client.NamespacedMetrics(namespace).List(metricName, selector) |
| 202 | + if err != nil { |
| 203 | + return []int64{}, time.Time{}, fmt.Errorf("unable to fetch metrics from external metrics API: %v", err) |
| 204 | + } |
| 205 | + |
| 206 | + if len(metrics.Items) == 0 { |
| 207 | + return nil, time.Time{}, fmt.Errorf("no metrics returned from external metrics API") |
| 208 | + } |
| 209 | + |
| 210 | + res := make([]int64, 0) |
| 211 | + for _, m := range metrics.Items { |
| 212 | + res = append(res, m.Value.MilliValue()) |
| 213 | + } |
| 214 | + timestamp := metrics.Items[0].Timestamp.Time |
| 215 | + return res, timestamp, nil |
| 216 | +} |
0 commit comments